chiark / gitweb /
mkphrase.c: Better error checking on the length range parameter.
[catacomb] / genprimes.c
1 /* -*-c-*-
2  *
3  * $Id: genprimes.c,v 1.7 2004/04/08 01:36:15 mdw Exp $
4  *
5  * Generate prime number table
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of Catacomb.
13  *
14  * Catacomb is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  *
19  * Catacomb is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  *
24  * You should have received a copy of the GNU Library General Public
25  * License along with Catacomb; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <mLib/darray.h>
39 #include <mLib/dstr.h>
40 #include <mLib/mdwopt.h>
41 #include <mLib/quis.h>
42 #include <mLib/report.h>
43
44 /*----- Data structures ---------------------------------------------------*/
45
46 DA_DECL(intv, int);
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 int main(int argc, char *argv[])
51 {
52   int p_max = 0, p_n = 0;
53   char *type = "unsigned int";
54   char *header = "primetab.h";
55   char *source = "primetab.c";
56   char *name = "primetab";
57   char *sym = 0;
58   intv p = DA_INIT;
59   int i;
60
61   ego(argv[0]);
62
63   for (;;) {
64     int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
65     if (i < 0)
66       break;
67     switch (i) {
68       case 'h':
69         header = optarg;
70         break;
71       case 'c':
72         source = optarg;
73         break;
74       case 'i':
75         name = optarg;
76         break;
77       case 'n':
78         p_max = 0;
79         p_n = atoi(optarg);
80         break;
81       case 'm':
82         p_n = 0;
83         p_max = atoi(optarg);
84         break;
85       case 't':
86         type = optarg;
87         break;
88       case 's':
89         sym = optarg;
90         break;
91       default:
92         pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
93         exit(EXIT_FAILURE);
94     }
95   }
96
97   if (!p_max && !p_n)
98     die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
99
100   if (p_n || p_max >= 2)
101     DA_PUSH(&p, 2);
102   for (i = 3; (!p_max && !p_n) ||
103               (p_n && DA_LEN(&p) < p_n) ||
104               (p_max && i <= p_max);
105        i += 2) {
106     int j;
107     for (j = 0; j < DA_LEN(&p); j++) {
108       if (i % DA(&p)[j] == 0)
109         goto composite;
110     }
111     DA_PUSH(&p, i);
112   composite:;
113   }
114
115   {
116     FILE *fp = fopen(header, "w");
117     dstr d = DSTR_INIT;
118     char *q;
119     if (!fp)
120       die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
121     if (!sym) {
122       for (q = header; *q; q++) {
123         int ch = (unsigned char)*q;
124         if (isalnum(ch))
125           ch = toupper(ch);
126         else
127           ch = '_';
128         DPUTC(&d, ch);
129       }
130       DPUTZ(&d);
131       sym = d.buf;
132     }
133     fprintf(fp, "\
134 /* -*-c-*-\n\
135  *\n\
136  * Table of small prime numbers [generated]\n\
137  */\n\
138 \n\
139 #ifndef %s\n\
140 #define %s\n\
141 \n\
142 #define NPRIME %luu\n\
143 #define MAXPRIME %uu\n\
144 \n\
145 typedef %s smallprime;\n\
146 extern const smallprime %s[];\n\
147 \n\
148 #endif\n\
149 ",
150             sym, sym,
151             (unsigned long)DA_LEN(&p),
152             DA_LAST(&p),
153             type, name);
154     dstr_destroy(&d);
155     if (fclose(fp) == EOF) {
156       remove(header);
157       die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
158     }
159   }
160
161   {
162     FILE *fp = fopen(source, "w");
163     int i;
164     if (!fp)
165       die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
166     fprintf(fp, "\
167 /* -*-c-*-\n\
168  *\n\
169  * Table of small prime numbers [generated]\n\
170  */\n\
171 \n\
172 #include \"%s\"\n\
173 \n\
174 const %s %s[] = {",
175             header, type, name);
176     for (i = 0; i < DA_LEN(&p); i++) {
177       if (i % 8 == 0)
178         fputs("\n  ", fp);
179       fprintf(fp, "%5i, ", DA(&p)[i]);
180     }
181     fputs("\n\
182 };\n\
183 ", fp);
184     if (fclose(fp) == EOF) {
185       remove(source);
186       die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
187     }
188   }
189
190   return (0);
191 }
192
193 /*----- That's all, folks -------------------------------------------------*/