scan_includes: add --strict so it doesn't have to be default

This commit is contained in:
yenatch 2017-05-28 01:12:16 -04:00
parent 27ba16d827
commit fe38946b0e

View File

@ -2,12 +2,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <getopt.h>
void usage(void) { void usage(void) {
printf("Usage: scan_includes filename\n"); printf("Usage: scan_includes [-h] [-s] filename\n"
exit(1); "-h, --help\n"
" Print usage and exit\n"
"-s, --strict\n"
" Fail if a file cannot be read\n");
} }
struct Options {
bool help;
bool strict;
};
struct Options Options = {0};
void scan_file(char* filename) { void scan_file(char* filename) {
FILE* f; FILE* f;
long size; long size;
@ -17,9 +29,13 @@ void scan_file(char* filename) {
int length; int length;
f = fopen(filename, "r"); f = fopen(filename, "r");
if (f == NULL) { if (!f) {
fprintf(stderr, "Could not open file: '%s'\n", filename); if (Options.strict) {
exit(1); fprintf(stderr, "Could not open file: '%s'\n", filename);
exit(1);
} else {
return;
}
} }
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
@ -68,10 +84,43 @@ void scan_file(char* filename) {
free(orig); free(orig);
} }
int main(int argc, char* argv[]) { void get_args(int argc, char *argv[]) {
if (argc < 2) { while (1) {
usage(); struct option long_options[] = {
{"strict", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{0}
};
int i = 0;
int opt = getopt_long(argc, argv, "sh", long_options, &i);
if (opt == -1) {
break;
}
switch (opt) {
case 's':
Options.strict = true;
break;
case 'h':
Options.help = true;
break;
}
} }
scan_file(argv[1]); }
int main(int argc, char* argv[]) {
get_args(argc, argv);
argc -= optind;
argv += optind;
if (Options.help) {
usage();
return 0;
}
if (argc < 1) {
usage();
exit(1);
}
scan_file(argv[0]);
return 0; return 0;
} }