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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/stat.h>
#ifdef TEST
#include "unit-test.h"
#include <stdbool.h>
#endif
const int EXIT_ERROR = 1;
const int EXIT_USAGE = 2;
int usage(FILE *stream) {
const char *const msg = "Usage: remembering -p PROFILE -c COMMAND\n";
if (fprintf(stream, msg) < 0) {
perror("usage() - fprintf()");
return -1;
}
return 0;
}
static int help(FILE *stream) {
const char *const msg =
"Options:\n"
" -p, PROFILE profile to be used for gathering and storing data\n"
" -c, COMMAND commant to be run, reading from STDIN, writing to STDOUT\n"
" -h, --help show this help\n"
" -V, --version print the version number\n"
"\nSee \"man remembering\" for more information\n";
if (fprintf(stream, msg) < 0) {
perror("help() - fprint()");
return -1;
}
return 0;
}
static int version(FILE *stream) {
const char *const msg = "remembering-" VERSION " " DATE "\n";
if (fprintf(stream, msg) < 0) {
perror("version() - fprintf()");
return -1;
}
return 0;
}
static int missing(FILE *stream, const char *const argument) {
const char *const msg = "Missing option: %s\n";
if (fprintf(stream, msg, argument) < 0) {
perror("missing() - fprintf()");
return -1;
}
return 0;
}
static char *get_profile_file(const char *const profile, FILE *stream) {
char *home = getenv("XDG_DATA_HOME");
if (!home || (strcmp(home, "")) == 0) {
if (!(home = getenv("HOME"))) {
fprintf(stream, "Unable to get $XDG_DATA_HOME or $HOME environment variables\n");
return NULL;
}
const char *const suffix = "/.local/share/remembering";
const size_t home_suffix_size =
strlen(home) +
strlen(suffix) +
sizeof(char);
char *const home_with_suffix = malloc(home_suffix_size);
if (!home_with_suffix) {
perror("get_profile_file() - malloc() - home_with_suffix");
return NULL;
}
strcat(home_with_suffix, home);
strcat(home_with_suffix, suffix);
home = home_with_suffix;
} else {
if (!(home = strdup(home))) {
perror("get_profile_file() - strdup()");
return NULL;
}
}
size_t size =
strlen(home) +
strlen("/") +
strlen(profile) +
sizeof(char);
char *const profile_file = malloc(size);
if (!profile_file) {
perror("get_profile_file() - malloc() - profile_file");
free(home);
return NULL;
}
strcat(profile_file, home);
strcat(profile_file, "/");
strcat(profile_file, profile);
return profile_file;
}
#ifdef TEST
void test_get_profile_file() {
#define EXPECTED_FMT "\nexpected: %s\ngot: %s\n"
FILE *f = fopen("/dev/null", "w");
assert(f);
{
testing("get_profile_file() when $XDG_DATA_HOME is *unset*");
const char *const previous_xdg_data_home = getenv("XDG_DATA_HOME");
unsetenv("XDG_DATA_HOME");
const char *const HOME = getenv("HOME");
const char *const SUFFIX = "/.local/share/remembering/";
const char *const A_PROFILE = "A-PROFILE-NAME";
assert(HOME);
const size_t expected_size =
strlen(HOME) +
strlen(SUFFIX) +
strlen(A_PROFILE) +
sizeof(char);
char *const expected = malloc(expected_size);
assert(expected);
strcat(expected, HOME);
strcat(expected, SUFFIX);
strcat(expected, A_PROFILE);
char *const profile_file = get_profile_file(A_PROFILE, f);
assert(profile_file);
assertf(
strcmp(profile_file, expected) == 0,
EXPECTED_FMT,
expected,
profile_file
);
free(profile_file);
free(expected);
if (previous_xdg_data_home) {
setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
}
test_ok();
}
{
testing("get_profile_file() when $XDG_DATA_HOME is *empty*");
const char *const previous_xdg_data_home = getenv("XDG_DATA_HOME");
setenv("XDG_DATA_HOME", "", true);
const char *const HOME = getenv("HOME");
const char *const SUFFIX = "/.local/share/remembering/";
const char *const A_PROFILE = "ANOTHER-PROFILE-NAME";
assert(HOME);
const size_t expected_size =
strlen(HOME) +
strlen(SUFFIX) +
strlen(A_PROFILE) +
sizeof(char);
char *const expected = malloc(expected_size);
assert(expected);
strcat(expected, HOME);
strcat(expected, SUFFIX);
strcat(expected, A_PROFILE);
char *const profile_file = get_profile_file(A_PROFILE, f);
assert(profile_file);
assertf(
strcmp(profile_file, expected) == 0,
EXPECTED_FMT,
expected,
profile_file
);
free(profile_file);
free(expected);
if (previous_xdg_data_home) {
setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
} else {
unsetenv("XDG_DATA_HOME");
}
test_ok();
}
{
testing("get_profile_file() when $XDG_DATA_HOME is *set*");
const char *const previous_xdg_data_home = getenv("XDG_DATA_HOME");
const char *const XDG_DATA_HOME = "/a/custom/path";
const char *const A_PROFILE = "YET-ANOTHER-PROFILE-NAME";
const char *const EXPECTED = "/a/custom/path/YET-ANOTHER-PROFILE-NAME";
setenv("XDG_DATA_HOME", XDG_DATA_HOME, true);
char *const profile_file = get_profile_file(A_PROFILE, f);
assert(profile_file);
assertf(
strcmp(profile_file, EXPECTED) == 0,
EXPECTED_FMT,
EXPECTED,
profile_file
);
free(profile_file);
if (previous_xdg_data_home) {
setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
} else {
unsetenv("XDG_DATA_HOME");
}
test_ok();
}
{
testing("get_profile_file() when $HOME is *unset*");
const char *const previous_home = getenv("HOME");
const char *const previous_xdg_data_home = getenv("XDG_DATA_HOME");
unsetenv("XDG_DATA_HOME");
unsetenv("HOME");
assert(get_profile_file("ANY-PROFILE", f) == NULL);
setenv("HOME", previous_home, true);
if (previous_xdg_data_home) {
setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
}
test_ok();
}
fclose(f);
}
#endif
static int mkdir_p(const char *const path, mode_t mode) {
struct stat s;
int ret;
if (stat(path, &s) == 0 && S_ISDIR(s.st_mode)) {
printf("ISDIR: %s\n", path);
return 0;
}
char *const path_dup = strdup(path);
if (!path_dup) {
perror("mkdir_p() - strdup()");
return -1;
}
const char *const parent = dirname(path_dup);
if ((ret = mkdir_p(parent, mode))) {
free(path_dup);
return ret;
}
free(path_dup);
return mkdir(path, mode);
}
int main(int argc, char *argv[]) {
#ifdef TEST
test_get_profile_file();
return EXIT_SUCCESS;
#endif
for (int i = 0; i < argc; i++) {
if (strcmp("--", argv[i]) == 0) {
break;
} else if (strcmp("--help", argv[i]) == 0) {
if (usage(stdout)) return EXIT_ERROR;
if (help(stdout)) return EXIT_ERROR;
return EXIT_SUCCESS;
} else if (strcmp("--version", argv[i]) == 0) {
if (version(stdout)) return EXIT_ERROR;
return EXIT_SUCCESS;
}
}
int option;
const char *profile = NULL;
const char *command = NULL;
while ((option = getopt(argc, argv, "p:c:hV")) != -1) {
switch (option) {
case 'p':
profile = optarg;
break;
case 'c':
command = optarg;
break;
case 'h':
if (usage(stdout)) return EXIT_ERROR;
if (help(stdout)) return EXIT_ERROR;
return EXIT_SUCCESS;
case 'V':
if (version(stdout)) return EXIT_ERROR;
return EXIT_SUCCESS;
default:
if (usage(stderr)) return EXIT_ERROR;
return EXIT_USAGE;
}
}
if (!profile) {
if (missing(stderr, "-p PROFILE")) return EXIT_ERROR;
if (usage(stderr)) return EXIT_ERROR;
return EXIT_USAGE;
}
if (!command) {
if (missing(stderr, "-c COMMAND")) return EXIT_ERROR;
if (usage(stderr)) return EXIT_ERROR;
return EXIT_USAGE;
}
// end getopts
int ret = EXIT_SUCCESS;
char *profile_file = NULL;
if (!(profile_file = get_profile_file(profile, stderr))) {
ret = EXIT_ERROR;
goto cleanup;
}
char *profile_file_dup = NULL;
if (!(profile_file_dup = strdup(profile_file))) {
perror("main() - strdup()");
ret = EXIT_ERROR;
goto cleanup;
}
if ((ret = mkdir_p(dirname(profile_file_dup), S_IRWXU | S_IRWXG | S_IRWXO))) {
goto cleanup;
}
printf("profile: %s\ncommand: %s\n", profile, command);
printf("profile_file: %s\n", profile_file);
cleanup:
free(profile_file_dup);
free(profile_file);
return ret;
}
|