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