New interface to find out whether a key has expired. Also, a bug fix
[u/mdw/catacomb] / factorial.c
1 /* -*-c-*-
2 *
3 * $Id: factorial.c,v 1.2 2001/06/16 13:22:59 mdw Exp $
4 *
5 * Example factorial computation
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: factorial.c,v $
33 * Revision 1.2 2001/06/16 13:22:59 mdw
34 * Added command-line option to select output radix.
35 *
36 * Revision 1.1 2000/07/09 21:30:49 mdw
37 * Demo program to compute factorials.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <stdio.h>
44 #include <stdlib.h>
45
46 #include <mLib/mdwopt.h>
47 #include <mLib/quis.h>
48 #include <mLib/report.h>
49
50 #include "mpmul.h"
51
52 /*----- Main code ---------------------------------------------------------*/
53
54 int main(int argc, char *argv[])
55 {
56 unsigned long x;
57 int r = 10;
58 char *p;
59 mp *f;
60
61 ego(argv[0]);
62
63 for (;;) {
64 static const struct option opt[] = {
65 { "radix", OPTF_ARGREQ, 0, 'r' },
66 { 0, 0, 0, 0 }
67 };
68 int i = mdwopt(argc, argv, "r:", opt, 0, 0, 0);
69 if (i < 0)
70 break;
71 switch (i) {
72 case 'r':
73 r = atoi(optarg);
74 if (r < 2 || r > 36)
75 die(EXIT_FAILURE, "bad radix `%s'", optarg);
76 break;
77 default:
78 exit(EXIT_FAILURE);
79 }
80 }
81
82 if (optind + 1 != argc) {
83 pquis(stderr, "Usage: $ [-r radix] integer\n");
84 exit(EXIT_FAILURE);
85 }
86 x = strtoul(argv[optind], &p, 0);
87 if (*p)
88 die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
89 f = mp_factorial(x);
90 mp_writefile(f, stdout, r);
91 fputc('\n', stdout);
92 mp_drop(f);
93 return (0);
94 }
95
96 /*----- That's all, folks -------------------------------------------------*/