summaryrefslogtreecommitdiffstats
path: root/mbbsd/args.c
blob: 1d1286731326e728eb4eb18e2074351b67cd98dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* $Id: args.c,v 1.7 2002/07/22 19:02:00 in2 Exp $ */
#include "bbs.h"
#ifdef HAVE_SETPROCTITLE

void
initsetproctitle(int argc, char **argv, char **envp)
{
}

#else


char          **Argv = NULL;    /* pointer to argument vector */
char           *LastArgv = NULL;/* end of argv */
extern char   **environ;

void
initsetproctitle(int argc, char **argv, char **envp)
{
    register int    i;

    /*
     * Move the environment so setproctitle can use the space at the top of
     * memory.
     */
    for (i = 0; envp[i]; i++);
    environ = malloc(sizeof(char *) * (i + 1));
    for (i = 0; envp[i]; i++)
    environ[i] = strdup(envp[i]);
    environ[i] = NULL;

    /* Save start and extent of argv for setproctitle. */
    Argv = argv;
    if (i > 0)
    LastArgv = envp[i - 1] + strlen(envp[i - 1]);
    else
    LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
}

static void
do_setproctitle(const char *cmdline)
{
    char            buf[256], *p;
    int             i;

    strncpy(buf, cmdline, 256);
    buf[255] = '\0';
    i = strlen(buf);
    if (i > LastArgv - Argv[0] - 2) {
    i = LastArgv - Argv[0] - 2;
    }
    strlcpy(Argv[0], buf, sizeof(buf));
    p = &Argv[0][i];
    while (p < LastArgv)
    *p++ = '\0';
    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