-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.c
110 lines (93 loc) · 2.98 KB
/
cmdline.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* This file is part of win_battery_log.
*
* win_battery_log is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* win_battery_log is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with win_battery_log. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <time.h>
#include "cmdline.h"
static int append_to_cmdline(char **pos, size_t rem, char *arg,
enum cmdline_build_mode mode, bool last)
{
if (!arg)
return 0;
char format[] = "\"%s\" ";
size_t fmtlen = strlen(format);
switch(mode) {
case CMDLINE_CREATE_PROCESS:
fmtlen = strlen(strncpy(format, "\"%s\" ", fmtlen));
break;
case CMDLINE_LOG_FILENAME:
fmtlen = strlen(strncpy(format, "%s_", fmtlen));
break;
default:
assert(0);
}
if (last)
format[fmtlen - 1]= '\0';
int added = snprintf(*pos, rem, format, arg);
assert(added <= rem);
*pos += added;
return added;
}
static void fix_log_filename(char *pos)
{
const char allowed[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_,.;";
if (pos && pos[0] &&
(pos[0] == '.' || pos[0] == '-'))
pos[0] = '_';
size_t rem = strlen(pos);
while (true) {
size_t offset = strspn(pos, allowed);
if (rem <= offset)
return;
rem -= offset;
pos += offset;
pos[0] = '-';
}
}
char *cmdline_build(int argc, char *argv[], enum cmdline_build_mode mode)
{
if (argc <= 1)
return NULL;
size_t len = 0;
for (int i = 1; i < argc; i++)
// the string, quotes and space or null
len += strlen(argv[i]) + 2 + 1;
const size_t maxlen = 32768;
if (maxlen < len) {
fprintf(stderr, "ERROR: Command line too long: %z characters > %z\n", len, maxlen);
return NULL;
}
char *suffix = NULL;
if (mode == CMDLINE_LOG_FILENAME)
len += asprintf(&suffix, "_%019lld.log", time(NULL));
char *cmdline = calloc(len, sizeof(char));
if (!cmdline) {
fprintf(stderr, "ERROR: Out of memory allocating command line string\n");
return NULL;
}
size_t rem = len;
char *pos = cmdline;
for (int i = 1; i < argc; i++)
rem -= append_to_cmdline(&pos, rem, argv[i], mode, i == argc - 1);
rem -= append_to_cmdline(&pos, rem, suffix, mode, true);
if (mode == CMDLINE_LOG_FILENAME)
fix_log_filename(cmdline);
return cmdline;
}