--- joko/Uni/BSArch/01/prime.c 2006/05/12 20:38:15 1.5 +++ joko/Uni/BSArch/01/prime.c 2006/05/13 12:06:38 1.10 @@ -1,34 +1,77 @@ -/* $Id: prime.c,v 1.5 2006/05/12 20:38:15 joko Exp $ */ +/* $Id: prime.c,v 1.10 2006/05/13 12:06:38 joko Exp $ */ #include +#include +#include +#include -#define PRINTPRIME(x) if(is_prime(x)) printf("%i\n", x) +#define BOOL int +#define TRUE 1 +#define FALSE 0 + +#define PRINTPRIME(x) if (is_prime(x)) printf("%i\n", x) #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message) +#define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message) /* check for prime number */ -int is_prime(int number) +BOOL is_prime(long int number) { int i; + + /* negative values, 0 and 1 are never prime numbers */ + if (number < 2) return FALSE; + + /* check all numbers 2..sqrt(number) for being a prime number */ for (i=2; i*i <= number; i++) { if ((number % i) == 0) - return 0; + return FALSE; } - return 1; + return TRUE; +} + +/* convert from string to long int, with error checking */ +long int convert_number(const char *nptr) { + + char * endptr; + long int number = strtol(nptr, &endptr, 10); + + errno = 0; + + /* invalid characters? */ + if (*endptr != '\0') { + char message[254]; + snprintf(message, 256, "Could not convert '%s' to a valid number.", nptr); + PRINTERROR(message); + exit(EXIT_FAILURE); + } + + /* invalid range? */ + /* if (number == LONG_MAX || number == LONG_MIN) { */ + if (errno == ERANGE) { + char message[254]; + snprintf(message, 256, "Number is not in range of 'long int': %s", nptr); + PRINTERROR(message); + exit(EXIT_FAILURE); + } + + return number; + } int main(int argc, char * argv[]) { if (argc == 1) { - PRINTERROR("No arguments given, will segfault under cygwin. :-)"); - return -1; + PRINTERROR("No arguments given."); + exit(EXIT_FAILURE); } /* (3) range mode */ if (argc > 2) { - int i, j; - j = atoi(argv[2]); - for (i = atoi(argv[1]); i< j; i++) { + long int i, j; + i = convert_number(argv[1]); + j = convert_number(argv[2]); + for (i; i< j; i++) { PRINTPRIME(i); } @@ -40,15 +83,43 @@ /* (1) test-single-number mode: first argument is not a filename */ if (fp == NULL) { - PRINTPRIME(atoi(argv[1])); + long int number = convert_number(argv[1]); + PRINTPRIME(number); /* (2) file mode: read numbers from file */ } else { - char num[11]; - while (fgets(num, 11, fp)) { - printf("raw: %s\n", num); - printf("num: %i\n", atoi(num)); - PRINTPRIME(atoi(num)); + char entry[81]; + long int number; + long int lineno = 0; + while (fgets(entry, 81, fp)) { + + /* count line number (for warnings) */ + lineno++; + + /* skip empty lines */ + if (strlen(entry) < 2) continue; + + /* line handling: policy = skip exceeding lines */ + + /* if last char is newline, strip it */ + if (entry[strlen(entry)-1] == '\n') { + entry[strlen(entry)-1] = '\0'; + + /* line exceeds max length */ + } else { + char message[254]; + snprintf(message, 256, "Line too long (max 80 chars) in line number: %i", lineno); + PRINTWARNING(message); + + /* eat all characters until newline */ + while (fgetc(fp) != 10); + + /* skip this line from prime calculation */ + continue; + } + + number = convert_number(entry); + PRINTPRIME(number); } fclose(fp); }