2 * UCW Library -- Setting of Process Title
4 * (c) 2001--2006 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/process.h>
19 static char **spt_argv;
20 static char *spt_start, *spt_end;
23 setproctitle_init(int argc, char **argv)
27 char **env, **oldenv, *t;
31 /* Create a backup copy of environment */
34 for (i=0; oldenv[i]; i++)
35 len += strlen(oldenv[i]) + 1;
36 __environ = env = xmalloc(sizeof(char *)*(i+1));
38 for (i=0; oldenv[i]; i++)
41 len = strlen(oldenv[i]) + 1;
42 memcpy(t, oldenv[i], len);
47 /* Scan for consecutive free space */
48 spt_start = spt_end = argv[0];
49 for (i=0; i<argc; i++)
50 if (!i || spt_end+1 == argv[i])
51 spt_end = argv[i] + strlen(argv[i]);
52 for (i=0; oldenv[i]; i++)
53 if (spt_end+1 == oldenv[i])
54 spt_end = oldenv[i] + strlen(oldenv[i]);
56 /* Copy all arguments */
57 for (i=0; i<argc; i++)
58 argv[i] = xstrdup(argv[i]);
60 /* Recalculate program_invocation_name, otherwise syslog() will be confused. */
62 program_invocation_name = name;
63 char *p = strrchr(name, '/');
65 program_invocation_short_name = p + 1;
67 program_invocation_short_name = name;
72 setproctitle(const char *msg, ...)
79 if (spt_end > spt_start)
81 n = vsnprintf(buf, sizeof(buf), msg, args);
82 if (n >= (int) sizeof(buf) || n < 0)
83 sprintf(buf, "<too-long>");
84 n = spt_end - spt_start;
85 strncpy(spt_start, buf, n);
87 spt_argv[0] = spt_start;
96 return (spt_start < spt_end) ? spt_start : NULL;