Home | History | Annotate | Line # | Download | only in jot
jot.c revision 1.22
      1  1.22       dsl /*	$NetBSD: jot.c,v 1.22 2008/03/02 21:33:42 dsl Exp $	*/
      2   1.2       jtc 
      3   1.1       jtc /*-
      4   1.1       jtc  * Copyright (c) 1993
      5   1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       jtc  *
      7   1.1       jtc  * Redistribution and use in source and binary forms, with or without
      8   1.1       jtc  * modification, are permitted provided that the following conditions
      9   1.1       jtc  * are met:
     10   1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     11   1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     12   1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       jtc  *    documentation and/or other materials provided with the distribution.
     15  1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       jtc  *    may be used to endorse or promote products derived from this software
     17   1.1       jtc  *    without specific prior written permission.
     18   1.1       jtc  *
     19   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       jtc  * SUCH DAMAGE.
     30   1.1       jtc  */
     31   1.1       jtc 
     32   1.4     lukem #include <sys/cdefs.h>
     33   1.1       jtc #ifndef lint
     34   1.4     lukem __COPYRIGHT("@(#) Copyright (c) 1993\n\
     35   1.4     lukem 	The Regents of the University of California.  All rights reserved.\n");
     36   1.1       jtc #endif /* not lint */
     37   1.1       jtc 
     38   1.1       jtc #ifndef lint
     39   1.2       jtc #if 0
     40   1.1       jtc static char sccsid[] = "@(#)jot.c	8.1 (Berkeley) 6/6/93";
     41   1.2       jtc #endif
     42  1.22       dsl __RCSID("$NetBSD: jot.c,v 1.22 2008/03/02 21:33:42 dsl Exp $");
     43   1.1       jtc #endif /* not lint */
     44   1.1       jtc 
     45   1.1       jtc /*
     46   1.1       jtc  * jot - print sequential or random data
     47   1.1       jtc  *
     48   1.1       jtc  * Author:  John Kunze, Office of Comp. Affairs, UCB
     49   1.1       jtc  */
     50   1.1       jtc 
     51   1.1       jtc #include <ctype.h>
     52   1.4     lukem #include <err.h>
     53   1.1       jtc #include <limits.h>
     54  1.14   garbled #include <math.h>
     55   1.1       jtc #include <stdio.h>
     56   1.1       jtc #include <stdlib.h>
     57   1.1       jtc #include <string.h>
     58   1.1       jtc #include <time.h>
     59   1.9    atatat #include <unistd.h>
     60   1.1       jtc 
     61   1.1       jtc #define	REPS_DEF	100
     62   1.1       jtc #define	BEGIN_DEF	1
     63   1.1       jtc #define	ENDER_DEF	100
     64   1.1       jtc #define	STEP_DEF	1
     65   1.1       jtc 
     66   1.7  jdolecek #define	is_default(s)	(strcmp((s), "-") == 0)
     67   1.1       jtc 
     68  1.21       dsl static double	begin = BEGIN_DEF;
     69  1.21       dsl static double	ender = ENDER_DEF;
     70  1.21       dsl static double	step = STEP_DEF;
     71  1.21       dsl static long	reps = REPS_DEF;
     72  1.21       dsl static int	randomize;
     73  1.21       dsl static int	boring;
     74  1.21       dsl static int	prec = -1;
     75  1.21       dsl static int	dox;
     76  1.21       dsl static int	chardata;
     77  1.21       dsl static int	nofinalnl;
     78  1.21       dsl static const char *sepstring = "\n";
     79  1.21       dsl static char	format[BUFSIZ];
     80   1.1       jtc 
     81  1.20       dsl static void	getargs(int, char *[]);
     82  1.20       dsl static void	getformat(void);
     83  1.20       dsl static int	getprec(char *);
     84  1.20       dsl static void	putdata(double, long);
     85  1.20       dsl static void	usage(void) __dead;
     86   1.1       jtc 
     87   1.1       jtc int
     88  1.13     perry main(int argc, char *argv[])
     89   1.1       jtc {
     90  1.22       dsl 	double	x;
     91  1.16       dsl 	long	i;
     92   1.1       jtc 
     93   1.1       jtc 	getargs(argc, argv);
     94   1.1       jtc 	if (randomize) {
     95  1.22       dsl 		x = ender - begin;
     96  1.22       dsl 		if (dox == 0)
     97  1.22       dsl 			/*
     98  1.22       dsl 			 * We are printing floating point, generate random
     99  1.22       dsl 			 * number that include both supplied limits.
    100  1.22       dsl 			 * Due to FP routing for display the low and high
    101  1.22       dsl 			 * values are likely to occur half as often as all
    102  1.22       dsl 			 * the others.
    103  1.22       dsl 			 */
    104  1.22       dsl 			x /= (1u << 31) - 1.0;
    105  1.22       dsl 		else {
    106  1.22       dsl 			/*
    107  1.22       dsl 			 * We are printing integers increase the range by
    108  1.22       dsl 			 * one but ensure we never generate it.
    109  1.22       dsl 			 * This makes all the integer values equally likely.
    110  1.22       dsl 			 */
    111  1.22       dsl 			if (ender > begin)
    112  1.22       dsl 				x += 1.0;
    113  1.22       dsl 			else
    114  1.22       dsl 				x -= 1.0;
    115  1.22       dsl 			x /= (1u << 31);
    116  1.22       dsl 		}
    117  1.18       dsl 		srandom((unsigned long) step);
    118  1.22       dsl 		for (i = 1; i <= reps || reps == 0; i++)
    119  1.22       dsl 			putdata(random() * x + begin, reps - i);
    120  1.20       dsl 	} else {
    121  1.22       dsl 		/*
    122  1.22       dsl 		 * If we are going to display as integer, add 0.5 here
    123  1.22       dsl 		 * and use floor(x) later to get sane rounding.
    124  1.22       dsl 		 */
    125  1.22       dsl 		x = begin;
    126  1.22       dsl 		if (dox)
    127  1.22       dsl 			x += 0.5;
    128  1.22       dsl 		for (i = 1; i <= reps || reps == 0; i++, x += step)
    129  1.16       dsl 			putdata(x, reps - i);
    130  1.20       dsl 	}
    131   1.1       jtc 	if (!nofinalnl)
    132   1.1       jtc 		putchar('\n');
    133   1.1       jtc 	exit(0);
    134   1.1       jtc }
    135   1.1       jtc 
    136  1.20       dsl static void
    137  1.13     perry getargs(int argc, char *argv[])
    138   1.1       jtc {
    139  1.21       dsl 	unsigned int have = 0;
    140  1.21       dsl #define BEGIN	1
    141  1.21       dsl #define	STEP	2	/* seed if -r */
    142  1.21       dsl #define REPS	4
    143  1.21       dsl #define	ENDER	8
    144  1.21       dsl 	int n = 0;
    145  1.21       dsl 	long t;
    146  1.20       dsl 	char *ep;
    147   1.1       jtc 
    148  1.20       dsl 	for (;;) {
    149  1.20       dsl 		switch (getopt(argc, argv, "b:cnp:rs:w:")) {
    150  1.20       dsl 		default:
    151  1.20       dsl 			usage();
    152  1.20       dsl 		case -1:
    153   1.1       jtc 			break;
    154   1.1       jtc 		case 'c':
    155   1.1       jtc 			chardata = 1;
    156  1.20       dsl 			continue;
    157   1.1       jtc 		case 'n':
    158   1.1       jtc 			nofinalnl = 1;
    159  1.20       dsl 			continue;
    160  1.20       dsl 		case 'p':
    161  1.20       dsl 			prec = strtol(optarg, &ep, 0);
    162  1.20       dsl 			if (*ep != 0 || prec < 0)
    163  1.20       dsl 				errx(EXIT_FAILURE, "Bad precision value");
    164  1.20       dsl 			continue;
    165  1.20       dsl 		case 'r':
    166  1.20       dsl 			randomize = 1;
    167  1.20       dsl 			continue;
    168  1.20       dsl 		case 's':
    169  1.20       dsl 			sepstring = optarg;
    170  1.20       dsl 			continue;
    171   1.1       jtc 		case 'b':
    172   1.1       jtc 			boring = 1;
    173  1.20       dsl 			/* FALLTHROUGH */
    174   1.1       jtc 		case 'w':
    175  1.20       dsl 			strlcpy(format, optarg, sizeof(format));
    176  1.20       dsl 			continue;
    177   1.1       jtc 		}
    178  1.20       dsl 		break;
    179  1.16       dsl 	}
    180  1.20       dsl 	argc -= optind;
    181  1.20       dsl 	argv += optind;
    182   1.1       jtc 
    183   1.7  jdolecek 	switch (argc) {	/* examine args right to left, falling thru cases */
    184   1.1       jtc 	case 4:
    185   1.7  jdolecek 		if (!is_default(argv[3])) {
    186  1.21       dsl 			step = strtod(argv[3], &ep);
    187  1.21       dsl 			if (*ep != 0)
    188  1.20       dsl 				errx(EXIT_FAILURE, "Bad step value:  %s",
    189  1.20       dsl 				    argv[3]);
    190  1.21       dsl 			have |= STEP;
    191   1.1       jtc 		}
    192   1.1       jtc 	case 3:
    193   1.7  jdolecek 		if (!is_default(argv[2])) {
    194   1.7  jdolecek 			if (!sscanf(argv[2], "%lf", &ender))
    195   1.7  jdolecek 				ender = argv[2][strlen(argv[2])-1];
    196  1.21       dsl 			have |= ENDER;
    197  1.18       dsl 			if (prec < 0)
    198   1.7  jdolecek 				n = getprec(argv[2]);
    199   1.1       jtc 		}
    200   1.1       jtc 	case 2:
    201   1.7  jdolecek 		if (!is_default(argv[1])) {
    202   1.7  jdolecek 			if (!sscanf(argv[1], "%lf", &begin))
    203   1.7  jdolecek 				begin = argv[1][strlen(argv[1])-1];
    204  1.21       dsl 			have |= BEGIN;
    205  1.18       dsl 			if (prec < 0)
    206   1.7  jdolecek 				prec = getprec(argv[1]);
    207   1.1       jtc 			if (n > prec)		/* maximum precision */
    208   1.1       jtc 				prec = n;
    209   1.1       jtc 		}
    210   1.1       jtc 	case 1:
    211   1.7  jdolecek 		if (!is_default(argv[0])) {
    212  1.21       dsl 			reps = strtoul(argv[0], &ep, 0);
    213  1.21       dsl 			if (*ep != 0 || reps < 0)
    214  1.20       dsl 				errx(EXIT_FAILURE, "Bad reps value:  %s",
    215  1.20       dsl 				    argv[0]);
    216  1.21       dsl 			have |= REPS;
    217   1.1       jtc 		}
    218   1.1       jtc 		break;
    219   1.1       jtc 	case 0:
    220   1.7  jdolecek 		usage();
    221   1.7  jdolecek 		break;
    222   1.1       jtc 	default:
    223  1.20       dsl 		errx(EXIT_FAILURE,
    224  1.20       dsl 		    "Too many arguments.  What do you mean by %s?", argv[4]);
    225   1.1       jtc 	}
    226   1.1       jtc 	getformat();
    227  1.21       dsl 
    228  1.21       dsl 	if (prec == -1)
    229  1.21       dsl 		prec = 0;
    230  1.21       dsl 
    231  1.21       dsl 	if (randomize) {
    232  1.21       dsl 		/* 'step' is the seed here, use pseudo-random default */
    233  1.21       dsl 		if (!(have & STEP))
    234  1.21       dsl 			step = time(NULL) * getpid();
    235  1.21       dsl 		/* Take the default values for everything else */
    236  1.21       dsl 		return;
    237  1.21       dsl 	}
    238  1.21       dsl 
    239  1.21       dsl 	/*
    240  1.21       dsl 	 * The loop we run uses begin/step/reps, so if we have been
    241  1.21       dsl 	 * given an end value (ender) we must use it to replace the
    242  1.21       dsl 	 * default values of the others.
    243  1.21       dsl 	 * We will assume a begin of 0 and step of 1 if necessary.
    244  1.21       dsl 	 */
    245  1.21       dsl 
    246  1.21       dsl 	switch (have) {
    247  1.21       dsl 
    248  1.21       dsl 	case ENDER | STEP:
    249  1.21       dsl 	case ENDER | STEP | BEGIN:
    250  1.21       dsl 		/* Calculate reps */
    251  1.21       dsl 		if (step == 0.0)
    252  1.21       dsl 			reps = 0;	/* ie infinite */
    253  1.21       dsl 		else {
    254  1.18       dsl 			reps = (ender - begin + step) / step;
    255   1.1       jtc 			if (reps <= 0)
    256  1.20       dsl 				errx(EXIT_FAILURE, "Impossible stepsize");
    257  1.21       dsl 		}
    258  1.21       dsl 		break;
    259  1.21       dsl 
    260  1.21       dsl 	case REPS | ENDER:
    261  1.21       dsl 	case REPS | ENDER | STEP:
    262  1.21       dsl 		/* Calculate begin */
    263  1.21       dsl 		if (reps == 0)
    264  1.21       dsl 			errx(EXIT_FAILURE,
    265  1.21       dsl 			    "Must specify begin if reps == 0");
    266  1.21       dsl 		begin = ender - reps * step + step;
    267  1.21       dsl 		break;
    268  1.21       dsl 
    269  1.21       dsl 	case REPS | BEGIN | ENDER:
    270  1.21       dsl 		/* Calculate step */
    271  1.21       dsl 		if (reps == 0)
    272  1.21       dsl 			errx(EXIT_FAILURE,
    273  1.21       dsl 			    "Infinite sequences cannot be bounded");
    274  1.21       dsl 		if (reps == 1)
    275  1.21       dsl 			step = 0.0;
    276  1.21       dsl 		else
    277  1.21       dsl 			step = (ender - begin) / (reps - 1);
    278  1.21       dsl 		break;
    279  1.21       dsl 
    280  1.21       dsl 	case REPS | BEGIN | ENDER | STEP:
    281  1.21       dsl 		/* reps given and implied - take smaller */
    282  1.21       dsl 		if (step == 0.0)
    283   1.1       jtc 			break;
    284  1.21       dsl 		t = (ender - begin + step) / step;
    285  1.21       dsl 		if (t <= 0)
    286  1.21       dsl 			errx(EXIT_FAILURE,
    287  1.21       dsl 			    "Impossible stepsize");
    288  1.21       dsl 		if (t < reps)
    289  1.21       dsl 			reps = t;
    290  1.21       dsl 		break;
    291  1.21       dsl 
    292  1.21       dsl 	default:
    293  1.21       dsl 		/* No values can be calculated, use defaults */
    294  1.21       dsl 		break;
    295  1.16       dsl 	}
    296   1.1       jtc }
    297   1.1       jtc 
    298  1.20       dsl static void
    299  1.13     perry putdata(double x, long notlast)
    300   1.1       jtc {
    301   1.1       jtc 
    302   1.1       jtc 	if (boring)				/* repeated word */
    303   1.3        pk 		printf("%s", format);
    304   1.1       jtc 	else if (dox)				/* scalar */
    305  1.22       dsl 		printf(format, (long)floor(x));
    306   1.1       jtc 	else					/* real */
    307   1.1       jtc 		printf(format, x);
    308   1.1       jtc 	if (notlast != 0)
    309   1.1       jtc 		fputs(sepstring, stdout);
    310   1.1       jtc }
    311   1.1       jtc 
    312  1.20       dsl __dead static void
    313   1.7  jdolecek usage(void)
    314   1.1       jtc {
    315  1.12     peter 	(void)fprintf(stderr, "usage: %s [-cnr] [-b word] [-p precision] "
    316  1.20       dsl 	    "[-s string] [-w word] [reps [begin [end [step | seed]]]]\n",
    317  1.20       dsl 	    getprogname());
    318   1.1       jtc 	exit(1);
    319   1.1       jtc }
    320   1.1       jtc 
    321  1.20       dsl static int
    322  1.20       dsl getprec(char *num_str)
    323   1.1       jtc {
    324   1.1       jtc 
    325  1.20       dsl 	num_str = strchr(num_str, '.');
    326  1.20       dsl 	if (num_str == NULL)
    327  1.20       dsl 		return 0;
    328  1.20       dsl 	return strspn(num_str + 1, "0123456789");
    329   1.1       jtc }
    330   1.1       jtc 
    331  1.20       dsl static void
    332  1.13     perry getformat(void)
    333   1.1       jtc {
    334   1.4     lukem 	char	*p;
    335   1.7  jdolecek 	size_t	sz;
    336   1.1       jtc 
    337   1.1       jtc 	if (boring)				/* no need to bother */
    338   1.1       jtc 		return;
    339  1.20       dsl 	for (p = format; *p; p++) {		/* look for '%' */
    340   1.7  jdolecek 		if (*p == '%') {
    341   1.7  jdolecek 			if (*(p+1) != '%')
    342   1.7  jdolecek 				break;
    343   1.7  jdolecek 			p++;		/* leave %% alone */
    344   1.7  jdolecek 		}
    345  1.20       dsl 	}
    346   1.7  jdolecek 	sz = sizeof(format) - strlen(format) - 1;
    347   1.7  jdolecek 	if (!*p) {
    348  1.17       dsl 		if (chardata || prec == 0) {
    349  1.17       dsl 			if (snprintf(p, sz, "%%%s", chardata ? "c" : "ld") >= sz)
    350  1.20       dsl 				errx(EXIT_FAILURE, "-w word too long");
    351   1.8    simonb 			dox = 1;
    352   1.7  jdolecek 		} else {
    353   1.8    simonb 			if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
    354  1.20       dsl 				errx(EXIT_FAILURE, "-w word too long");
    355   1.7  jdolecek 		}
    356   1.7  jdolecek 	} else if (!*(p+1)) {
    357   1.7  jdolecek 		if (sz <= 0)
    358  1.20       dsl 			errx(EXIT_FAILURE, "-w word too long");
    359   1.1       jtc 		strcat(format, "%");		/* cannot end in single '%' */
    360   1.7  jdolecek 	} else {
    361   1.7  jdolecek 		p++;				/* skip leading % */
    362   1.7  jdolecek 		for(; *p && !isalpha((unsigned char)*p); p++) {
    363   1.7  jdolecek 			/* allow all valid printf(3) flags, but deny '*' */
    364   1.7  jdolecek 			if (!strchr("0123456789#-+. ", *p))
    365   1.7  jdolecek 				break;
    366   1.7  jdolecek 		}
    367   1.7  jdolecek 		/* Allow 'l' prefix, but no other. */
    368   1.7  jdolecek 		if (*p == 'l')
    369   1.7  jdolecek 			p++;
    370   1.1       jtc 		switch (*p) {
    371   1.1       jtc 		case 'f': case 'e': case 'g': case '%':
    372   1.7  jdolecek 		case 'E': case 'G':
    373   1.1       jtc 			break;
    374   1.1       jtc 		case 's':
    375  1.20       dsl 			errx(EXIT_FAILURE,
    376  1.20       dsl 			    "cannot convert numeric data to strings");
    377   1.1       jtc 			break;
    378   1.7  jdolecek 		case 'd': case 'o': case 'x': case 'u':
    379   1.7  jdolecek 		case 'D': case 'O': case 'X': case 'U':
    380   1.7  jdolecek 		case 'c': case 'i':
    381   1.1       jtc 			dox = 1;
    382   1.1       jtc 			break;
    383   1.7  jdolecek 		default:
    384  1.20       dsl 			errx(EXIT_FAILURE, "unknown or invalid format `%s'",
    385  1.20       dsl 			    format);
    386   1.1       jtc 		}
    387   1.7  jdolecek 		/* Need to check for trailing stuff to print */
    388   1.7  jdolecek 		for (; *p; p++)		/* look for '%' */
    389   1.7  jdolecek 			if (*p == '%') {
    390   1.7  jdolecek 				if (*(p+1) != '%')
    391   1.7  jdolecek 					break;
    392   1.7  jdolecek 				p++;		/* leave %% alone */
    393   1.7  jdolecek 			}
    394   1.7  jdolecek 		if (*p)
    395  1.20       dsl 			errx(EXIT_FAILURE, "unknown or invalid format `%s'",
    396  1.20       dsl 			    format);
    397   1.1       jtc 	}
    398   1.1       jtc }
    399