external/toybox
Revision | 66f12b756a5431fe364c6817ec64e4e373bdc55a (tree) |
---|---|
Time | 2018-02-19 10:35:10 |
Author | Rob Landley <rob@land...> |
Commiter | Rob Landley |
Work around a musl-libc bug that has facilitynames/prioritynames in headers
but then the link fails.
@@ -29,29 +29,54 @@ GLOBALS( | ||
29 | 29 | char *ident; |
30 | 30 | ) |
31 | 31 | |
32 | +// find str in names[], accepting unambiguous short matches | |
33 | +// returns offset into array of match, or -1 if no match | |
34 | +int arrayfind(char *str, char *names[], int len) | |
35 | +{ | |
36 | + int try, i, matchlen = 0, found = -1, ambiguous = 1; | |
37 | + | |
38 | + for (try = 0; try<len; try++) { | |
39 | + for (i=0; ; i++) { | |
40 | + if (!str[i]) { | |
41 | + if (matchlen<i) found = try, ambiguous = 0; | |
42 | + if (matchlen==i) ambiguous++; | |
43 | + if (!names[try][i]) return try; | |
44 | + break; | |
45 | + } | |
46 | + if (!names[try][i]) break; | |
47 | + if (toupper(str[i]) != toupper(names[try][i])) break; | |
48 | + } | |
49 | + } | |
50 | + return ambiguous ? -1 : found; | |
51 | +} | |
52 | + | |
32 | 53 | void logger_main(void) |
33 | 54 | { |
34 | 55 | int facility = LOG_USER, priority = LOG_NOTICE, len; |
35 | - char *s1, *s2, **arg; | |
36 | - CODE *code; | |
56 | + char *s1, *s2, **arg, | |
57 | + *priorities[] = {"emerg", "alert", "crit", "error", "warning", "notice", | |
58 | + "info", "debug"}, | |
59 | + *facilities[] = {"kern", "user", "mail", "daemon", "auth", "syslog", | |
60 | + "lpr", "news", "uucp", "cron", "authpriv", "ftp"}; | |
37 | 61 | |
38 | 62 | if (!TT.ident) TT.ident = xstrdup(xgetpwuid(geteuid())->pw_name); |
39 | 63 | if (toys.optflags & FLAG_p) { |
40 | 64 | if (!(s1 = strchr(TT.priority, '.'))) s1 = TT.priority; |
41 | 65 | else { |
42 | - *s1++ = 0; | |
43 | - for (code = facilitynames; code->c_name; code++) | |
44 | - if (!strcasecmp(TT.priority, code->c_name)) break; | |
45 | - if (!code->c_name) error_exit("bad facility: %s", TT.priority); | |
46 | - facility = code->c_val; | |
66 | + *s1++ = len = 0; | |
67 | + facility = arrayfind(TT.priority, facilities, ARRAY_LEN(facilities)); | |
68 | + if (facility == -1 && strncasecmp(TT.priority, "local", 5)) { | |
69 | + facility = s1[5]-'0'; | |
70 | + if (facility>7 || s1[6]) facility = -1; | |
71 | + if (facility>=0) facility += 16; | |
72 | + } | |
73 | + if (facility<0) error_exit("bad facility: %s", TT.priority); | |
47 | 74 | } |
48 | 75 | |
49 | - for (code = prioritynames; code->c_name; code++) | |
50 | - if (!strcasecmp(s1, code->c_name)) break; | |
51 | - if (!code->c_name) error_exit("bad priority: %s", s1); | |
76 | + priority = arrayfind(s1, priorities, ARRAY_LEN(priorities)); | |
77 | + if (priority<0) error_exit("bad priority: %s", s1); | |
52 | 78 | } |
53 | 79 | |
54 | - | |
55 | 80 | if (toys.optc) { |
56 | 81 | for (len = 0, arg = toys.optargs; *arg; arg++) len += strlen(*arg)+1; |
57 | 82 | s1 = s2 = xmalloc(len); |