3 * $Id: genprimes.c,v 1.2 1999/12/22 15:48:39 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.2 1999/12/22 15:48:39 mdw
34 * Rename output file. Make output constants unsigned.
36 * Revision 1.1 1999/11/19 13:19:37 mdw
37 * Generate small primes table.
41 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/darray.h>
50 #include <mLib/dstr.h>
51 #include <mLib/mdwopt.h>
52 #include <mLib/quis.h>
53 #include <mLib/report.h>
55 /*----- Data structures ---------------------------------------------------*/
59 /*----- Main code ---------------------------------------------------------*/
61 int main(int argc
, char *argv
[])
63 int p_max
= 0, p_n
= 0;
64 char *type
= "unsigned int";
65 char *header
= "primetab.h";
66 char *source
= "primetab.c";
67 char *name
= "primetab";
74 int i
= getopt(argc
, argv
, "h:c:i:n:m:t:");
99 pquis(stderr
, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n");
105 die(EXIT_FAILURE
, "bad arguments to `-n' or `-m'");
109 for (i
= 3; (!p_max
&& !p_n
) ||
110 (p_n
&& DA_LEN(&p
) < p_n
) ||
111 (p_max
&& i
<= p_max
);
114 for (j
= 0; j
< DA_LEN(&p
); j
++) {
115 if (i
% DA(&p
)[j
] == 0)
123 FILE *fp
= fopen(header
, "w");
127 die(EXIT_FAILURE
, "couldn't write `%s': %s", header
, strerror(errno
));
128 for (q
= header
; *q
; q
++) {
129 int ch
= (unsigned char)*q
;
140 * Table of small prime numbers [generated]\n\
146 #define NPRIME %luu\n\
147 #define MAXPRIME %uu\n\
154 (unsigned long)DA_LEN(&p
),
155 DA(&p
)[DA_LEN(&p
) - 1],
158 if (fclose(fp
) == EOF
) {
160 die(EXIT_FAILURE
, "error writing `%s': %s", header
, strerror(errno
));
165 FILE *fp
= fopen(source
, "w");
168 die(EXIT_FAILURE
, "couldn't write `%s': %s", source
, strerror(errno
));
172 * Table of small prime numbers [generated]\n\
179 for (i
= 0; i
< DA_LEN(&p
); i
++) {
182 fprintf(fp
, "%5i, ", DA(&p
)[i
]);
187 if (fclose(fp
) == EOF
) {
189 die(EXIT_FAILURE
, "error writing `%s': %s", source
, strerror(errno
));
196 /*----- That's all, folks -------------------------------------------------*/