Add __attribute__((__noreturn__)) to various function prototypes in games/.
[dragonfly.git] / games / number / number.c
CommitLineData
6693db17 1/*-
984263bc
MD
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.
6693db17 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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.
1de703da
MD
28 *
29 * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)number.c 8.3 (Berkeley) 5/4/95
31 * $FreeBSD: src/games/number/number.c,v 1.12 1999/12/12 03:22:35 billf Exp $
984263bc
MD
32 */
33
984263bc
MD
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43#define MAXNUM 65 /* Biggest number we handle. */
44
45static const char *name1[] = {
46 "", "one", "two", "three",
47 "four", "five", "six", "seven",
48 "eight", "nine", "ten", "eleven",
49 "twelve", "thirteen", "fourteen", "fifteen",
50 "sixteen", "seventeen", "eighteen", "nineteen",
51},
52 *name2[] = {
53 "", "ten", "twenty", "thirty",
54 "forty", "fifty", "sixty", "seventy",
55 "eighty", "ninety",
56},
57 *name3[] = {
58 "hundred", "thousand", "million", "billion",
59 "trillion", "quadrillion", "quintillion", "sextillion",
60 "septillion", "octillion", "nonillion", "decillion",
61 "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
62 "quindecillion", "sexdecillion",
63 "septendecillion", "octodecillion",
64 "novemdecillion", "vigintillion",
65};
66
a3a53138
SW
67static void convert(char *);
68static int number(char *, int);
69static void pfract(int);
70static int unit(int, char *);
09ac707a 71static void usage(void) __dead2;
984263bc 72
a3a53138 73static int lflag;
984263bc
MD
74
75int
e1acdbad 76main(int argc, char **argv)
984263bc
MD
77{
78 int ch, first;
79 char line[256];
80
81 lflag = 0;
82 while ((ch = getopt(argc, argv, "l")) != -1)
83 switch (ch) {
84 case 'l':
85 lflag = 1;
86 break;
87 case '?':
88 default:
89 usage();
90 }
91 argc -= optind;
92 argv += optind;
93
94 if (*argv == NULL)
95 for (first = 1;
96 fgets(line, sizeof(line), stdin) != NULL; first = 0) {
97 if (strchr(line, '\n') == NULL)
98 errx(1, "line too long.");
99 if (!first)
d9f85b33 100 printf("...\n");
984263bc
MD
101 convert(line);
102 }
103 else
104 for (first = 1; *argv != NULL; first = 0, ++argv) {
105 if (!first)
d9f85b33 106 printf("...\n");
984263bc
MD
107 convert(*argv);
108 }
109 exit(0);
110}
111
a3a53138 112static void
e1acdbad 113convert(char *line)
984263bc
MD
114{
115 int flen, len, rval;
116 char *p, *fraction;
117
3641b7ca 118 flen = 0;
984263bc
MD
119 fraction = NULL;
120 for (p = line; *p != '\0' && *p != '\n'; ++p) {
121 if (isblank(*p)) {
122 if (p == line) {
123 ++line;
124 continue;
125 }
126 goto badnum;
127 }
128 if (isdigit(*p))
129 continue;
130 switch (*p) {
131 case '.':
132 if (fraction != NULL)
133 goto badnum;
134 fraction = p + 1;
135 *p = '\0';
136 break;
137 case '-':
138 if (p == line)
139 break;
140 /* FALLTHROUGH */
141 default:
142badnum: errx(1, "illegal number: %s", line);
143 break;
144 }
145 }
146 *p = '\0';
147
148 if ((len = strlen(line)) > MAXNUM ||
149 (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM)))
150 errx(1, "number too large, max %d digits.", MAXNUM);
151
152 if (*line == '-') {
d9f85b33 153 printf("minus%s", lflag ? " " : "\n");
984263bc
MD
154 ++line;
155 --len;
156 }
157
158 rval = len > 0 ? unit(len, line) : 0;
159 if (fraction != NULL && flen != 0)
160 for (p = fraction; *p != '\0'; ++p)
161 if (*p != '0') {
162 if (rval)
d9f85b33 163 printf("%sand%s",
984263bc
MD
164 lflag ? " " : "",
165 lflag ? " " : "\n");
166 if (unit(flen, fraction)) {
167 if (lflag)
d9f85b33 168 printf(" ");
984263bc
MD
169 pfract(flen);
170 rval = 1;
171 }
172 break;
173 }
174 if (!rval)
d9f85b33 175 printf("zero%s", lflag ? "" : ".\n");
984263bc 176 if (lflag)
d9f85b33 177 printf("\n");
984263bc
MD
178}
179
a3a53138 180static int
e1acdbad 181unit(int len, char *p)
984263bc
MD
182{
183 int off, rval;
184
185 rval = 0;
186 if (len > 3) {
187 if (len % 3) {
188 off = len % 3;
189 len -= off;
190 if (number(p, off)) {
191 rval = 1;
d9f85b33 192 printf(" %s%s",
984263bc
MD
193 name3[len / 3], lflag ? " " : ".\n");
194 }
195 p += off;
196 }
197 for (; len > 3; p += 3) {
198 len -= 3;
199 if (number(p, 3)) {
200 rval = 1;
d9f85b33 201 printf(" %s%s",
984263bc
MD
202 name3[len / 3], lflag ? " " : ".\n");
203 }
204 }
205 }
206 if (number(p, len)) {
207 if (!lflag)
d9f85b33 208 printf(".\n");
984263bc
MD
209 rval = 1;
210 }
211 return (rval);
212}
213
a3a53138 214static int
e1acdbad 215number(char *p, int len)
984263bc
MD
216{
217 int val, rval;
218
219 rval = 0;
220 switch (len) {
221 case 3:
222 if (*p != '0') {
223 rval = 1;
d9f85b33 224 printf("%s hundred", name1[*p - '0']);
984263bc
MD
225 }
226 ++p;
227 /* FALLTHROUGH */
228 case 2:
229 val = (p[1] - '0') + (p[0] - '0') * 10;
230 if (val) {
231 if (rval)
d9f85b33 232 printf(" ");
984263bc 233 if (val < 20)
d9f85b33 234 printf("%s", name1[val]);
984263bc 235 else {
d9f85b33 236 printf("%s", name2[val / 10]);
984263bc 237 if (val % 10)
d9f85b33 238 printf("-%s", name1[val % 10]);
984263bc
MD
239 }
240 rval = 1;
241 }
242 break;
243 case 1:
244 if (*p != '0') {
245 rval = 1;
d9f85b33 246 printf("%s", name1[*p - '0']);
984263bc
MD
247 }
248 }
249 return (rval);
250}
251
a3a53138 252static void
e1acdbad 253pfract(int len)
984263bc 254{
e1acdbad 255 static const char *pref[] = { "", "ten-", "hundred-" };
984263bc
MD
256
257 switch(len) {
258 case 1:
d9f85b33 259 printf("tenths.\n");
984263bc
MD
260 break;
261 case 2:
d9f85b33 262 printf("hundredths.\n");
984263bc
MD
263 break;
264 default:
d9f85b33 265 printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
984263bc
MD
266 break;
267 }
268}
269
a3a53138 270static void
e1acdbad 271usage(void)
984263bc 272{
d9f85b33 273 fprintf(stderr, "usage: number [# ...]\n");
984263bc
MD
274 exit(1);
275}