jot.c revision 164025
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527423Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127423Scharnier#if 0
421590Srgrimesstatic char sccsid[] = "@(#)jot.c	8.1 (Berkeley) 6/6/93";
4327423Scharnier#endif
4498254Sjmallett#endif
4598254Sjmallett#include <sys/cdefs.h>
4698254Sjmallett__FBSDID("$FreeBSD: head/usr.bin/jot/jot.c 164025 2006-11-06 09:15:21Z dds $");
4798254Sjmallett
481590Srgrimes/*
491590Srgrimes * jot - print sequential or random data
501590Srgrimes *
511590Srgrimes * Author:  John Kunze, Office of Comp. Affairs, UCB
521590Srgrimes */
531590Srgrimes
541590Srgrimes#include <ctype.h>
5527423Scharnier#include <err.h>
561590Srgrimes#include <limits.h>
571590Srgrimes#include <stdio.h>
5899457Smike#include <stdint.h>
591590Srgrimes#include <stdlib.h>
601590Srgrimes#include <string.h>
611590Srgrimes#include <time.h>
6223511Sache#include <unistd.h>
631590Srgrimes
641590Srgrimes#define	REPS_DEF	100
651590Srgrimes#define	BEGIN_DEF	1
661590Srgrimes#define	ENDER_DEF	100
671590Srgrimes#define	STEP_DEF	1
681590Srgrimes
69164021Sdds#define HAVE_STEP	1
70164021Sdds#define HAVE_ENDER	2
71164021Sdds#define HAVE_BEGIN	4
72164021Sdds#define HAVE_REPS	8
73164021Sdds
7477276Sdd#define	is_default(s)	(strcmp((s), "-") == 0)
751590Srgrimes
761590Srgrimesdouble	begin;
771590Srgrimesdouble	ender;
781590Srgrimesdouble	s;
791590Srgrimeslong	reps;
801590Srgrimesint	randomize;
811590Srgrimesint	infinity;
821590Srgrimesint	boring;
831590Srgrimesint	prec;
8455515Ssheldonhint	longdata;
8548995Ssheldonhint	intdata;
861590Srgrimesint	chardata;
8748995Ssheldonhint	nosign;
881590Srgrimesint	nofinalnl;
8977287Sddconst	char *sepstring = "\n";
901590Srgrimeschar	format[BUFSIZ];
911590Srgrimes
9292920Simpvoid		getformat(void);
9392920Simpint		getprec(char *);
9492920Simpint		putdata(double, long);
9592920Simpstatic void	usage(void);
961590Srgrimes
971590Srgrimesint
9898254Sjmallettmain(int argc, char **argv)
991590Srgrimes{
100164025Sdds	double	x, y;
101164025Sdds	long	i;
10277276Sdd	unsigned int	mask = 0;
10377276Sdd	int	n = 0;
10477276Sdd	int	ch;
1051590Srgrimes
10677276Sdd	while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1)
10798254Sjmallett		switch (ch) {
1081590Srgrimes		case 'r':
1091590Srgrimes			randomize = 1;
1101590Srgrimes			break;
1111590Srgrimes		case 'c':
1121590Srgrimes			chardata = 1;
1131590Srgrimes			break;
1141590Srgrimes		case 'n':
1151590Srgrimes			nofinalnl = 1;
1161590Srgrimes			break;
1171590Srgrimes		case 'b':
1181590Srgrimes			boring = 1;
11977276Sdd			/* FALLTHROUGH */
1201590Srgrimes		case 'w':
12177276Sdd			if (strlcpy(format, optarg, sizeof(format)) >=
12277276Sdd			    sizeof(format))
12377276Sdd				errx(1, "-%c word too long", ch);
1241590Srgrimes			break;
1251590Srgrimes		case 's':
12677276Sdd			sepstring = optarg;
1271590Srgrimes			break;
1281590Srgrimes		case 'p':
12977276Sdd			prec = atoi(optarg);
1301590Srgrimes			if (prec <= 0)
13127423Scharnier				errx(1, "bad precision value");
1321590Srgrimes			break;
1331590Srgrimes		default:
13427423Scharnier			usage();
1351590Srgrimes		}
13677276Sdd	argc -= optind;
13777276Sdd	argv += optind;
1381590Srgrimes
13977276Sdd	switch (argc) {	/* examine args right to left, falling thru cases */
1401590Srgrimes	case 4:
14177276Sdd		if (!is_default(argv[3])) {
14277276Sdd			if (!sscanf(argv[3], "%lf", &s))
14377276Sdd				errx(1, "bad s value: %s", argv[3]);
144164021Sdds			mask |= HAVE_STEP;
1451590Srgrimes		}
146164021Sdds		/* FALLTHROUGH */
1471590Srgrimes	case 3:
14877276Sdd		if (!is_default(argv[2])) {
14977276Sdd			if (!sscanf(argv[2], "%lf", &ender))
15077276Sdd				ender = argv[2][strlen(argv[2])-1];
151164021Sdds			mask |= HAVE_ENDER;
1521590Srgrimes			if (!prec)
15377276Sdd				n = getprec(argv[2]);
1541590Srgrimes		}
155164021Sdds		/* FALLTHROUGH */
1561590Srgrimes	case 2:
15777276Sdd		if (!is_default(argv[1])) {
15877276Sdd			if (!sscanf(argv[1], "%lf", &begin))
15977276Sdd				begin = argv[1][strlen(argv[1])-1];
160164021Sdds			mask |= HAVE_BEGIN;
1611590Srgrimes			if (!prec)
16277276Sdd				prec = getprec(argv[1]);
1631590Srgrimes			if (n > prec)		/* maximum precision */
1641590Srgrimes				prec = n;
1651590Srgrimes		}
166164021Sdds		/* FALLTHROUGH */
1671590Srgrimes	case 1:
16877276Sdd		if (!is_default(argv[0])) {
16977276Sdd			if (!sscanf(argv[0], "%ld", &reps))
17077276Sdd				errx(1, "bad reps value: %s", argv[0]);
171164021Sdds			mask |= HAVE_REPS;
1721590Srgrimes		}
1731590Srgrimes		break;
1741590Srgrimes	case 0:
17527423Scharnier		usage();
1761590Srgrimes	default:
17777276Sdd		errx(1, "too many arguments.  What do you mean by %s?",
17877276Sdd		    argv[4]);
1791590Srgrimes	}
1801590Srgrimes	getformat();
1811590Srgrimes	while (mask)	/* 4 bit mask has 1's where last 4 args were given */
1821590Srgrimes		switch (mask) {	/* fill in the 0's by default or computation */
183164021Sdds		case HAVE_STEP:
184164021Sdds		case HAVE_ENDER:
185164021Sdds		case HAVE_ENDER | HAVE_STEP:
186164021Sdds		case HAVE_BEGIN:
187164021Sdds		case HAVE_BEGIN | HAVE_STEP:
188164021Sdds		case HAVE_BEGIN | HAVE_ENDER:
1891590Srgrimes			reps = REPS_DEF;
190164023Sdds			mask |= HAVE_REPS;
1911590Srgrimes			break;
192164021Sdds		case HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
193164023Sdds			if (randomize)
1941590Srgrimes				reps = REPS_DEF;
195164023Sdds			else if (s == 0.0)
1961590Srgrimes				reps = 0;
197164023Sdds			else
198164023Sdds				reps = (ender - begin + s) / s;
1991590Srgrimes			if (reps <= 0)
20027423Scharnier				errx(1, "impossible stepsize");
2011590Srgrimes			mask = 0;
2021590Srgrimes			break;
203164021Sdds		case HAVE_REPS:
204164021Sdds		case HAVE_REPS | HAVE_STEP:
2051590Srgrimes			begin = BEGIN_DEF;
206164023Sdds			mask |= HAVE_BEGIN;
2071590Srgrimes			break;
208164021Sdds		case HAVE_REPS | HAVE_ENDER:
20977276Sdd			s = (randomize ? time(NULL) : STEP_DEF);
210164021Sdds			mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP;
2111590Srgrimes			break;
212164021Sdds		case HAVE_REPS | HAVE_ENDER | HAVE_STEP:
2131590Srgrimes			if (randomize)
2141590Srgrimes				begin = BEGIN_DEF;
2151590Srgrimes			else if (reps == 0)
21627423Scharnier				errx(1, "must specify begin if reps == 0");
21777276Sdd			begin = ender - reps * s + s;
2181590Srgrimes			mask = 0;
2191590Srgrimes			break;
220164021Sdds		case HAVE_REPS | HAVE_BEGIN:
22124419Sache			s = (randomize ? -1.0 : STEP_DEF);
222164021Sdds			mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP;
2231590Srgrimes			break;
224164021Sdds		case HAVE_REPS | HAVE_BEGIN | HAVE_STEP:
2251590Srgrimes			if (randomize)
2261590Srgrimes				ender = ENDER_DEF;
2271590Srgrimes			else
2281590Srgrimes				ender = begin + reps * s - s;
2291590Srgrimes			mask = 0;
2301590Srgrimes			break;
231164021Sdds		case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER:
2321590Srgrimes			if (randomize)
23324419Sache				s = -1.0;
2341590Srgrimes			else if (reps == 0)
23527423Scharnier				errx(1, "infinite sequences cannot be bounded");
2361590Srgrimes			else if (reps == 1)
2371590Srgrimes				s = 0.0;
2381590Srgrimes			else
2391590Srgrimes				s = (ender - begin) / (reps - 1);
2401590Srgrimes			mask = 0;
2411590Srgrimes			break;
242164021Sdds		case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER | HAVE_STEP:
243164021Sdds			/* if reps given and implied, */
2441590Srgrimes			if (!randomize && s != 0.0) {
2451590Srgrimes				long t = (ender - begin + s) / s;
2461590Srgrimes				if (t <= 0)
24727423Scharnier					errx(1, "impossible stepsize");
2481590Srgrimes				if (t < reps)		/* take lesser */
2491590Srgrimes					reps = t;
2501590Srgrimes			}
2511590Srgrimes			mask = 0;
2521590Srgrimes			break;
2531590Srgrimes		default:
25427423Scharnier			errx(1, "bad mask");
2551590Srgrimes		}
2561590Srgrimes	if (reps == 0)
2571590Srgrimes		infinity = 1;
25877276Sdd	if (randomize) {
259164025Sdds		x = (ender - begin) * (ender > begin ? 1 : -1);
260164025Sdds		for (i = 1; i <= reps || infinity; i++) {
261164025Sdds			y = arc4random() / ((double)UINT32_MAX + 1);
262164025Sdds			if (putdata(y * x + begin, reps - i))
26377276Sdd				errx(1, "range error in conversion");
26477276Sdd		}
26577276Sdd	} else
266164025Sdds		for (i = 1, x = begin; i <= reps || infinity; i++, x += s)
267164025Sdds			if (putdata(x, reps - i))
26877276Sdd				errx(1, "range error in conversion");
26977276Sdd	if (!nofinalnl)
27077276Sdd		putchar('\n');
27177276Sdd	exit(0);
2721590Srgrimes}
2731590Srgrimes
27455515Ssheldonhint
27598254Sjmallettputdata(double x, long int notlast)
2761590Srgrimes{
2771590Srgrimes
27855515Ssheldonh	if (boring)
27962871Skris		printf("%s", format);
28055515Ssheldonh	else if (longdata && nosign) {
28155515Ssheldonh		if (x <= (double)ULONG_MAX && x >= (double)0)
28255515Ssheldonh			printf(format, (unsigned long)x);
28355515Ssheldonh		else
28455515Ssheldonh			return (1);
28555515Ssheldonh	} else if (longdata) {
28655515Ssheldonh		if (x <= (double)LONG_MAX && x >= (double)LONG_MIN)
28755515Ssheldonh			printf(format, (long)x);
28855515Ssheldonh		else
28955515Ssheldonh			return (1);
29055515Ssheldonh	} else if (chardata || (intdata && !nosign)) {
29155515Ssheldonh		if (x <= (double)INT_MAX && x >= (double)INT_MIN)
29255515Ssheldonh			printf(format, (int)x);
29355515Ssheldonh		else
29455515Ssheldonh			return (1);
29555515Ssheldonh	} else if (intdata) {
29655515Ssheldonh		if (x <= (double)UINT_MAX && x >= (double)0)
29755515Ssheldonh			printf(format, (unsigned int)x);
29855515Ssheldonh		else
29955515Ssheldonh			return (1);
30055515Ssheldonh
30155515Ssheldonh	} else
3021590Srgrimes		printf(format, x);
3031590Srgrimes	if (notlast != 0)
3041590Srgrimes		fputs(sepstring, stdout);
30555515Ssheldonh
30655515Ssheldonh	return (0);
3071590Srgrimes}
3081590Srgrimes
30927423Scharnierstatic void
31098254Sjmallettusage(void)
3111590Srgrimes{
31227423Scharnier	fprintf(stderr, "%s\n%s\n",
31330908Scharnier	"usage: jot [-cnr] [-b word] [-w word] [-s string] [-p precision]",
31430908Scharnier	"           [reps [begin [end [s]]]]");
3151590Srgrimes	exit(1);
3161590Srgrimes}
3171590Srgrimes
3181590Srgrimesint
31998254Sjmallettgetprec(char *str)
3201590Srgrimes{
32177276Sdd	char	*p;
32277276Sdd	char	*q;
3231590Srgrimes
32477287Sdd	for (p = str; *p; p++)
3251590Srgrimes		if (*p == '.')
3261590Srgrimes			break;
3271590Srgrimes	if (!*p)
3281590Srgrimes		return (0);
3291590Srgrimes	for (q = ++p; *p; p++)
330132240Stjr		if (!isdigit((unsigned char)*p))
3311590Srgrimes			break;
3321590Srgrimes	return (p - q);
3331590Srgrimes}
3341590Srgrimes
3351590Srgrimesvoid
33698254Sjmallettgetformat(void)
3371590Srgrimes{
33877287Sdd	char	*p, *p2;
33955515Ssheldonh	int dot, hash, space, sign, numbers = 0;
34077276Sdd	size_t sz;
3411590Srgrimes
3421590Srgrimes	if (boring)				/* no need to bother */
3431590Srgrimes		return;
3441590Srgrimes	for (p = format; *p; p++)		/* look for '%' */
3451590Srgrimes		if (*p == '%' && *(p+1) != '%')	/* leave %% alone */
3461590Srgrimes			break;
34777276Sdd	sz = sizeof(format) - strlen(format) - 1;
34877276Sdd	if (!*p && !chardata) {
34977276Sdd		if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
35077276Sdd			errx(1, "-w word too long");
35177276Sdd	} else if (!*p && chardata) {
35277276Sdd		if (strlcpy(p, "%c", sz) >= sz)
35377276Sdd			errx(1, "-w word too long");
35448995Ssheldonh		intdata = 1;
35577276Sdd	} else if (!*(p+1)) {
35677276Sdd		if (sz <= 0)
35777276Sdd			errx(1, "-w word too long");
3581590Srgrimes		strcat(format, "%");		/* cannot end in single '%' */
35977276Sdd	} else {
36048995Ssheldonh		/*
36148995Ssheldonh		 * Allow conversion format specifiers of the form
36248995Ssheldonh		 * %[#][ ][{+,-}][0-9]*[.[0-9]*]? where ? must be one of
36348995Ssheldonh		 * [l]{d,i,o,u,x} or {f,e,g,E,G,d,o,x,D,O,U,X,c,u}
36448995Ssheldonh		 */
36577287Sdd		p2 = p++;
36648995Ssheldonh		dot = hash = space = sign = numbers = 0;
367132240Stjr		while (!isalpha((unsigned char)*p)) {
368132240Stjr			if (isdigit((unsigned char)*p)) {
36948995Ssheldonh				numbers++;
37048995Ssheldonh				p++;
37148995Ssheldonh			} else if ((*p == '#' && !(numbers|dot|sign|space|
37248995Ssheldonh			    hash++)) ||
37348995Ssheldonh			    (*p == ' ' && !(numbers|dot|space++)) ||
37448995Ssheldonh			    ((*p == '+' || *p == '-') && !(numbers|dot|sign++))
37548995Ssheldonh			    || (*p == '.' && !(dot++)))
37648995Ssheldonh				p++;
37748995Ssheldonh			else
37855515Ssheldonh				goto fmt_broken;
37948995Ssheldonh		}
38055515Ssheldonh		if (*p == 'l') {
38155515Ssheldonh			longdata = 1;
38255515Ssheldonh			if (*++p == 'l') {
38355515Ssheldonh				if (p[1] != '\0')
38455515Ssheldonh					p++;
38555515Ssheldonh				goto fmt_broken;
38655515Ssheldonh			}
38755515Ssheldonh		}
38848995Ssheldonh		switch (*p) {
38948995Ssheldonh		case 'o': case 'u': case 'x': case 'X':
39048995Ssheldonh			intdata = nosign = 1;
3911590Srgrimes			break;
39248995Ssheldonh		case 'd': case 'i':
39348995Ssheldonh			intdata = 1;
39448995Ssheldonh			break;
39548995Ssheldonh		case 'D':
39655515Ssheldonh			if (!longdata) {
39748995Ssheldonh				intdata = 1;
39848995Ssheldonh				break;
39948995Ssheldonh			}
40048995Ssheldonh		case 'O': case 'U':
40155515Ssheldonh			if (!longdata) {
40248995Ssheldonh				intdata = nosign = 1;
40348995Ssheldonh				break;
40448995Ssheldonh			}
40548995Ssheldonh		case 'c':
40655515Ssheldonh			if (!(intdata | longdata)) {
40748995Ssheldonh				chardata = 1;
40848995Ssheldonh				break;
40948995Ssheldonh			}
41055515Ssheldonh		case 'h': case 'n': case 'p': case 'q': case 's': case 'L':
41148995Ssheldonh		case '$': case '*':
41255515Ssheldonh			goto fmt_broken;
41348995Ssheldonh		case 'f': case 'e': case 'g': case 'E': case 'G':
41455515Ssheldonh			if (!longdata)
41548995Ssheldonh				break;
41648995Ssheldonh			/* FALLTHROUGH */
4171590Srgrimes		default:
41855515Ssheldonhfmt_broken:
41948995Ssheldonh			*++p = '\0';
42077287Sdd			errx(1, "illegal or unsupported format '%s'", p2);
42148995Ssheldonh			/* NOTREACHED */
4221590Srgrimes		}
42348995Ssheldonh		while (*++p)
42448995Ssheldonh			if (*p == '%' && *(p+1) && *(p+1) != '%')
42548995Ssheldonh				errx(1, "too many conversions");
42648995Ssheldonh			else if (*p == '%' && *(p+1) == '%')
42748995Ssheldonh				p++;
42848995Ssheldonh			else if (*p == '%' && !*(p+1)) {
42948995Ssheldonh				strcat(format, "%");
43048995Ssheldonh				break;
43148995Ssheldonh			}
4321590Srgrimes	}
4331590Srgrimes}
434