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