diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-06 02:42:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-06 02:42:50 +0000 |
commit | 8cb83eee5a58b1fad74c34094ce3afb9e430b5a4 (patch) | |
tree | a9b2e7baeca1be40eb734371e3c8b11b02294497 /lib/setproctitle.c | |
parent | Initial commit. (diff) | |
download | util-linux-93fc1e48d951bf0d0c3e3d7ff6a2489f43f9c857.tar.xz util-linux-93fc1e48d951bf0d0c3e3d7ff6a2489f43f9c857.zip |
Adding upstream version 2.33.1.upstream/2.33.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/setproctitle.c')
-rw-r--r-- | lib/setproctitle.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/setproctitle.c b/lib/setproctitle.c new file mode 100644 index 0000000..7168e46 --- /dev/null +++ b/lib/setproctitle.c @@ -0,0 +1,75 @@ +/* + * set process title for ps (from sendmail) + * + * Clobbers argv of our main procedure so ps(1) will display the title. + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> + +#include "setproctitle.h" + +#ifndef SPT_BUFSIZE +# define SPT_BUFSIZE 2048 +#endif + +extern char **environ; + +static char **argv0; +static size_t argv_lth; + +void initproctitle (int argc, char **argv) +{ + int i; + char **envp = environ; + + /* + * Move the environment so we can reuse the memory. + * (Code borrowed from sendmail.) + * WARNING: ugly assumptions on memory layout here; + * if this ever causes problems, #undef DO_PS_FIDDLING + */ + for (i = 0; envp[i] != NULL; i++) + continue; + + environ = malloc(sizeof(char *) * (i + 1)); + if (environ == NULL) + return; + + for (i = 0; envp[i] != NULL; i++) + if ((environ[i] = strdup(envp[i])) == NULL) + return; + environ[i] = NULL; + + if (i > 0) + argv_lth = envp[i-1] + strlen(envp[i-1]) - argv[0]; + else + argv_lth = argv[argc-1] + strlen(argv[argc-1]) - argv[0]; + if (argv_lth > 1) + argv0 = argv; +} + +void setproctitle (const char *prog, const char *txt) +{ + size_t i; + char buf[SPT_BUFSIZE]; + + if (!argv0) + return; + + if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE) + return; + + sprintf(buf, "%s -- %s", prog, txt); + + i = strlen(buf); + if (i > argv_lth - 2) { + i = argv_lth - 2; + buf[i] = '\0'; + } + memset(argv0[0], '\0', argv_lth); /* clear the memory area */ + strcpy(argv0[0], buf); + + argv0[1] = NULL; +} |