chiark / gitweb /
Merge branch '2.4.x' into 2.5.x
[catacomb] / progs / factorial.c
1 /* -*-c-*-
2  *
3  * Example factorial computation
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <ctype.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <mLib/mdwopt.h>
38 #include <mLib/quis.h>
39 #include <mLib/report.h>
40
41 #include "mpint.h"
42 #include "mpmul.h"
43 #include "mptext.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 static void usage(FILE *fp)
48 {
49   pquis(fp, "Usage: $ [-r RADIX] INTEGER\n");
50 }
51
52 static void version(FILE *fp)
53 {
54   pquis(fp, "$, Catacomb version " VERSION "\n");
55 }
56
57 static void help(FILE *fp)
58 {
59   version(fp);
60   putc('\n', fp);
61   usage(fp);
62   fputs("\n\
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\
71 the value 36.\n\
72 \n\
73 Options provided:\n\
74 \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\
78 \n\
79 -r, --radix=N           Write output in base N.\n\
80 ", fp);
81 }
82
83 int main(int argc, char *argv[])
84 {
85   unsigned long x;
86   int r = 10;
87   char *p;
88   mp *f, *ulmax, *xx;
89   unsigned fl = 0;
90
91 #define f_bogus 1u
92
93   ego(argv[0]);
94
95   for (;;) {
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' },
101       { 0,              0,              0,      0 }
102     };
103     int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
104     if (i < 0)
105       break;
106     switch (i) {
107       case 'h':
108         help(stdout);
109         exit(0);
110       case 'v':
111         version(stdout);
112         exit(0);
113       case 'u':
114         usage(stdout);
115         exit(0);
116       case 'r':
117         r = atoi(optarg);
118         if (r < 2 || r > 62)
119           die(EXIT_FAILURE, "bad radix `%s'", optarg);
120         break;
121       default:
122         fl |= f_bogus;
123         break;
124     }
125   }
126
127   if (optind + 1 != argc || (fl & f_bogus)) {
128     usage(stderr);
129     exit(EXIT_FAILURE);
130   }
131   ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
132   p = argv[optind];
133   while (isspace((unsigned char)*p))
134     p++;
135   xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
136   while (isspace((unsigned char)*p))
137     p++;
138   if (!xx || *p || MP_CMP(xx, <, MP_ZERO) || MP_CMP(xx, >, ulmax))
139     die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
140   x = mp_toulong(xx);
141   mp_drop(xx);
142   mp_drop(ulmax);
143   f = mp_factorial(x);
144   mp_writefile(f, stdout, r);
145   fputc('\n', stdout);
146   mp_drop(f);
147   return (0);
148 }
149
150 /*----- That's all, folks -------------------------------------------------*/