-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharguments.c
148 lines (141 loc) · 3.41 KB
/
arguments.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Simple command line forwarder
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
size_t idx;
size_t len;
size_t growth;
char *buf;
} charbuf;
charbuf create_charbuf(size_t len, size_t growth) {
charbuf cb = {0};
cb.idx = 0;
cb.len = len;
cb.growth = growth;
cb.buf = (char *)malloc(sizeof(char) * len);
return cb;
}
void append_grow(charbuf *buffer, char character) {
if (buffer->idx == buffer->len) {
size_t newsize = buffer->len + buffer->growth;
char *temp = (char *)malloc(buffer->len);
for (size_t ptr = 0; ptr < buffer->len; ptr++) {
temp[ptr] = buffer->buf[ptr];
}
free(buffer->buf);
buffer->len = newsize;
buffer->buf = temp;
}
buffer->buf[buffer->idx++] = character;
}
int main(size_t argc, char *argv[]) {
// Constants
char DQT = '\"';
char BSL = '\\';
char *CONFIG = "./app.cfg";
char *FNF = "app.cfg not found, creating";
char *FAIL = "malloc failed";
char *R = "r";
char *WB = "wb";
// Accumulate the length of arguments, excluding the executable
size_t len = 0;
for (size_t i = 1; i < argc; i++) {
len += strlen(argv[i]);
}
FILE *config = fopen(CONFIG, R);
if (config == NULL) {
// Create if absent
printf(FNF);
config = fopen(CONFIG, WB);
// We aren't going to read an empty file
if (config != NULL) {
fclose(config);
config = NULL;
}
} else {
// Accumulate the length of the prepended arguments file
fseek(config, 0L, SEEK_END);
len += ftell(config);
rewind(config);
}
// Buffer for system call, don't worry, it gets freed
// +2: one separating file contents and our arguments, one null terminator
charbuf cmd = create_charbuf(len + 2, 32);
// Just being responsible
if (cmd.buf == NULL) {
printf(FAIL);
return -1;
}
// Number of consecutive control characters
size_t control = 0;
// Copy prepended arguments with escaping
if (config != NULL) {
// Number of consecutive printable characters
size_t normal = 0;
// Number of bypassed characters
size_t bypass = 0;
// Read entire file
for (char c = fgetc(config); c != EOF; c = fgetc(config)) {
if (c < ' ') {
// Contiguous control codes are replaced with a single space
if (control == 0) {
cmd.buf[cmd.idx++] = ' ';
}
control++;
normal = 0;
bypass = 0;
} else if (bypass > 0) {
// We are in a #comment
normal++;
bypass++;
} else if (normal == 0 && bypass == 0 && c == '#') {
// We are starting a #comment
control = 0;
bypass++;
} else {
// Normal appending arguments
cmd.buf[cmd.idx++] = c;
control = 0;
normal++;
}
}
// Clean up
fclose(config);
}
// Copy our arguments to system call
for (size_t i = 1; i < argc; i++) {
char *in = argv[i];
size_t l = strlen(in);
char escqt = 0;
append_grow(&cmd, ' ');
for (size_t j = 0; j < l; j++) {
if (DQT == in[j]) {
escqt++;
break;
}
}
if (escqt != 0) {
append_grow(&cmd, DQT);
}
for (size_t j = 0; j < l; j++) {
char argchar = in[j];
if (escqt != 0 && DQT == argchar) {
append_grow(&cmd, BSL);
}
append_grow(&cmd, argchar);
}
if (escqt != 0) {
append_grow(&cmd, DQT);
}
}
// Null terminator fill
while (cmd.idx < cmd.len) {
cmd.buf[cmd.idx++] = '\0';
}
// Run
int result = system(cmd.buf);
// Clean up
free(cmd.buf);
return result;
}