3 * $Id: genprimes.c,v 1.7 2004/04/08 01:36:15 mdw Exp $
5 * Generate prime number table
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
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>
44 /*----- Data structures ---------------------------------------------------*/
48 /*----- Main code ---------------------------------------------------------*/
50 int main(int argc
, char *argv
[])
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";
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 (p_n
|| p_max
>= 2)
102 for (i
= 3; (!p_max
&& !p_n
) ||
103 (p_n
&& DA_LEN(&p
) < p_n
) ||
104 (p_max
&& i
<= p_max
);
107 for (j
= 0; j
< DA_LEN(&p
); j
++) {
108 if (i
% DA(&p
)[j
] == 0)
116 FILE *fp
= fopen(header
, "w");
120 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
122 for (q
= header
; *q
; q
++) {
123 int ch
= (unsigned char)*q
;
136 * Table of small prime numbers [generated]\n\
142 #define NPRIME %luu\n\
143 #define MAXPRIME %uu\n\
145 typedef %s smallprime;\n\
146 extern const smallprime %s[];\n\
151 (unsigned long)DA_LEN(&p
),
155 if (fclose(fp
) == EOF
) {
157 die(EXIT_FAILURE
, "error writing `%s': %s", header
, strerror(errno
));
162 FILE *fp
= fopen(source
, "w");
165 die(EXIT_FAILURE
, "couldn't write `%s': %s", source
, strerror(errno
));
169 * Table of small prime numbers [generated]\n\
176 for (i
= 0; i
< DA_LEN(&p
); i
++) {
179 fprintf(fp
, "%5i, ", DA(&p
)[i
]);
184 if (fclose(fp
) == EOF
) {
186 die(EXIT_FAILURE
, "error writing `%s': %s", source
, strerror(errno
));
193 /*----- That's all, folks -------------------------------------------------*/