3 * $Id: genprimes.c,v 1.4 2001/03/04 13:08:10 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.4 2001/03/04 13:08:10 mdw
34 * Use @DA_LAST@ to determine @MAXPRIME@, now that it exists.
36 * Revision 1.3 2000/08/15 21:41:58 mdw
37 * Create a new type for the small primes table elements.
39 * Revision 1.2 1999/12/22 15:48:39 mdw
40 * Rename output file. Make output constants unsigned.
42 * Revision 1.1 1999/11/19 13:19:37 mdw
43 * Generate small primes table.
47 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/darray.h>
56 #include <mLib/dstr.h>
57 #include <mLib/mdwopt.h>
58 #include <mLib/quis.h>
59 #include <mLib/report.h>
61 /*----- Data structures ---------------------------------------------------*/
65 /*----- Main code ---------------------------------------------------------*/
67 int main(int argc
, char *argv
[])
69 int p_max
= 0, p_n
= 0;
70 char *type
= "unsigned int";
71 char *header
= "primetab.h";
72 char *source
= "primetab.c";
73 char *name
= "primetab";
80 int i
= getopt(argc
, argv
, "h:c:i:n:m:t:");
105 pquis(stderr
, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
111 die(EXIT_FAILURE
, "bad arguments to `-n' or `-m'");
113 if (p_n
|| p_max
>= 2)
115 for (i
= 3; (!p_max
&& !p_n
) ||
116 (p_n
&& DA_LEN(&p
) < p_n
) ||
117 (p_max
&& i
<= p_max
);
120 for (j
= 0; j
< DA_LEN(&p
); j
++) {
121 if (i
% DA(&p
)[j
] == 0)
129 FILE *fp
= fopen(header
, "w");
133 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
134 for (q
= header
; *q
; q
++) {
135 int ch
= (unsigned char)*q
;
146 * Table of small prime numbers [generated]\n\
152 #define NPRIME %luu\n\
153 #define MAXPRIME %uu\n\
155 typedef %s smallprime;\n\
156 extern smallprime %s[];\n\
161 (unsigned long)DA_LEN(&p
),
165 if (fclose(fp
) == EOF
) {
167 die(EXIT_FAILURE
, "error writing `%s': %s", header
, strerror(errno
));
172 FILE *fp
= fopen(source
, "w");
175 die(EXIT_FAILURE
, "couldn't write `%s': %s", source
, strerror(errno
));
179 * Table of small prime numbers [generated]\n\
186 for (i
= 0; i
< DA_LEN(&p
); i
++) {
189 fprintf(fp
, "%5i, ", DA(&p
)[i
]);
194 if (fclose(fp
) == EOF
) {
196 die(EXIT_FAILURE
, "error writing `%s': %s", source
, strerror(errno
));
203 /*----- That's all, folks -------------------------------------------------*/