3 * $Id: genprimes.c,v 1.3 2000/08/15 21:41:58 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 /*----- Revision history --------------------------------------------------*
32 * $Log: genprimes.c,v $
33 * Revision 1.3 2000/08/15 21:41:58 mdw
34 * Create a new type for the small primes table elements.
36 * Revision 1.2 1999/12/22 15:48:39 mdw
37 * Rename output file. Make output constants unsigned.
39 * Revision 1.1 1999/11/19 13:19:37 mdw
40 * Generate small primes table.
44 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/darray.h>
53 #include <mLib/dstr.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
58 /*----- Data structures ---------------------------------------------------*/
62 /*----- Main code ---------------------------------------------------------*/
64 int main(int argc
, char *argv
[])
66 int p_max
= 0, p_n
= 0;
67 char *type
= "unsigned int";
68 char *header
= "primetab.h";
69 char *source
= "primetab.c";
70 char *name
= "primetab";
77 int i
= getopt(argc
, argv
, "h:c:i:n:m:t:");
102 pquis(stderr
, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
108 die(EXIT_FAILURE
, "bad arguments to `-n' or `-m'");
110 if (p_n
|| p_max
>= 2)
112 for (i
= 3; (!p_max
&& !p_n
) ||
113 (p_n
&& DA_LEN(&p
) < p_n
) ||
114 (p_max
&& i
<= p_max
);
117 for (j
= 0; j
< DA_LEN(&p
); j
++) {
118 if (i
% DA(&p
)[j
] == 0)
126 FILE *fp
= fopen(header
, "w");
130 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
131 for (q
= header
; *q
; q
++) {
132 int ch
= (unsigned char)*q
;
143 * Table of small prime numbers [generated]\n\
149 #define NPRIME %luu\n\
150 #define MAXPRIME %uu\n\
152 typedef %s smallprime;\n\
153 extern smallprime %s[];\n\
158 (unsigned long)DA_LEN(&p
),
159 DA(&p
)[DA_LEN(&p
) - 1],
162 if (fclose(fp
) == EOF
) {
164 die(EXIT_FAILURE
, "error writing `%s': %s", header
, strerror(errno
));
169 FILE *fp
= fopen(source
, "w");
172 die(EXIT_FAILURE
, "couldn't write `%s': %s", source
, strerror(errno
));
176 * Table of small prime numbers [generated]\n\
183 for (i
= 0; i
< DA_LEN(&p
); i
++) {
186 fprintf(fp
, "%5i, ", DA(&p
)[i
]);
191 if (fclose(fp
) == EOF
) {
193 die(EXIT_FAILURE
, "error writing `%s': %s", source
, strerror(errno
));
200 /*----- That's all, folks -------------------------------------------------*/