use globals instead of calloc/free

This commit is contained in:
Connor Olding 2015-04-11 19:53:28 -07:00
parent 020acb022c
commit d5e14c8edd

41
main.c
View File

@ -12,13 +12,10 @@ typedef int bool;
#endif #endif
char buff[BUFFER_SIZE]; char buff[BUFFER_SIZE];
typedef struct string_node_s string_node; #define MAX_INPUTS 256
struct string_node_s {
char *s;
string_node *next;
};
static string_node *input_node = NULL; static char *inputs[MAX_INPUTS];
static unsigned int input_n = 0;
static uint32_t starting = 0xFFFFFFFF; static uint32_t starting = 0xFFFFFFFF;
static bool big_endian = 0; static bool big_endian = 0;
@ -88,19 +85,11 @@ handle_flag(char flag, char *(*nextarg)())
static void static void
add_input(char *arg) add_input(char *arg)
{ {
static string_node *last = NULL; if (input_n >= MAX_INPUTS) {
string_node *n = calloc(1, sizeof(string_node)); fprintf(stderr, "Too many inputs specified\n");
if (!n) {
fprintf(stderr, "calloc failed\n");
exit(1); exit(1);
} }
inputs[input_n++] = arg;
n->s = arg;
if (!input_node)
input_node = n;
else
last->next = n;
last = n;
} }
static FILE * static FILE *
@ -163,31 +152,19 @@ print_crc(uint32_t remainder)
printf("%08X\n", (int) remainder); printf("%08X\n", (int) remainder);
} }
static void
free_nodes(string_node *n)
{
string_node *next;
while (n) {
next = n->next;
free(n);
n = next;
}
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
args_parse(argc, argv, handle_flag, add_input); args_parse(argc, argv, handle_flag, add_input);
if (!input_node) { if (!input_n) {
freopen(NULL, "rb", stdin); freopen(NULL, "rb", stdin);
print_crc(cycle_file(stdin)); print_crc(cycle_file(stdin));
} }
for (string_node *n = input_node; n; n = n->next) { for (int i = 0; i < input_n; i++) {
FILE *stream = open_stream(n->s); FILE *stream = open_stream(inputs[i]);
print_crc(cycle_file(stream)); print_crc(cycle_file(stream));
fclose(stream); fclose(stream);
} }
free_nodes(input_node);
return 0; return 0;
} }