-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproctitle.c
96 lines (80 loc) · 2.01 KB
/
proctitle.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) 2009 Message Systems, Inc. All rights reserved
* For licensing information, see:
* https://bitbucket.org/wez/gimli/src/tip/LICENSE
*/
#include "impl.h"
/* SysV style ps title changing */
static char *title = NULL;
static size_t title_size = 0;
char *gimli_progname = "";
extern char **child_argv;
#ifndef __linux__
extern char **environ;
#endif
char **gimli_init_proctitle(int argc, char **argv)
{
char *end = NULL;
int i;
char **new_vec;
char buf[1024];
/* walk over the args; find the end. This assumes contiguous strings */
end = argv[0] + strlen(argv[0]);
for (i = 1; i < argc; i++) {
end = argv[i] + strlen(argv[i]);
}
if (end + 1 == environ[0]) {
/* if the environment is contiguous with the args, use that space too */
for (i = 0; environ[i] != NULL; i++) {
if (end + 1 == environ[i]) {
end = environ[i] + strlen(environ[i]);
}
}
new_vec = malloc((i + 1) * sizeof(char*));
for (i = 0; environ[i] != NULL; i++) {
new_vec[i] = strdup(environ[i]);
}
new_vec[i] = NULL;
environ = new_vec;
}
title = argv[0];
title_size = end - title;
new_vec = malloc((argc + 1) * sizeof(char*));
for (i = 0; i < argc; i++) {
new_vec[i] = strdup(argv[i]);
}
new_vec[i] = NULL;
argv = new_vec;
strcpy(buf, argv[0]);
gimli_progname = strdup(basename(buf));
return argv;
}
void gimli_set_proctitle(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
gimli_set_proctitlev(fmt, ap);
va_end(ap);
}
void gimli_set_proctitlev(const char *fmt, va_list ap)
{
int len;
size_t size = title_size;
char *start = title;
if (fmt[0] == '-') {
/* omit the gimli_progname */
fmt++;
} else {
snprintf(title, title_size, "%s: ", child_argv ? child_argv[0] : gimli_progname);
start += strlen(title);
size -= strlen(title);
}
vsnprintf(start, size, fmt, ap);
len = strlen(title);
memset(title + len, '\0', title_size - len);
if (debug) {
logprint("%s\n", title);
}
}
/* vim:ts=2:sw=2:et:
*/