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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(_WIN32) || defined(_WIN64)
#define HAVE_WINDOWS 1
#else
#define HAVE_WINDOWS 0
#endif
#if HAVE_WINDOWS && (defined(_MSC_BUILD) || defined(_MSC_VER) || defined(_MSC_EXTENSIONS))
#define HAVE_MSVC 1
#else
#define HAVE_MSVC 0
#endif
#if HAVE_WINDOWS && (defined(__MINGW32__) || defined(__MINGW64__))
#define HAVE_MINGW 1
#else
#define HAVE_MINGW 0
#endif
#if HAVE_WINDOWS
#if HAVE_MSVC
#include <io.h>
#if !defined(F_OK)
#define F_OK 0
#endif
typedef long long ssize_t;
typedef unsigned long long size_t;
#endif
#if HAVE_MINGW
#include <dirent.h>
#endif
#include <direct.h>
#include <windows.h>
#define DIRSEP_C '\\'
#define DIRSEP_S "\\"
#define PATHSEP_C ';'
#define PATHSEP_S ";"
#define PATHVAR "path"
#define mkdir(X, Y) mkdir(X)
#define mkstemp _mktemp
#else
#include <limits.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <errno.h>
#define DIRSEP_C '/'
#define DIRSEP_S "/"
#define PATHSEP_C ':'
#define PATHSEP_S ":"
#define PATHVAR "PATH"
#endif
#if !defined(PATH_MAX)
#define PATH_MAX 1024
#endif
#define ARG(X) strcmp(argv[i], X) == 0
#define ARG_VERIFY_NEXT() (argv[i+1] != NULL)
#define RECORD_STYLE_SHORT 0
#define RECORD_STYLE_LONG 1
#define RECORD_STYLE_CSV 2
#define RECORD_STYLE_DICT 3
char *program_name;
char *homedir;
char journalroot[PATH_MAX] = {0};
char intermediates[PATH_MAX] = {0};
const char *VERSION = "1.0.0";
const char *FMT_HEADER = "\x01\x01\x01## date: %s\n"
"## time: %s\n"
"## author: %s\n"
"## host: %s\n\x02\x02\x02";
const char *FMT_FOOTER = "\x03\x03\x03";
const char *USAGE_STATEMENT = \
"usage: %s [-h] [-V] [-dDys] [-]\n\n"
"Weekly Report Generator v%s\n\n"
"Options:\n"
"--help -h Show this usage statement\n"
"--dump-relative -d Dump records relative to current week\n"
"--dump-absolute -D Dump records by week value\n"
"--dump-year -y Set dump-[relative|absolute] year\n"
"--dump-style -s Set output style:\n"
" long (default)\n"
" short\n"
" csv\n"
" dict\n"
"--version -V Show version\n";
void usage() {
char *name;
unsigned is_base;
is_base = 0;
name = strrchr(program_name, DIRSEP_C);
if (name != NULL) {
is_base = 1;
}
printf(USAGE_STATEMENT, is_base ? name + 1 : program_name, VERSION);
}
char *find_program(const char *name) {
#if HAVE_WINDOWS
int found_extension;
#endif
static char exe[PATH_MAX] = {0};
char *token;
char *pathvar;
char *abs_prefix[] = {
"/", "./", ".\\"
};
for (size_t i = 0; i < sizeof(abs_prefix) / sizeof(char *); i++) {
if ((strlen(name) > 1 && name[1] == ':') || !strncmp(name, abs_prefix[i], strlen(abs_prefix[i]))) {
strcpy(exe, name);
return exe;
}
}
pathvar = getenv(PATHVAR);
if (!pathvar) {
return NULL;
}
token = strtok(pathvar, PATHSEP_S);
while (token != NULL) {
char filename[PATH_MAX] = {0};
sprintf(filename, "%s%c%s", token, DIRSEP_C, name);
#if HAVE_WINDOWS
// I am aware of PATHEXT. Let's stick to binary executables and shell scripts
char *ext[] = {".bat", ".cmd", ".com", ".exe"};
char filename_orig[PATH_MAX] = {0};
found_extension = 0;
for (size_t i = 0; i < sizeof(ext) / sizeof(char *); i++) {
if (strstr(filename, ext[i]) != NULL) {
found_extension = 1;
break;
}
}
if (!found_extension) {
for (size_t i = 0; i < sizeof(ext) / sizeof(char *); i++) {
strcpy(filename_orig, filename);
strcat(filename, ext[i]);
if (access(filename, F_OK) == 0) {
strcpy(exe, filename);
return exe;
}
strcpy(filename, filename_orig);
}
} else {
if (access(filename, F_OK) == 0) {
strcpy(exe, filename);
return exe;
}
}
#else
if (access(filename, F_OK) == 0) {
strcpy(exe, filename);
return exe;
}
#endif
token = strtok(NULL, PATHSEP_S);
}
return NULL;
}
int edit_file(const char *filename) {
char editor[255] = {0};
char editor_cmd[PATH_MAX] = {0};
int result;
const char *user_editor;
// Allow the user to override the default editor (vi/notepad)
user_editor = getenv("EDITOR");
char *editor_path;
if (user_editor != NULL) {
sprintf(editor, "%s", user_editor);
} else {
editor_path = find_program("vim");
if (editor_path != NULL) {
sprintf(editor, "\"%s\"", editor_path);
}
#if HAVE_WINDOWS
else {
strcpy(editor, "notepad");
}
#endif
}
if (!strlen(editor)) {
fprintf(stderr, "Unable to find editor: %s\n", editor);
exit(1);
}
// Tell editor to jump to the end of the file (when supported)
// Standard 'vi' does not support '+'
if(strstr(editor, "vim")) {
strcat(editor, " +");
} else if (strstr(editor, "nano") != NULL) {
strcat(editor, " +9999");
}
sprintf(editor_cmd, "%s %s", editor, filename);
result = system(editor_cmd);
return result;
}
int make_path(char *basepath) {
return mkdir(basepath, 0755);
}
char *make_output_path(char *basepath, char *path, int year, int week, int day_of_week) {
strcpy(path, basepath);
// Add trailing slash if it's not there
if (strlen(basepath) > 0 && strlen(basepath) - 1 != DIRSEP_C) {
strcat(path, DIRSEP_S);
}
make_path(path);
// year
sprintf(path + strlen(path), "%d%c", year, DIRSEP_C);
make_path(path);
// week of year
sprintf(path + strlen(path), "%d%c", week, DIRSEP_C);
make_path(path);
// day of that week
sprintf(path + strlen(path), "%d", day_of_week);
return path;
}
ssize_t get_file_size(const char *filename) {
ssize_t result;
FILE *fp;
fp = fopen(filename, "r");
if (!fp) {
return -1;
}
fseek(fp, 0, SEEK_END);
result = ftell(fp);
fclose(fp);
return result;
}
char *init_tempfile(const char *basepath, const char *ident, char *data) {
FILE *fp;
char *filename;
filename = calloc(PATH_MAX, sizeof(char));
sprintf(filename, "%s%cweekly_%s.XXXXXX", basepath, DIRSEP_C, ident);
if ((mkstemp(filename)) < 0) {
return NULL;
}
unlink(filename);
fp = fopen(filename, "w+b");
if (!fp) {
return NULL;
}
if (data != NULL) {
fwrite(data, sizeof(char), strlen(data), fp);
//fprintf(fp, "%s\n", data);
}
fclose(fp);
if (chmod(filename, 0600) < 0) {
return NULL;
}
return filename;
}
int append_stdin(const char *filename) {
FILE *fp;
size_t bufsz;
char *buf;
bufsz = BUFSIZ;
buf = malloc(bufsz);
if (!buf) {
return -1;
}
fp = fopen(filename, "a");
if (!fp) {
perror(filename);
free(buf);
return -1;
}
#if HAVE_WINDOWS
while (fgets(buf, (int) bufsz, stdin) != NULL) {
fprintf(fp, "%s", buf);
}
#else
while (getline(&buf, &bufsz, stdin) >= 0) {
fprintf(fp, "%s", buf);
}
#endif
free(buf);
fclose(fp);
return 0;
}
int append_contents(const char *dest, const char *src) {
char buf[BUFSIZ] = {0};
FILE *fpi, *fpo;
fpi = fopen(src, "rb+");
if (!fpi) {
perror(src);
return -1;
}
fpo = fopen(dest, "ab+");
if (!fpo) {
perror(dest);
fclose(fpi);
return -1;
}
// Append source file to destination file
while (fread(buf, sizeof(char), sizeof(buf), fpi) > 0) {
fwrite(buf, sizeof(char), strlen(buf), fpo);
}
buf[0] = '\n';
fwrite(buf, sizeof(char), 1, fpo);
fclose(fpo);
fclose(fpi);
return 0;
}
struct Record {
char *date;
char *time;
char *user;
char *host;
char *data;
};
void record_free(struct Record *record) {
if (record != NULL) {
free(record->date);
free(record->time);
free(record->host);
free(record->user);
free(record->data);
free(record);
}
}
struct Record *record_parse(const char *content) {
char *next;
struct Record *result;
result = calloc(1, sizeof(*result));
if (!result) {
perror("Unable to allocate record");
return NULL;
}
char *p = strdup(content);
next = strtok(p, "\n");
while (next != NULL) {
char key[10] = {0};
char value[255] = {0};
sscanf(next, "## %9[^: ]:%254s[^\n]", key, value);
if (strncmp(next, "## ", 2) != 0) {
break;
}
if (!strcmp(key, "date"))
result->date = strdup(value);
else if (!strcmp(key, "time"))
result->time = strdup(value);
else if (!strcmp(key, "author"))
result->user = strdup(value);
else if (!strcmp(key, "host"))
result->host = strdup(value);
next = strtok(NULL, "\n");
}
if (next != NULL) {
if (memcmp(next, "\x02\x02\x02", 3) == 0) {
next += 4;
}
result->data = strdup(next);
} else {
// Empty data record, die
record_free(result);
result = NULL;
}
free(p);
return result;
}
struct Record *get_records(FILE **fp) {
ssize_t soh, sot, eot;
size_t record_size;
char task[2] = {0};
char *buf = calloc(BUFSIZ, sizeof(char));
if (!buf) {
return NULL;
}
if (!*fp) {
free(buf);
return NULL;
}
// Start of header offset
soh = 0;
// Start of text offset
sot = 0;
// End of text offset
eot = 0;
while (fread(task, sizeof(char), 1, *fp) > 0) {
if (task[0] == '\x01' && fread(task, sizeof(char), 2, *fp) > 0) {
// start header
if (memcmp(task, "\x01\x01", 2) == 0) {
soh = ftell(*fp);
}
} else if (task[0] == '\x02' && fread(task, sizeof(char), 2, *fp) > 0) {
if (memcmp(task, "\x02\x02", 2) == 0) {
sot = ftell(*fp);
}
// start of text
} else if (task[0] == '\x03' && fread(task, sizeof(char), 2, *fp) > 0) {
if (memcmp(task, "\x03\x03", 2) == 0) {
eot = ftell(*fp);
break;
}
} else {
continue;
}
memset(task, '\0', sizeof(task));
}
// Verify the record is not too small, and contained a start of text marker
record_size = eot - soh;
if (record_size < 1 && !sot) {
free(buf);
return NULL;
}
// Go back to start of header
fseek(*fp, soh, SEEK_SET);
// Read the entire record
fread(buf, sizeof(char), record_size, *fp);
// Remove end of text marker
memset(buf + (record_size - 4), '\0', 4);
// Truncate buffer at end of line
buf[strlen(buf) - 1] = '\0';
// Emit record
struct Record *result;
result = record_parse(buf);
free(buf);
return result;
}
void record_show(struct Record *record, int style) {
const char *fmt;
switch (style) {
case RECORD_STYLE_LONG:
fmt = "## Date: %s\n## Time: %s\n## User: %s\n## Host: %s\n%s\n";
break;
case RECORD_STYLE_CSV:
fmt = "%s,%s,%s,%s,\"%s\""; // Trailing linefeed omitted for visual clarity
break;
case RECORD_STYLE_DICT:
fmt = "{"
"\"date\": \"%s\",\n"
"\"time\": \"%s\",\n"
"\"user\": \"%s\",\n"
"\"host\": \"%s\",\n"
"\"data\": \"%s\"}\n";
break;
case RECORD_STYLE_SHORT:
default:
fmt = "%s - %s - %s (%s):\n%s\n";
break;
}
printf(fmt, record->date, record->time, record->user, record->host, record->data);
}
int dump_file(const char *filename, int style) {
FILE *fp;
fp = fopen(filename, "r");
if (!fp) {
return -1;
}
struct Record *record;
while ((record = get_records(&fp)) != NULL) {
record_show(record, style);
record_free(record);
puts("");
}
fclose(fp);
return 0;
}
#if HAVE_MSVC
int dir_empty (const char *path) {
HANDLE hd;
WIN32_FIND_DATA data;
size_t i;
char winpath[PATH_MAX] = {0};
i = 0;
sprintf(winpath, "%s%c*.*", path, DIRSEP_C);
if ((hd = FindFirstFile(path, &data)) != INVALID_HANDLE_VALUE) {
do {
i++;
} while (FindNextFile(hd, &data) != 0);
FindClose(hd);
} else {
return -1;
}
return i != 0;
}
#else
int dir_empty(const char *path) {
DIR *dir;
struct dirent *dp;
size_t i;
dir = opendir(path);
if (!dir) {
return -1;
}
i = 0;
while ((dp = readdir(dir)) != NULL) {
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
continue;
}
i++;
}
closedir(dir);
return i != 0;
}
#endif
int dump_week(const char *root, int year, int week, int style) {
char path_week[PATH_MAX] = {0};
char path_year[PATH_MAX] = {0};
const int max_days = 7;
sprintf(path_year, "%s%c%d", root, DIRSEP_C, year);
sprintf(path_week, "%s%c%d%c%d", root, DIRSEP_C, year, DIRSEP_C, week);
if (!dir_empty(path_year)) {
return -1;
}
for (int i = 0; i < max_days; i++) {
char tmp[PATH_MAX];
sprintf(tmp, "%s%c%d", path_week, DIRSEP_C, i);
dump_file(tmp, style);
}
return 0;
}
int main(int argc, char *argv[]) {
// System info
char sysname[255] = {0};
char username[255] = {0};
#if HAVE_WINDOWS
DWORD usernamesz;
usernamesz = sizeof(username);
DWORD sysnamesz;
sysnamesz = sizeof(sysname);
homedir = getenv("USERPROFILE");
#else
struct passwd *user;
homedir = getenv("HOME");
#endif
// Time and datestamp
time_t t;
struct tm *tm_;
int year, week, day_of_week;
char datestamp[255] = {0};
char timestamp[255] = {0};
// Path and data buffers
char *headerfile;
char *tempfile;
char *footerfile;
char journalfile[PATH_MAX] = {0};
char header[255];
char footer[255];
// Argument triggers
int do_stdin;
int do_dump;
int do_year;
int do_style;
int user_year;
char *user_year_error;
int user_week;
char *user_week_error;
int style;
// Set program name
program_name = argv[0];
// Get current time
t = time(NULL);
// Populate tm struct
tm_ = localtime(&t);
// Time data fix ups
year = tm_->tm_year + 1900;
week = (tm_->tm_yday + 7 - (tm_->tm_wday + 1 ? (tm_->tm_wday - 1) : 6)) / 7;
day_of_week = tm_->tm_wday;
// Set default output style
style = RECORD_STYLE_LONG;
#if HAVE_WINDOWS
// Get system name
if(!GetComputerName(sysname, &sysnamesz)) {
perror("Unable to get system host name");
strcpy(sysname, "unknown");
}
// Get user information
if(!GetUserName(username, &usernamesz)) {
perror("Unable to read account information");
strcpy(username, "unknown");
}
#else
// Get system name
if (gethostname(sysname, sizeof(sysname) - 1) < 0) {
perror("Unable to get system host name");
strcpy(sysname, "unknown");
}
// Get user information
user = getpwuid(getuid());
if (user == NULL) {
perror("Unable to read account information");
strcpy(username, "unknown");
} else {
strcpy(username, user->pw_name);
}
#endif
// Get data;
strftime(datestamp, sizeof(datestamp) - 1, "%m/%d/%Y", tm_);
strftime(timestamp, sizeof(timestamp) - 1, "%H:%M:%S", tm_);
// Populate header string
sprintf(header, FMT_HEADER, datestamp, timestamp, username, sysname);
// Populate footer string
strcpy(footer, FMT_FOOTER);
// Populate path(s)
sprintf(journalroot, "%s%c.weekly", homedir, DIRSEP_C);
sprintf(intermediates, "%s%ctmp", journalroot, DIRSEP_C);
// Prime argument triggers
do_stdin = 0;
do_dump = 0;
do_year = 0;
// Parse user arguments
for (int i = 1; i < argc; i++) {
if (ARG("-h") || ARG("--help")) {
usage();
exit(0);
}
if (ARG("-V") || ARG("--version")) {
puts(VERSION);
exit(0);
}
if (ARG("-")) {
do_stdin = 1;
}
if (ARG("-d") || ARG("--dump-relative")) {
if (ARG_VERIFY_NEXT()) {
user_week = (int) strtol(argv[i + 1], &user_week_error, 10);
if (*user_week_error != '\0') {
fprintf(stderr, "Invalid integer\n");
exit(1);
}
week -= user_week;
}
do_dump = 1;
}
if (ARG("-D") || ARG("--dump-absolute")) {
if (ARG_VERIFY_NEXT()) {
user_week = (int) strtol(argv[i + 1], &user_week_error, 10);
if (*user_week_error != '\0') {
fprintf(stderr, "Invalid integer\n");
exit(1);
}
week = user_week;
}
do_dump = 1;
}
if (ARG("-y") || ARG("--dump-year")) {
if (!ARG_VERIFY_NEXT()) {
fprintf(stderr, "--dump-year (-y) requires an integer year\n");
exit(1);
}
user_year = (int) strtol(argv[i + 1], &user_year_error, 10);
if (*user_year_error != '\0') {
fprintf(stderr, "Invalid integer\n");
exit(1);
}
year = user_year;
do_year = 1;
}
if (ARG("-s") || ARG("--dump-style")) {
if (!ARG_VERIFY_NEXT()) {
fprintf(stderr, "--dump-style (-s) requires a style argument (i.e. short, long, csv, dict)\n");
exit(1);
}
if (!strcmp(argv[i + 1], "short")) {
style = RECORD_STYLE_SHORT;
} else if (!strcmp(argv[i + 1], "long")) {
style = RECORD_STYLE_LONG;
} else if (!strcmp(argv[i + 1], "csv")) {
style = RECORD_STYLE_CSV;
} else if (!strcmp(argv[i + 1], "dict")) {
style = RECORD_STYLE_DICT;
}
do_style = 1;
}
}
if (do_year && !do_dump) {
fprintf(stderr, "Option --dump-year (-y) requires options -d or -D\n");
exit(1);
}
if (do_style && !do_dump) {
fprintf(stderr, "Option --dump-style (-s) requires options -d or -D\n");
exit(1);
}
if (do_dump) {
if (week < 1) {
week = 1;
}
if (dump_week(journalroot, year, week, style) < 0) {
fprintf(stderr, "No entries found for week %d of %d\n", week, year);
exit(1);
}
exit(0);
}
// Create weekly root directory
make_path(journalroot);
// Write header string to temporary file
make_path(intermediates);
if ((headerfile = init_tempfile(intermediates, "header", header)) == NULL) {
perror("Unable to create temporary header file");
exit(1);
}
char nothing[1] = {0};
if ((tempfile = init_tempfile(intermediates, "tempfile", nothing)) == NULL) {
perror("Unable to create temporary file");
exit(1);
}
if ((footerfile = init_tempfile(intermediates, "footer", footer)) == NULL) {
perror("Unable to create footer file");
exit(1);
}
// Create new weekly journalfile path
if (!make_output_path(journalroot, journalfile, year, week, day_of_week)) {
fprintf(stderr, "Unable to create output path: %s (%s)\n", journalfile, strerror(errno));
perror(journalfile);
unlink(headerfile);
unlink(tempfile);
unlink(footerfile);
exit(1);
}
// Get original size of the temporary file
ssize_t tempfile_size;
tempfile_size = get_file_size(tempfile);
if (do_stdin) {
if (append_stdin(tempfile) < 0) {
fprintf(stderr, "Failed to read from stdin\n");
exit(1);
}
} else {
// Open the temporary file with an editor so the user can write their notes
if (edit_file(tempfile) != 0) {
fprintf(stderr, "Non-zero exit status from editor. Aborting.\n");
fprintf(stderr, "Dead entry file: %s\n", tempfile);
exit(1);
}
}
// Test whether temporary file size increased. If not, die.
ssize_t tempfile_newsize;
tempfile_newsize = get_file_size(tempfile);
if (tempfile_newsize <= tempfile_size) {
fprintf(stderr, "Empty message, aborting.\n");
unlink(headerfile);
unlink(tempfile);
unlink(footerfile);
exit(1);
}
// Copy data from the header file to the weekly journal path
if (append_contents(journalfile, headerfile) < 0) {
fprintf(stderr, "Unable to append contents of '%s' to '%s' (%s)\n", headerfile, journalfile, strerror(errno));
exit(1);
}
// Copy data from the temporary file to the weekly journal path
if (append_contents(journalfile, tempfile) < 0) {
fprintf(stderr, "Unable to append contents of '%s' to '%s' (%s)\n", tempfile, journalfile, strerror(errno));
exit(1);
}
// Copy data from the footer file to the weekly journal path
if (append_contents(journalfile, footerfile) < 0) {
fprintf(stderr, "Unable to append contents of '%s' to '%s' (%s)\n", footerfile, journalfile, strerror(errno));
exit(1);
}
// Nuke the temporary files (report on error, but keep going)
if (access(headerfile, F_OK) == 0 && unlink(headerfile) < 0) {
fprintf(stderr, "Unable to remove header file: %s (%s)\n", headerfile, strerror(errno));
}
if (access(tempfile, F_OK) == 0 && unlink(tempfile) < 0) {
fprintf(stderr, "Unable to remove temporary file: %s (%s)\n", tempfile, strerror(errno));
}
if (access(footerfile, F_OK) == 0 && unlink(footerfile) < 0) {
fprintf(stderr, "Unable to remove footer file: %s (%s)\n", footerfile, strerror(errno));
}
// Inform the user
printf("Message written to: %s\n", journalfile);
return 0;
}
|