3 * Example Fibonacci number computation
5 * (c) 2013 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/macros.h>
38 #include <mLib/mdwopt.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
45 #include "mp-fibonacci.h"
47 /*----- Main code ---------------------------------------------------------*/
49 static void usage(FILE *fp
)
50 { pquis(fp
, "Usage: $ [-r RADIX] INTEGER\n"); }
52 static void version(FILE *fp
)
53 { pquis(fp
, "$, Catacomb version " VERSION
"\n"); }
55 static void help(FILE *fp
)
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\
73 -h, --help Display this help message.\n\
74 -v, --version Display the version number.\n\
75 -u, --usage Display a usage message.\n\
77 -r, --radix=N Write output in base N.\n\
81 int main(int argc
, char *argv
[])
86 mp
*f
, *lmin
, *lmax
, *nn
;
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' },
101 int i
= mdwopt(argc
, argv
, "hvur:", opt
, 0, 0, 0);
117 die(EXIT_FAILURE
, "bad radix `%s'", optarg
);
125 if (optind
+ 1 != argc
|| (fl
& f_bogus
)) {
129 lmin
= mp_fromlong(MP_NEW
, LONG_MIN
);
130 lmax
= mp_fromlong(MP_NEW
, LONG_MAX
);
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
]);
138 mp_drop(nn
); mp_drop(lmin
); mp_drop(lmax
);
140 mp_writefile(f
, stdout
, r
);
146 /*----- That's all, folks -------------------------------------------------*/