3 * Generate prime number table
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
36 #include <mLib/darray.h>
37 #include <mLib/dstr.h>
38 #include <mLib/mdwopt.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
42 /*----- Data structures ---------------------------------------------------*/
46 /*----- Main code ---------------------------------------------------------*/
48 int main(int argc, char *argv[])
50 int p_max = 0, p_n = 0;
51 char *type = "unsigned int";
52 char *header = "primetab.h";
53 char *source = "primetab.c";
54 char *name = "primetab";
63 int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
91 pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
97 die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
99 if ((hdrbase = strrchr(header, '/')) == 0) hdrbase = header;
102 if (p_n || p_max >= 2)
104 for (i = 3; (!p_max && !p_n) ||
105 (p_n && DA_LEN(&p) < p_n) ||
106 (p_max && i <= p_max);
109 for (j = 0; j < DA_LEN(&p); j++) {
110 if (i % DA(&p)[j] == 0)
118 FILE *fp = fopen(header, "w");
122 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
124 for (q = header; *q; q++) {
125 int ch = (unsigned char)*q;
138 * Table of small prime numbers [generated]\n\
144 #define NPRIME %luu\n\
145 #define MAXPRIME %uu\n\
147 typedef %s smallprime;\n\
148 extern const smallprime %s[];\n\
153 (unsigned long)DA_LEN(&p),
157 if (fclose(fp) == EOF) {
159 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
164 FILE *fp = fopen(source, "w");
167 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
171 * Table of small prime numbers [generated]\n\
177 hdrbase, type, name);
178 for (i = 0; i < DA_LEN(&p); i++) {
181 fprintf(fp, "%5i, ", DA(&p)[i]);
186 if (fclose(fp) == EOF) {
188 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
195 /*----- That's all, folks -------------------------------------------------*/