Home | History | Annotate | Line # | Download | only in jot
jot.c revision 1.19
      1  1.19       dsl /*	$NetBSD: jot.c,v 1.19 2008/02/24 00:04:00 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.19       dsl __RCSID("$NetBSD: jot.c,v 1.19 2008/02/24 00:04:00 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.1       jtc double	begin;
     69   1.1       jtc double	ender;
     70  1.18       dsl double	step;
     71   1.1       jtc long	reps;
     72   1.1       jtc int	randomize;
     73   1.1       jtc int	infinity;
     74   1.1       jtc int	boring;
     75  1.18       dsl int	prec = -1;
     76   1.1       jtc int	dox;
     77   1.1       jtc int	chardata;
     78   1.1       jtc int	nofinalnl;
     79   1.3        pk char	sepstring[BUFSIZ] = "\n";
     80   1.1       jtc char	format[BUFSIZ];
     81   1.1       jtc 
     82  1.13     perry void	getargs(int, char *[]);
     83  1.13     perry void	getformat(void);
     84  1.13     perry int	getprec(char *);
     85  1.13     perry int	main(int, char **);
     86  1.13     perry void	putdata(double, long);
     87  1.13     perry static void	usage(void);
     88   1.1       jtc 
     89   1.1       jtc int
     90  1.13     perry main(int argc, char *argv[])
     91   1.1       jtc {
     92  1.16       dsl 	double	x, y;
     93  1.16       dsl 	long	i;
     94   1.1       jtc 
     95   1.1       jtc 	getargs(argc, argv);
     96   1.1       jtc 	if (randomize) {
     97  1.17       dsl 		x = (ender + dox - begin) * (ender > begin ? 1 : -1);
     98  1.18       dsl 		srandom((unsigned long) step);
     99  1.16       dsl 		for (i = 1; i <= reps || infinity; i++) {
    100  1.16       dsl 			y = (double) random() / INT_MAX;
    101  1.16       dsl 			putdata(y * x + begin, reps - i);
    102  1.16       dsl 		}
    103  1.16       dsl 	} else
    104  1.18       dsl 		for (i = 1, x = begin; i <= reps || infinity; i++, x += step)
    105  1.16       dsl 			putdata(x, reps - i);
    106   1.1       jtc 	if (!nofinalnl)
    107   1.1       jtc 		putchar('\n');
    108   1.1       jtc 	exit(0);
    109   1.1       jtc }
    110   1.1       jtc 
    111   1.1       jtc void
    112  1.13     perry getargs(int argc, char *argv[])
    113   1.1       jtc {
    114   1.4     lukem 	unsigned int	mask = 0;
    115   1.4     lukem 	int		n = 0;
    116   1.1       jtc 
    117  1.16       dsl 	while (--argc && **++argv == '-' && !is_default(*argv)) {
    118   1.7  jdolecek 		switch ((*argv)[1]) {
    119   1.1       jtc 		case 'r':
    120   1.1       jtc 			randomize = 1;
    121   1.1       jtc 			break;
    122   1.1       jtc 		case 'c':
    123   1.1       jtc 			chardata = 1;
    124   1.1       jtc 			break;
    125   1.1       jtc 		case 'n':
    126   1.1       jtc 			nofinalnl = 1;
    127   1.1       jtc 			break;
    128   1.1       jtc 		case 'b':
    129   1.1       jtc 			boring = 1;
    130   1.1       jtc 		case 'w':
    131   1.7  jdolecek 			if ((*argv)[2])
    132  1.15  christos 				strlcpy(format, *argv + 2, sizeof(format));
    133   1.7  jdolecek 			else if (!--argc)
    134   1.7  jdolecek 				errx(1, "Need context word after -w or -b");
    135   1.1       jtc 			else
    136  1.15  christos 				strlcpy(format, *++argv, sizeof(format));
    137   1.1       jtc 			break;
    138   1.1       jtc 		case 's':
    139   1.7  jdolecek 			if ((*argv)[2])
    140  1.15  christos 				strlcpy(sepstring, *argv + 2, sizeof(sepstring));
    141   1.7  jdolecek 			else if (!--argc)
    142   1.7  jdolecek 				errx(1, "Need string after -s");
    143   1.1       jtc 			else
    144  1.15  christos 				strlcpy(sepstring, *++argv, sizeof(sepstring));
    145   1.1       jtc 			break;
    146   1.1       jtc 		case 'p':
    147   1.7  jdolecek 			if ((*argv)[2])
    148   1.7  jdolecek 				prec = atoi(*argv + 2);
    149   1.7  jdolecek 			else if (!--argc)
    150   1.7  jdolecek 				errx(1, "Need number after -p");
    151   1.1       jtc 			else
    152   1.7  jdolecek 				prec = atoi(*++argv);
    153  1.18       dsl 			if (prec < 0)
    154   1.7  jdolecek 				errx(1, "Bad precision value");
    155   1.1       jtc 			break;
    156   1.1       jtc 		default:
    157   1.7  jdolecek 			warnx("unknown option `%s'", *argv);
    158   1.7  jdolecek 			usage();
    159   1.1       jtc 		}
    160  1.16       dsl 	}
    161   1.1       jtc 
    162   1.7  jdolecek 	switch (argc) {	/* examine args right to left, falling thru cases */
    163   1.1       jtc 	case 4:
    164   1.7  jdolecek 		if (!is_default(argv[3])) {
    165  1.18       dsl 			if (!sscanf(argv[3], "%lf", &step))
    166  1.18       dsl 				errx(1, "Bad step value:  %s", argv[3]);
    167   1.1       jtc 			mask |= 01;
    168   1.1       jtc 		}
    169   1.1       jtc 	case 3:
    170   1.7  jdolecek 		if (!is_default(argv[2])) {
    171   1.7  jdolecek 			if (!sscanf(argv[2], "%lf", &ender))
    172   1.7  jdolecek 				ender = argv[2][strlen(argv[2])-1];
    173   1.1       jtc 			mask |= 02;
    174  1.18       dsl 			if (prec < 0)
    175   1.7  jdolecek 				n = getprec(argv[2]);
    176   1.1       jtc 		}
    177   1.1       jtc 	case 2:
    178   1.7  jdolecek 		if (!is_default(argv[1])) {
    179   1.7  jdolecek 			if (!sscanf(argv[1], "%lf", &begin))
    180   1.7  jdolecek 				begin = argv[1][strlen(argv[1])-1];
    181   1.1       jtc 			mask |= 04;
    182  1.18       dsl 			if (prec < 0)
    183   1.7  jdolecek 				prec = getprec(argv[1]);
    184   1.1       jtc 			if (n > prec)		/* maximum precision */
    185   1.1       jtc 				prec = n;
    186   1.1       jtc 		}
    187   1.1       jtc 	case 1:
    188   1.7  jdolecek 		if (!is_default(argv[0])) {
    189   1.7  jdolecek 			if (!sscanf(argv[0], "%ld", &reps))
    190   1.7  jdolecek 				errx(1, "Bad reps value:  %s", argv[0]);
    191   1.1       jtc 			mask |= 010;
    192   1.1       jtc 		}
    193   1.1       jtc 		break;
    194   1.1       jtc 	case 0:
    195   1.7  jdolecek 		usage();
    196   1.7  jdolecek 		break;
    197   1.1       jtc 	default:
    198   1.7  jdolecek 		errx(1, "Too many arguments.  What do you mean by %s?", argv[4]);
    199   1.1       jtc 	}
    200   1.1       jtc 	getformat();
    201  1.16       dsl 	while (mask) {	/* 4 bit mask has 1's where last 4 args were given */
    202   1.1       jtc 		switch (mask) {	/* fill in the 0's by default or computation */
    203   1.1       jtc 		case 001:
    204   1.1       jtc 			reps = REPS_DEF;
    205   1.1       jtc 			mask = 011;
    206   1.1       jtc 			break;
    207   1.1       jtc 		case 002:
    208   1.1       jtc 			reps = REPS_DEF;
    209   1.1       jtc 			mask = 012;
    210   1.1       jtc 			break;
    211   1.1       jtc 		case 003:
    212   1.1       jtc 			reps = REPS_DEF;
    213   1.1       jtc 			mask = 013;
    214   1.1       jtc 			break;
    215   1.1       jtc 		case 004:
    216   1.1       jtc 			reps = REPS_DEF;
    217   1.1       jtc 			mask = 014;
    218   1.1       jtc 			break;
    219   1.1       jtc 		case 005:
    220   1.1       jtc 			reps = REPS_DEF;
    221   1.1       jtc 			mask = 015;
    222   1.1       jtc 			break;
    223   1.1       jtc 		case 006:
    224   1.1       jtc 			reps = REPS_DEF;
    225   1.1       jtc 			mask = 016;
    226   1.1       jtc 			break;
    227   1.1       jtc 		case 007:
    228   1.1       jtc 			if (randomize) {
    229   1.1       jtc 				reps = REPS_DEF;
    230   1.1       jtc 				mask = 0;
    231   1.1       jtc 				break;
    232   1.1       jtc 			}
    233  1.18       dsl 			if (step == 0.0) {
    234   1.1       jtc 				reps = 0;
    235   1.1       jtc 				mask = 0;
    236   1.1       jtc 				break;
    237   1.1       jtc 			}
    238  1.18       dsl 			reps = (ender - begin + step) / step;
    239   1.1       jtc 			if (reps <= 0)
    240   1.7  jdolecek 				errx(1, "Impossible stepsize");
    241   1.1       jtc 			mask = 0;
    242   1.1       jtc 			break;
    243   1.1       jtc 		case 010:
    244   1.1       jtc 			begin = BEGIN_DEF;
    245   1.1       jtc 			mask = 014;
    246   1.1       jtc 			break;
    247   1.1       jtc 		case 011:
    248   1.1       jtc 			begin = BEGIN_DEF;
    249   1.1       jtc 			mask = 015;
    250   1.1       jtc 			break;
    251   1.1       jtc 		case 012:
    252  1.18       dsl 			step = (randomize ? time(NULL) * getpid() : STEP_DEF);
    253   1.1       jtc 			mask = 013;
    254   1.1       jtc 			break;
    255   1.1       jtc 		case 013:
    256   1.1       jtc 			if (randomize)
    257   1.1       jtc 				begin = BEGIN_DEF;
    258   1.1       jtc 			else if (reps == 0)
    259   1.7  jdolecek 				errx(1, "Must specify begin if reps == 0");
    260  1.18       dsl 			begin = ender - reps * step + step;
    261   1.1       jtc 			mask = 0;
    262   1.1       jtc 			break;
    263   1.1       jtc 		case 014:
    264  1.18       dsl 			step = (randomize ? time(NULL) * getpid() : STEP_DEF);
    265   1.1       jtc 			mask = 015;
    266   1.1       jtc 			break;
    267   1.1       jtc 		case 015:
    268   1.1       jtc 			if (randomize)
    269   1.1       jtc 				ender = ENDER_DEF;
    270   1.1       jtc 			else
    271  1.18       dsl 				ender = begin + reps * step - step;
    272   1.1       jtc 			mask = 0;
    273   1.1       jtc 			break;
    274   1.1       jtc 		case 016:
    275   1.1       jtc 			if (randomize)
    276  1.18       dsl 				step = time(NULL) * getpid();
    277   1.1       jtc 			else if (reps == 0)
    278   1.7  jdolecek 				errx(1, "Infinite sequences cannot be bounded");
    279   1.1       jtc 			else if (reps == 1)
    280  1.18       dsl 				step = 0.0;
    281   1.1       jtc 			else
    282  1.18       dsl 				step = (ender - begin) / (reps - 1);
    283   1.1       jtc 			mask = 0;
    284   1.1       jtc 			break;
    285   1.1       jtc 		case 017:		/* if reps given and implied, */
    286  1.18       dsl 			if (!randomize && step != 0.0) {
    287  1.18       dsl 				long t = (ender - begin + step) / step;
    288   1.1       jtc 				if (t <= 0)
    289   1.7  jdolecek 					errx(1, "Impossible stepsize");
    290   1.1       jtc 				if (t < reps)		/* take lesser */
    291   1.1       jtc 					reps = t;
    292   1.1       jtc 			}
    293   1.1       jtc 			mask = 0;
    294   1.1       jtc 			break;
    295   1.1       jtc 		default:
    296   1.7  jdolecek 			errx(1, "bad mask");
    297   1.1       jtc 		}
    298  1.16       dsl 	}
    299   1.1       jtc 	if (reps == 0)
    300   1.1       jtc 		infinity = 1;
    301  1.18       dsl 
    302  1.18       dsl 	if (prec == -1)
    303  1.18       dsl 		prec = 0;
    304   1.1       jtc }
    305   1.1       jtc 
    306   1.1       jtc void
    307  1.13     perry putdata(double x, long notlast)
    308   1.1       jtc {
    309  1.19       dsl 	long	d = floor(x);
    310   1.1       jtc 
    311   1.1       jtc 	if (boring)				/* repeated word */
    312   1.3        pk 		printf("%s", format);
    313   1.1       jtc 	else if (dox)				/* scalar */
    314  1.16       dsl 		printf(format, d);
    315   1.1       jtc 	else					/* real */
    316   1.1       jtc 		printf(format, x);
    317   1.1       jtc 	if (notlast != 0)
    318   1.1       jtc 		fputs(sepstring, stdout);
    319   1.1       jtc }
    320   1.1       jtc 
    321   1.7  jdolecek static void
    322   1.7  jdolecek usage(void)
    323   1.1       jtc {
    324  1.12     peter 	(void)fprintf(stderr, "usage: %s [-cnr] [-b word] [-p precision] "
    325  1.18       dsl 	    "[-s string] [-w word] [reps [begin [end [step]]]]\n", getprogname());
    326   1.1       jtc 	exit(1);
    327   1.1       jtc }
    328   1.1       jtc 
    329   1.1       jtc int
    330  1.13     perry getprec(char *s)
    331   1.1       jtc {
    332   1.4     lukem 	char	*p;
    333   1.4     lukem 	char	*q;
    334   1.1       jtc 
    335   1.1       jtc 	for (p = s; *p; p++)
    336   1.1       jtc 		if (*p == '.')
    337   1.1       jtc 			break;
    338   1.1       jtc 	if (!*p)
    339   1.1       jtc 		return (0);
    340   1.1       jtc 	for (q = ++p; *p; p++)
    341   1.5  christos 		if (!isdigit((unsigned char)*p))
    342   1.1       jtc 			break;
    343   1.1       jtc 	return (p - q);
    344   1.1       jtc }
    345   1.1       jtc 
    346   1.1       jtc void
    347  1.13     perry getformat(void)
    348   1.1       jtc {
    349   1.4     lukem 	char	*p;
    350   1.7  jdolecek 	size_t	sz;
    351   1.1       jtc 
    352   1.1       jtc 	if (boring)				/* no need to bother */
    353   1.1       jtc 		return;
    354   1.1       jtc 	for (p = format; *p; p++)		/* look for '%' */
    355   1.7  jdolecek 		if (*p == '%') {
    356   1.7  jdolecek 			if (*(p+1) != '%')
    357   1.7  jdolecek 				break;
    358   1.7  jdolecek 			p++;		/* leave %% alone */
    359   1.7  jdolecek 		}
    360   1.7  jdolecek 	sz = sizeof(format) - strlen(format) - 1;
    361   1.7  jdolecek 	if (!*p) {
    362  1.17       dsl 		if (chardata || prec == 0) {
    363  1.17       dsl 			if (snprintf(p, sz, "%%%s", chardata ? "c" : "ld") >= sz)
    364   1.7  jdolecek 				errx(1, "-w word too long");
    365   1.8    simonb 			dox = 1;
    366   1.7  jdolecek 		} else {
    367   1.8    simonb 			if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
    368   1.7  jdolecek 				errx(1, "-w word too long");
    369   1.7  jdolecek 		}
    370   1.7  jdolecek 	} else if (!*(p+1)) {
    371   1.7  jdolecek 		if (sz <= 0)
    372   1.7  jdolecek 			errx(1, "-w word too long");
    373   1.1       jtc 		strcat(format, "%");		/* cannot end in single '%' */
    374   1.7  jdolecek 	} else {
    375   1.7  jdolecek 		p++;				/* skip leading % */
    376   1.7  jdolecek 		for(; *p && !isalpha((unsigned char)*p); p++) {
    377   1.7  jdolecek 			/* allow all valid printf(3) flags, but deny '*' */
    378   1.7  jdolecek 			if (!strchr("0123456789#-+. ", *p))
    379   1.7  jdolecek 				break;
    380   1.7  jdolecek 		}
    381   1.7  jdolecek 		/* Allow 'l' prefix, but no other. */
    382   1.7  jdolecek 		if (*p == 'l')
    383   1.7  jdolecek 			p++;
    384   1.1       jtc 		switch (*p) {
    385   1.1       jtc 		case 'f': case 'e': case 'g': case '%':
    386   1.7  jdolecek 		case 'E': case 'G':
    387   1.1       jtc 			break;
    388   1.1       jtc 		case 's':
    389   1.7  jdolecek 			errx(1, "cannot convert numeric data to strings");
    390   1.1       jtc 			break;
    391   1.7  jdolecek 		case 'd': case 'o': case 'x': case 'u':
    392   1.7  jdolecek 		case 'D': case 'O': case 'X': case 'U':
    393   1.7  jdolecek 		case 'c': case 'i':
    394   1.1       jtc 			dox = 1;
    395   1.1       jtc 			break;
    396   1.7  jdolecek 		default:
    397   1.7  jdolecek 			errx(1, "unknown or invalid format `%s'", format);
    398   1.1       jtc 		}
    399   1.7  jdolecek 		/* Need to check for trailing stuff to print */
    400   1.7  jdolecek 		for (; *p; p++)		/* look for '%' */
    401   1.7  jdolecek 			if (*p == '%') {
    402   1.7  jdolecek 				if (*(p+1) != '%')
    403   1.7  jdolecek 					break;
    404   1.7  jdolecek 				p++;		/* leave %% alone */
    405   1.7  jdolecek 			}
    406   1.7  jdolecek 		if (*p)
    407   1.7  jdolecek 			errx(1, "unknown or invalid format `%s'", format);
    408   1.1       jtc 	}
    409   1.1       jtc }
    410