blowfish-mktab.c: Remove the eye-candy progress meter.
[u/mdw/catacomb] / fibonacci.c
CommitLineData
ab916894
MW
1/* -*-c-*-
2 *
3 * Example Fibonacci number computation
4 *
5 * (c) 2013 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 "mptext.h"
43
44#include "mp-fibonacci.h"
45
46/*----- Main code ---------------------------------------------------------*/
47
48static void usage(FILE *fp)
49 { pquis(fp, "Usage: $ [-r RADIX] INTEGER\n"); }
50
51static void version(FILE *fp)
52 { pquis(fp, "$, Catacomb version " VERSION "\n"); }
53
54static void help(FILE *fp)
55{
56 version(fp);
57 putc('\n', fp);
58 usage(fp);
59 fputs("\n\
60Prints the Nth Fibonacci number for a given inteer N. Input may be in\n\
61decimal (the default), octal with preceding zero, hex with preceding `0x',\n\
62or any base N between 2 and 62 inclusive with preceding `N_'. Output\n\
63may be in any base between 2 and 62; the default is base 10. For bases\n\
64between 11 and 36 inclusive, lowercase letters of either case are used as\n\
65additional digits with values 10 upwards; lowercase is always used for\n\
66output. For bases between 37 and 62 inclusive, lowercase letters have\n\
67lower value than uppercase; hence `a' has the value 10, while `A' has the\n\
68value 36.\n\
69\n\
70Options provided:\n\
71\n\
72-h, --help Display this help message.\n\
73-v, --version Display the version number.\n\
74-u, --usage Display a usage message.\n\
75\n\
76-r, --radix=N Write output in base N.\n\
77", fp);
78}
79
80int main(int argc, char *argv[])
81{
82 long n;
83 int r = 10;
84 char *p;
85 mp *f, *lmin, *lmax, *nn;
86 unsigned fl = 0;
87
88#define f_bogus 1u
89
90 ego(argv[0]);
91
92 for (;;) {
93 static const struct option opt[] = {
94 { "help", 0, 0, 'h' },
95 { "version", 0, 0, 'v' },
96 { "usage", 0, 0, 'u' },
97 { "radix", OPTF_ARGREQ, 0, 'r' },
98 { 0, 0, 0, 0 }
99 };
100 int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
101 if (i < 0)
102 break;
103 switch (i) {
104 case 'h':
105 help(stdout);
106 exit(0);
107 case 'v':
108 version(stdout);
109 exit(0);
110 case 'u':
111 usage(stdout);
112 exit(0);
113 case 'r':
114 r = atoi(optarg);
115 if (r < 2 || r > 62)
116 die(EXIT_FAILURE, "bad radix `%s'", optarg);
117 break;
118 default:
119 fl |= f_bogus;
120 break;
121 }
122 }
123
124 if (optind + 1 != argc || (fl & f_bogus)) {
125 usage(stderr);
126 exit(EXIT_FAILURE);
127 }
128 lmin = mp_fromlong(MP_NEW, LONG_MIN);
129 lmax = mp_fromlong(MP_NEW, LONG_MAX);
130 p = argv[optind];
131 while (isspace((unsigned char)*p)) p++;
132 nn = mp_readstring(MP_NEW, argv[optind], &p, 0);
133 while (isspace((unsigned char)*p)) p++;
134 if (!nn || *p || MP_CMP(lmin, >, nn) || MP_CMP(nn, >, lmax))
135 die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
136 n = mp_tolong(nn);
137 mp_drop(nn); mp_drop(lmin); mp_drop(lmax);
138 f = mp_fibonacci(n);
139 mp_writefile(f, stdout, r);
140 fputc('\n', stdout);
141 mp_drop(f);
142 return (0);
143}
144
145/*----- That's all, folks -------------------------------------------------*/