130775Sbostic /*
266524Sbostic * Copyright (c) 1988, 1993, 1994
360823Sbostic * The Regents of the University of California. All rights reserved.
432703Sbostic *
542583Sbostic * %sccs.include.redist.c%
630775Sbostic */
78865Smckusick
830775Sbostic #ifndef lint
960823Sbostic static char copyright[] =
1066524Sbostic "@(#) Copyright (c) 1988, 1993, 1994\n\
1160823Sbostic The Regents of the University of California. All rights reserved.\n";
1232703Sbostic #endif /* not lint */
138865Smckusick
1432703Sbostic #ifndef lint
15*69223Sbostic static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 05/04/95";
1632703Sbostic #endif /* not lint */
1732703Sbostic
1866524Sbostic #include <sys/types.h>
1966524Sbostic
2066524Sbostic #include <ctype.h>
21*69223Sbostic #include <err.h>
2230214Sbostic #include <stdio.h>
2366524Sbostic #include <stdlib.h>
2466524Sbostic #include <string.h>
25*69223Sbostic #include <unistd.h>
2630214Sbostic
2766524Sbostic #define MAXNUM 65 /* Biggest number we handle. */
2830775Sbostic
2930775Sbostic static char *name1[] = {
3030775Sbostic "", "one", "two", "three",
3130775Sbostic "four", "five", "six", "seven",
3230775Sbostic "eight", "nine", "ten", "eleven",
3330775Sbostic "twelve", "thirteen", "fourteen", "fifteen",
3430775Sbostic "sixteen", "seventeen", "eighteen", "nineteen",
3530775Sbostic },
3630775Sbostic *name2[] = {
3730775Sbostic "", "ten", "twenty", "thirty",
3830775Sbostic "forty", "fifty", "sixty", "seventy",
3930775Sbostic "eighty", "ninety",
4030775Sbostic },
4130775Sbostic *name3[] = {
4230775Sbostic "hundred", "thousand", "million", "billion",
4330775Sbostic "trillion", "quadrillion", "quintillion", "sextillion",
4430775Sbostic "septillion", "octillion", "nonillion", "decillion",
4530775Sbostic "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
4630775Sbostic "quindecillion", "sexdecillion",
4730775Sbostic "septendecillion", "octodecillion",
4830775Sbostic "novemdecillion", "vigintillion",
498865Smckusick };
5030775Sbostic
5166524Sbostic void convert __P((char *));
5266524Sbostic int number __P((char *, int));
5366524Sbostic void pfract __P((int));
5466524Sbostic void toobig __P((void));
5566524Sbostic int unit __P((int, char *));
5666524Sbostic void usage __P((void));
5766524Sbostic
5866524Sbostic int lflag;
5966524Sbostic
6066524Sbostic int
main(argc,argv)6166524Sbostic main(argc, argv)
6266524Sbostic int argc;
6366524Sbostic char *argv[];
648865Smckusick {
6566524Sbostic int ch, first;
6666524Sbostic char line[256];
678865Smckusick
6866524Sbostic lflag = 0;
6966524Sbostic while ((ch = getopt(argc, argv, "l")) != EOF)
7066524Sbostic switch (ch) {
7166524Sbostic case 'l':
7266524Sbostic lflag = 1;
7366524Sbostic break;
7466524Sbostic case '?':
7566524Sbostic default:
7666524Sbostic usage();
778865Smckusick }
7866524Sbostic argc -= optind;
7966524Sbostic argv += optind;
8066524Sbostic
8166524Sbostic if (*argv == NULL)
8266524Sbostic for (first = 1;
8366524Sbostic fgets(line, sizeof(line), stdin) != NULL; first = 0) {
8466524Sbostic if (strchr(line, '\n') == NULL)
8566524Sbostic errx(1, "line too long.");
8666524Sbostic if (!first)
8766524Sbostic (void)printf("...\n");
8830775Sbostic convert(line);
898865Smckusick }
9066524Sbostic else
9166524Sbostic for (first = 1; *argv != NULL; first = 0, ++argv) {
9266524Sbostic if (!first)
9366524Sbostic (void)printf("...\n");
9466524Sbostic convert(*argv);
9566524Sbostic }
9630775Sbostic exit(0);
978865Smckusick }
988865Smckusick
9966524Sbostic void
convert(line)10030775Sbostic convert(line)
10166524Sbostic char *line;
1028865Smckusick {
10366524Sbostic register flen, len, rval;
10466524Sbostic register char *p, *fraction;
1058865Smckusick
10666524Sbostic fraction = NULL;
10766524Sbostic for (p = line; *p != '\0' && *p != '\n'; ++p) {
10866524Sbostic if (isblank(*p)) {
10966524Sbostic if (p == line) {
11066524Sbostic ++line;
11166524Sbostic continue;
11266524Sbostic }
11366524Sbostic goto badnum;
11466524Sbostic }
11566524Sbostic if (isdigit(*p))
11666524Sbostic continue;
11766524Sbostic switch (*p) {
11866524Sbostic case '.':
11966524Sbostic if (fraction != NULL)
12066524Sbostic goto badnum;
12166524Sbostic fraction = p + 1;
12266524Sbostic *p = '\0';
12366524Sbostic break;
12466524Sbostic case '-':
12566524Sbostic if (p == line)
12630775Sbostic break;
12766524Sbostic /* FALLTHROUGH */
12866524Sbostic default:
12966524Sbostic badnum: errx(1, "illegal number: %s", line);
13066524Sbostic break;
13166524Sbostic }
13266524Sbostic }
13366524Sbostic *p = '\0';
13466524Sbostic
13566524Sbostic if ((len = strlen(line)) > MAXNUM ||
13666524Sbostic fraction != NULL && (flen = strlen(fraction)) > MAXNUM)
13766524Sbostic errx(1, "number too large, max %d digits.", MAXNUM);
13866524Sbostic
13930775Sbostic if (*line == '-') {
14066524Sbostic (void)printf("minus%s", lflag ? " " : "\n");
14130775Sbostic ++line;
1428865Smckusick }
14366524Sbostic
14466524Sbostic rval = len > 0 ? unit(len, line) : 0;
14566524Sbostic if (fraction != NULL && flen != 0)
14666524Sbostic for (p = fraction; *p != '\0'; ++p)
14766524Sbostic if (*p != '0') {
14866524Sbostic if (rval)
14966524Sbostic (void)printf("%sand%s",
15066524Sbostic lflag ? " " : "",
15166524Sbostic lflag ? " " : "\n");
15266524Sbostic if (unit(flen, fraction)) {
15366524Sbostic if (lflag)
15466524Sbostic (void)printf(" ");
15566524Sbostic pfract(flen);
15666524Sbostic rval = 1;
15730775Sbostic }
15830775Sbostic break;
15930775Sbostic }
16066524Sbostic if (!rval)
16166524Sbostic (void)printf("zero%s", lflag ? "" : ".\n");
16266524Sbostic if (lflag)
16366524Sbostic (void)printf("\n");
1648865Smckusick }
1658865Smckusick
16666524Sbostic int
unit(len,p)16766524Sbostic unit(len, p)
16866524Sbostic register int len;
16966524Sbostic register char *p;
1708865Smckusick {
17166524Sbostic register int off, rval;
1728865Smckusick
17366524Sbostic rval = 0;
17430775Sbostic if (len > 3) {
17530775Sbostic if (len % 3) {
17630775Sbostic off = len % 3;
17730775Sbostic len -= off;
17866524Sbostic if (number(p, off)) {
17966524Sbostic rval = 1;
18066524Sbostic (void)printf(" %s%s",
18166524Sbostic name3[len / 3], lflag ? " " : ".\n");
18230775Sbostic }
18366524Sbostic p += off;
18430775Sbostic }
18566524Sbostic for (; len > 3; p += 3) {
18630775Sbostic len -= 3;
18766524Sbostic if (number(p, 3)) {
18866524Sbostic rval = 1;
18966524Sbostic (void)printf(" %s%s",
19066524Sbostic name3[len / 3], lflag ? " " : ".\n");
19130775Sbostic }
19230775Sbostic }
1938865Smckusick }
19466524Sbostic if (number(p, len)) {
19566524Sbostic if (!lflag)
19666524Sbostic (void)printf(".\n");
19766524Sbostic rval = 1;
19830775Sbostic }
19966524Sbostic return (rval);
2008865Smckusick }
2018865Smckusick
20266524Sbostic int
number(p,len)20366524Sbostic number(p, len)
20466524Sbostic register char *p;
20566524Sbostic int len;
2068865Smckusick {
20766524Sbostic register int val, rval;
2088865Smckusick
20966524Sbostic rval = 0;
21066524Sbostic switch (len) {
21130775Sbostic case 3:
21266524Sbostic if (*p != '0') {
21366524Sbostic rval = 1;
21466524Sbostic (void)printf("%s hundred", name1[*p - '0']);
21530775Sbostic }
21666524Sbostic ++p;
21766524Sbostic /* FALLTHROUGH */
21830775Sbostic case 2:
21966524Sbostic val = (p[1] - '0') + (p[0] - '0') * 10;
22030775Sbostic if (val) {
22166524Sbostic if (rval)
22266524Sbostic (void)printf(" ");
22330775Sbostic if (val < 20)
22466524Sbostic (void)printf("%s", name1[val]);
22530775Sbostic else {
22666524Sbostic (void)printf("%s", name2[val / 10]);
22730775Sbostic if (val % 10)
22866524Sbostic (void)printf("-%s", name1[val % 10]);
22930775Sbostic }
23066524Sbostic rval = 1;
23130775Sbostic }
23230775Sbostic break;
23330775Sbostic case 1:
23466524Sbostic if (*p != '0') {
23566524Sbostic rval = 1;
23666524Sbostic (void)printf("%s", name1[*p - '0']);
23730775Sbostic }
23830775Sbostic }
23966524Sbostic return (rval);
2408865Smckusick }
2418865Smckusick
24266524Sbostic void
pfract(len)24330775Sbostic pfract(len)
24466524Sbostic int len;
2458865Smckusick {
24666524Sbostic static char *pref[] = { "", "ten-", "hundred-" };
2478865Smckusick
24830775Sbostic switch(len) {
24930775Sbostic case 1:
25066524Sbostic (void)printf("tenths.\n");
25130775Sbostic break;
25230775Sbostic case 2:
25366524Sbostic (void)printf("hundredths.\n");
25430775Sbostic break;
25530775Sbostic default:
25666524Sbostic (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
25766524Sbostic break;
25830775Sbostic }
2598865Smckusick }
2608865Smckusick
26166524Sbostic void
usage()26266524Sbostic usage()
2638865Smckusick {
26466524Sbostic (void)fprintf(stderr, "usage: number [# ...]\n");
26566524Sbostic exit(1);
2668865Smckusick }
267