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