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.
17 static char **spt_argv;
18 static char *spt_start, *spt_end;
21 setproctitle_init(int argc, char **argv)
25 char **env, **oldenv, *t;
29 /* Create a backup copy of environment */
32 for (i=0; oldenv[i]; i++)
33 len += strlen(oldenv[i]) + 1;
34 __environ = env = xmalloc(sizeof(char *)*(i+1));
36 for (i=0; oldenv[i]; i++)
39 len = strlen(oldenv[i]) + 1;
40 memcpy(t, oldenv[i], len);
45 /* Scan for consecutive free space */
46 spt_start = spt_end = argv[0];
47 for (i=0; i<argc; i++)
48 if (!i || spt_end+1 == argv[i])
49 spt_end = argv[i] + strlen(argv[i]);
50 for (i=0; oldenv[i]; i++)
51 if (spt_end+1 == oldenv[i])
52 spt_end = oldenv[i] + strlen(oldenv[i]);
57 setproctitle(const char *msg, ...)
64 if (spt_end > spt_start)
66 n = vsnprintf(buf, sizeof(buf), msg, args);
67 if (n >= (int) sizeof(buf) || n < 0)
68 sprintf(buf, "<too-long>");
69 n = spt_end - spt_start;
70 strncpy(spt_start, buf, n);
72 spt_argv[0] = spt_start;
81 return (spt_start < spt_end) ? spt_start : NULL;