aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-02-18 19:35:10 -0600
committerRob Landley <rob@landley.net>2018-02-18 19:35:10 -0600
commit66f12b756a5431fe364c6817ec64e4e373bdc55a (patch)
tree8c7b1c29b55b0cda4a5a4b0a5bfc944dc7aa5f03 /toys
parentafb4bb9e5060e02f8bf12fdf9e71eb46c73d789a (diff)
downloadtoybox-66f12b756a5431fe364c6817ec64e4e373bdc55a.tar.gz
Work around a musl-libc bug that has facilitynames/prioritynames in headers
but then the link fails.
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/logger.c47
1 files changed, 36 insertions, 11 deletions
diff --git a/toys/posix/logger.c b/toys/posix/logger.c
index 16262c9f..b06699eb 100644
--- a/toys/posix/logger.c
+++ b/toys/posix/logger.c
@@ -29,29 +29,54 @@ GLOBALS(
char *ident;
)
+// find str in names[], accepting unambiguous short matches
+// returns offset into array of match, or -1 if no match
+int arrayfind(char *str, char *names[], int len)
+{
+ int try, i, matchlen = 0, found = -1, ambiguous = 1;
+
+ for (try = 0; try<len; try++) {
+ for (i=0; ; i++) {
+ if (!str[i]) {
+ if (matchlen<i) found = try, ambiguous = 0;
+ if (matchlen==i) ambiguous++;
+ if (!names[try][i]) return try;
+ break;
+ }
+ if (!names[try][i]) break;
+ if (toupper(str[i]) != toupper(names[try][i])) break;
+ }
+ }
+ return ambiguous ? -1 : found;
+}
+
void logger_main(void)
{
int facility = LOG_USER, priority = LOG_NOTICE, len;
- char *s1, *s2, **arg;
- CODE *code;
+ char *s1, *s2, **arg,
+ *priorities[] = {"emerg", "alert", "crit", "error", "warning", "notice",
+ "info", "debug"},
+ *facilities[] = {"kern", "user", "mail", "daemon", "auth", "syslog",
+ "lpr", "news", "uucp", "cron", "authpriv", "ftp"};
if (!TT.ident) TT.ident = xstrdup(xgetpwuid(geteuid())->pw_name);
if (toys.optflags & FLAG_p) {
if (!(s1 = strchr(TT.priority, '.'))) s1 = TT.priority;
else {
- *s1++ = 0;
- for (code = facilitynames; code->c_name; code++)
- if (!strcasecmp(TT.priority, code->c_name)) break;
- if (!code->c_name) error_exit("bad facility: %s", TT.priority);
- facility = code->c_val;
+ *s1++ = len = 0;
+ facility = arrayfind(TT.priority, facilities, ARRAY_LEN(facilities));
+ if (facility == -1 && strncasecmp(TT.priority, "local", 5)) {
+ facility = s1[5]-'0';
+ if (facility>7 || s1[6]) facility = -1;
+ if (facility>=0) facility += 16;
+ }
+ if (facility<0) error_exit("bad facility: %s", TT.priority);
}
- for (code = prioritynames; code->c_name; code++)
- if (!strcasecmp(s1, code->c_name)) break;
- if (!code->c_name) error_exit("bad priority: %s", s1);
+ priority = arrayfind(s1, priorities, ARRAY_LEN(priorities));
+ if (priority<0) error_exit("bad priority: %s", s1);
}
-
if (toys.optc) {
for (len = 0, arg = toys.optargs; *arg; arg++) len += strlen(*arg)+1;
s1 = s2 = xmalloc(len);