diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-12-29 08:51:20 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-12-29 08:51:20 +0000 |
commit | 2c21275fdd232d956693ac9c03b362ca0ba9d50a (patch) | |
tree | f93efedb97cc7ec9e6b1af95d4f1199f74bb8f7d /usr.bin/jot/jot.c | |
parent | ce61cde32137d0cf9eaf1932fef9a3c70d8d36e4 (diff) |
Cleanup: KNF, -Wall, do not use pointers if the actual var is
available, plus some other minor cleanup.
New features: Allow explicit precision 0. For infinite sequences,
do not print trailing newline if the counter wraps.
Testing by henning@ and jmc@
ok deraadt@
Diffstat (limited to 'usr.bin/jot/jot.c')
-rw-r--r-- | usr.bin/jot/jot.c | 131 |
1 files changed, 65 insertions, 66 deletions
diff --git a/usr.bin/jot/jot.c b/usr.bin/jot/jot.c index e5961734e21..014bfc15775 100644 --- a/usr.bin/jot/jot.c +++ b/usr.bin/jot/jot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: jot.c,v 1.14 2003/12/15 23:24:20 jmc Exp $ */ +/* $OpenBSD: jot.c,v 1.15 2003/12/29 08:51:19 otto Exp $ */ /* $NetBSD: jot.c,v 1.3 1994/12/02 20:29:43 pk Exp $ */ /*- @@ -31,7 +31,7 @@ */ #ifndef lint -static char copyright[] = +static const char copyright[] = "@(#) Copyright (c) 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ @@ -40,7 +40,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93"; #endif -static char rcsid[] = "$OpenBSD: jot.c,v 1.14 2003/12/15 23:24:20 jmc Exp $"; +static const char rcsid[] = "$OpenBSD: jot.c,v 1.15 2003/12/29 08:51:19 otto Exp $"; #endif /* not lint */ /* @@ -50,6 +50,7 @@ static char rcsid[] = "$OpenBSD: jot.c,v 1.14 2003/12/15 23:24:20 jmc Exp $"; */ #include <err.h> +#include <stdbool.h> #include <ctype.h> #include <limits.h> #include <stdio.h> @@ -64,51 +65,48 @@ static char rcsid[] = "$OpenBSD: jot.c,v 1.14 2003/12/15 23:24:20 jmc Exp $"; #define is_default(s) (strcmp((s), "-") == 0) -double begin; -double ender; -double s; -long reps; -int randomize; -int infinity; -int boring; -int prec; -int dox; -int chardata; -int nofinalnl; -char sepstring[BUFSIZ] = "\n"; -char format[BUFSIZ]; +static double begin; +static double ender; +static double s; +static long reps; +static bool randomize; +static bool infinity; +static bool boring; +static int prec = -1; +static bool dox; +static bool chardata; +static bool finalnl = true; +static char sepstring[BUFSIZ] = "\n"; +static char format[BUFSIZ]; -void getargs(int, char *[]); -void getformat(void); -int getprec(char *); -void putdata(double, long); +static void getformat(void); +static int getprec(char *); +static void putdata(double, bool); static void usage(void); int main(int argc, char *argv[]) { - double xd, yd; - long id; - double *x = &xd; - double *y = &yd; - long *i = &id; + double x; + double y; + long i; unsigned int mask = 0; - int n = 0; - int ch; + int n = 0; + int ch; while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1) - switch((char)ch) { + switch (ch) { case 'r': - randomize = 1; + randomize = true; break; case 'c': - chardata = 1; + chardata = true; break; case 'n': - nofinalnl = 1; + finalnl = false; break; case 'b': - boring = 1; + boring = true; if (strlcpy(format, optarg, sizeof(format)) >= sizeof(format)) errx(1, "-b word too long"); @@ -125,7 +123,7 @@ main(int argc, char *argv[]) break; case 'p': prec = atoi(optarg); - if (prec <= 0) + if (prec < 0) errx(1, "bad precision value"); break; default: @@ -146,7 +144,7 @@ main(int argc, char *argv[]) if (!sscanf(argv[2], "%lf", &ender)) ender = argv[2][strlen(argv[2])-1]; mask |= 02; - if (!prec) + if (prec == -1) n = getprec(argv[2]); } case 2: @@ -154,7 +152,7 @@ main(int argc, char *argv[]) if (!sscanf(argv[1], "%lf", &begin)) begin = argv[1][strlen(argv[1])-1]; mask |= 04; - if (!prec) + if (prec == -1) prec = getprec(argv[1]); if (n > prec) /* maximum precision */ prec = n; @@ -164,13 +162,16 @@ main(int argc, char *argv[]) if (!sscanf(argv[0], "%ld", &reps)) errx(1, "Bad reps value: %s", argv[0]); mask |= 010; + if (prec == -1) + prec = 0; } break; case 0: usage(); break; default: - errx(1, "Too many arguments. What do you mean by %s?", argv[4]); + errx(1, "Too many arguments. What do you mean by %s?", + argv[4]); } getformat(); while (mask) /* 4 bit mask has 1's where last 4 args were given */ @@ -271,35 +272,32 @@ main(int argc, char *argv[]) errx(1, "bad mask"); } if (reps == 0) - infinity = 1; + infinity = true; if (randomize) { - *x = (ender - begin) * (ender > begin ? 1 : -1); - for (*i = 1; *i <= reps || infinity; (*i)++) { - *y = (double) arc4random() / UINT_MAX; - putdata(*y * *x + begin, reps - *i); + x = (ender - begin) * (ender > begin ? 1 : -1); + for (i = 1; i <= reps || infinity; i++) { + y = (double) arc4random() / UINT_MAX; + putdata(y * x + begin, reps - i == 0 && !infinity); } } else - for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s) - putdata(*x, reps - *i); - if (!nofinalnl) + for (i = 1, x = begin; i <= reps || infinity; i++, x += s) + putdata(x, reps - i == 0 && !infinity); + if (finalnl) putchar('\n'); exit(0); } -void -putdata(double x, long notlast) +static void +putdata(double x, bool last) { - long d = x; - long *dp = &d; - if (boring) /* repeated word */ printf("%s", format); else if (dox) /* scalar */ - printf(format, *dp); + printf(format, (long)x); else /* real */ printf(format, x); - if (notlast != 0) + if (!last) fputs(sepstring, stdout); } @@ -312,24 +310,24 @@ usage(void) exit(1); } -int +static int getprec(char *s) { char *p; char *q; - for (p = s; *p; p++) + for (p = s; *p != '\0'; p++) if (*p == '.') break; - if (!*p) + if (*p == '\0') return (0); - for (q = ++p; *p; p++) + for (q = ++p; *p != '\0'; p++) if (!isdigit(*p)) break; return (p - q); } -void +static void getformat(void) { char *p; @@ -337,26 +335,27 @@ getformat(void) if (boring) /* no need to bother */ return; - for (p = format; *p; p++) /* look for '%' */ + for (p = format; *p != '\0'; p++) /* look for '%' */ if (*p == '%') { if (*(p+1) != '%') break; p++; /* leave %% alone */ } sz = sizeof(format) - strlen(format) - 1; - if (!*p && !chardata) { + if (*p == '\0' && !chardata) { if (snprintf(p, sz, "%%.%df", prec) >= (int)sz) errx(1, "-w word too long"); - } else if (!*p && chardata) { + } else if (*p == '\0' && chardata) { if (strlcpy(p, "%c", sz) >= sz) errx(1, "-w word too long"); - dox = 1; - } else if (!*(p+1)) { + dox = true; + } else if (*(p+1) == '\0') { if (sz <= 0) errx(1, "-w word too long"); - strlcat(format, "%", sizeof format); /* cannot end in single '%' */ + /* cannot end in single '%' */ + strlcat(format, "%", sizeof format); } else { - for (; *p && !isalpha(*p); p++) + for (; *p != '\0' && !isalpha(*p); p++) /* Certain nonalphanumerics we can't allow */ if (*p == '$' || *p == '*') break; @@ -373,19 +372,19 @@ getformat(void) case 'd': case 'o': case 'x': case 'u': case 'D': case 'O': case 'X': case 'U': case 'c': case 'i': - dox = 1; + dox = true; break; default: errx(1, "unknown or invalid format `%s'", format); } /* Need to check for trailing stuff to print */ - for (; *p; p++) /* look for '%' */ + for (; *p != '\0'; p++) /* look for '%' */ if (*p == '%') { if (*(p+1) != '%') break; p++; /* leave %% alone */ } - if (*p) + if (*p != '\0') errx(1, "unknown or invalid format `%s'", format); } } |