3 * Example factorial computation
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/macros.h>
38 #include <mLib/mdwopt.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
46 /*----- Main code ---------------------------------------------------------*/
48 static void usage(FILE *fp
)
50 pquis(fp
, "Usage: $ [-r RADIX] INTEGER\n");
53 static void version(FILE *fp
)
55 pquis(fp
, "$, Catacomb version " VERSION
"\n");
58 static void help(FILE *fp
)
64 Prints the factorial of the given integer on its output. Input may be\n\
65 in decimal (the default), octal with preceding zero, hex with preceding\n\
66 `0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\
67 Output may be in any base between 2 and 62; the default is base 10. For\n\
68 bases between 11 and 36 inclusive, lowercase letters of either case are\n\
69 used as additional digits with values 10 upwards; lowercase is always\n\
70 used for output. For bases between 37 and 62 inclusive, lowercase letters\n\
71 have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
76 -h, --help Display this help message.\n\
77 -v, --version Display the version number.\n\
78 -u, --usage Display a usage message.\n\
80 -r, --radix=N Write output in base N.\n\
84 int main(int argc
, char *argv
[])
97 static const struct option opt
[] = {
98 { "help", 0, 0, 'h' },
99 { "version", 0, 0, 'v' },
100 { "usage", 0, 0, 'u' },
101 { "radix", OPTF_ARGREQ
, 0, 'r' },
104 int i
= mdwopt(argc
, argv
, "hvur:", opt
, 0, 0, 0);
120 die(EXIT_FAILURE
, "bad radix `%s'", optarg
);
128 if (optind
+ 1 != argc
|| (fl
& f_bogus
)) {
132 ulmax
= mp_fromulong(MP_NEW
, ULONG_MAX
);
136 xx
= mp_readstring(MP_NEW
, argv
[optind
], &p
, 0);
139 if (!xx
|| *p
|| MP_CMP(xx
, <, MP_ZERO
) || MP_CMP(xx
, >, ulmax
))
140 die(EXIT_FAILURE
, "bad integer `%s'", argv
[optind
]);
145 mp_writefile(f
, stdout
, r
);
151 /*----- That's all, folks -------------------------------------------------*/