-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmkiocccentry.c
8845 lines (8248 loc) · 296 KB
/
mkiocccentry.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
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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mkiocccentry - form IOCCC entry compressed tarball
*
* "Because even printf has a return value worth paying attention to." :-)
*
* Make an IOCCC compressed tarball for an IOCCC submission.
*
* We will form the IOCCC entry compressed tarball "by hand" in C.
* Not in some high level language, but standard vanilla (with a healthy
* overdose of chocolate :-) ) C. Why? Because this is an obfuscated C
* contest. But then why isn't this code obfuscated? Because the IOCCC judges
* prefer to write in robust unobfuscated code. Besides, the IOCCC was started
* as an ironic commentary on the Bourne shell source and finger daemon source.
* Moreover, irony is well baked-in to the IOCCC. :-)
*
* OK, we do make use of shell scripts to help build and test
* this repo: but who doesn't use a bit of shell scripting now and then? :-)
* Nevertheless, the core of building your IOCCC entry compressed tarball,
* the code that you, the IOCCC contestant will use, is all written in
* C because this is the IOCCC.
*
* If you do find a problem with this code, please let the us know by opening an
* issue at the GitHub issues page:
*
* https://github.com/ioccc-src/mkiocccentry/issues
*
* Many thanks are due to a number of people who provided important
* and valuable testing, suggestions, issue reports and GitHub pull
* requests to this code. Without their time and effort, this tool
* would not work very well!
*
* Among the GitHub users we wish to thank include these fine developers
* in alphabetical GitHub username order:
*
* @ilyakurdyukov Ilya Kurdyukov
* @SirWumpus Anthony Howe
* @vog Volker Diels-Grabsch
*
* Copyright (c) 2021-2025 by Landon Curt Noll and Cody Boone Ferguson.
* All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright, this permission notice and text
* this comment, and the disclaimer below appear in all of the following:
*
* supporting documentation
* source copies
* source works derived from this source
* binaries derived from this source or from derived source
*
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* This tool was co-developed in 2021-2025 by Cody Boone Ferguson and Landon
* Curt Noll:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
* and:
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* "Because sometimes even the IOCCC Judges need some help." :-)
*
* Share and enjoy! :-)
* -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
*/
/* special comments for the seqcexit tool */
/* exit code out of numerical order - ignore in sequencing - ooo */
/* exit code change of order - use new value in sequencing - coo */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h> /* for WEXITSTATUS() */
#include <fcntl.h> /* for open() */
#include <fts.h>
#include <locale.h>
/*
* mkiocccentry - form IOCCC entry compressed tarball
*/
#include "mkiocccentry.h"
/*
* definitions
*/
#define REQUIRED_ARGS (2) /* number of required arguments on the command line */
#define CHUNK (39) /* allocate CHUNK elements at a time */
/*
* usage message
*
* Use the usage() function to print the usage_msg([0-9]?)+ strings.
*/
static const char * const usage_msg0 =
"usage: %s [options] workdir topdir\n"
"\noptions:\n"
"\t-h\t\tprint help message and exit\n"
"\t-v level\tset verbosity level: (def level: %d)\n"
"\t-J level\tset JSON verbosity level (def level: %d)\n"
"\t-q\t\tquiet mode (def: loud :-) )\n"
"\t\t\t NOTE: -q will also silence msg(), warn(), warnp() if -v 0\n"
"\t-V\t\tprint version string and exit\n"
"\t-W\t\tignore all warnings\n"
"\t\t\t NOTE: the judges will NOT use this option\n"
"\t-E\t\texit non-zero after the first warning (def: do not)\n"
"\t\t\t NOTE: one cannot use both -W and -E.\n"
"\t-y\t\tanswer yes to most questions (use with EXTREME caution!)\n"
"\t-Y\t\tforce answer yes even when using -i answers";
static const char * const usage_msg1 =
"\t-t tar\t\tpath to tar(1) that supports the -J (xz) option (def: %s)\n"
"\t-l ls\t\tpath to ls(1) (def: %s)\n"
"\t-T txzchk\tpath to txzchk(1) (def: %s)\n"
"\t-e\t\tentertainment mode (for txzchk)\n"
"\t-f feathers\tdefine how many feathers is feathery (for -e)\n"
"\t-F fnamchk\tpath to fnamchk(1) (used by txzchk) (def: %s)";
static const char * const usage_msg2 =
"\t-C chkentry\tpath to chkentry(1) (def: %s)\n"
"\t-m make\t\tpath to GNU compatible make(1) (def: %s)";
static const char * const usage_msg3 =
"\t-a answers\twrite answers to a text file for future updates to the submission\n"
"\t-A answers\twrite answers file even if it already exists\n"
"\t-i answers\tread answers from file previously written by -a|-A answers\n"
"\t\t\t NOTE: One cannot use both -a/-A answers and -i answers.\n"
"\t-u uuidfile\tread UUID from a file (def: prompt for UUID)\n"
"\t\t\t NOTE: if an invalid UUID is in the file, it will try the usual way\n"
"\t\t\t NOTE: this option may not be used with -U UUID, -d, -s seed or -i answers\n"
"\t-U UUID\t\tset UUID to UUID\n"
"\t\t\t NOTE: if an invalid UUID is in the file, it will try the usual way\n"
"\t\t\t NOTE: this option may not be used with -u uuidfile, -d, -s seed or -i answers\n"
"\t-s seed\t\tGenerate and use pseudo-random answers, seeding with\n"
"\t\t\t seed & 0x%08u (def: do not)\n"
"\t-d\t\tAlias for -s %u\n"
"\t\t\t NOTE: implies -y -E -A random_answers.seed and -i random_answers.seed\n"
"\t\t\t NOTE: one cannot use -a/-A or -i with -s seed/-d.\n"
"\t\t\t NOTE: this is the only time -a/-A can be used with -i answers.\n"
"\t-I path\t\tignore path (to file or directory) under topdir\n"
"\t\t\t NOTE: you can ignore more than one file or directory with multiple -I args\n";
static const char * const usage_msg4 =
"\t-x\t\tforce delete submission directory if it already exists (def: don't)\n"
"\t-r rm\t\tset path to rm\n\n"
"\tworkdir\t\tdirectory where the submission directory and tarball are formed\n"
"\t\t\t NOTE: the submission directory under this directory must NOT exist\n"
"\ttopdir\t\tdirectory with required, optional and extra files and/or directories\n"
"\t\t\t NOTE: the topdir MUST have your prog.c, Makefile and remarks.md\n";
static const char * const usage_msg5 =
"\n"
"Exit codes:\n"
" 0 all OK\n"
" 1 -E and some warning was issued\n"
" 2 -h and help string printed or -V and version string printed\n"
" 3 invalid command line, invalid option or option missing an argument\n"
" 4 something went wrong in scanning, copying or verifying topdir and workdir\n"
" 5 user says something about the topdir or workdir is not okay\n"
" >= 10 internal error\n"
"\n"
"%s version: %s\n"
"jparse utils version: %s\n"
"jparse UTF-8 version: %s\n"
"jparse library version: %s";
/*
* globals
*/
static bool quiet = false; /* true ==> quiet mode */
static bool need_confirm = true; /* true ==> ask for confirmations */
static bool need_hints = true; /* true ==> show hints */
static bool ignore_warnings = false; /* true ==> ignore all warnings (this does NOT mean the judges will! :) */
static bool abort_on_warning = false; /* true ==> exit non-zero after first warning */
static FILE *input_stream = NULL; /* input file: stdin or answers file */
static bool answer_yes = false; /* true ==> -y used: always answer yes (use with EXTREME caution!) */
static bool entertain = false; /* entertainment mode for txzchk -e */
static uintmax_t feathery = 3; /* for entertain option of txzchk (-e) */
static bool silence_prompt = false; /* true ==> do not display prompts */
static bool read_answers_flag_used = false; /* true ==> -i read answers from answers file */
static bool seed_used = false; /* true ==> -d or -s seed given */
static bool copying_topdir = false; /* true ==> copying topdir and checking submission dir */
static bool saved_answer_yes = false; /* set to answer_yes before modifying it for scanning/copying topdir */
static bool saved_silence_prompt = false; /* set to silence_prompt before modifying it for scanning/copying topdir */
static bool force_yes = false; /* force -y even when scanning/copying/verifying in -i answers mode */
static struct stat topdir_st; /* stat(2) information of topdir */
static struct stat workdir_st; /* stat(2) information of workdir */
/*
* forward declarations
*/
static void usage(int exitcode, char const *program, char const *str) __attribute__((noreturn));
static bool noprompt_yes_or_no(void);
static void append_unique_filename(struct dyn_array *array, char *str);
int
main(int argc, char *argv[])
{
char const *program = NULL; /* our name */
extern char *optarg; /* option argument */
extern int optind; /* argv index of the next arg */
struct timeval tp; /* gettimeofday time value */
char const *workdir = NULL; /* where the submission directory and tarball are formed */
char *prog_c = NULL; /* path to prog.c */
char *Makefile = NULL; /* path to Makefile */
char *remarks_md = NULL; /* path to remarks.md */
char **topdir = NULL; /* directory from which files are to be copied to the workdir */
char *tar = TAR_PATH_0; /* path to tar executable that supports the -J (xz) option */
char *rm = RM_PATH_0; /* path to rm tool */
char *ls = LS_PATH_0; /* path to ls executable */
char *txzchk = TXZCHK_PATH_0; /* path to txzchk executable */
char *fnamchk = FNAMCHK_PATH_0; /* path to fnamchk executable */
char *chkentry = CHKENTRY_PATH_0; /* path to chkentry executable */
char *make = MAKE_PATH_0; /* path to make(1) executable */
char *answers = NULL; /* path to the answers file (recording input given on stdin) */
FILE *answersp = NULL; /* file pointer to the answers file */
char *uuidfile = NULL; /* path to the UUID file */
char *uuidstr = NULL; /* -U UUID option or UUID environmental var */
char *submission_dir = NULL; /* submission directory from which to form a compressed tarball */
char *tarball_path = NULL; /* path of the compressed tarball to form */
struct info info; /* data to form .info.json */
struct auth auth; /* data to form .auth.json */
int author_count = 0; /* number of authors */
struct author *author_set = NULL; /* list of authors */
bool answers_flag_used = false; /* true ==> -a write answers to answers file */
bool read_uuid_flag_used = false; /* true ==> -u uuidfile used */
bool uuid_flag_used = false; /* true ==> -U uuid used */
bool overwrite_answers_flag_used = false; /* true ==> don't prompt to overwrite answers if it already exists */
bool overwrite_answers = false; /* true ==> overwrite answers file even if it already exists */
bool force_delete = false; /* true ==> force delete workdir/slot directory if it exists */
RuleCount size; /* rule_count() processing results */
int ret; /* libc return code */
int i;
bool found_tar = false; /* for find_utils */
bool found_ls = false; /* for find_utils */
bool found_txzchk = false; /* for find_utils */
bool found_fnamchk = false; /* for find_utils */
bool found_chkentry = false; /* for find_utils */
bool found_make = false; /* for find_utils */
bool found_rm = false; /* for find_utils */
/* IOCCC requires use of C locale */
set_ioccc_locale();
/*
* zeroize info
*/
memset(&info, 0, sizeof(info));
/*
* zeroize auth
*/
memset(&auth, 0, sizeof(auth));
/*
* even though these stat structs are in file scope, make sure they are
* zeroed out
*/
memset(&topdir_st, 0, sizeof(topdir_st));
memset(&workdir_st, 0, sizeof(workdir_st));
/*
* parse args
*/
input_stream = stdin; /* default to reading from standard in */
program = argv[0];
while ((i = getopt(argc, argv, ":hv:J:qVt:l:a:i:A:WT:ef:F:C:yYds:m:I:u:U:xr:")) != -1) {
switch (i) {
case 'h': /* -h - print help to stderr and exit 2 */
usage(2, program, ""); /*ooo*/
not_reached();
break;
case 'v': /* -v verbosity */
/*
* parse verbosity
*/
verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -v verbosity"); /*ooo*/
not_reached();
}
break;
case 'J': /* -J json_verbosity */
/*
* parse json verbosity level
*/
json_verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -J json_verbosity"); /*ooo*/
not_reached();
}
break;
case 'q':
quiet = true;
msg_warn_silent = true;
break;
case 'V': /* -V - print version and exit 2 */
print("%s version: %s\n", MKIOCCCENTRY_BASENAME, MKIOCCCENTRY_VERSION);
print("jparse utils version: %s\n", JPARSE_UTILS_VERSION);
print("jparse UTF-8 version: %s\n", JPARSE_UTF8_VERSION);
print("jparse library version: %s\n", JPARSE_LIBRARY_VERSION);
exit(2); /*ooo*/
not_reached();
break;
case 't': /* -t /path/to/tar */
tar = optarg;
break;
case 'l': /* -l /path/to/ls */
ls = optarg;
break;
case 'A': /* -A answers overwrite answers file */
answers = optarg;
overwrite_answers_flag_used = true;
answers_flag_used = true;
break;
case 'a': /* -a record_answers */
answers = optarg;
answers_flag_used = true;
break;
case 'i': /* -i input_recorded_answers */
read_answers_flag_used = true;
/*
* set need_confirm to false to prevent problem where a user might
* have a different file set
*/
need_confirm = false;
/*
* set answer_yes for things like author details
*/
answer_yes = true;
need_hints = false;
silence_prompt = true;
answers = optarg;
break;
case 'W': /* -W ignores all warnings (this does NOT the judges will! :) ) */
ignore_warnings = true;
break;
case 'E': /* -E exit non-zero after first warning */
abort_on_warning = true;
break;
case 'T':
txzchk = optarg;
break;
case 'e':
entertain = true;
break;
case 'f': /* how many feathers is feathery? */
if (!string_to_uintmax(optarg, &feathery)) {
usage(3, program, "invalid -f feathers"); /*ooo*/
not_reached();
}
break;
case 'F':
fnamchk = optarg;
break;
case 'C':
chkentry = optarg;
break;
case 'y':
answer_yes = true;
need_confirm = false;
break;
case 'Y': /* force yes even when we would normally temporarily undo it */
force_yes = true;
answer_yes = true;
need_confirm = false;
break;
case 'd': /* alias for -s DEFAULT_SEED */
answer_seed = DEFAULT_SEED;
seed_used = true;
/* set -y */
answer_yes = true;
/* set -i input_recorded_answers booleans except for read_answers_flag_used */
need_confirm = false;
need_hints = false;
silence_prompt = true;
/* set -E boolean */
abort_on_warning = true;
force_yes = true;
break;
case 's': /* set seed as seed & SEED_MASK */
/*
* parse seed
*/
errno = 0; /* pre-clear errno for warnp() */
answer_seed = (long)strtol(optarg, NULL, 0);
if (errno != 0) {
warnp(__func__, "invalid -s argument, must be an integer >= 0, disabling -s seed");
answer_seed = NO_SEED;
} else if (answer_seed < 0) {
warnp(__func__, "invalid -s argument, must be >= 0, disabling -s seed");
answer_seed = NO_SEED;
} else {
seed_used = true;
/* set -y */
answer_yes = true;
/* set -i input_recorded_answers booleans except for read_answers_flag_used */
need_confirm = false;
need_hints = false;
silence_prompt = true;
/* set -E boolean */
abort_on_warning = true;
force_yes = true;
}
break;
case 'm': /* set path to make(1) */
make = optarg;
break;
case 'I': /* ignore a path */
append_path(&info.ignore_paths, optarg, true, false, false);
break;
case 'u':
uuidfile = optarg;
read_uuid_flag_used = true;
break;
case 'U':
uuidstr = optarg;
uuid_flag_used = true;
break;
case 'x':
force_delete = true;
break;
case 'r':
rm = optarg;
break;
case ':': /* option requires an argument */
case '?': /* illegal option */
default: /* anything else but should not actually happen */
check_invalid_option(program, i, optopt);
usage(3, program, ""); /*ooo*/
not_reached();
break;
}
}
/* must have the required number of args */
if (argc - optind != REQUIRED_ARGS) {
usage(3, program, "wrong number of arguments"); /*ooo*/
not_reached();
}
/*
* find utilities we need.
*/
find_utils(&found_tar, &tar, &found_ls, &ls, &found_txzchk, &txzchk,
&found_fnamchk, &fnamchk, &found_chkentry, &chkentry,
&found_make, &make, &found_rm, &rm);
/*
* check that conflicting answers file options are not used together
*/
if (answers_flag_used && read_answers_flag_used) {
err(3, __func__, "-a answers and -i answers cannot be used together"); /*ooo*/
not_reached();
}
if (seed_used && ignore_warnings) {
err(3, __func__, "-W cannot be used with with either -d or -s seed"); /*ooo*/
not_reached();
}
if (seed_used && answers_flag_used) {
err(3, __func__, "-a answers cannot be used with with either -d or -s seed"); /*ooo*/
not_reached();
}
if (seed_used && overwrite_answers_flag_used) {
err(3, __func__, "-A answers cannot be used with with either -d or -s seed"); /*ooo*/
not_reached();
}
if (seed_used && read_answers_flag_used) {
err(3, __func__, "-i answers cannot be used with with either -d or -s seed"); /*ooo*/
not_reached();
}
if (seed_used && read_uuid_flag_used) {
err(3, __func__, "-u uuidfile cannot be used with either -d or -s seed");/*ooo*/
not_reached();
}
if (seed_used && uuidstr != NULL) {
err(3, __func__, "-U UUID cannot be used with -d or -s seed");/*ooo*/
not_reached();
}
if (read_uuid_flag_used && uuid_flag_used) {
err(3, __func__, "-u uuidfile and -U UUID cannot be used together");/*ooo*/
not_reached();
}
if ((read_uuid_flag_used || uuid_flag_used) && seed_used) {
err(3, __func__, "-U UUID or -u uuidfile cannot be used with -d or -s seed");/*ooo*/
not_reached();
}
if ((read_uuid_flag_used || uuid_flag_used) && read_answers_flag_used) {
err(3, __func__, "-U UUID or -u uuidfile cannot be used with -i answers");/*ooo*/
not_reached();
}
if (ignore_warnings && abort_on_warning) {
err(3, __func__, "-W and -E cannot be used with together"); /*ooo*/
not_reached();
}
/*
* if we are seeding (-s seed or -d), setup seed and answers file
*/
if (seed_used) {
int answer_len; /* length of the answers filename we will form */
/*
* seed using random offset and srandom arg mask
*
* We add the non-zero SEED_OFFSET_BIAS value so that -s 0
* will not simply select the default 0 argument value. Why?
* Well, why not? :-) Sure, someone could use -s 2147470909
* to get the default 0 argument value, but they'd have to really
* try. :-)
*
* BTW: There is nothing wrong with using the default 0 argument,
* we are just having fun.
*/
srandom((unsigned)((answer_seed+SEED_OFFSET_BIAS) & SEED_MASK));
/*
* form the answers filename based on seed
*
* We need to form the filename:
*
* random_answers.answer_seed
*/
answer_len = (sizeof("random_answers.")-1) + SEED_DECIMAL_DIGITS + 1 + 1; /* +1 for NULL, +1 for paranoia */
answers = malloc((size_t)answer_len + 1); /* +1 for paranoia */
if (answers == NULL) {
err(3, __func__, "failed to malloc %d bytes for answers filename", answer_len + 1); /*ooo*/
not_reached();
}
answers[answer_len] = '\0'; /* paranoia */
ret = snprintf(answers, answer_len, "random_answers.%u", (unsigned)answer_seed);
if (ret <= 0) {
errp(3, __func__, "snprintf of random_answers.%u filename failed", (unsigned)answer_seed); /*ooo*/
not_reached();
}
/*
* load the random_answers.answer_seed file with randomly generated answers
*/
/* temporarily turn on -A random_answers.seed */
overwrite_answers_flag_used = true;
answers_flag_used = true;
/* write random answers into random_answers.seed */
generate_answers(answers);
/* turn off -A random_answers.seed */
overwrite_answers_flag_used = false;
answers_flag_used = false;
/* turn on -i random_answers.seed */
read_answers_flag_used = true;
}
dbg(DBG_MED, "tar: %s", tar);
dbg(DBG_MED, "ls: %s", ls);
dbg(DBG_MED, "rm: %s", rm);
workdir = argv[optind];
if (workdir == NULL || *workdir == '\0') {
err(3, __func__, "workdir is NULL or empty string");/*ooo*/
not_reached();
}
if (!is_dir(workdir) || !is_write(workdir) || !is_exec(workdir)) {
err(3, __func__, "workdir is not a writable and searchable directory: %s", workdir);/*ooo*/
not_reached();
}
/*
* get stat(2) info for workdir
*/
errno = 0; /* pre-clear errno for errp() */
if (stat(workdir, &workdir_st) != 0) {
errp(3, __func__, "failed to get stat(2) info for workdir: %s", workdir);/*ooo*/
not_reached();
}
topdir = argv + optind + REQUIRED_ARGS - 1;
if (topdir == NULL || *topdir == NULL || **topdir == '\0') {
err(3, __func__, "topdir is NULL or empty string");/*ooo*/
not_reached();
}
if (!is_dir(*topdir) || !is_read(*topdir) || !is_exec(*topdir)) {
err(3, __func__, "topdir is not a readable and searchable directory: %s", *topdir);/*ooo*/
not_reached();
}
/*
* get stat(2) info for topdir
*/
errno = 0; /* pre-clear errno for errp() */
if (stat(*topdir, &topdir_st) != 0) {
errp(3, __func__, "failed to get stat(2) info for topdir: %s", *topdir);/*ooo*/
not_reached();
}
/*
* check if topdir is the same as workdir
*/
if (topdir_st.st_ino == workdir_st.st_ino && topdir_st.st_dev == workdir_st.st_dev) {
err(3, __func__, "topdir cannot be the same as the workdir"); /*ooo*/
not_reached();
}
dbg(DBG_MED, "workdir: %s", workdir);
dbg(DBG_MED, "topdir: %s", *topdir);
if (answers != NULL) {
dbg(DBG_MED, "answers file: %s", answers);
}
if (seed_used) {
dbg(DBG_MED, "pseudo random seed: %u", (unsigned)answer_seed);
}
/*
* record the time
*/
errno = 0; /* pre-clear errno for errp() */
ret = gettimeofday(&tp, NULL);
if (ret < 0) {
errp(246, __func__, "gettimeofday failed");
not_reached();
}
info.tstamp = tp.tv_sec;
if ((time_t)(-1) > 0) {
/* case: unsigned time_t */
dbg(DBG_HIGH, "info.tstamp: %ju", (uintmax_t)info.tstamp);
} else {
/* case: signed time_t */
dbg(DBG_HIGH, "info.tstamp: %jd", (intmax_t)info.tstamp);
}
info.usec = tp.tv_usec;
dbg(DBG_HIGH, "info.usec: %jd", (intmax_t)info.usec);
/*
* Welcome
*/
if (!quiet) {
print("Welcome to mkiocccentry version: %s\n", MKIOCCCENTRY_VERSION);
}
/*
* warn about -Y option
*/
if (force_yes) {
para("",
"WARNING: you've chosen to answer YES to ALL prompts. If this was",
"unintentional, run the program again without specifying -Y. We cannot",
"stress the importance of this enough! Well OK, we can overstress most things",
"but you get the point; do not use the -Y option without EXTREME caution!",
"",
"Hint: this option is mostly useful for mkiocccentry_test.sh; if you REALLY",
"want to answer yes you should use -y instead which will allow you to still",
"verify certain things.",
"",
NULL);
/*
* if not quiet give a shorter warning as well
*/
if (!quiet) {
print("%s", "Notice: we will ALWAYS answer YES to questions.\n");
}
} else if (answer_yes) {
/* warn about -y option */
para("",
"WARNING: you've chosen to answer yes to ALMOST ALL prompts. If this was",
"unintentional, run the program again without specifying -y. We cannot",
"stress the importance of this enough! Well OK, we can overstress most things",
"but you get the point; do not use the -y option without EXTREME caution!",
"",
NULL);
/*
* if not quiet give a shorter warning as well
*/
if (!quiet) {
print("%s", "Notice: we will answer YES to MOST questions.\n");
}
}
/*
* save our version
*/
info.mkiocccentry_ver = MKIOCCCENTRY_VERSION;
dbg(DBG_HIGH, "info.mkiocccentry_ver: %s", info.mkiocccentry_ver);
/* if the user requested to ignore warnings, and now -E, then ignore this once and warn them :) */
if (ignore_warnings) {
para("",
"WARNING: You've chosen to ignore all warnings. While we will not show",
"you any additional warnings, you should note that The Judges will NOT",
"ignore warnings! If this was unintentional, run the program again",
"without specifying -W. We cannot stress the importance of this enough!",
"Well OK, we can overstress most things, but you get the point; do not",
"use the -W option without EXTREME caution!",
"",
NULL);
}
/*
* environment sanity checks
*/
if (!quiet) {
para("", "Performing sanity checks on your environment ...", NULL);
}
mkiocccentry_sanity_chks(&info, workdir, tar, ls, txzchk, fnamchk, chkentry, make, rm);
if (!quiet) {
para("... environment looks OK", "", NULL);
}
/*
* if -a answers was specified and answers file exists, prompt user if they
* want to overwrite it; if they don't tell them how to use it and abort.
* Else it will be overwritten.
*/
if (answers_flag_used && !overwrite_answers_flag_used && answers != NULL && strlen(answers) > 0 && exists(answers)) {
overwrite_answers = yes_or_no("WARNING: The answers file already exists! Do you wish to overwrite it? [Ny]", false);
if (!overwrite_answers) {
errno = 0; /* pre-clear errno for errp() */
ret = printf("\nTo use the answers file, try:\n\n\t./mkiocccentry -i %s [...]\n\n", answers);
if (ret <= 0) {
errp(247, __func__, "printf error telling the user how to use the answers file");
not_reached();
}
err(248, __func__, "won't overwrite answers file");
not_reached();
}
}
/*
* check if we should read input from answers file
*/
if (read_answers_flag_used && answers != NULL && strlen(answers) > 0) {
if (!is_read(answers)) {
errp(249, __func__, "answers file not readable");
not_reached();
}
errno = 0; /* pre-clear errno for errp() */
answersp = fopen(answers, "r");
if (answersp == NULL) {
errp(10, __func__, "cannot open answers file");
not_reached();
}
input_stream = answersp;
}
/*
* obtain the IOCCC contest ID
*/
info.ioccc_id = get_contest_id(&info.test_mode, uuidfile, uuidstr);
dbg(DBG_LOW, "Submission: IOCCC contest ID: %s", info.ioccc_id);
/*
* found the answer file header in stdin
*/
if (read_answers_flag_used && answers == NULL) {
answersp = stdin;
}
/*
* obtain submit slot number
*/
info.submit_slot = get_submit_slot(&info);
dbg(DBG_LOW, "Submission: slot number: %d", info.submit_slot);
/*
* create submission directory
*/
submission_dir = mk_submission_dir(workdir, info.ioccc_id, info.submit_slot, &tarball_path, info.tstamp, info.test_mode,
force_delete, rm);
errno = 0;
info.tarball = strdup(tarball_path);
if (info.tarball == NULL) {
errp(11, __func__, "strdup() tarball path %s failed", tarball_path);
not_reached();
}
dbg(DBG_MED, "Directory formed: %s", submission_dir);
/*
* if -a, open the answers file. We only do it after verifying that we can
* make the submission directory because if we get input before and find out the
* directory already exists then the answers file will have invalid data.
*/
if (answers_flag_used && answers != NULL && strlen(answers) > 0) {
errno = 0; /* pre-clear errno for errp() */
answersp = fopen(answers, "w");
if (answersp == NULL) {
errp(12, __func__, "cannot create answers file: %s", answers);
not_reached();
}
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp, "%s\n", MKIOCCCENTRY_ANSWERS_VERSION);
if (ret <= 0) {
errp(13, __func__, "fprintf error printing header to the answers file");
not_reached();
}
}
/*
* write the IOCCC id and submit slot number to the answers file
*/
if (answersp != NULL && answers_flag_used) {
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp, "%s\n", info.ioccc_id);
if (ret <= 0) {
errp(14, __func__, "fprintf error printing IOCCC contest id to the answers file");
not_reached();
}
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp, "%d\n", info.submit_slot);
if (ret <= 0) {
errp(15, __func__, "fprintf error printing submit slot number to the answers file");
not_reached();
}
}
/*
* scan and collect files and directories in topdir, copying to the
* submission directory and then verify everything is in order (through
* copy_topdir() and then check_submission_dir()).
*/
scan_topdir(*topdir, &info, make, submission_dir, &size);
/*
* obtain the title
*/
info.title = get_title(&info);
dbg(DBG_LOW, "Submission: title: %s", info.title);
if (answersp != NULL && answers_flag_used) {
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp, "%s\n", info.title);
if (ret <= 0) {
errp(16, __func__, "fprintf error printing title to the answers file");
not_reached();
}
}
/*
* obtain the abstract
*/
info.abstract = get_abstract(&info);
dbg(DBG_LOW, "Submission: abstract: %s", info.abstract);
if (answersp != NULL && answers_flag_used) {
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp, "%s\n", info.abstract);
if (ret <= 0) {
errp(17, __func__, "fprintf error printing abstract to the answers file");
not_reached();
}
}
/*
* obtain author information
*/
author_count = get_author_info(&author_set);
dbg(DBG_MED, "collected information on %d authors", author_count);
/*
* if we have an answers file, record the verified author information
*/
if (answersp != NULL && !read_answers_flag_used) {
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp, "%d\n", author_count);
if (ret <= 0) {
errp(18, __func__, "fprintf error printing IOCCC author count to the answers file");
not_reached();
}
/*
* write answers for each author to the answers file
*/
for (i = 0; i < author_count; i++) {
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp,
"%s\n" /* name */
"%s\n" /* location code */
"%s\n" /* email */
"%s\n" /* url */
"%s\n" /* alt_url */
"%s\n" /* mastodon handle */
"%s\n" /* GitHub */
"%s\n" /* affiliation */
"%s\n" /* author_handle */
,
author_set[i].name,
author_set[i].location_code,
author_set[i].email,
author_set[i].url,
author_set[i].alt_url,
author_set[i].mastodon,
author_set[i].github,
author_set[i].affiliation,
author_set[i].author_handle);
if (ret <= 0) {
errp(19, __func__, "fprintf error printing author info to the answers file");
not_reached();
}
}
}
/*
* form info
*/
form_info(&info);
/*
* form auth
*/
form_auth(&auth, &info, author_count, author_set);
/*
* write the .auth.json file and the .info.json file
*/
if (!quiet) {
para("", "Forming the .auth.json file and .info json file ...", NULL);
}
write_json_files(&auth, &info, submission_dir, chkentry);
if (!quiet) {
para("... completed .auth.json and .info.json files.", "", NULL);
}
/*
* finalize the answers file: either write the final answers (if writing
* answers) or read the answers EOF marker and then finally closing the
* stream.
*/
if (answersp != NULL) {
if (read_answers_flag_used) {
char *linep = NULL;
char *line;
bool error = true;
line = readline_dup(&linep, true, NULL, answersp);
if (line != NULL) {
if (line != NULL) {
error = strcmp(line, MKIOCCCENTRY_ANSWERS_EOF) != 0;
free(linep);
linep = NULL;
} else {
error = false;
}
}
if (error) {
errp(20, __func__, "expected ANSWERS_EOF marker at the end of the answers file");
not_reached();
}
input_stream = stdin;
if (line != NULL) {
free(line);
line = NULL;
}
} else {
errno = 0; /* pre-clear errno for errp() */
ret = fprintf(answersp, "%s\n", MKIOCCCENTRY_ANSWERS_EOF);
if (ret <= 0) {
errp(21, __func__, "fprintf error writing ANSWERS_EOF marker to the answers file");
not_reached();
}
}
if (answers != NULL) {
errno = 0; /* pre-clear errno for errp() */
ret = fclose(answersp);
if (ret != 0) {
errp(22, __func__, "error in fclose to the answers file");
not_reached();
}
}
answersp = NULL;
}
/*
* remind the user about their answers file
*/
if (answers != NULL) {
if (need_hints) {
errno = 0; /* pre-clear errno for warnp() */
ret = printf("\nTo more easily update this submission you can run:\n\n ./mkiocccentry -i %s workdir topdir",
answers);
if (ret <= 0) {
warnp(__func__, "unable to tell user how to more easily update submission");
}
}
}
/*
* If the answers file (-i answers or -d or -s seed) was used, and there were warnings/problems
* discovered with the submission that were overridden, warn the user.
*/
if (read_answers_flag_used || seed_used) {
if (info.empty_override ||
info.rule_2a_override ||
info.rule_2a_mismatch ||
info.rule_2b_override ||