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