]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - number/number.c
- avoid multipling a boolean value, use &&.
[bsdgames-darwin.git] / number / number.c
index 110400ead9528d93bfb99d404482f6c4e2fc9f59..0e7fbb03b097e868925b3592fc52e652e54f48c3 100644 (file)
@@ -1,6 +1,8 @@
+/*     $NetBSD: number.c,v 1.16 2014/03/23 00:03:04 dholland Exp $     */
+
 /*
- * Copyright (c) 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1988, 1993, 1994
+ *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1988 Regents of the University of California.\n\
- All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
+ The Regents of the University of California.  All rights reserved.");
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)number.c   5.1 (Berkeley) 2/28/91";
+#if 0
+static char sccsid[] = "@(#)number.c   8.3 (Berkeley) 5/4/95";
+#else
+__RCSID("$NetBSD: number.c,v 1.16 2014/03/23 00:03:04 dholland Exp $");
+#endif
 #endif /* not lint */
 
-#include <stdio.h>
+#include <sys/types.h>
+
 #include <ctype.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
-#define        YES             1
-#define        NO              0
-#define        EOS             '\0'
-#define        MAXNUM          65              /* biggest number we handle */
+#define        MAXNUM          65              /* Biggest number we handle. */
 
-static char    *name1[] = {
+static const char      *const name1[] = {
        "",             "one",          "two",          "three",
        "four",         "five",         "six",          "seven",
        "eight",        "nine",         "ten",          "eleven",
        "twelve",       "thirteen",     "fourteen",     "fifteen",
        "sixteen",      "seventeen",    "eighteen",     "nineteen",
 },
-               *name2[] = {
+               *const name2[] = {
        "",             "ten",          "twenty",       "thirty",
        "forty",        "fifty",        "sixty",        "seventy",
        "eighty",       "ninety",
 },
-               *name3[] = {
+               *const name3[] = {
        "hundred",      "thousand",     "million",      "billion",
        "trillion",     "quadrillion",  "quintillion",  "sextillion",
        "septillion",   "octillion",    "nonillion",    "decillion",
@@ -71,173 +76,215 @@ static char       *name1[] = {
        "novemdecillion",               "vigintillion",
 };
 
-main(argc,argv)
-       int     argc;
-       char    **argv;
+int    main(int, char *[]);
+static void convert(char *);
+static int number(const char *, size_t);
+static void pfract(size_t);
+static int unit(size_t, const char *);
+static void usage(void) __dead;
+
+static int lflag;
+
+int
+main(int argc, char *argv[])
 {
-       register int    cnt;
-       char    line[MAXNUM * 2 + 2];           /* MAXNUM '.' MAXNUM '\0' */
+       int ch, first;
+       char line[256];
 
-       if (argc > 1)
-               for (cnt = 1;cnt < argc;++cnt) {
-                       convert(argv[cnt]);
-                       puts("...");
+       lflag = 0;
+       while ((ch = getopt(argc, argv, "l")) != -1)
+               switch (ch) {
+               case 'l':
+                       lflag = 1;
+                       break;
+               case '?':
+               default:
+                       usage();
                }
-       else
-               while (fgets(line,sizeof(line),stdin)) {
+       argc -= optind;
+       argv += optind;
+
+       if (*argv == NULL)
+               for (first = 1;
+                   fgets(line, sizeof(line), stdin) != NULL; first = 0) {
+                       if (strchr(line, '\n') == NULL)
+                               errx(1, "line too long.");
+                       if (!first)
+                               (void)printf("...\n");
                        convert(line);
-                       puts("...");
+               }
+       else
+               for (first = 1; *argv != NULL; first = 0, ++argv) {
+                       if (!first)
+                               (void)printf("...\n");
+                       convert(*argv);
                }
        exit(0);
 }
 
-convert(line)
-       char    *line;
+void
+convert(char *line)
 {
-       register int    len,
-                       ret;
-       register char   *C,
-                       *fraction;
-
-       for (fraction = NULL, C = line;*C && *C != '\n';++C)
-               if (!isdigit(*C))
-                       switch(*C) {
-                       case '-':
-                               if (C != line)
-                                       usage(NO);
-                               break;
-                       case '.':
-                               if (!fraction) {
-                                       fraction = C + 1;
-                                       *C = EOS;
-                                       break;
-                               }
-                       default:
-                               usage(NO);
+       size_t flen, len;
+       int rval;
+       char *p, *fraction;
+
+       flen = 0;
+       fraction = NULL;
+       for (p = line; *p != '\0' && *p != '\n'; ++p) {
+               if (isblank((unsigned char)*p)) {
+                       if (p == line) {
+                               ++line;
+                               continue;
                        }
-       *C = EOS;
+                       goto badnum;
+               }
+               if (isdigit((unsigned char)*p))
+                       continue;
+               switch (*p) {
+               case '.':
+                       if (fraction != NULL)
+                               goto badnum;
+                       fraction = p + 1;
+                       *p = '\0';
+                       break;
+               case '-':
+                       if (p == line)
+                               break;
+                       /* FALLTHROUGH */
+               default:
+badnum:                        errx(1, "illegal number: %s", line);
+                       break;
+               }
+       }
+       *p = '\0';
+
+       if ((len = strlen(line)) > MAXNUM ||
+           (fraction != NULL && (flen = strlen(fraction)) > MAXNUM))
+               errx(1, "number too large, max %d digits.", MAXNUM);
+
        if (*line == '-') {
-               puts("minus");
+               (void)printf("minus%s", lflag ? " " : "\n");
                ++line;
+               --len;
        }
-       ret = NO;
-       if (len = strlen(line)) {
-               if (len > MAXNUM)
-                       usage(YES);
-               ret = unit(len,line);
-       }
-       if (fraction && (len = strlen(fraction))) {
-               if (len > MAXNUM)
-                       usage(YES);
-               for (C = fraction;*C;++C)
-                       if (*C != '0') {
-                               if (ret)
-                                       puts("and");
-                               if (unit(len,fraction)) {
-                                       ++ret;
-                                       pfract(len);
+
+       rval = len > 0 ? unit(len, line) : 0;
+       if (fraction != NULL && flen != 0)
+               for (p = fraction; *p != '\0'; ++p)
+                       if (*p != '0') {
+                               if (rval)
+                                       (void)printf("%sand%s",
+                                           lflag ? " " : "",
+                                           lflag ? " " : "\n");
+                               if (unit(flen, fraction)) {
+                                       if (lflag)
+                                               (void)printf(" ");
+                                       pfract(flen);
+                                       rval = 1;
                                }
                                break;
                        }
-       }
-       if (!ret)
-               puts("zero.");
+       if (!rval)
+               (void)printf("zero%s", lflag ? "" : ".\n");
+       if (lflag)
+               (void)printf("\n");
 }
 
-unit(len,C)
-       register int    len;
-       register char   *C;
+int
+unit(size_t len, const char *p)
 {
-       register int    off,
-                       ret;
+       size_t off;
+       int rval;
 
-       ret = NO;
+       rval = 0;
        if (len > 3) {
                if (len % 3) {
                        off = len % 3;
                        len -= off;
-                       if (number(C,off)) {
-                               ret = YES;
-                               printf(" %s.\n",name3[len / 3]);
+                       if (number(p, off)) {
+                               rval = 1;
+                               (void)printf(" %s%s",
+                                   name3[len / 3], lflag ? " " : ".\n");
                        }
-                       C += off;
+                       p += off;
                }
-               for (;len > 3;C += 3) {
+               for (; len > 3; p += 3) {
                        len -= 3;
-                       if (number(C,3)) {
-                               ret = YES;
-                               printf(" %s.\n",name3[len / 3]);
+                       if (number(p, 3)) {
+                               rval = 1;
+                               (void)printf(" %s%s",
+                                   name3[len / 3], lflag ? " " : ".\n");
                        }
                }
        }
-       if (number(C,len)) {
-               puts(".");
-               ret = YES;
+       if (number(p, len)) {
+               if (!lflag)
+                       (void)printf(".\n");
+               rval = 1;
        }
-       return(ret);
+       return (rval);
 }
 
-number(C,len)
-       register char   *C;
-       int     len;
+int
+number(const char *p, size_t len)
 {
-       register int    val,
-                       ret;
+       int val, rval;
 
-       ret = 0;
-       switch(len) {
+       rval = 0;
+       switch (len) {
        case 3:
-               if (*C != '0') {
-                       ++ret;
-                       printf("%s hundred",name1[*C - '0']);
+               if (*p != '0') {
+                       rval = 1;
+                       (void)printf("%s hundred", name1[*p - '0']);
                }
-               ++C;
-               /*FALLTHROUGH*/
+               ++p;
+               /* FALLTHROUGH */
        case 2:
-               val = (C[1] - '0') + (C[0] - '0') * 10;
+               val = (p[1] - '0') + (p[0] - '0') * 10;
                if (val) {
-                       if (ret++)
-                               putchar(' ');
+                       if (rval)
+                               (void)printf(" ");
                        if (val < 20)
-                               fputs(name1[val],stdout);
+                               (void)printf("%s", name1[val]);
                        else {
-                               fputs(name2[val / 10],stdout);
+                               (void)printf("%s", name2[val / 10]);
                                if (val % 10)
-                                       printf("-%s",name1[val % 10]);
+                                       (void)printf("-%s", name1[val % 10]);
                        }
+                       rval = 1;
                }
                break;
        case 1:
-               if (*C != '0') {
-                       ++ret;
-                       fputs(name1[*C - '0'],stdout);
+               if (*p != '0') {
+                       rval = 1;
+                       (void)printf("%s", name1[*p - '0']);
                }
        }
-       return(ret);
+       return (rval);
 }
 
-pfract(len)
-       register int    len;
+void
+pfract(size_t len)
 {
-       static char     *pref[] = { "", "ten-", "hundred-" };
+       static const char *const pref[] = { "", "ten-", "hundred-" };
 
        switch(len) {
        case 1:
-               puts("tenths.");
+               (void)printf("tenths.\n");
                break;
        case 2:
-               puts("hundredths.");
+               (void)printf("hundredths.\n");
                break;
        default:
-               printf("%s%sths.\n",pref[len % 3],name3[len / 3]);
+               (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
+               break;
        }
 }
 
-usage(toobig)
-       int     toobig;
+void
+usage(void)
 {
-       if (toobig)
-               fprintf(stderr,"number: number too large, max %d digits.\n",MAXNUM);
-       fputs("usage: number # ...\n",stderr);
-       exit(-1);
+       (void)fprintf(stderr, "usage: number [# ...]\n");
+       exit(1);
 }