1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#define FOR_logger
25#include "toys.h"
26
27GLOBALS(
28 char *priority;
29 char *ident;
30)
31
32
33
34int 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
53void logger_main(void)
54{
55 int facility = LOG_USER, priority = LOG_NOTICE, len;
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"};
61
62 if (!TT.ident) TT.ident = xstrdup(xgetpwuid(geteuid())->pw_name);
63 if (toys.optflags & FLAG_p) {
64 if (!(s1 = strchr(TT.priority, '.'))) s1 = TT.priority;
65 else {
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);
74 facility *= 8;
75 }
76
77 priority = arrayfind(s1, priorities, ARRAY_LEN(priorities));
78 if (priority<0) error_exit("bad priority: %s", s1);
79 }
80
81 if (toys.optc) {
82 for (len = 0, arg = toys.optargs; *arg; arg++) len += strlen(*arg)+1;
83 s1 = s2 = xmalloc(len);
84 for (arg = toys.optargs; *arg; arg++) {
85 if (arg != toys.optargs) *s2++ = ' ';
86 s2 = stpcpy(s2, *arg);
87 }
88 } else {
89 toybuf[readall(0, toybuf, sizeof(toybuf)-1)] = 0;
90 s1 = toybuf;
91 }
92
93 openlog(TT.ident, LOG_PERROR*!!(toys.optflags&FLAG_s), facility);
94 syslog(priority, "%s", s1);
95 closelog();
96}
97