]>
Commit | Line | Data |
---|---|---|
5c36a0eb KZ |
1 | /* proctitle code - we know this to work only on linux... */ |
2 | ||
3 | /* | |
4 | ** SETPROCTITLE -- set process title for ps (from sendmail) | |
5 | ** | |
6 | ** Parameters: | |
7 | ** fmt -- a printf style format string. | |
8 | ** | |
9 | ** Returns: | |
10 | ** none. | |
11 | ** | |
12 | ** Side Effects: | |
13 | ** Clobbers argv of our main procedure so ps(1) will | |
14 | ** display the title. | |
15 | */ | |
16 | ||
17 | #include <stdio.h> | |
18 | #include <stdlib.h> | |
19 | #include <string.h> | |
20 | #include <stdarg.h> | |
21 | #include "setproctitle.h" | |
22 | ||
5c36a0eb KZ |
23 | #ifndef SPT_BUFSIZE |
24 | #define SPT_BUFSIZE 2048 | |
25 | #endif | |
26 | ||
27 | extern char** environ; | |
28 | ||
29 | static char** argv0; | |
30 | static int argv_lth; | |
31 | ||
32 | void | |
33 | initproctitle (int argc, char **argv) { | |
34 | int i; | |
35 | char **envp = environ; | |
36 | ||
37 | /* | |
38 | * Move the environment so we can reuse the memory. | |
39 | * (Code borrowed from sendmail.) | |
40 | * WARNING: ugly assumptions on memory layout here; | |
41 | * if this ever causes problems, #undef DO_PS_FIDDLING | |
42 | */ | |
43 | for (i = 0; envp[i] != NULL; i++) | |
44 | continue; | |
45 | environ = (char **) malloc(sizeof(char *) * (i + 1)); | |
46 | if (environ == NULL) | |
47 | return; | |
48 | for (i = 0; envp[i] != NULL; i++) | |
49 | if ((environ[i] = strdup(envp[i])) == NULL) | |
50 | return; | |
51 | environ[i] = NULL; | |
52 | ||
53 | argv0 = argv; | |
54 | if (i > 0) | |
55 | argv_lth = envp[i-1] + strlen(envp[i-1]) - argv0[0]; | |
56 | else | |
57 | argv_lth = argv0[argc-1] + strlen(argv0[argc-1]) - argv0[0]; | |
58 | } | |
59 | ||
60 | #if 0 | |
61 | /* Nice code, but many places do not know about vsnprintf ... */ | |
62 | void | |
63 | setproctitle (const char *fmt,...) { | |
5c36a0eb KZ |
64 | int i; |
65 | char buf[SPT_BUFSIZE]; | |
66 | va_list ap; | |
67 | ||
68 | if (!argv0) | |
69 | return; | |
70 | ||
71 | va_start(ap, fmt); | |
72 | (void) vsnprintf(buf, SPT_BUFSIZE, fmt, ap); | |
73 | va_end(ap); | |
74 | ||
75 | i = strlen (buf); | |
76 | if (i > argv_lth - 2) { | |
77 | i = argv_lth - 2; | |
78 | buf[i] = '\0'; | |
79 | } | |
7eda085c | 80 | memset(argv0[0], '\0', argv_lth); /* clear the memory area */ |
5c36a0eb | 81 | (void) strcpy (argv0[0], buf); |
7eda085c | 82 | |
5c36a0eb KZ |
83 | argv0[1] = NULL; |
84 | } | |
85 | #else | |
86 | void | |
87 | setproctitle (const char *prog, const char *txt) { | |
5c36a0eb KZ |
88 | int i; |
89 | char buf[SPT_BUFSIZE]; | |
90 | ||
91 | if (!argv0) | |
92 | return; | |
93 | ||
94 | if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE) | |
95 | return; | |
96 | ||
97 | (void) sprintf(buf, "%s -- %s", prog, txt); | |
98 | ||
99 | i = strlen (buf); | |
100 | if (i > argv_lth - 2) { | |
101 | i = argv_lth - 2; | |
102 | buf[i] = '\0'; | |
103 | } | |
7eda085c | 104 | memset(argv0[0], '\0', argv_lth); /* clear the memory area */ |
5c36a0eb | 105 | (void) strcpy (argv0[0], buf); |
7eda085c | 106 | |
5c36a0eb KZ |
107 | argv0[1] = NULL; |
108 | } | |
109 | #endif |