--- joko/Uni/BSArch/01/prime.c 2006/05/12 21:13:49 1.6 +++ joko/Uni/BSArch/01/prime.c 2006/05/15 11:22:55 1.14 @@ -1,8 +1,14 @@ -/* $Id: prime.c,v 1.6 2006/05/12 21:13:49 joko Exp $ */ +/* $Id: prime.c,v 1.14 2006/05/15 11:22:55 franky Exp $ */ #include #include +#include #include +#include + +#ifdef _MSC_VER +#define snprintf _snprintf +#endif #define BOOL int #define TRUE 1 @@ -10,24 +16,61 @@ #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) + +#define MAX_LINE_LENGTH 80 + /* check for prime number */ BOOL is_prime(long int number) { - int i; - for (i=2; i*i <= number; i++) { + int i,sq; + /* negative values, 0 and 1 are never prime numbers */ + if (number < 2) return FALSE; + sq=sqrt(number); + + /* check all numbers 2..sqrt(number) for being a prime number */ + for (i=2; i <= sq; i++) { if ((number % i) == 0) return FALSE; } return TRUE; } -/* convert from string to long, with error checking */ -long int convert_number(const char *nptr) { - long int number = strtol(nptr, (char **)NULL, 10); - if (number == LONG_MAX) { - PRINTERROR("Number '%s' is not in range of 'long int'."); +/* convert from string to long int, with error checking */ +long int convert_number(const char *nptr, BOOL warn) { + + char * endptr; + errno = 0; + long int number = strtol(nptr, &endptr, 10); + + /* invalid characters? */ + if (*endptr != '\0') { + char message[256]; + snprintf(message, 255, "Could not convert '%s' to a valid (integer) number.", nptr); + if(warn) + PRINTWARNING(message); + else{ + PRINTERROR(message); + exit(EXIT_FAILURE); + } } + + /* invalid range? */ + /* if (number == LONG_MAX || number == LONG_MIN) { */ + if (errno == ERANGE) { + char message[256]; + snprintf(message, 255, "Number is not in range of 'long int': %s", nptr); + if(warn) + PRINTWARNING(message); + else{ + PRINTERROR(message); + exit(EXIT_FAILURE); + } + } + + return number; + } int main(int argc, char * argv[]) @@ -35,35 +78,76 @@ if (argc == 1) { PRINTERROR("No arguments given."); - return -1; + 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],FALSE); + j = convert_number(argv[2],FALSE); + for (i; i< j; i++) { PRINTPRIME(i); } /* other modes */ } else { - + int err; /* try to open file for reading */ FILE * fp = fopen(argv[1], "r"); - + err=errno; /* (1) test-single-number mode: first argument is not a filename */ if (fp == NULL) { - /* PRINTPRIME(atoi(argv[1])); */ - PRINTPRIME(convert_number(argv[1])); + long int number = convert_number(argv[1],TRUE); + if(err){ + fprintf(stderr, "%s: %s\n", argv[1], strerror(err)); + return -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 line[MAX_LINE_LENGTH + 1]; + long int number; + int line_length; + long int line_no = 0; + while (fgets(line, MAX_LINE_LENGTH + 1, fp)) { + + /* count line number (for warnings) */ + line_no++; + + line_length = strlen(line); + + /* skip empty lines */ + if (strlen(line) < 2) continue; + + /* line handling: policy = skip exceeding lines */ + + /* line exceeds max length */ + if (line_length == MAX_LINE_LENGTH && line[line_length-1] != '\n') { + char message[254]; + snprintf(message, 256, "Line too long (max %i chars) in line number: %i", MAX_LINE_LENGTH, line_no); + PRINTWARNING(message); + + /* eat all characters until newline or EOF */ + while (1) { + int charcode = fgetc(fp); + if (charcode == 10 || charcode == EOF) break; + } + + /* skip this line from prime calculation */ + continue; + } + + /* if last char is newline, strip it */ + if (line[line_length-1] == '\n') { + line[line_length-1] = '\0'; + } + + /* finally: prime number calculation and output */ + number = convert_number(line,FALSE); + PRINTPRIME(number); + } fclose(fp); }