]>
Commit | Line | Data |
---|---|---|
5c36a0eb | 1 | /* |
52b7b487 KZ |
2 | * set process title for ps (from sendmail) |
3 | * | |
4 | * Clobbers argv of our main procedure so ps(1) will display the title. | |
5 | */ | |
5c36a0eb KZ |
6 | #include <stdio.h> |
7 | #include <stdlib.h> | |
8 | #include <string.h> | |
9 | #include <stdarg.h> | |
52b7b487 | 10 | |
5c36a0eb KZ |
11 | #include "setproctitle.h" |
12 | ||
5c36a0eb | 13 | #ifndef SPT_BUFSIZE |
52b7b487 | 14 | # define SPT_BUFSIZE 2048 |
5c36a0eb KZ |
15 | #endif |
16 | ||
52b7b487 | 17 | extern char **environ; |
5c36a0eb | 18 | |
52b7b487 | 19 | static char **argv0; |
c7f87da2 | 20 | static size_t argv_lth; |
5c36a0eb | 21 | |
52b7b487 KZ |
22 | void initproctitle (int argc, char **argv) |
23 | { | |
5c36a0eb KZ |
24 | int i; |
25 | char **envp = environ; | |
26 | ||
27 | /* | |
28 | * Move the environment so we can reuse the memory. | |
29 | * (Code borrowed from sendmail.) | |
30 | * WARNING: ugly assumptions on memory layout here; | |
31 | * if this ever causes problems, #undef DO_PS_FIDDLING | |
32 | */ | |
33 | for (i = 0; envp[i] != NULL; i++) | |
34 | continue; | |
52b7b487 | 35 | |
fea1cbf7 | 36 | environ = malloc(sizeof(char *) * (i + 1)); |
5c36a0eb KZ |
37 | if (environ == NULL) |
38 | return; | |
52b7b487 | 39 | |
5c36a0eb KZ |
40 | for (i = 0; envp[i] != NULL; i++) |
41 | if ((environ[i] = strdup(envp[i])) == NULL) | |
42 | return; | |
43 | environ[i] = NULL; | |
44 | ||
5c36a0eb | 45 | if (i > 0) |
c7f87da2 | 46 | argv_lth = envp[i-1] + strlen(envp[i-1]) - argv[0]; |
5c36a0eb | 47 | else |
c7f87da2 TS |
48 | argv_lth = argv[argc-1] + strlen(argv[argc-1]) - argv[0]; |
49 | if (argv_lth > 1) | |
50 | argv0 = argv; | |
5c36a0eb | 51 | } |
52b7b487 KZ |
52 | |
53 | void setproctitle (const char *prog, const char *txt) | |
54 | { | |
c7f87da2 | 55 | size_t i; |
52b7b487 | 56 | char buf[SPT_BUFSIZE]; |
5c36a0eb KZ |
57 | |
58 | if (!argv0) | |
59 | return; | |
60 | ||
61 | if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE) | |
62 | return; | |
63 | ||
52b7b487 | 64 | sprintf(buf, "%s -- %s", prog, txt); |
5c36a0eb | 65 | |
52b7b487 | 66 | i = strlen(buf); |
5c36a0eb KZ |
67 | if (i > argv_lth - 2) { |
68 | i = argv_lth - 2; | |
69 | buf[i] = '\0'; | |
70 | } | |
7eda085c | 71 | memset(argv0[0], '\0', argv_lth); /* clear the memory area */ |
52b7b487 | 72 | strcpy(argv0[0], buf); |
7eda085c | 73 | |
5c36a0eb KZ |
74 | argv0[1] = NULL; |
75 | } |