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