3 * $Id: genprimes.c,v 1.1 1999/11/19 13:19:37 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.1 1999/11/19 13:19:37 mdw
34 * Generate small primes table.
38 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/darray.h>
47 #include <mLib/dstr.h>
48 #include <mLib/mdwopt.h>
49 #include <mLib/quis.h>
50 #include <mLib/report.h>
52 /*----- Data structures ---------------------------------------------------*/
56 /*----- Main code ---------------------------------------------------------*/
58 int main(int argc
, char *argv
[])
60 int p_max
= 0, p_n
= 0;
61 char *type
= "unsigned int";
62 char *header
= "ptab.h";
63 char *source
= "ptab.c";
71 int i
= getopt(argc
, argv
, "h:c:i:n:m:t:");
96 pquis(stderr
, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
102 die(EXIT_FAILURE
, "bad arguments to `-n' or `-m'");
106 for (i
= 3; (!p_max
&& !p_n
) ||
107 (p_n
&& DA_LEN(&p
) < p_n
) ||
108 (p_max
&& i
<= p_max
);
111 for (j
= 0; j
< DA_LEN(&p
); j
++) {
112 if (i
% DA(&p
)[j
] == 0)
120 FILE *fp
= fopen(header
, "w");
124 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
125 for (q
= header
; *q
; q
++) {
126 int ch
= (unsigned char)*q
;
137 * Table of small prime numbers [generated]\n\
143 #define NPRIME %lu\n\
144 #define MAXPRIME %i\n\
151 (unsigned long)DA_LEN(&p
),
152 DA(&p
)[DA_LEN(&p
) - 1],
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 -------------------------------------------------*/