From 5f9253d4384c3ed9f78f48ca108b10b84c72cb41 Mon Sep 17 00:00:00 2001 From: Thomas Cort Date: Sun, 16 Mar 2014 10:38:37 -0400 Subject: [PATCH] Importing games/number No Minix specific changes needed. Change-Id: Iaf87f29a954f241d41c101dd74fe338b9d7c4d95 --- distrib/sets/lists/minix/mi | 2 + games/Makefile | 2 +- games/number/Makefile | 7 + games/number/number.6 | 59 ++++++++ games/number/number.c | 288 ++++++++++++++++++++++++++++++++++++ releasetools/nbsd_ports | 1 + 6 files changed, 358 insertions(+), 1 deletion(-) create mode 100644 games/number/Makefile create mode 100644 games/number/number.6 create mode 100644 games/number/number.c diff --git a/distrib/sets/lists/minix/mi b/distrib/sets/lists/minix/mi index 82bd96f12..559a0e0c6 100644 --- a/distrib/sets/lists/minix/mi +++ b/distrib/sets/lists/minix/mi @@ -561,6 +561,7 @@ ./usr/games/fortune minix-sys ./usr/games/hide minix-sys ./usr/games/morse minix-sys +./usr/games/number minix-sys ./usr/games/pig minix-sys ./usr/games/ppt minix-sys ./usr/games/primes minix-sys @@ -4758,6 +4759,7 @@ ./usr/man/man6/factor.6 minix-sys ./usr/man/man6/fortune.6 minix-sys ./usr/man/man6/morse.6 minix-sys +./usr/man/man6/number.6 minix-sys ./usr/man/man6/pig.6 minix-sys ./usr/man/man6/ppt.6 minix-sys ./usr/man/man6/primes.6 minix-sys diff --git a/games/Makefile b/games/Makefile index d732af8ff..75d15e425 100644 --- a/games/Makefile +++ b/games/Makefile @@ -11,7 +11,7 @@ SUBDIR= adventure \ bcd \ \ factor fortune \ - morse \ + morse number \ pig ppt primes .if !defined(__MINIX) diff --git a/games/number/Makefile b/games/number/Makefile new file mode 100644 index 000000000..7f20784dc --- /dev/null +++ b/games/number/Makefile @@ -0,0 +1,7 @@ +# $NetBSD: Makefile,v 1.4 1995/03/23 08:35:27 cgd Exp $ +# @(#)Makefile 8.1 (Berkeley) 5/31/93 + +PROG= number +MAN= number.6 + +.include diff --git a/games/number/number.6 b/games/number/number.6 new file mode 100644 index 000000000..722663793 --- /dev/null +++ b/games/number/number.6 @@ -0,0 +1,59 @@ +.\" $NetBSD: number.6,v 1.7 2003/08/07 09:37:30 agc Exp $ +.\" +.\" Copyright (c) 1989, 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 +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 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. 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. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)number.6 8.2 (Berkeley) 3/31/94 +.\" +.Dd March 31, 1994 +.Dt NUMBER 6 +.Os +.Sh NAME +.Nm number +.Nd convert Arabic numerals to English +.Sh SYNOPSIS +.Nm +.Op Fl l +.Op Ar number ... +.Sh DESCRIPTION +The +.Nm +utility prints the English equivalent of the number to the standard +output, with each 10^3 magnitude displayed on a separate line. +If no argument is specified, +.Nm +reads lines from the standard input. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl l +Display the number on a single line. +.El +.Sh BUGS +Although +.Nm +understand fractions, it doesn't understand exponents. diff --git a/games/number/number.c b/games/number/number.c new file mode 100644 index 000000000..5cf417054 --- /dev/null +++ b/games/number/number.c @@ -0,0 +1,288 @@ +/* $NetBSD: number.c,v 1.15 2012/06/19 05:46:09 dholland Exp $ */ + +/* + * 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 + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 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. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#ifndef lint +__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\ + The Regents of the University of California. All rights reserved."); +#endif /* not lint */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95"; +#else +__RCSID("$NetBSD: number.c,v 1.15 2012/06/19 05:46:09 dholland Exp $"); +#endif +#endif /* not lint */ + +#include + +#include +#include +#include +#include +#include +#include + +#define MAXNUM 65 /* Biggest number we handle. */ + +static const char *const name1[] = { + "", "one", "two", "three", + "four", "five", "six", "seven", + "eight", "nine", "ten", "eleven", + "twelve", "thirteen", "fourteen", "fifteen", + "sixteen", "seventeen", "eighteen", "nineteen", +}, + *const name2[] = { + "", "ten", "twenty", "thirty", + "forty", "fifty", "sixty", "seventy", + "eighty", "ninety", +}, + *const name3[] = { + "hundred", "thousand", "million", "billion", + "trillion", "quadrillion", "quintillion", "sextillion", + "septillion", "octillion", "nonillion", "decillion", + "undecillion", "duodecillion", "tredecillion", "quattuordecillion", + "quindecillion", "sexdecillion", + "septendecillion", "octodecillion", + "novemdecillion", "vigintillion", +}; + +int main(int, char *[]); +static void convert(char *); +static int number(const char *, int); +static void pfract(int); +static int unit(int, const char *); +static void usage(void) __dead; + +static int lflag; + +int +main(int argc, char *argv[]) +{ + int ch, first; + char line[256]; + + lflag = 0; + while ((ch = getopt(argc, argv, "l")) != -1) + switch (ch) { + case 'l': + lflag = 1; + break; + case '?': + default: + usage(); + } + 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); + } + else + for (first = 1; *argv != NULL; first = 0, ++argv) { + if (!first) + (void)printf("...\n"); + convert(*argv); + } + exit(0); +} + +void +convert(char *line) +{ + int flen, len, 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; + } + 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 == '-') { + (void)printf("minus%s", lflag ? " " : "\n"); + ++line; + --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 (!rval) + (void)printf("zero%s", lflag ? "" : ".\n"); + if (lflag) + (void)printf("\n"); +} + +int +unit(int len, const char *p) +{ + int off, rval; + + rval = 0; + if (len > 3) { + if (len % 3) { + off = len % 3; + len -= off; + if (number(p, off)) { + rval = 1; + (void)printf(" %s%s", + name3[len / 3], lflag ? " " : ".\n"); + } + p += off; + } + for (; len > 3; p += 3) { + len -= 3; + if (number(p, 3)) { + rval = 1; + (void)printf(" %s%s", + name3[len / 3], lflag ? " " : ".\n"); + } + } + } + if (number(p, len)) { + if (!lflag) + (void)printf(".\n"); + rval = 1; + } + return (rval); +} + +int +number(const char *p, int len) +{ + int val, rval; + + rval = 0; + switch (len) { + case 3: + if (*p != '0') { + rval = 1; + (void)printf("%s hundred", name1[*p - '0']); + } + ++p; + /* FALLTHROUGH */ + case 2: + val = (p[1] - '0') + (p[0] - '0') * 10; + if (val) { + if (rval) + (void)printf(" "); + if (val < 20) + (void)printf("%s", name1[val]); + else { + (void)printf("%s", name2[val / 10]); + if (val % 10) + (void)printf("-%s", name1[val % 10]); + } + rval = 1; + } + break; + case 1: + if (*p != '0') { + rval = 1; + (void)printf("%s", name1[*p - '0']); + } + } + return (rval); +} + +void +pfract(int len) +{ + static const char *const pref[] = { "", "ten-", "hundred-" }; + + switch(len) { + case 1: + (void)printf("tenths.\n"); + break; + case 2: + (void)printf("hundredths.\n"); + break; + default: + (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]); + break; + } +} + +void +usage(void) +{ + (void)fprintf(stderr, "usage: number [# ...]\n"); + exit(1); +} diff --git a/releasetools/nbsd_ports b/releasetools/nbsd_ports index d97015ed0..8691ba3e3 100644 --- a/releasetools/nbsd_ports +++ b/releasetools/nbsd_ports @@ -75,6 +75,7 @@ 2013/12/1 12:00:00,games/Makefile 2013/12/1 12:00:00,games/Makefile.inc 2013/12/1 12:00:00,games/morse +2013/12/1 12:00:00,games/number 2013/12/1 12:00:00,games/pig 2013/12/1 12:00:00,games/ppt 2013/12/1 12:00:00,games/primes -- 2.44.0