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/mdwopt.h>
38 #include <mLib/quis.h>
39 #include <mLib/report.h>
45 /*----- Main code ---------------------------------------------------------*/
47 static void usage(FILE *fp)
49 pquis(fp, "Usage: $ [-r RADIX] INTEGER\n");
52 static void version(FILE *fp)
54 pquis(fp, "$, Catacomb version " VERSION "\n");
57 static void help(FILE *fp)
63 Prints the factorial of the given integer on its output. Input may be\n\
64 in decimal (the default), octal with preceding zero, hex with preceding\n\
65 `0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\
66 Output may be in any base between 2 and 62; the default is base 10. For\n\
67 bases between 11 and 36 inclusive, lowercase letters of either case are\n\
68 used as additional digits with values 10 upwards; lowercase is always\n\
69 used for output. For bases between 37 and 62 inclusive, lowercase letters\n\
70 have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
75 -h, --help Display this help message.\n\
76 -v, --version Display the version number.\n\
77 -u, --usage Display a usage message.\n\
79 -r, --radix=N Write output in base N.\n\
83 int main(int argc, char *argv[])
96 static const struct option opt[] = {
97 { "help", 0, 0, 'h' },
98 { "version", 0, 0, 'v' },
99 { "usage", 0, 0, 'u' },
100 { "radix", OPTF_ARGREQ, 0, 'r' },
103 int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
119 die(EXIT_FAILURE, "bad radix `%s'", optarg);
127 if (optind + 1 != argc || (fl & f_bogus)) {
131 ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
133 while (isspace((unsigned char)*p))
135 xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
136 while (isspace((unsigned char)*p))
138 if (!xx || *p || MP_CMP(xx, >, ulmax))
139 die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
144 mp_writefile(f, stdout, r);
150 /*----- That's all, folks -------------------------------------------------*/