]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/setproctitle.c
2 * set process title for ps (from sendmail)
4 * Clobbers argv of our main procedure so ps(1) will display the title.
11 #include "setproctitle.h"
14 # define SPT_BUFSIZE 2048
17 extern char **environ
;
20 static size_t argv_lth
;
22 void initproctitle (int argc
, char **argv
)
25 char **envp
= environ
;
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
33 for (i
= 0; envp
[i
] != NULL
; i
++)
36 environ
= malloc(sizeof(char *) * (i
+ 1));
40 for (i
= 0; envp
[i
] != NULL
; i
++)
41 if ((environ
[i
] = strdup(envp
[i
])) == NULL
)
46 argv_lth
= envp
[i
-1] + strlen(envp
[i
-1]) - argv
[0];
48 argv_lth
= argv
[argc
-1] + strlen(argv
[argc
-1]) - argv
[0];
53 void setproctitle (const char *prog
, const char *txt
)
56 char buf
[SPT_BUFSIZE
];
61 if (strlen(prog
) + strlen(txt
) + 5 > SPT_BUFSIZE
)
64 sprintf(buf
, "%s -- %s", prog
, txt
);
67 if (i
> argv_lth
- 2) {
71 memset(argv0
[0], '\0', argv_lth
); /* clear the memory area */
72 strcpy(argv0
[0], buf
);