diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-11-14 09:28:59 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-11-14 09:28:59 -0500 |
commit | 0e90fd78a1759af0af31e214aa4c4ba84ffd009d (patch) | |
tree | 3821aad56b2d42c6eee62f4363dc4a08fd3071f3 | |
parent | 0256732772c3fdf8a8adc77e54b848e1a2f92c75 (diff) | |
download | reloc-0e90fd78a1759af0af31e214aa4c4ba84ffd009d.tar.gz |
Incorporate error handler
-rw-r--r-- | main.c | 19 | ||||
-rw-r--r-- | reloc.c | 39 |
2 files changed, 41 insertions, 17 deletions
@@ -43,14 +43,29 @@ int main(int argc, char *argv[]) { char *input_file = strdup(argv[3]); char *output_file = strdup(argv[4]); RelocData *info = reloc_read(input_file); + if (!info) { + reloc_perror(input_file); + exit(reloc_error); + } size_t records = 0; + size_t replacement_length = strlen(replacement); for (size_t i = 0; i < info->size; i++) { RelocMatch *match = NULL; if (!(match = reloc_match(&info->data[i], needle))) { + if (reloc_error) { + reloc_perror("reloc_match: "); + exit(reloc_error); + } // No match found continue; } + + if (replacement_length > match->length) { + fprintf(stderr, "Replacement string is too long ("SIZE_T_FMT " > " SIZE_T_FMT ")\n", replacement_length, match->length); + free(match); + continue; + } // Replace string reloc_replace(match, replacement); free(match); @@ -59,6 +74,10 @@ int main(int argc, char *argv[]) { // Write output file to disk reloc_write(info, output_file); + if (reloc_error) { + perror(output_file); + exit(reloc_error); + } // Report number of strings processed printf(SIZE_T_FMT"\n", records); @@ -1,6 +1,5 @@ #include "reloc.h" - RelocMatch *reloc_match(char *haystack, const char *needle) { char *data = haystack; size_t needle_size = strlen(needle); @@ -9,8 +8,8 @@ RelocMatch *reloc_match(char *haystack, const char *needle) { // Search the needle in the data if (!(memcmp(data, needle, needle_size))) { if (!(match = calloc(1, sizeof(RelocMatch)))) { - fprintf(stderr,"Failed to allocate RelocMatch structure: %s\n", strerror(errno)); - exit(1); + reloc_error = RELOC_ENOMEM; + return NULL; } size_t data_end = strlen(data); match->begin = data; @@ -20,6 +19,8 @@ RelocMatch *reloc_match(char *haystack, const char *needle) { match->post_length = strlen(match->post); match->total_length = data_end; } + + reloc_error = RELOC_ESUCCESS; return match; } @@ -32,8 +33,8 @@ RelocData *reloc_read(const char *filename) { // Open file for reading in binary mode if (!(fp = fopen(filename, "rb"))) { - fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno)); - exit(1); + reloc_error = RELOC_EREAD; + return NULL; } // Determine file size @@ -42,34 +43,42 @@ RelocData *reloc_read(const char *filename) { rewind(fp); if (!(data = calloc(sizeof(char), size + 1))) { - fprintf(stderr, "Failed to allocate data array: %s\n", strerror(errno)); - exit(1); + reloc_error = RELOC_ENOMEM; + return NULL; } // Read data into array fread(data, sizeof(char), size, fp); if (!(result = (RelocData *)malloc(sizeof(RelocData)))) { - fprintf(stderr, "Failed to allocate RelocData structure: %s\n", strerror(errno)); - exit(1); + reloc_error = RELOC_ENOMEM; + return NULL; } + fclose(fp); + result->size = size; result->data = data; result->path = strdup(filename); + reloc_error = RELOC_ESUCCESS; return result; } size_t reloc_write(RelocData *finfo, const char *filename) { + size_t bytes = 0; FILE *fp; // Open file for writing in binary mode if (!(fp = fopen(filename, "w+b"))) { - fprintf(stderr,"Cannot open %s for writing: %s\n", filename, strerror(errno)); - exit(1); + reloc_error = RELOC_EWRITE; + return 0; } // Write data - return fwrite(finfo->data, sizeof(char), finfo->size, fp); + bytes = fwrite(finfo->data, sizeof(char), finfo->size, fp); + fclose(fp); + + reloc_error = RELOC_ESUCCESS; + return bytes; } @@ -85,10 +94,6 @@ void reloc_deinit_data(RelocData *finfo) { void reloc_replace(RelocMatch *match, const char *rstr) { size_t rstr_length = strlen(rstr); - if (rstr_length > match->length) { - fprintf(stderr, "Replacement string is too long ("SIZE_T_FMT " > " SIZE_T_FMT ")\n", rstr_length, match->length); - return; - } char *data = match->begin; size_t i = 0; @@ -111,5 +116,5 @@ void reloc_replace(RelocMatch *match, const char *rstr) { } // Destroy bytes between the end of the original data and the end of the replaced string - memset(data, '\0', post - data); + memset(data, NULLBYTE, post - data); } |