diff options
author | wens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2008-06-17 14:18:53 +0800 |
---|---|---|
committer | wens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2008-06-17 14:18:53 +0800 |
commit | 8cb29110950daa71ef6247ed5d0ba1ea9320fa20 (patch) | |
tree | 0d1e5083acd77e6593d78ee34dc906de8740edae /common/osdep/proctitle.c | |
parent | 2d6dc07feca4e5e3dc8333f514197573dfaefda0 (diff) | |
download | pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.gz pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.bz2 pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.lz pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.xz pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.tar.zst pttbbs-8cb29110950daa71ef6247ed5d0ba1ea9320fa20.zip |
Split out os dependent library
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4378 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'common/osdep/proctitle.c')
-rw-r--r-- | common/osdep/proctitle.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/common/osdep/proctitle.c b/common/osdep/proctitle.c new file mode 100644 index 00000000..c6891e67 --- /dev/null +++ b/common/osdep/proctitle.c @@ -0,0 +1,71 @@ +/* $Id$ */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include "osdep.h" + +#ifdef NEED_SETPROCTITLE + +static char **Argv = NULL; /* pointer to argument vector */ +static int argv_size; /* end of argv */ + +extern char **environ; + +void +initsetproctitle(int argc, char **argv, char **envp) +{ + int i, len=0, nenv=0; + + /* + * Move the environment so setproctitle can use the space at the top of + * memory. + */ + for (i = 0; envp[i]; i++) + len += strlen(envp[i])+1; + + nenv = i + 1; + len += sizeof(char*)*nenv; + environ = malloc(len); + len = 0; + for (i = 0; envp[i]; i++) { + environ[i] = (char*)environ + nenv * sizeof(char*) + len; + strcpy(environ[i], envp[i]); + len += strlen(envp[i])+1; + } + environ[i] = NULL; + + /* Save start and extent of argv for setproctitle. */ + Argv = argv; + if (i > 0) + argv_size = envp[i - 1] + strlen(envp[i - 1]) - Argv[0]; + else + argv_size = argv[argc - 1] + strlen(argv[argc - 1]) - Argv[0]; +} + +static void +do_setproctitle(const char *cmdline) +{ + int len; + + len = strlen(cmdline) + 1; // +1 for '\0' + if (len > argv_size - 2) // 2 ?? + len = argv_size - 2; + memset(Argv[0], 0, argv_size); + strlcpy(Argv[0], cmdline, len); + Argv[1] = NULL; +} + +void +setproctitle(const char *format,...) +{ + char buf[256]; + va_list args; + va_start(args, format); + vsnprintf(buf, sizeof(buf), format, args); + do_setproctitle(buf); + va_end(args); +} + +#endif + |