aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstddjb
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2025-11-04 15:44:18 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2025-11-04 15:44:18 +0000
commit32ac3aa6ac8f0dc9a240611a3696c0e5510b841d (patch)
treee90307b76f90493739ad31506a9890c4c82c22a3 /src/libstddjb
parent81c0e9dc69dcb2a5e498ee53ce47acef06ab4f77 (diff)
downloadskalibs-32ac3aa6ac8f0dc9a240611a3696c0e5510b841d.tar.gz
Optimize sig_name and sig_number, add stringkey_bcasecmp etc.
Diffstat (limited to 'src/libstddjb')
-rw-r--r--src/libstddjb/sig-internal.h15
-rw-r--r--src/libstddjb/sig_name.c90
-rw-r--r--src/libstddjb/sig_number.c93
-rw-r--r--src/libstddjb/sig_table.c86
-rw-r--r--src/libstddjb/str_bcasecmp.c10
-rw-r--r--src/libstddjb/str_casecmp.c10
-rw-r--r--src/libstddjb/stringkey_bcasecmp.c12
-rw-r--r--src/libstddjb/stringkey_casecmp.c13
8 files changed, 217 insertions, 112 deletions
diff --git a/src/libstddjb/sig-internal.h b/src/libstddjb/sig-internal.h
deleted file mode 100644
index 573fbfa..0000000
--- a/src/libstddjb/sig-internal.h
+++ /dev/null
@@ -1,15 +0,0 @@
-/* ISC license. */
-
-#ifndef SKALIBS_SIG_INTERNAL_H
-#define SKALIBS_SIG_INTERNAL_H
-
-typedef struct sigtable_s sigtable, *sigtable_ref ;
-struct sigtable_s
-{
- int number ;
- char const *name ;
-} ;
-
-extern sigtable const skalibs_sigtable[] ;
-
-#endif
diff --git a/src/libstddjb/sig_name.c b/src/libstddjb/sig_name.c
index c8d7b6f..36b6ac0 100644
--- a/src/libstddjb/sig_name.c
+++ b/src/libstddjb/sig_name.c
@@ -1,11 +1,89 @@
-/* ISC license. */
+ /* ISC license. */
-#include <skalibs/sig.h>
-#include "sig-internal.h"
+#include <skalibs/nonposix.h>
+
+#include <signal.h>
+#include <skalibs/nsig.h>
+
+static char const *const table[SKALIBS_NSIG] =
+{
+ [SIGABRT] = "ABRT",
+ [SIGALRM] = "ALRM",
+ [SIGBUS] = "BUS",
+ [SIGCHLD] = "CHLD",
+ [SIGCONT] = "CONT",
+ [SIGFPE] = "FPE",
+ [SIGHUP] = "HUP",
+ [SIGILL] = "ILL",
+ [SIGINT] = "INT",
+ [SIGKILL] = "KILL",
+ [SIGPIPE] = "PIPE",
+ [SIGQUIT] = "QUIT",
+ [SIGSEGV] = "SEGV",
+ [SIGSTOP] = "STOP",
+ [SIGTERM] = "TERM",
+ [SIGTSTP] = "TSTP",
+ [SIGTTIN] = "TTIN",
+ [SIGTTOU] = "TTOU",
+ [SIGUSR1] = "USR1",
+ [SIGUSR2] = "USR2",
+#ifdef SIGPOLL
+ [SIGPOLL] = "POLL",
+#endif
+#ifdef SIGPROF
+ [SIGPROF] = "PROF",
+#endif
+#ifdef SIGSYS
+ [SIGSYS] = "SYS",
+#endif
+#ifdef SIGTRAP
+ [SIGTRAP] = "TRAP",
+#endif
+#ifdef SIGURG
+ [SIGURG] = "URG",
+#endif
+#ifdef SIGVTALRM
+ [SIGVTALRM] = "VTALRM",
+#endif
+#ifdef SIGXCPU
+ [SIGXCPU] = "XCPU",
+#endif
+#ifdef SIGXFSZ
+ [SIGXFSZ] = "XFSZ",
+#endif
+#ifdef SIGIOT
+ [SIGIOT] = "IOT",
+#endif
+#ifdef SIGEMT
+ [SIGEMT] = "EMT",
+#endif
+#ifdef SIGSTKFLT
+ [SIGSTKFLT] = "STKFLT",
+#endif
+#ifdef SIGCLD
+ [SIGCLD] = "CLD",
+#endif
+#ifdef SIGWINCH
+ [SIGWINCH] = "WINCH",
+#endif
+#ifdef SIGIO
+ [SIGIO] = "IO",
+#endif
+#ifdef SIGINFO
+ [SIGINFO] = "INFO",
+#endif
+#ifdef SIGLOST
+ [SIGLOST] = "LOST",
+#endif
+#ifdef SIGPWR
+ [SIGPWR] = "PWR",
+#endif
+#ifdef SIGUNUSED
+ [SIGUNUSED] = "UNUSED"
+#endif
+} ;
char const *sig_name (int sig)
{
- sigtable const *p = skalibs_sigtable ;
- for (; p->number ; p++) if (sig == p->number) break ;
- return p->number ? p->name : "???" ;
+ return sig <= 0 || sig > SKALIBS_NSIG || !table[sig] ? "???" : table[sig] ;
}
diff --git a/src/libstddjb/sig_number.c b/src/libstddjb/sig_number.c
index d234185..ed5bf44 100644
--- a/src/libstddjb/sig_number.c
+++ b/src/libstddjb/sig_number.c
@@ -1,13 +1,96 @@
/* ISC license. */
-#include <strings.h>
+#include <stdlib.h>
+#include <skalibs/bytestr.h>
#include <skalibs/sig.h>
-#include "sig-internal.h"
+
+struct sigtable_s
+{
+ char const *name ;
+ int number ;
+} ;
+
+static struct sigtable_s const table[] =
+{
+ { "ABRT", SIGABRT },
+ { "ALRM", SIGALRM },
+ { "BUS", SIGBUS },
+ { "CHLD", SIGCHLD },
+#ifdef SIGCLD
+ { "CLD", SIGCLD },
+#endif
+ { "CONT", SIGCONT },
+#ifdef SIGEMT
+ { "EMT", SIGEMT },
+#endif
+ { "FPE", SIGFPE },
+ { "HUP", SIGHUP },
+ { "ILL", SIGILL },
+#ifdef SIGINFO
+ { "INFO", SIGINFO },
+#endif
+ { "INT", SIGINT },
+#ifdef SIGIO
+ { "IO", SIGIO },
+#endif
+#ifdef SIGIOT
+ { "IOT", SIGIOT },
+#endif
+ { "KILL", SIGKILL },
+#ifdef SIGLOST
+ { "LOST", SIGLOST },
+#endif
+ { "PIPE", SIGPIPE },
+#ifdef SIGPOLL
+ { "POLL", SIGPOLL },
+#endif
+#ifdef SIGPROF
+ { "PROF", SIGPROF },
+#endif
+#ifdef SIGPWR
+ { "PWR", SIGPWR },
+#endif
+ { "QUIT", SIGQUIT },
+ { "SEGV", SIGSEGV },
+#ifdef SIGSTKFLT
+ { "STKFLT", SIGSTKFLT },
+#endif
+ { "STOP", SIGSTOP },
+#ifdef SIGSYS
+ { "SYS", SIGSYS },
+#endif
+ { "TERM", SIGTERM },
+#ifdef SIGTRAP
+ { "TRAP", SIGTRAP },
+#endif
+ { "TSTP", SIGTSTP },
+ { "TTIN", SIGTTIN },
+ { "TTOU", SIGTTOU },
+#ifdef SIGUNUSED
+ { "UNUSED", SIGUNUSED },
+#endif
+#ifdef SIGURG
+ { "URG", SIGURG },
+#endif
+ { "USR1", SIGUSR1 },
+ { "USR2", SIGUSR2 },
+#ifdef SIGVTALRM
+ { "VTALRM", SIGVTALRM },
+#endif
+#ifdef SIGWINCH
+ { "WINCH", SIGWINCH },
+#endif
+#ifdef SIGXCPU
+ { "XCPU", SIGXCPU },
+#endif
+#ifdef SIGXFSZ
+ { "XFSZ", SIGXFSZ },
+#endif
+} ;
int sig_number (char const *name)
{
- sigtable const *p = skalibs_sigtable ;
- for (; p->name ; p++) if (!strcasecmp(name, p->name)) break ;
- return p->number ;
+ struct sigtable_s *p = bsearch(name, table, sizeof(table)/sizeof(struct sigtable_s), sizeof(struct sigtable_s), &stringkey_bcasecmp) ;
+ return p ? p->number : 0 ;
}
diff --git a/src/libstddjb/sig_table.c b/src/libstddjb/sig_table.c
deleted file mode 100644
index 9d3bad8..0000000
--- a/src/libstddjb/sig_table.c
+++ /dev/null
@@ -1,86 +0,0 @@
- /* ISC license. */
-
-#include <skalibs/nonposix.h>
-
-#include <signal.h>
-
-#include "sig-internal.h"
-
-sigtable const skalibs_sigtable[] =
-{
- { SIGABRT, "ABRT" },
- { SIGALRM, "ALRM" },
- { SIGBUS, "BUS" },
- { SIGCHLD, "CHLD" },
- { SIGCONT, "CONT" },
- { SIGFPE, "FPE" },
- { SIGHUP, "HUP" },
- { SIGILL, "ILL" },
- { SIGINT, "INT" },
- { SIGKILL, "KILL" },
- { SIGPIPE, "PIPE" },
- { SIGQUIT, "QUIT" },
- { SIGSEGV, "SEGV" },
- { SIGSTOP, "STOP" },
- { SIGTERM, "TERM" },
- { SIGTSTP, "TSTP" },
- { SIGTTIN, "TTIN" },
- { SIGTTOU, "TTOU" },
- { SIGUSR1, "USR1" },
- { SIGUSR2, "USR2" },
-#ifdef SIGPOLL
- { SIGPOLL, "POLL" },
-#endif
-#ifdef SIGPROF
- { SIGPROF, "PROF" },
-#endif
-#ifdef SIGSYS
- { SIGSYS, "SYS" },
-#endif
-#ifdef SIGTRAP
- { SIGTRAP, "TRAP" },
-#endif
-#ifdef SIGURG
- { SIGURG, "URG" },
-#endif
-#ifdef SIGVTALRM
- { SIGVTALRM, "VTALRM" },
-#endif
-#ifdef SIGXCPU
- { SIGXCPU, "XCPU" },
-#endif
-#ifdef SIGXFSZ
- { SIGXFSZ, "XFSZ" },
-#endif
-#ifdef SIGIOT
- { SIGIOT, "IOT" },
-#endif
-#ifdef SIGEMT
- { SIGEMT, "EMT" },
-#endif
-#ifdef SIGSTKFLT
- { SIGSTKFLT, "STKFLT" },
-#endif
-#ifdef SIGCLD
- { SIGCLD, "CLD" },
-#endif
-#ifdef SIGWINCH
- { SIGWINCH, "WINCH" },
-#endif
-#ifdef SIGIO
- { SIGIO, "IO" },
-#endif
-#ifdef SIGINFO
- { SIGINFO, "INFO" },
-#endif
-#ifdef SIGLOST
- { SIGLOST, "LOST" },
-#endif
-#ifdef SIGPWR
- { SIGPWR, "PWR" },
-#endif
-#ifdef SIGUNUSED
- { SIGUNUSED, "UNUSED" },
-#endif
- { 0, 0 }
-} ;
diff --git a/src/libstddjb/str_bcasecmp.c b/src/libstddjb/str_bcasecmp.c
new file mode 100644
index 0000000..809bf7b
--- /dev/null
+++ b/src/libstddjb/str_bcasecmp.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <strings.h>
+
+#include <skalibs/bytestr.h>
+
+int str_bcasecmp (void const *a, void const *b)
+{
+ return strcasecmp((char const *)a, *(char const *const *)b) ;
+}
diff --git a/src/libstddjb/str_casecmp.c b/src/libstddjb/str_casecmp.c
new file mode 100644
index 0000000..fead2bf
--- /dev/null
+++ b/src/libstddjb/str_casecmp.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <strings.h>
+
+#include <skalibs/bytestr.h>
+
+int str_casecmp (void const *a, void const *b)
+{
+ return strcasecmp(*(char const *const *)a, *(char const *const *)b) ;
+}
diff --git a/src/libstddjb/stringkey_bcasecmp.c b/src/libstddjb/stringkey_bcasecmp.c
new file mode 100644
index 0000000..1171f0d
--- /dev/null
+++ b/src/libstddjb/stringkey_bcasecmp.c
@@ -0,0 +1,12 @@
+/* ISC license. */
+
+#include <strings.h>
+
+#include <skalibs/bytestr.h>
+#include "bytestr-internal.h"
+
+int stringkey_bcasecmp (void const *a, void const *b)
+{
+ struct stringkey_s const *bb = b ;
+ return strcasecmp((char const *)a, bb->s) ;
+}
diff --git a/src/libstddjb/stringkey_casecmp.c b/src/libstddjb/stringkey_casecmp.c
new file mode 100644
index 0000000..355c0d1
--- /dev/null
+++ b/src/libstddjb/stringkey_casecmp.c
@@ -0,0 +1,13 @@
+/* ISC license. */
+
+#include <strings.h>
+
+#include <skalibs/bytestr.h>
+#include "bytestr-internal.h"
+
+int stringkey_casecmp (void const *a, void const *b)
+{
+ struct stringkey_s const *aa = a ;
+ struct stringkey_s const *bb = b ;
+ return strcasecmp(aa->s, bb->s) ;
+}