ff07f543 |
1 | /* -*-c-*- |
ff07f543 |
2 | * |
3 | * Example factorial computation |
4 | * |
5 | * (c) 2000 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
ff07f543 |
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. |
45c0fd36 |
16 | * |
ff07f543 |
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. |
45c0fd36 |
21 | * |
ff07f543 |
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 | |
ff07f543 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
3235b49b |
30 | #include "config.h" |
31 | |
32 | #include <ctype.h> |
33 | #include <limits.h> |
ff07f543 |
34 | #include <stdio.h> |
35 | #include <stdlib.h> |
36 | |
4afa7ff8 |
37 | #include <mLib/mdwopt.h> |
ff07f543 |
38 | #include <mLib/quis.h> |
39 | #include <mLib/report.h> |
40 | |
3235b49b |
41 | #include "mpint.h" |
ff07f543 |
42 | #include "mpmul.h" |
3235b49b |
43 | #include "mptext.h" |
ff07f543 |
44 | |
45 | /*----- Main code ---------------------------------------------------------*/ |
46 | |
3235b49b |
47 | static void usage(FILE *fp) |
48 | { |
80be0230 |
49 | pquis(fp, "Usage: $ [-r RADIX] INTEGER\n"); |
3235b49b |
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 | |
ff07f543 |
83 | int main(int argc, char *argv[]) |
84 | { |
85 | unsigned long x; |
4afa7ff8 |
86 | int r = 10; |
ff07f543 |
87 | char *p; |
3235b49b |
88 | mp *f, *ulmax, *xx; |
89 | unsigned fl = 0; |
90 | |
91 | #define f_bogus 1u |
ff07f543 |
92 | |
93 | ego(argv[0]); |
94 | |
4afa7ff8 |
95 | for (;;) { |
96 | static const struct option opt[] = { |
3235b49b |
97 | { "help", 0, 0, 'h' }, |
98 | { "version", 0, 0, 'v' }, |
99 | { "usage", 0, 0, 'u' }, |
4afa7ff8 |
100 | { "radix", OPTF_ARGREQ, 0, 'r' }, |
101 | { 0, 0, 0, 0 } |
102 | }; |
3235b49b |
103 | int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0); |
4afa7ff8 |
104 | if (i < 0) |
105 | break; |
106 | switch (i) { |
3235b49b |
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); |
4afa7ff8 |
116 | case 'r': |
117 | r = atoi(optarg); |
3235b49b |
118 | if (r < 2 || r > 62) |
4afa7ff8 |
119 | die(EXIT_FAILURE, "bad radix `%s'", optarg); |
120 | break; |
121 | default: |
3235b49b |
122 | fl |= f_bogus; |
123 | break; |
4afa7ff8 |
124 | } |
125 | } |
126 | |
3235b49b |
127 | if (optind + 1 != argc || (fl & f_bogus)) { |
128 | usage(stderr); |
ff07f543 |
129 | exit(EXIT_FAILURE); |
130 | } |
3235b49b |
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++; |
1f14c0c6 |
138 | if (!xx || *p || MP_CMP(xx, <, MP_ZERO) || MP_CMP(xx, >, ulmax)) |
4afa7ff8 |
139 | die(EXIT_FAILURE, "bad integer `%s'", argv[optind]); |
3235b49b |
140 | x = mp_toulong(xx); |
141 | mp_drop(xx); |
142 | mp_drop(ulmax); |
ff07f543 |
143 | f = mp_factorial(x); |
4afa7ff8 |
144 | mp_writefile(f, stdout, r); |
ff07f543 |
145 | fputc('\n', stdout); |
146 | mp_drop(f); |
147 | return (0); |
148 | } |
149 | |
150 | /*----- That's all, folks -------------------------------------------------*/ |