1 /* proctitle code - we know this to work only on linux... */
4 ** SETPROCTITLE -- set process title for ps (from sendmail)
7 ** fmt -- a printf style format string.
13 ** Clobbers argv of our main procedure so ps(1) will
21 #include "setproctitle.h"
24 #define SPT_BUFSIZE 2048
27 extern char** environ;
33 initproctitle (int argc, char **argv) {
35 char **envp = environ;
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
43 for (i = 0; envp[i] != NULL; i++)
45 environ = (char **) malloc(sizeof(char *) * (i + 1));
48 for (i = 0; envp[i] != NULL; i++)
49 if ((environ[i] = strdup(envp[i])) == NULL)
55 argv_lth = envp[i-1] + strlen(envp[i-1]) - argv0[0];
57 argv_lth = argv0[argc-1] + strlen(argv0[argc-1]) - argv0[0];
61 /* Nice code, but many places do not know about vsnprintf ... */
63 setproctitle (const char *fmt,...) {
65 char buf[SPT_BUFSIZE];
72 (void) vsnprintf(buf, SPT_BUFSIZE, fmt, ap);
76 if (i > argv_lth - 2) {
80 memset(argv0[0], '\0', argv_lth); /* clear the memory area */
81 (void) strcpy (argv0[0], buf);
87 setproctitle (const char *prog, const char *txt) {
89 char buf[SPT_BUFSIZE];
94 if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE)
97 (void) sprintf(buf, "%s -- %s", prog, txt);
100 if (i > argv_lth - 2) {
104 memset(argv0[0], '\0', argv_lth); /* clear the memory area */
105 (void) strcpy (argv0[0], buf);