Home | History | Annotate | Line # | Download | only in jot
jot.c revision 1.22.2.1
      1  1.22.2.1      yamt /*	$NetBSD: jot.c,v 1.22.2.1 2008/05/18 12:36:06 yamt 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.2.1      yamt __RCSID("$NetBSD: jot.c,v 1.22.2.1 2008/05/18 12:36:06 yamt 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.2.1      yamt 		if (x < 0) {
     97  1.22.2.1      yamt 			x = -x;
     98  1.22.2.1      yamt 			begin = ender;
     99  1.22.2.1      yamt 		}
    100      1.22       dsl 		if (dox == 0)
    101      1.22       dsl 			/*
    102      1.22       dsl 			 * We are printing floating point, generate random
    103      1.22       dsl 			 * number that include both supplied limits.
    104      1.22       dsl 			 * Due to FP routing for display the low and high
    105      1.22       dsl 			 * values are likely to occur half as often as all
    106      1.22       dsl 			 * the others.
    107      1.22       dsl 			 */
    108      1.22       dsl 			x /= (1u << 31) - 1.0;
    109      1.22       dsl 		else {
    110      1.22       dsl 			/*
    111      1.22       dsl 			 * We are printing integers increase the range by
    112      1.22       dsl 			 * one but ensure we never generate it.
    113      1.22       dsl 			 * This makes all the integer values equally likely.
    114      1.22       dsl 			 */
    115  1.22.2.1      yamt 			x += 1.0;
    116      1.22       dsl 			x /= (1u << 31);
    117      1.22       dsl 		}
    118      1.18       dsl 		srandom((unsigned long) step);
    119      1.22       dsl 		for (i = 1; i <= reps || reps == 0; i++)
    120      1.22       dsl 			putdata(random() * x + begin, reps - i);
    121      1.20       dsl 	} else {
    122      1.22       dsl 		/*
    123      1.22       dsl 		 * If we are going to display as integer, add 0.5 here
    124      1.22       dsl 		 * and use floor(x) later to get sane rounding.
    125      1.22       dsl 		 */
    126      1.22       dsl 		x = begin;
    127      1.22       dsl 		if (dox)
    128      1.22       dsl 			x += 0.5;
    129      1.22       dsl 		for (i = 1; i <= reps || reps == 0; i++, x += step)
    130      1.16       dsl 			putdata(x, reps - i);
    131      1.20       dsl 	}
    132       1.1       jtc 	if (!nofinalnl)
    133       1.1       jtc 		putchar('\n');
    134       1.1       jtc 	exit(0);
    135       1.1       jtc }
    136       1.1       jtc 
    137      1.20       dsl static void
    138      1.13     perry getargs(int argc, char *argv[])
    139       1.1       jtc {
    140      1.21       dsl 	unsigned int have = 0;
    141      1.21       dsl #define BEGIN	1
    142      1.21       dsl #define	STEP	2	/* seed if -r */
    143      1.21       dsl #define REPS	4
    144      1.21       dsl #define	ENDER	8
    145      1.21       dsl 	int n = 0;
    146      1.21       dsl 	long t;
    147      1.20       dsl 	char *ep;
    148       1.1       jtc 
    149      1.20       dsl 	for (;;) {
    150      1.20       dsl 		switch (getopt(argc, argv, "b:cnp:rs:w:")) {
    151      1.20       dsl 		default:
    152      1.20       dsl 			usage();
    153      1.20       dsl 		case -1:
    154       1.1       jtc 			break;
    155       1.1       jtc 		case 'c':
    156       1.1       jtc 			chardata = 1;
    157      1.20       dsl 			continue;
    158       1.1       jtc 		case 'n':
    159       1.1       jtc 			nofinalnl = 1;
    160      1.20       dsl 			continue;
    161      1.20       dsl 		case 'p':
    162      1.20       dsl 			prec = strtol(optarg, &ep, 0);
    163      1.20       dsl 			if (*ep != 0 || prec < 0)
    164      1.20       dsl 				errx(EXIT_FAILURE, "Bad precision value");
    165      1.20       dsl 			continue;
    166      1.20       dsl 		case 'r':
    167      1.20       dsl 			randomize = 1;
    168      1.20       dsl 			continue;
    169      1.20       dsl 		case 's':
    170      1.20       dsl 			sepstring = optarg;
    171      1.20       dsl 			continue;
    172       1.1       jtc 		case 'b':
    173       1.1       jtc 			boring = 1;
    174      1.20       dsl 			/* FALLTHROUGH */
    175       1.1       jtc 		case 'w':
    176      1.20       dsl 			strlcpy(format, optarg, sizeof(format));
    177      1.20       dsl 			continue;
    178       1.1       jtc 		}
    179      1.20       dsl 		break;
    180      1.16       dsl 	}
    181      1.20       dsl 	argc -= optind;
    182      1.20       dsl 	argv += optind;
    183       1.1       jtc 
    184       1.7  jdolecek 	switch (argc) {	/* examine args right to left, falling thru cases */
    185       1.1       jtc 	case 4:
    186       1.7  jdolecek 		if (!is_default(argv[3])) {
    187      1.21       dsl 			step = strtod(argv[3], &ep);
    188      1.21       dsl 			if (*ep != 0)
    189      1.20       dsl 				errx(EXIT_FAILURE, "Bad step value:  %s",
    190      1.20       dsl 				    argv[3]);
    191      1.21       dsl 			have |= STEP;
    192       1.1       jtc 		}
    193       1.1       jtc 	case 3:
    194       1.7  jdolecek 		if (!is_default(argv[2])) {
    195       1.7  jdolecek 			if (!sscanf(argv[2], "%lf", &ender))
    196       1.7  jdolecek 				ender = argv[2][strlen(argv[2])-1];
    197      1.21       dsl 			have |= ENDER;
    198      1.18       dsl 			if (prec < 0)
    199       1.7  jdolecek 				n = getprec(argv[2]);
    200       1.1       jtc 		}
    201       1.1       jtc 	case 2:
    202       1.7  jdolecek 		if (!is_default(argv[1])) {
    203       1.7  jdolecek 			if (!sscanf(argv[1], "%lf", &begin))
    204       1.7  jdolecek 				begin = argv[1][strlen(argv[1])-1];
    205      1.21       dsl 			have |= BEGIN;
    206      1.18       dsl 			if (prec < 0)
    207       1.7  jdolecek 				prec = getprec(argv[1]);
    208       1.1       jtc 			if (n > prec)		/* maximum precision */
    209       1.1       jtc 				prec = n;
    210       1.1       jtc 		}
    211       1.1       jtc 	case 1:
    212       1.7  jdolecek 		if (!is_default(argv[0])) {
    213      1.21       dsl 			reps = strtoul(argv[0], &ep, 0);
    214      1.21       dsl 			if (*ep != 0 || reps < 0)
    215      1.20       dsl 				errx(EXIT_FAILURE, "Bad reps value:  %s",
    216      1.20       dsl 				    argv[0]);
    217      1.21       dsl 			have |= REPS;
    218       1.1       jtc 		}
    219       1.1       jtc 		break;
    220       1.1       jtc 	case 0:
    221       1.7  jdolecek 		usage();
    222       1.7  jdolecek 		break;
    223       1.1       jtc 	default:
    224      1.20       dsl 		errx(EXIT_FAILURE,
    225      1.20       dsl 		    "Too many arguments.  What do you mean by %s?", argv[4]);
    226       1.1       jtc 	}
    227       1.1       jtc 	getformat();
    228      1.21       dsl 
    229      1.21       dsl 	if (prec == -1)
    230      1.21       dsl 		prec = 0;
    231      1.21       dsl 
    232      1.21       dsl 	if (randomize) {
    233      1.21       dsl 		/* 'step' is the seed here, use pseudo-random default */
    234      1.21       dsl 		if (!(have & STEP))
    235      1.21       dsl 			step = time(NULL) * getpid();
    236      1.21       dsl 		/* Take the default values for everything else */
    237      1.21       dsl 		return;
    238      1.21       dsl 	}
    239      1.21       dsl 
    240      1.21       dsl 	/*
    241      1.21       dsl 	 * The loop we run uses begin/step/reps, so if we have been
    242      1.21       dsl 	 * given an end value (ender) we must use it to replace the
    243      1.21       dsl 	 * default values of the others.
    244      1.21       dsl 	 * We will assume a begin of 0 and step of 1 if necessary.
    245      1.21       dsl 	 */
    246      1.21       dsl 
    247      1.21       dsl 	switch (have) {
    248      1.21       dsl 
    249      1.21       dsl 	case ENDER | STEP:
    250      1.21       dsl 	case ENDER | STEP | BEGIN:
    251      1.21       dsl 		/* Calculate reps */
    252      1.21       dsl 		if (step == 0.0)
    253      1.21       dsl 			reps = 0;	/* ie infinite */
    254      1.21       dsl 		else {
    255      1.18       dsl 			reps = (ender - begin + step) / step;
    256       1.1       jtc 			if (reps <= 0)
    257      1.20       dsl 				errx(EXIT_FAILURE, "Impossible stepsize");
    258      1.21       dsl 		}
    259      1.21       dsl 		break;
    260      1.21       dsl 
    261      1.21       dsl 	case REPS | ENDER:
    262      1.21       dsl 	case REPS | ENDER | STEP:
    263      1.21       dsl 		/* Calculate begin */
    264      1.21       dsl 		if (reps == 0)
    265      1.21       dsl 			errx(EXIT_FAILURE,
    266      1.21       dsl 			    "Must specify begin if reps == 0");
    267      1.21       dsl 		begin = ender - reps * step + step;
    268      1.21       dsl 		break;
    269      1.21       dsl 
    270      1.21       dsl 	case REPS | BEGIN | ENDER:
    271      1.21       dsl 		/* Calculate step */
    272      1.21       dsl 		if (reps == 0)
    273      1.21       dsl 			errx(EXIT_FAILURE,
    274      1.21       dsl 			    "Infinite sequences cannot be bounded");
    275      1.21       dsl 		if (reps == 1)
    276      1.21       dsl 			step = 0.0;
    277      1.21       dsl 		else
    278      1.21       dsl 			step = (ender - begin) / (reps - 1);
    279      1.21       dsl 		break;
    280      1.21       dsl 
    281      1.21       dsl 	case REPS | BEGIN | ENDER | STEP:
    282      1.21       dsl 		/* reps given and implied - take smaller */
    283      1.21       dsl 		if (step == 0.0)
    284       1.1       jtc 			break;
    285      1.21       dsl 		t = (ender - begin + step) / step;
    286      1.21       dsl 		if (t <= 0)
    287      1.21       dsl 			errx(EXIT_FAILURE,
    288      1.21       dsl 			    "Impossible stepsize");
    289      1.21       dsl 		if (t < reps)
    290      1.21       dsl 			reps = t;
    291      1.21       dsl 		break;
    292      1.21       dsl 
    293      1.21       dsl 	default:
    294      1.21       dsl 		/* No values can be calculated, use defaults */
    295      1.21       dsl 		break;
    296      1.16       dsl 	}
    297       1.1       jtc }
    298       1.1       jtc 
    299      1.20       dsl static void
    300      1.13     perry putdata(double x, long notlast)
    301       1.1       jtc {
    302       1.1       jtc 
    303       1.1       jtc 	if (boring)				/* repeated word */
    304       1.3        pk 		printf("%s", format);
    305       1.1       jtc 	else if (dox)				/* scalar */
    306      1.22       dsl 		printf(format, (long)floor(x));
    307       1.1       jtc 	else					/* real */
    308       1.1       jtc 		printf(format, x);
    309       1.1       jtc 	if (notlast != 0)
    310       1.1       jtc 		fputs(sepstring, stdout);
    311       1.1       jtc }
    312       1.1       jtc 
    313      1.20       dsl __dead static void
    314       1.7  jdolecek usage(void)
    315       1.1       jtc {
    316      1.12     peter 	(void)fprintf(stderr, "usage: %s [-cnr] [-b word] [-p precision] "
    317      1.20       dsl 	    "[-s string] [-w word] [reps [begin [end [step | seed]]]]\n",
    318      1.20       dsl 	    getprogname());
    319       1.1       jtc 	exit(1);
    320       1.1       jtc }
    321       1.1       jtc 
    322      1.20       dsl static int
    323      1.20       dsl getprec(char *num_str)
    324       1.1       jtc {
    325       1.1       jtc 
    326      1.20       dsl 	num_str = strchr(num_str, '.');
    327      1.20       dsl 	if (num_str == NULL)
    328      1.20       dsl 		return 0;
    329      1.20       dsl 	return strspn(num_str + 1, "0123456789");
    330       1.1       jtc }
    331       1.1       jtc 
    332      1.20       dsl static void
    333      1.13     perry getformat(void)
    334       1.1       jtc {
    335       1.4     lukem 	char	*p;
    336       1.7  jdolecek 	size_t	sz;
    337       1.1       jtc 
    338       1.1       jtc 	if (boring)				/* no need to bother */
    339       1.1       jtc 		return;
    340      1.20       dsl 	for (p = format; *p; p++) {		/* look for '%' */
    341       1.7  jdolecek 		if (*p == '%') {
    342       1.7  jdolecek 			if (*(p+1) != '%')
    343       1.7  jdolecek 				break;
    344       1.7  jdolecek 			p++;		/* leave %% alone */
    345       1.7  jdolecek 		}
    346      1.20       dsl 	}
    347       1.7  jdolecek 	sz = sizeof(format) - strlen(format) - 1;
    348       1.7  jdolecek 	if (!*p) {
    349      1.17       dsl 		if (chardata || prec == 0) {
    350      1.17       dsl 			if (snprintf(p, sz, "%%%s", chardata ? "c" : "ld") >= sz)
    351      1.20       dsl 				errx(EXIT_FAILURE, "-w word too long");
    352       1.8    simonb 			dox = 1;
    353       1.7  jdolecek 		} else {
    354       1.8    simonb 			if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
    355      1.20       dsl 				errx(EXIT_FAILURE, "-w word too long");
    356       1.7  jdolecek 		}
    357       1.7  jdolecek 	} else if (!*(p+1)) {
    358       1.7  jdolecek 		if (sz <= 0)
    359      1.20       dsl 			errx(EXIT_FAILURE, "-w word too long");
    360       1.1       jtc 		strcat(format, "%");		/* cannot end in single '%' */
    361       1.7  jdolecek 	} else {
    362       1.7  jdolecek 		p++;				/* skip leading % */
    363       1.7  jdolecek 		for(; *p && !isalpha((unsigned char)*p); p++) {
    364       1.7  jdolecek 			/* allow all valid printf(3) flags, but deny '*' */
    365       1.7  jdolecek 			if (!strchr("0123456789#-+. ", *p))
    366       1.7  jdolecek 				break;
    367       1.7  jdolecek 		}
    368       1.7  jdolecek 		/* Allow 'l' prefix, but no other. */
    369       1.7  jdolecek 		if (*p == 'l')
    370       1.7  jdolecek 			p++;
    371       1.1       jtc 		switch (*p) {
    372       1.1       jtc 		case 'f': case 'e': case 'g': case '%':
    373       1.7  jdolecek 		case 'E': case 'G':
    374       1.1       jtc 			break;
    375       1.1       jtc 		case 's':
    376      1.20       dsl 			errx(EXIT_FAILURE,
    377      1.20       dsl 			    "cannot convert numeric data to strings");
    378       1.1       jtc 			break;
    379       1.7  jdolecek 		case 'd': case 'o': case 'x': case 'u':
    380       1.7  jdolecek 		case 'D': case 'O': case 'X': case 'U':
    381       1.7  jdolecek 		case 'c': case 'i':
    382       1.1       jtc 			dox = 1;
    383       1.1       jtc 			break;
    384       1.7  jdolecek 		default:
    385      1.20       dsl 			errx(EXIT_FAILURE, "unknown or invalid format `%s'",
    386      1.20       dsl 			    format);
    387       1.1       jtc 		}
    388       1.7  jdolecek 		/* Need to check for trailing stuff to print */
    389       1.7  jdolecek 		for (; *p; p++)		/* look for '%' */
    390       1.7  jdolecek 			if (*p == '%') {
    391       1.7  jdolecek 				if (*(p+1) != '%')
    392       1.7  jdolecek 					break;
    393       1.7  jdolecek 				p++;		/* leave %% alone */
    394       1.7  jdolecek 			}
    395       1.7  jdolecek 		if (*p)
    396      1.20       dsl 			errx(EXIT_FAILURE, "unknown or invalid format `%s'",
    397      1.20       dsl 			    format);
    398       1.1       jtc 	}
    399       1.1       jtc }
    400