1#include <my_global.h>
2#include <m_string.h>
3#include <my_sys.h>
4#include <my_pthread.h>
5#ifdef HAVE_PWD_H
6#include <pwd.h>
7#endif
8#include <stdlib.h>
9#include <stdio.h>
10
11void my_exit(int c)
12{
13 my_end(0);
14 exit(c);
15}
16
17void do_usage(void)
18{
19 printf("Usage:\n"
20 " %s <user> log <filename>\n"
21 " %s <user> exec <command> <args>\n",
22 my_progname, my_progname);
23 my_exit(1);
24}
25
26void do_log(const char *logfile)
27{
28 FILE *f;
29 uchar buf[4096];
30 int size;
31
32 if (!logfile)
33 do_usage();
34
35 f= my_fopen(logfile, O_WRONLY|O_APPEND|O_CREAT, MYF(MY_WME));
36 if (!f)
37 my_exit(1);
38
39 while ((size= my_fread(stdin, buf, sizeof(buf), MYF(MY_WME))) > 0)
40 if ((int)my_fwrite(f, buf, size, MYF(MY_WME)) != size)
41 my_exit(1);
42
43 my_fclose(f, MYF(0));
44 my_exit(0);
45}
46
47void do_exec(char *args[])
48{
49 if (!args[0])
50 do_usage();
51
52 my_end(0);
53 execvp(args[0], args);
54}
55
56int main(int argc, char *argv[])
57{
58 struct passwd *user_info;
59 MY_INIT(argv[0]);
60
61 if (argc < 3)
62 do_usage();
63
64 user_info= my_check_user(argv[1], MYF(0));
65 if (user_info ? my_set_user(argv[1], user_info, MYF(MY_WME))
66 : my_errno == EINVAL)
67 my_exit(1);
68
69 if (strcmp(argv[2], "log") == 0)
70 do_log(argv[3]);
71
72 if (strcmp(argv[2], "exec") == 0)
73 do_exec(argv+3);
74
75 my_end(0);
76 return 1;
77}
78