forked from viabtc/viabtc_exchange_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathut_title.c
82 lines (64 loc) · 1.65 KB
/
ut_title.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
/*
* Description:
* History: [email protected], 2016/03/26, create
*/
# ifndef _GNU_SOURCE
# define _GNU_SOURCE
# endif
# include <stdio.h>
# include <stddef.h>
# include <stdlib.h>
# include <stdarg.h>
# include <string.h>
# include <errno.h>
# include "ut_title.h"
extern char **environ;
static char *title_base;
static char *title_tail;
void process_title_init(int argc, char *argv[])
{
if (title_base)
return;
char *base = argv[0];
char *tail = argv[argc-1] + strlen(argv[argc-1]) + 1;
char **envp = environ;
for (int i = 0; envp[i]; ++i) {
if (envp[i] < tail)
continue;
tail = envp[i] + strlen(envp[i]) + 1;
}
/* dup program name */
program_invocation_name = strdup(program_invocation_name);
program_invocation_short_name = strdup(program_invocation_short_name);
/* dup argv */
for (int i = 0; i < argc; ++i) {
argv[i] = strdup(argv[i]);
}
/* dup environ */
clearenv();
for (int i = 0; envp[i]; ++i) {
char *eq = strchr(envp[i], '=');
if (eq == NULL)
continue;
*eq = '\0';
setenv(envp[i], eq + 1, 1);
*eq = '=';
}
title_base = base;
title_tail = tail;
memset(title_base, 0, title_tail - title_base);
}
void process_title_set(const char *fmt, ...)
{
if (!title_base)
return;
char buf[256];
va_list ap;
va_start(ap, fmt);
int len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
int title_len = len < (title_tail - title_base) ? len : (title_tail - title_base - 1);
memcpy(title_base, buf, title_len);
title_base[title_len] = '\0';
return;
}