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/macros.h>
39 #include <mLib/mdwopt.h>
40 #include <mLib/quis.h>
41 #include <mLib/report.h>
43 /*----- Data structures ---------------------------------------------------*/
47 /*----- Main code ---------------------------------------------------------*/
49 int main(int argc, char *argv[])
51 int p_max = 0, p_n = 0;
52 char *type = "unsigned int";
53 char *header = "primetab.h";
54 char *source = "primetab.c";
55 char *name = "primetab";
64 int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
92 pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
98 die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
100 if ((hdrbase = strrchr(header, '/')) == 0) hdrbase = header;
103 if (p_n || p_max >= 2)
105 for (i = 3; (!p_max && !p_n) ||
106 (p_n && DA_LEN(&p) < p_n) ||
107 (p_max && i <= p_max);
110 for (j = 0; j < DA_LEN(&p); j++) {
111 if (i % DA(&p)[j] == 0)
119 FILE *fp = fopen(header, "w");
123 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
125 for (q = header; *q; q++) {
126 int ch = (unsigned char)*q;
139 * Table of small prime numbers [generated]\n\
145 #define NPRIME %luu\n\
146 #define MAXPRIME %uu\n\
148 typedef %s smallprime;\n\
149 extern const smallprime %s[];\n\
154 (unsigned long)DA_LEN(&p),
158 if (fclose(fp) == EOF) {
160 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
165 FILE *fp = fopen(source, "w");
168 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
172 * Table of small prime numbers [generated]\n\
178 hdrbase, type, name);
179 for (i = 0; i < DA_LEN(&p); i++) {
182 fprintf(fp, "%5i, ", DA(&p)[i]);
187 if (fclose(fp) == EOF) {
189 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
196 /*----- That's all, folks -------------------------------------------------*/