1 |
joko |
1.9 |
/* $Id: prime.c,v 1.8 2006/05/12 23:38:15 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.4 |
|
15 |
joko |
1.5 |
/* check for prime number */ |
16 |
joko |
1.6 |
BOOL is_prime(long int number) |
17 |
joko |
1.1 |
{ |
18 |
joko |
1.4 |
int i; |
19 |
joko |
1.7 |
|
20 |
joko |
1.9 |
/* negative values, 0 and 1 are never prime numbers */ |
21 |
joko |
1.7 |
if (number < 2) return FALSE; |
22 |
|
|
|
23 |
|
|
/* check all numbers 2..sqrt(number) for being a prime number */ |
24 |
joko |
1.4 |
for (i=2; i*i <= number; i++) { |
25 |
|
|
if ((number % i) == 0) |
26 |
joko |
1.6 |
return FALSE; |
27 |
|
|
} |
28 |
|
|
return TRUE; |
29 |
|
|
} |
30 |
|
|
|
31 |
joko |
1.7 |
/* convert from string to long int, with error checking */ |
32 |
joko |
1.6 |
long int convert_number(const char *nptr) { |
33 |
joko |
1.9 |
|
34 |
|
|
errno = 0; |
35 |
|
|
char * endptr; |
36 |
|
|
long int number = strtol(nptr, &endptr, 10); |
37 |
|
|
|
38 |
|
|
/* invalid characters? */ |
39 |
|
|
if (*endptr != '\0') { |
40 |
|
|
char message[254]; |
41 |
|
|
snprintf(message, 256, "Could not convert '%s' to a valid number.", nptr); |
42 |
|
|
PRINTERROR(message); |
43 |
|
|
exit(EXIT_FAILURE); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
/* invalid range? */ |
47 |
|
|
/* if (number == LONG_MAX || number == LONG_MIN) { */ |
48 |
|
|
if (errno == ERANGE) { |
49 |
joko |
1.7 |
char message[254]; |
50 |
joko |
1.8 |
snprintf(message, 256, "Number is not in range of 'long int': %s", nptr); |
51 |
joko |
1.7 |
PRINTERROR(message); |
52 |
joko |
1.9 |
exit(EXIT_FAILURE); |
53 |
joko |
1.4 |
} |
54 |
joko |
1.9 |
|
55 |
|
|
return number; |
56 |
|
|
|
57 |
joko |
1.1 |
} |
58 |
joko |
1.3 |
|
59 |
joko |
1.1 |
int main(int argc, char * argv[]) |
60 |
|
|
{ |
61 |
joko |
1.4 |
|
62 |
|
|
if (argc == 1) { |
63 |
joko |
1.6 |
PRINTERROR("No arguments given."); |
64 |
joko |
1.9 |
exit(EXIT_FAILURE); |
65 |
joko |
1.4 |
} |
66 |
|
|
|
67 |
joko |
1.5 |
/* (3) range mode */ |
68 |
joko |
1.4 |
if (argc > 2) { |
69 |
joko |
1.7 |
long int i, j; |
70 |
joko |
1.9 |
i = convert_number(argv[1]); |
71 |
joko |
1.7 |
j = convert_number(argv[2]); |
72 |
joko |
1.9 |
for (i; i< j; i++) { |
73 |
joko |
1.1 |
PRINTPRIME(i); |
74 |
|
|
} |
75 |
joko |
1.4 |
|
76 |
joko |
1.5 |
/* other modes */ |
77 |
joko |
1.4 |
} else { |
78 |
|
|
|
79 |
joko |
1.5 |
/* try to open file for reading */ |
80 |
joko |
1.4 |
FILE * fp = fopen(argv[1], "r"); |
81 |
|
|
|
82 |
joko |
1.5 |
/* (1) test-single-number mode: first argument is not a filename */ |
83 |
joko |
1.4 |
if (fp == NULL) { |
84 |
joko |
1.7 |
long int number = convert_number(argv[1]); |
85 |
|
|
PRINTPRIME(number); |
86 |
joko |
1.4 |
|
87 |
joko |
1.5 |
/* (2) file mode: read numbers from file */ |
88 |
joko |
1.4 |
} else { |
89 |
joko |
1.7 |
char entry[11]; |
90 |
|
|
while (fgets(entry, 11, fp)) { |
91 |
|
|
long int number = convert_number(entry); |
92 |
|
|
PRINTPRIME(number); |
93 |
joko |
1.5 |
} |
94 |
joko |
1.1 |
fclose(fp); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
} |