3 * $Id: factorial.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
5 * Example factorial computation
7 * (c) 2000 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 ------------------------------------------------------*/
39 #include <mLib/mdwopt.h>
40 #include <mLib/quis.h>
41 #include <mLib/report.h>
47 /*----- Main code ---------------------------------------------------------*/
49 static void usage(FILE *fp
)
51 pquis(fp
, "Usage: $ [-r radix] integer\n");
54 static void version(FILE *fp
)
56 pquis(fp
, "$, Catacomb version " VERSION
"\n");
59 static void help(FILE *fp
)
65 Prints the factorial of the given integer on its output. Input may be\n\
66 in decimal (the default), octal with preceding zero, hex with preceding\n\
67 `0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\
68 Output may be in any base between 2 and 62; the default is base 10. For\n\
69 bases between 11 and 36 inclusive, lowercase letters of either case are\n\
70 used as additional digits with values 10 upwards; lowercase is always\n\
71 used for output. For bases between 37 and 62 inclusive, lowercase letters\n\
72 have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
77 -h, --help Display this help message.\n\
78 -v, --version Display the version number.\n\
79 -u, --usage Display a usage message.\n\
81 -r, --radix=N Write output in base N.\n\
85 int main(int argc
, char *argv
[])
98 static const struct option opt
[] = {
99 { "help", 0, 0, 'h' },
100 { "version", 0, 0, 'v' },
101 { "usage", 0, 0, 'u' },
102 { "radix", OPTF_ARGREQ
, 0, 'r' },
105 int i
= mdwopt(argc
, argv
, "hvur:", opt
, 0, 0, 0);
121 die(EXIT_FAILURE
, "bad radix `%s'", optarg
);
129 if (optind
+ 1 != argc
|| (fl
& f_bogus
)) {
133 ulmax
= mp_fromulong(MP_NEW
, ULONG_MAX
);
135 while (isspace((unsigned char)*p
))
137 xx
= mp_readstring(MP_NEW
, argv
[optind
], &p
, 0);
138 while (isspace((unsigned char)*p
))
140 if (!xx
|| *p
|| MP_CMP(xx
, >, ulmax
))
141 die(EXIT_FAILURE
, "bad integer `%s'", argv
[optind
]);
146 mp_writefile(f
, stdout
, r
);
152 /*----- That's all, folks -------------------------------------------------*/