1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define FOR_logwrapper
20#include "toys.h"
21
22void logwrapper_main(void)
23{
24 char *log = getenv("WRAPLOG"), *omnom = basename(*toys.argv),
25 *s, *ss, *sss;
26 struct string_list *list;
27 int i, len;
28
29
30 if (!log) error_exit("no $WRAPLOG");
31 len = strlen(omnom)+2;
32 for (i = 0; i<toys.optc; i++) len += 2*strlen(toys.optargs[i])+3;
33 ss = stpcpy(s = xmalloc(len), omnom);
34
35
36 for (i = 0; i<toys.optc; i++) {
37 *(ss++) = ' ';
38 *(ss++) = '"';
39 for (sss = toys.optargs[i]; *sss; sss++) {
40 if (-1 == (len = stridx("\n\\\"", *sss))) *(ss++) = *sss;
41 else {
42 *(ss++) = '\\';
43 *(ss++) = "n\\\""[len];
44 }
45 }
46 *(ss++) = '"';
47 }
48 *(ss++) = '\n';
49
50
51 i = xcreate(log, O_RDWR|O_CREAT|O_APPEND, 0644);
52 xwrite(i, s, ss-s);
53 close(i);
54 free(s);
55
56
57
58 list = find_in_path(getenv("PATH"), omnom);
59 if (**toys.argv == '/') {
60 while (list) {
61 if (!strcmp(list->str, *toys.argv)) break;
62 free(llist_pop(&list));
63 }
64 }
65
66
67 for (;;) {
68 if (list) free(llist_pop(&list));
69 if (!list)
70 error_exit("no %s after %s in $PATH=%s", omnom,
71 **toys.argv == '/' ? *toys.argv : "logwrapper", getenv("PATH"));
72 *toys.argv = list->str;
73 execve(list->str, toys.argv, environ);
74 }
75}
76