| 1 |
|
/* $Id$ */ |
| 2 |
|
|
| 3 |
#include <stdio.h> |
#include <stdio.h> |
| 4 |
#define PRINTPRIME(x) if(prime(x)) printf("%i\n", x) |
#include <stdlib.h> |
| 5 |
|
#include <limits.h> |
| 6 |
|
#include <errno.h> |
| 7 |
|
|
| 8 |
|
#define BOOL int |
| 9 |
|
#define TRUE 1 |
| 10 |
|
#define FALSE 0 |
| 11 |
|
|
| 12 |
|
#define PRINTPRIME(x) if (is_prime(x)) printf("%i\n", x) |
| 13 |
|
#define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message) |
| 14 |
|
#define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message) |
| 15 |
|
|
| 16 |
|
#define MAX_LINE_LENGTH 80 |
| 17 |
|
|
| 18 |
int prime(int number) |
|
| 19 |
|
/* check for prime number */ |
| 20 |
|
BOOL is_prime(long int number) |
| 21 |
{ |
{ |
| 22 |
int i; |
int i; |
| 23 |
for(i=2;i*i<=number;i++){ |
|
| 24 |
if((number%i) == 0) |
/* negative values, 0 and 1 are never prime numbers */ |
| 25 |
return 0; |
if (number < 2) return FALSE; |
| 26 |
} |
|
| 27 |
return 1; |
/* check all numbers 2..sqrt(number) for being a prime number */ |
| 28 |
|
for (i=2; i*i <= number; i++) { |
| 29 |
|
if ((number % i) == 0) |
| 30 |
|
return FALSE; |
| 31 |
|
} |
| 32 |
|
return TRUE; |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
/* convert from string to long int, with error checking */ |
| 36 |
|
long int convert_number(const char *nptr) { |
| 37 |
|
|
| 38 |
|
char * endptr; |
| 39 |
|
long int number = strtol(nptr, &endptr, 10); |
| 40 |
|
|
| 41 |
|
errno = 0; |
| 42 |
|
|
| 43 |
|
/* invalid characters? */ |
| 44 |
|
if (*endptr != '\0') { |
| 45 |
|
char message[254]; |
| 46 |
|
snprintf(message, 256, "Could not convert '%s' to a valid (integer) number.", nptr); |
| 47 |
|
PRINTERROR(message); |
| 48 |
|
exit(EXIT_FAILURE); |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
/* invalid range? */ |
| 52 |
|
/* if (number == LONG_MAX || number == LONG_MIN) { */ |
| 53 |
|
if (errno == ERANGE) { |
| 54 |
|
char message[254]; |
| 55 |
|
snprintf(message, 256, "Number is not in range of 'long int': %s", nptr); |
| 56 |
|
PRINTERROR(message); |
| 57 |
|
exit(EXIT_FAILURE); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
return number; |
| 61 |
|
|
| 62 |
} |
} |
| 63 |
|
|
| 64 |
int main(int argc, char * argv[]) |
int main(int argc, char * argv[]) |
| 65 |
{ |
{ |
| 66 |
int i, j; |
|
| 67 |
FILE * fp; |
if (argc == 1) { |
| 68 |
char num[11]; |
PRINTERROR("No arguments given."); |
| 69 |
if(argc > 2 ){ |
exit(EXIT_FAILURE); |
| 70 |
j=atoi(argv[2]); |
} |
| 71 |
for(i=atoi(argv[1]) ; i< j; i++){ |
|
| 72 |
|
/* (3) range mode */ |
| 73 |
|
if (argc > 2) { |
| 74 |
|
long int i, j; |
| 75 |
|
i = convert_number(argv[1]); |
| 76 |
|
j = convert_number(argv[2]); |
| 77 |
|
for (i; i< j; i++) { |
| 78 |
PRINTPRIME(i); |
PRINTPRIME(i); |
| 79 |
} |
} |
| 80 |
}else { |
|
| 81 |
fp=fopen(argv[1], "r"); |
/* other modes */ |
| 82 |
if(fp==NULL){ |
} else { |
| 83 |
PRINTPRIME(atoi(argv[1])); |
|
| 84 |
}else{ |
/* try to open file for reading */ |
| 85 |
while(fgets(num, 11, fp)) |
FILE * fp = fopen(argv[1], "r"); |
| 86 |
PRINTPRIME(atoi(num)); |
|
| 87 |
|
/* (1) test-single-number mode: first argument is not a filename */ |
| 88 |
|
if (fp == NULL) { |
| 89 |
|
long int number = convert_number(argv[1]); |
| 90 |
|
PRINTPRIME(number); |
| 91 |
|
|
| 92 |
|
/* (2) file mode: read numbers from file */ |
| 93 |
|
} else { |
| 94 |
|
char line[MAX_LINE_LENGTH + 1]; |
| 95 |
|
long int number; |
| 96 |
|
int line_length; |
| 97 |
|
long int line_no = 0; |
| 98 |
|
while (fgets(line, MAX_LINE_LENGTH + 1, fp)) { |
| 99 |
|
|
| 100 |
|
/* count line number (for warnings) */ |
| 101 |
|
line_no++; |
| 102 |
|
|
| 103 |
|
line_length = strlen(line); |
| 104 |
|
|
| 105 |
|
/* skip empty lines */ |
| 106 |
|
if (strlen(line) < 2) continue; |
| 107 |
|
|
| 108 |
|
/* line handling: policy = skip exceeding lines */ |
| 109 |
|
|
| 110 |
|
/* line exceeds max length */ |
| 111 |
|
if (line_length == MAX_LINE_LENGTH && line[line_length-1] != '\n') { |
| 112 |
|
char message[254]; |
| 113 |
|
snprintf(message, 256, "Line too long (max %i chars) in line number: %i", MAX_LINE_LENGTH, line_no); |
| 114 |
|
PRINTWARNING(message); |
| 115 |
|
|
| 116 |
|
/* eat all characters until newline or EOF */ |
| 117 |
|
while (1) { |
| 118 |
|
int charcode = fgetc(fp); |
| 119 |
|
if (charcode == 10 || charcode == EOF) break; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
/* skip this line from prime calculation */ |
| 123 |
|
continue; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
/* if last char is newline, strip it */ |
| 127 |
|
if (line[line_length-1] == '\n') { |
| 128 |
|
line[line_length-1] = '\0'; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
/* finally: prime number calculation and output */ |
| 132 |
|
number = convert_number(line); |
| 133 |
|
PRINTPRIME(number); |
| 134 |
|
|
| 135 |
|
} |
| 136 |
fclose(fp); |
fclose(fp); |
| 137 |
} |
} |
| 138 |
} |
} |