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";
62 int i = getopt(argc, argv, "h:c:i:n:m:t:s:");
90 pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
96 die(EXIT_FAILURE, "bad arguments to `-n' or `-m'");
98 if (p_n || p_max >= 2)
100 for (i = 3; (!p_max && !p_n) ||
101 (p_n && DA_LEN(&p) < p_n) ||
102 (p_max && i <= p_max);
105 for (j = 0; j < DA_LEN(&p); j++) {
106 if (i % DA(&p)[j] == 0)
114 FILE *fp = fopen(header, "w");
118 die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno));
120 for (q = header; *q; q++) {
121 int ch = (unsigned char)*q;
134 * Table of small prime numbers [generated]\n\
140 #define NPRIME %luu\n\
141 #define MAXPRIME %uu\n\
143 typedef %s smallprime;\n\
144 extern const smallprime %s[];\n\
149 (unsigned long)DA_LEN(&p),
153 if (fclose(fp) == EOF) {
155 die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno));
160 FILE *fp = fopen(source, "w");
163 die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno));
167 * Table of small prime numbers [generated]\n\
174 for (i = 0; i < DA_LEN(&p); i++) {
177 fprintf(fp, "%5i, ", DA(&p)[i]);
182 if (fclose(fp) == EOF) {
184 die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno));
191 /*----- That's all, folks -------------------------------------------------*/