1/*
2 * Copyright (c) 1988, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1988, 1993, 1994\n\
33	The Regents of the University of California.  All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)number.c	8.3 (Berkeley) 5/4/95";
39#endif
40static const char rcsid[] =
41 "$FreeBSD$";
42#endif /* not lint */
43
44#include <sys/types.h>
45
46#include <ctype.h>
47#include <err.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52
53#define	MAXNUM		65		/* Biggest number we handle. */
54
55static const char	*name1[] = {
56	"",		"one",		"two",		"three",
57	"four",		"five",		"six",		"seven",
58	"eight",	"nine",		"ten",		"eleven",
59	"twelve",	"thirteen",	"fourteen",	"fifteen",
60	"sixteen",	"seventeen",	"eighteen",	"nineteen",
61},
62		*name2[] = {
63	"",		"ten",		"twenty",	"thirty",
64	"forty",	"fifty",	"sixty",	"seventy",
65	"eighty",	"ninety",
66},
67		*name3[] = {
68	"hundred",	"thousand",	"million",	"billion",
69	"trillion",	"quadrillion",	"quintillion",	"sextillion",
70	"septillion",	"octillion",	"nonillion",	"decillion",
71	"undecillion",	"duodecillion",	"tredecillion",	"quattuordecillion",
72	"quindecillion",		"sexdecillion",
73	"septendecillion",		"octodecillion",
74	"novemdecillion",		"vigintillion",
75};
76
77static void	convert(char *);
78static int	number(char *, int);
79static void	pfract(int);
80static int	unit(int, char *);
81static void	usage(void);
82
83static int lflag;
84
85int
86main(int argc, char *argv[])
87{
88	int ch, first;
89	char line[256];
90
91	lflag = 0;
92	while ((ch = getopt(argc, argv, "l")) != -1)
93		switch (ch) {
94		case 'l':
95			lflag = 1;
96			break;
97		case '?':
98		default:
99			usage();
100		}
101	argc -= optind;
102	argv += optind;
103
104	if (*argv == NULL)
105		for (first = 1;
106		    fgets(line, sizeof(line), stdin) != NULL; first = 0) {
107			if (strchr(line, '\n') == NULL)
108				errx(1, "line too long.");
109			if (!first)
110				(void)printf("...\n");
111			convert(line);
112		}
113	else
114		for (first = 1; *argv != NULL; first = 0, ++argv) {
115			if (!first)
116				(void)printf("...\n");
117			convert(*argv);
118		}
119	exit(0);
120}
121
122static void
123convert(char *line)
124{
125	int flen, len, rval;
126	char *p, *fraction;
127
128	flen = 0;
129	fraction = NULL;
130	for (p = line; *p != '\0' && *p != '\n'; ++p) {
131		if (isblank(*p)) {
132			if (p == line) {
133				++line;
134				continue;
135			}
136			goto badnum;
137		}
138		if (isdigit(*p))
139			continue;
140		switch (*p) {
141		case '.':
142			if (fraction != NULL)
143				goto badnum;
144			fraction = p + 1;
145			*p = '\0';
146			break;
147		case '-':
148			if (p == line)
149				break;
150			/* FALLTHROUGH */
151		default:
152badnum:			errx(1, "illegal number: %s", line);
153			break;
154		}
155	}
156	*p = '\0';
157
158	if ((len = strlen(line)) > MAXNUM ||
159	    (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM)))
160		errx(1, "number too large, max %d digits.", MAXNUM);
161
162	if (*line == '-') {
163		(void)printf("minus%s", lflag ? " " : "\n");
164		++line;
165		--len;
166	}
167
168	rval = len > 0 ? unit(len, line) : 0;
169	if (fraction != NULL && flen != 0)
170		for (p = fraction; *p != '\0'; ++p)
171			if (*p != '0') {
172				if (rval)
173					(void)printf("%sand%s",
174					    lflag ? " " : "",
175					    lflag ? " " : "\n");
176				if (unit(flen, fraction)) {
177					if (lflag)
178						(void)printf(" ");
179					pfract(flen);
180					rval = 1;
181				}
182				break;
183			}
184	if (!rval)
185		(void)printf("zero%s", lflag ? "" : ".\n");
186	if (lflag)
187		(void)printf("\n");
188}
189
190static int
191unit(int len, char *p)
192{
193	int off, rval;
194
195	rval = 0;
196	if (len > 3) {
197		if (len % 3) {
198			off = len % 3;
199			len -= off;
200			if (number(p, off)) {
201				rval = 1;
202				(void)printf(" %s%s",
203				    name3[len / 3], lflag ? " " : ".\n");
204			}
205			p += off;
206		}
207		for (; len > 3; p += 3) {
208			len -= 3;
209			if (number(p, 3)) {
210				rval = 1;
211				(void)printf(" %s%s",
212				    name3[len / 3], lflag ? " " : ".\n");
213			}
214		}
215	}
216	if (number(p, len)) {
217		if (!lflag)
218			(void)printf(".\n");
219		rval = 1;
220	}
221	return (rval);
222}
223
224static int
225number(char *p, int len)
226{
227	int val, rval;
228
229	rval = 0;
230	switch (len) {
231	case 3:
232		if (*p != '0') {
233			rval = 1;
234			(void)printf("%s hundred", name1[*p - '0']);
235		}
236		++p;
237		/* FALLTHROUGH */
238	case 2:
239		val = (p[1] - '0') + (p[0] - '0') * 10;
240		if (val) {
241			if (rval)
242				(void)printf(" ");
243			if (val < 20)
244				(void)printf("%s", name1[val]);
245			else {
246				(void)printf("%s", name2[val / 10]);
247				if (val % 10)
248					(void)printf("-%s", name1[val % 10]);
249			}
250			rval = 1;
251		}
252		break;
253	case 1:
254		if (*p != '0') {
255			rval = 1;
256			(void)printf("%s", name1[*p - '0']);
257		}
258	}
259	return (rval);
260}
261
262static void
263pfract(int len)
264{
265	static char const * const pref[] = { "", "ten-", "hundred-" };
266
267	switch(len) {
268	case 1:
269		(void)printf("tenths.\n");
270		break;
271	case 2:
272		(void)printf("hundredths.\n");
273		break;
274	default:
275		(void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
276		break;
277	}
278}
279
280static void
281usage(void)
282{
283	(void)fprintf(stderr, "usage: number [-l] [# ...]\n");
284	exit(1);
285}
286