Files

67 lines
1.4 KiB
C
Raw Permalink Normal View History

2025-10-06 00:14:04 -04:00
/*
* CS 261: Main driver
*
* Name: Nicholas Tamassia
*/
#include "p1-check.h"
/*
* helper function for printing help text
*/
void usage (char **argv)
{
printf("Usage: %s <option(s)> mini-elf-file\n", argv[0]);
printf(" Options are:\n");
printf(" -h Display usage\n");
printf(" -H Show the Mini-ELF header\n");
}
int main (int argc, char **argv)
{
int c;
bool display_header = false;
while ((c = getopt(argc, argv, "hH")) != -1) {
switch (c) {
case 'h':
usage(argv);
return EXIT_SUCCESS;
case 'H':
display_header = true;
break;
default:
usage(argv);
return EXIT_FAILURE;
}
}
/* Check if file path was the last argument */
if (optind != argc - 1) {
usage(argv);
return EXIT_FAILURE;
}
elf_hdr_t header = { 0 };
FILE *header_file = fopen(argv[optind], "r");
/* Invalid file path or failed to read correct data from file */
if (!read_header(header_file, &header)) {
printf("Failed to read file\n");
if (header_file != NULL) {
fclose(header_file);
}
return EXIT_FAILURE;
}
if (display_header) {
dump_header(&header);
}
fclose(header_file);
return EXIT_SUCCESS;
}