progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / progs / factorial.c
CommitLineData
ff07f543 1/* -*-c-*-
2 *
ff07f543 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
141c1284 37#include <mLib/macros.h>
4afa7ff8 38#include <mLib/mdwopt.h>
ff07f543 39#include <mLib/quis.h>
40#include <mLib/report.h>
41
3235b49b 42#include "mpint.h"
ff07f543 43#include "mpmul.h"
3235b49b 44#include "mptext.h"
ff07f543 45
46/*----- Main code ---------------------------------------------------------*/
47
3235b49b 48static void usage(FILE *fp)
49{
80be0230 50 pquis(fp, "Usage: $ [-r RADIX] INTEGER\n");
3235b49b 51}
52
53static void version(FILE *fp)
54{
55 pquis(fp, "$, Catacomb version " VERSION "\n");
56}
57
58static void help(FILE *fp)
59{
60 version(fp);
61 putc('\n', fp);
62 usage(fp);
63 fputs("\n\
64Prints the factorial of the given integer on its output. Input may be\n\
65in 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\
67Output may be in any base between 2 and 62; the default is base 10. For\n\
68bases between 11 and 36 inclusive, lowercase letters of either case are\n\
69used as additional digits with values 10 upwards; lowercase is always\n\
70used for output. For bases between 37 and 62 inclusive, lowercase letters\n\
71have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
72the value 36.\n\
73\n\
74Options provided:\n\
75\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\
79\n\
80-r, --radix=N Write output in base N.\n\
81", fp);
82}
83
ff07f543 84int main(int argc, char *argv[])
85{
86 unsigned long x;
4afa7ff8 87 int r = 10;
ff07f543 88 char *p;
3235b49b 89 mp *f, *ulmax, *xx;
90 unsigned fl = 0;
91
92#define f_bogus 1u
ff07f543 93
94 ego(argv[0]);
95
4afa7ff8 96 for (;;) {
97 static const struct option opt[] = {
3235b49b 98 { "help", 0, 0, 'h' },
99 { "version", 0, 0, 'v' },
100 { "usage", 0, 0, 'u' },
4afa7ff8 101 { "radix", OPTF_ARGREQ, 0, 'r' },
102 { 0, 0, 0, 0 }
103 };
3235b49b 104 int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
4afa7ff8 105 if (i < 0)
106 break;
107 switch (i) {
3235b49b 108 case 'h':
109 help(stdout);
110 exit(0);
111 case 'v':
112 version(stdout);
113 exit(0);
114 case 'u':
115 usage(stdout);
116 exit(0);
4afa7ff8 117 case 'r':
118 r = atoi(optarg);
3235b49b 119 if (r < 2 || r > 62)
4afa7ff8 120 die(EXIT_FAILURE, "bad radix `%s'", optarg);
121 break;
122 default:
3235b49b 123 fl |= f_bogus;
124 break;
4afa7ff8 125 }
126 }
127
3235b49b 128 if (optind + 1 != argc || (fl & f_bogus)) {
129 usage(stderr);
ff07f543 130 exit(EXIT_FAILURE);
131 }
3235b49b 132 ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
133 p = argv[optind];
141c1284 134 while (ISSPACE(*p))
3235b49b 135 p++;
136 xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
141c1284 137 while (ISSPACE(*p))
3235b49b 138 p++;
1f14c0c6 139 if (!xx || *p || MP_CMP(xx, <, MP_ZERO) || MP_CMP(xx, >, ulmax))
4afa7ff8 140 die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
3235b49b 141 x = mp_toulong(xx);
142 mp_drop(xx);
143 mp_drop(ulmax);
ff07f543 144 f = mp_factorial(x);
4afa7ff8 145 mp_writefile(f, stdout, r);
ff07f543 146 fputc('\n', stdout);
147 mp_drop(f);
148 return (0);
149}
150
151/*----- That's all, folks -------------------------------------------------*/