X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/4afa7ff852b99306b4d82d7d9c295f73169ac208..c65df27983057ec76ed0e72bb370f9a5ae7dad28:/factorial.c diff --git a/factorial.c b/factorial.c index 71f2ad2..54b9845 100644 --- a/factorial.c +++ b/factorial.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: factorial.c,v 1.2 2001/06/16 13:22:59 mdw Exp $ + * $Id: factorial.c,v 1.4 2004/04/08 01:36:15 mdw Exp $ * * Example factorial computation * @@ -27,19 +27,12 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: factorial.c,v $ - * Revision 1.2 2001/06/16 13:22:59 mdw - * Added command-line option to select output radix. - * - * Revision 1.1 2000/07/09 21:30:49 mdw - * Demo program to compute factorials. - * - */ - /*----- Header files ------------------------------------------------------*/ +#include "config.h" + +#include +#include #include #include @@ -47,45 +40,108 @@ #include #include +#include "mpint.h" #include "mpmul.h" +#include "mptext.h" /*----- Main code ---------------------------------------------------------*/ +static void usage(FILE *fp) +{ + pquis(fp, "Usage: $ [-r radix] integer\n"); +} + +static void version(FILE *fp) +{ + pquis(fp, "$, Catacomb version " VERSION "\n"); +} + +static void help(FILE *fp) +{ + version(fp); + putc('\n', fp); + usage(fp); + fputs("\n\ +Prints the factorial of the given integer on its output. Input may be\n\ +in decimal (the default), octal with preceding zero, hex with preceding\n\ +`0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\ +Output may be in any base between 2 and 62; the default is base 10. For\n\ +bases between 11 and 36 inclusive, lowercase letters of either case are\n\ +used as additional digits with values 10 upwards; lowercase is always\n\ +used for output. For bases between 37 and 62 inclusive, lowercase letters\n\ +have lower value than uppercase; hence `a' has the value 10, while `A' has\n\ +the value 36.\n\ +\n\ +Options provided:\n\ +\n\ +-h, --help Display this help message.\n\ +-v, --version Display the version number.\n\ +-u, --usage Display a usage message.\n\ +\n\ +-r, --radix=N Write output in base N.\n\ +", fp); +} + int main(int argc, char *argv[]) { unsigned long x; int r = 10; char *p; - mp *f; + mp *f, *ulmax, *xx; + unsigned fl = 0; + +#define f_bogus 1u ego(argv[0]); for (;;) { static const struct option opt[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'v' }, + { "usage", 0, 0, 'u' }, { "radix", OPTF_ARGREQ, 0, 'r' }, { 0, 0, 0, 0 } }; - int i = mdwopt(argc, argv, "r:", opt, 0, 0, 0); + int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0); if (i < 0) break; switch (i) { + case 'h': + help(stdout); + exit(0); + case 'v': + version(stdout); + exit(0); + case 'u': + usage(stdout); + exit(0); case 'r': r = atoi(optarg); - if (r < 2 || r > 36) + if (r < 2 || r > 62) die(EXIT_FAILURE, "bad radix `%s'", optarg); break; default: - exit(EXIT_FAILURE); + fl |= f_bogus; + break; } } - if (optind + 1 != argc) { - pquis(stderr, "Usage: $ [-r radix] integer\n"); + if (optind + 1 != argc || (fl & f_bogus)) { + usage(stderr); exit(EXIT_FAILURE); } - x = strtoul(argv[optind], &p, 0); - if (*p) + ulmax = mp_fromulong(MP_NEW, ULONG_MAX); + p = argv[optind]; + while (isspace((unsigned char)*p)) + p++; + xx = mp_readstring(MP_NEW, argv[optind], &p, 0); + while (isspace((unsigned char)*p)) + p++; + if (!xx || *p || MP_CMP(xx, >, ulmax)) die(EXIT_FAILURE, "bad integer `%s'", argv[optind]); + x = mp_toulong(xx); + mp_drop(xx); + mp_drop(ulmax); f = mp_factorial(x); mp_writefile(f, stdout, r); fputc('\n', stdout);