diff --git a/src/files.c b/src/files.c index acb9d9c..71796d9 100644 --- a/src/files.c +++ b/src/files.c @@ -12,7 +12,9 @@ int inputPrimes(char *file, List *list) { if(pFile == NULL) return 1; mpz_t n; mpz_init(n); - while(mpz_inp_raw(n, pFile) != 0) addToList(list, n); + while(mpz_inp_raw(n, pFile) != 0) { + if(addToList(list, n) == 1) return 3; + } if(fclose(pFile) != 0) return 2; return 0; } diff --git a/src/files.h b/src/files.h index ded15bc..f89c48d 100644 --- a/src/files.h +++ b/src/files.h @@ -12,7 +12,8 @@ * @param file File to input primes from. * @param list List to load primes into. * @returns If 0 then load was successful, if 1 then failed to open, - * if 2 failed to close. + * if 2 failed to close, if 3 failed to allocate new memory to List + * (see `addToList()') */ int inputPrimes(char *file, List *list); diff --git a/src/list.c b/src/list.c index 58c6bac..d8e05df 100644 --- a/src/list.c +++ b/src/list.c @@ -7,14 +7,12 @@ */ #define BLOCK_SIZE 1024 -void initList(List *restrict l) { +int initList(List *restrict l) { l->list = malloc(sizeof(mpz_t) * BLOCK_SIZE); - if(!l->list) { - fprintf(stderr, "Failed to allocate memory to list!\n"); - exit(1); - } + if(!l->list) return 1; l->size = BLOCK_SIZE; l->end = 0; + return 0; } void deInitList(List *restrict l) { @@ -24,16 +22,14 @@ void deInitList(List *restrict l) { free(l->list); } -void addToList(List *l, mpz_t n) { +int addToList(List *restrict l, mpz_t n) { if(l->end == l->size) { l->size += BLOCK_SIZE; void *tmp = realloc(l->list, sizeof(mpz_t) * l->size); - if(!tmp) { - fprintf(stderr, "Failed to allocate more memory to list!\n"); - exit(1); - } + if(!tmp) return 1; l->list = tmp; } mpz_init(l->list[l->end]); mpz_set(l->list[l->end++], n); + return 0; } diff --git a/src/list.h b/src/list.h index 77b5506..b600654 100644 --- a/src/list.h +++ b/src/list.h @@ -23,8 +23,9 @@ typedef struct { * @details Initialize the list and its variables, allocating memory * to the pointer array inside. * @param[in] l A pointer to a List type to be initialized. + * @returns Returns 0 if successful and 1 if failed. */ -void initList(List *restrict l); +int initList(List *restrict l); /** * @brief Deinitialize a List. @@ -38,5 +39,6 @@ void deInitList(List *restrict l); * @details Add item `n' at the end of a List type. * @param[out] l List to which the variable should be appended. * @param[in] n variable to be appended to the list. + * @returns Returns 0 if successful and 1 if failed. */ -void addToList(List *l, mpz_t n); +int addToList(List *restrict l, mpz_t n); diff --git a/src/main.c b/src/main.c index 6722785..467e9fb 100644 --- a/src/main.c +++ b/src/main.c @@ -45,7 +45,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Invalid base `%d'. Base must be between 2 and 62.\n", base); - exit(1); + return 1; } break; case 'f': @@ -93,7 +93,10 @@ int main(int argc, char *argv[]) { // Primes we've found List primes; - initList(&primes); + if(initList(&primes) == 1) { + fprintf(stderr, "Failed to initialize primes list.\n"); + exit(1); + } // The number we're going to be testing for mpz_t num; @@ -108,13 +111,13 @@ int main(int argc, char *argv[]) { if(newFile) { // Add 2, a known prime to this list mpz_set_ui(num, 2); - addToList(&primes, num); + if(addToList(&primes, num) == 1) { + fprintf(stderr, "Failed to allocate more memory for list.\n"); + goto releaseMemory; + } if(!f_quiet) { - if(mpz_out_str(stdout, base, num) == 0) { + if(mpz_out_str(stdout, base, num) == 0) fprintf(stderr, "Could not print to `stdout'!\n"); - exitCode = 1; - goto releaseMemory; - } printf("\n"); } mpz_add_ui(num, num, 1); @@ -129,6 +132,8 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Failed to open Indivisible file `%s'.\n", file); else if(err == 2) fprintf(stderr, "Failed to close Indivisible file `%s'.\n", file); + else if(err == 3) + fprintf(stderr, "Failed to allocate more memory for list.\n"); exitCode = 1; goto releaseMemory; } @@ -170,13 +175,13 @@ int main(int argc, char *argv[]) { } // `num' is a prime so we add it to the list and print it - addToList(&primes, num); + if(addToList(&primes, num) == 1) { + fprintf(stderr, "Failed to allocate more memory for list.\n"); + goto releaseMemory; + } if(!f_quiet) { - if(mpz_out_str(stdout, base, num) == 0) { + if(mpz_out_str(stdout, base, num) == 0) fprintf(stderr, "Could not print to `stdout'!\n"); - exitCode = 1; - goto releaseMemory; - } printf("\n"); }