[Midnightbsd-cvs] src: usr.bin/progress: Add progress(1) from NetBSD.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Tue Sep 2 01:29:40 EDT 2008


Log Message:
-----------
Add progress(1) from NetBSD.

progress(1) is a standalone progress bar similar to the one in lukemftp.  It is suitable for measu
ring input to arbitrary pipes.

You can use this to create progress bars in scripts, etc.

Added Files:
-----------
    src/usr.bin/progress:
        Makefile (r1.1)
        progress.1 (r1.1)
        progress.c (r1.1)
        strsuftoll.c (r1.1)

-------------- next part --------------
--- /dev/null
+++ usr.bin/progress/progress.1
@@ -0,0 +1,150 @@
+.\" $MidnightBSD: src/usr.bin/progress/progress.1,v 1.1 2008/09/02 05:29:39 laffer1 Exp $
+.\"	$NetBSD: progress.1,v 1.14 2008/04/30 13:11:01 martin Exp $
+.\"
+.\" Copyright (c) 2003-2007 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by John Hawkinson.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd June 6, 2007
+.Dt PROGRESS 1
+.Os
+.Sh NAME
+.Nm progress
+.Nd feed input to a command, displaying a progress bar
+.Sh SYNOPSIS
+.Nm
+.Op Fl ez
+.Op Fl b Ar buffersize
+.Op Fl f Ar file
+.Op Fl l Ar length
+.Op Fl p Ar prefix
+.Ar cmd
+.Op Ar args ...
+.Sh DESCRIPTION
+The
+.Nm
+utility opens a pipe to
+.Ar cmd
+and feeds an input stream into it, while displaying a progress bar to
+standard output.
+If no filename is specified,
+.Nm
+reads from standard input.
+Where feasible,
+.Nm
+.Xr fstat 2 Ns s
+the input to determine the length, so a time estimate can be calculated.
+.Pp
+If no length is specified or determined,
+.Nm
+simply displays a count of the data and the data rate.
+.Pp
+The options are as follows:
+.Bl -tag -width XlXlengthXX
+.It Fl b Ar buffersize
+Read in buffers of the specified size (default 64k).
+An optional suffix (per
+.Xr strsuftoll 3 )
+may be given.
+.It Fl e
+Display progress to standard error instead of standard output.
+.It Fl f Ar file
+Read from the specified
+.Ar file
+instead of standard input.
+.It Fl l Ar length
+Use the specified length for the time estimate, rather than attempting to
+.Xr fstat 2
+the input.
+An optional suffix (per
+.Xr strsuftoll 3 )
+may be given.
+.It Fl p Ar prefix
+Print the given
+.Dq prefix
+text before (left of) the progress bar.
+.It Fl z
+Filter the input through
+.Xr gunzip 1 .
+If
+.Fl f
+is specified, calculate the length using
+.Ic gzip -l .
+.El
+.Sh EXIT STATUS
+.Nm
+exits 0 on success.
+.Sh EXAMPLES
+The command
+.Dl progress -zf file.tar.gz tar xf -
+will extract the
+.Pa file.tar.gz
+displaying the progress bar as time passes:
+.Bd -literal
+  0% |                               |     0        0.00 KiB/s    --:-- ETA
+ 40% |********                       |   273 KiB  271.95 KiB/s    00:01 ETA
+ 81% |***********************        |   553 KiB  274.61 KiB/s    00:00 ETA
+100% |*******************************|   680 KiB  264.59 KiB/s    00:00 ETA
+.Ed
+.Pp
+If it is preferred to monitor the progress of the decompression
+process (unlikely), then
+.Dl progress -f file.tar.gz tar zxf -
+could be used.
+.Pp
+The command
+.Dl dd if=/dev/rwd0d ibs=64k | \e
+.Dl progress -l 120g dd of=/dev/rwd1d obs=64k
+will copy the 120 GiB disk
+.Sy wd0
+.Pa ( /dev/rwd0d )
+to
+.Sy wd1
+.Pa ( /dev/rwd1d ) ,
+displaying a progress bar during the operation.
+.Sh SEE ALSO
+.Xr ftp 1 ,
+.Xr strsuftoll 3
+.Sh HISTORY
+.Nm
+first appeared in
+.Nx 1.6.1 and
+.Mx 0.3 .
+The dynamic progress bar display code is part of
+.Xr ftp 1 .
+.Sh AUTHORS
+.Nm
+was written by
+.An John Hawkinson
+.Aq jhawk at NetBSD.org .
+.Xr ftp 1 Ns 's
+dynamic progress bar was written by Luke Mewburn.
+.Sh BUGS
+Since the progress bar is displayed asynchronously, it may be
+difficult to read some error messages, both those produced by the
+pipeline, as well as those produced by
+.Nm
+itself.
--- /dev/null
+++ usr.bin/progress/progress.c
@@ -0,0 +1,287 @@
+/* $MidnightBSD: src/usr.bin/progress/progress.c,v 1.1 2008/09/02 05:29:39 laffer1 Exp $ */
+/*	$NetBSD: progress.c,v 1.17 2008/05/26 04:53:11 dholland Exp $ */
+
+/*-
+ * Copyright (c) 2003 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by John Hawkinson.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: progress.c,v 1.17 2008/05/26 04:53:11 dholland Exp $");
+#endif				/* not lint */
+__MBSDID("$MidnightBSD: src/usr.bin/progress/progress.c,v 1.1 2008/09/02 05:29:39 laffer1 Exp $");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <netinet/in.h>
+#include <arpa/ftp.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <glob.h>
+#include <signal.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <time.h>
+#include "/usr/src/lib/libc/stdtime/tzfile.h"
+#include <unistd.h>
+
+#define GLOBAL			/* force GLOBAL decls in progressbar.h to be
+				 * declared */
+#include "progressbar.h"
+
+static void broken_pipe(int unused);
+static void usage(void);
+int main(int, char *[]);
+
+static void
+broken_pipe(int unused)
+{
+	signal(SIGPIPE, SIG_DFL);
+	progressmeter(1);
+	kill(getpid(), SIGPIPE);
+}
+
+static void
+usage(void)
+{
+	fprintf(stderr,
+	    "usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n"
+	    "       %*.s [-p prefix] cmd [args...]\n",
+	    getprogname(), (int) strlen(getprogname()), "");
+	exit(EXIT_FAILURE);
+}
+
+
+int
+main(int argc, char *argv[])
+{
+	char *fb_buf;
+	char *infile = NULL;
+	pid_t pid = 0, gzippid = 0, deadpid;
+	int ch, fd, outpipe[2];
+	int ws, gzipstat, cmdstat;
+	int eflag = 0, lflag = 0, zflag = 0;
+	ssize_t nr, nw, off;
+	size_t buffersize;
+	struct stat statb;
+	struct ttysize ts;
+
+	setprogname(argv[0]);
+
+	/* defaults: Read from stdin, 0 filesize (no completion estimate) */
+	fd = STDIN_FILENO;
+	filesize = 0;
+	buffersize = 64 * 1024;
+	prefix = NULL;
+
+	while ((ch = getopt(argc, argv, "b:ef:l:p:z")) != -1)
+		switch (ch) {
+		case 'b':
+			buffersize = (size_t) strsuftoll("buffer size", optarg,
+			    0, SIZE_T_MAX);
+			break;
+		case 'e':
+			eflag++;
+			break;
+		case 'f':
+			infile = optarg;
+			break;
+		case 'l':
+			lflag++;
+			filesize = strsuftoll("input size", optarg, 0,
+			    LLONG_MAX);
+			break;
+		case 'p':
+			prefix = optarg;
+			break;
+		case 'z':
+			zflag++;
+			break;
+		case '?':
+		default:
+			usage();
+			/* NOTREACHED */
+		}
+	argc -= optind;
+	argv += optind;
+
+	if (argc < 1)
+		usage();
+
+	if (infile && (fd = open(infile, O_RDONLY, 0)) < 0)
+		err(1, "%s", infile);
+
+	/* stat() to get the filesize unless overridden, or -z */
+	if (!zflag && !lflag && (fstat(fd, &statb) == 0)) {
+		if (S_ISFIFO(statb.st_mode)) {
+			/* stat(2) on pipe may return only the
+			 * first few bytes with more coming.
+			 * Don't trust!
+			 */
+		} else {
+			filesize = statb.st_size;
+		}
+	}
+
+	/* gzip -l the file if we have the name and -z is given */
+	if (zflag && !lflag && infile != NULL) {
+		FILE *gzipsizepipe;
+		char buf[256], *cp, *cmd;
+
+		/*
+		 * Read second word of last line of gzip -l output. Looks like:
+		 * % gzip -l ../etc.tgz 
+		 *   compressed uncompressed  ratio uncompressed_name
+		 * 	 119737       696320  82.8% ../etc.tar
+		 */
+
+		asprintf(&cmd, "gzip -l %s", infile);
+		if ((gzipsizepipe = popen(cmd, "r")) == NULL)
+			err(1, "reading compressed file length");
+		for (; fgets(buf, 256, gzipsizepipe) != NULL;)
+		    continue;
+		strtoimax(buf, &cp, 10);
+		filesize = strtoimax(cp, NULL, 10);
+		if (pclose(gzipsizepipe) < 0)
+			err(1, "closing compressed file length pipe");
+		free(cmd);
+	}
+	/* Pipe input through gzip -dc if -z is given */
+	if (zflag) {
+		int gzippipe[2];
+
+		if (pipe(gzippipe) < 0)
+			err(1, "gzip pipe");
+		gzippid = fork();
+		if (gzippid < 0)
+			err(1, "fork for gzip");
+
+		if (gzippid) {
+			/* parent */
+			dup2(gzippipe[0], fd);
+			close(gzippipe[0]);
+			close(gzippipe[1]);
+		} else {
+			dup2(gzippipe[1], STDOUT_FILENO);
+			dup2(fd, STDIN_FILENO);
+			close(gzippipe[0]);
+			close(gzippipe[1]);
+			if (execlp("gzip", "gzip", "-dc", NULL))
+				err(1, "exec()ing gzip");
+		}
+	}
+
+	/* Initialize progressbar.c's global state */
+	bytes = 0;
+	progress = 1;
+	ttyout = eflag ? stderr : stdout;
+
+	if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1)
+		ttywidth = 80;
+	else
+		ttywidth = ts.ts_cols;
+
+	fb_buf = malloc(buffersize);
+	if (fb_buf == NULL)
+		err(1, "malloc for buffersize");
+
+	if (pipe(outpipe) < 0)
+		err(1, "output pipe");
+	pid = fork();
+	if (pid < 0)
+		err(1, "fork for output pipe");
+
+	if (pid == 0) {
+		/* child */
+		dup2(outpipe[0], STDIN_FILENO);
+		close(outpipe[0]);
+		close(outpipe[1]);
+		execvp(argv[0], argv);
+		err(1, "could not exec %s", argv[0]);
+	}
+	close(outpipe[0]);
+
+	signal(SIGPIPE, broken_pipe);
+	progressmeter(-1);
+
+	while ((nr = read(fd, fb_buf, buffersize)) > 0)
+		for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
+			if ((nw = write(outpipe[1], fb_buf + off,
+			    (size_t) nr)) < 0) {
+				progressmeter(1);
+				err(1, "writing %u bytes to output pipe",
+							(unsigned) nr);
+			}
+	close(outpipe[1]);
+
+	gzipstat = 0;
+	cmdstat = 0;
+	while (pid || gzippid) {
+		deadpid = wait(&ws);
+		/*
+		 * We need to exit with an error if the command (or gzip)
+		 * exited abnormally.
+		 * Unfortunately we can't generate a true 'exited by signal'
+		 * error without sending the signal to ourselves :-(
+		 */
+		ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws);
+
+		if (deadpid != -1 && errno == EINTR)
+			continue;
+		if (deadpid == pid) {
+			pid = 0;
+			cmdstat = ws;
+			continue;
+		}
+		if (deadpid == gzippid) {
+			gzippid = 0;
+			gzipstat = ws;
+			continue;
+		}
+		break;
+	}
+
+	progressmeter(1);
+	signal(SIGPIPE, SIG_DFL);
+
+	free(fb_buf);
+
+	exit(cmdstat ? cmdstat : gzipstat);
+}
--- /dev/null
+++ usr.bin/progress/Makefile
@@ -0,0 +1,13 @@
+# $MidnightBSD: src/usr.bin/progress/Makefile,v 1.1 2008/09/02 05:29:39 laffer1 Exp $
+# $NetBSD: Makefile,v 1.2 2003/01/22 02:56:30 lukem Exp $
+
+.include <bsd.own.mk>
+
+PROG=	progress
+SRCS=	progress.c progressbar.c strsuftoll.c
+
+CFLAGS+=-I${.CURDIR}/../../contrib/lukemftp/src -DSTANDALONE_PROGRESS
+
+.PATH:  ${.CURDIR}/../../contrib/lukemftp/src
+
+.include <bsd.prog.mk>
--- /dev/null
+++ usr.bin/progress/strsuftoll.c
@@ -0,0 +1,245 @@
+/* $MidnightBSD: src/usr.bin/progress/strsuftoll.c,v 1.1 2008/09/02 05:29:39 laffer1 Exp $ */
+/*	$NetBSD: strsuftoll.c,v 1.8 2008/04/28 20:23:00 martin Exp $	*/
+/*-
+ * Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Keith Muller of the University of California, San Diego and Lance
+ * Visser of Convex Computer Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/cdefs.h>
+
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: strsuftoll.c,v 1.8 2008/04/28 20:23:00 martin Exp $");
+#endif /* LIBC_SCCS and not lint */
+
+#ifdef _LIBC
+#include "namespace.h"
+#endif
+
+#if !HAVE_STRSUFTOLL
+
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef _LIBC
+# ifdef __weak_alias
+__weak_alias(strsuftoll, _strsuftoll)
+__weak_alias(strsuftollx, _strsuftollx)
+# endif
+#endif /* LIBC */
+
+long long
+strsuftoll(const char *desc, const char *val,
+    long long min, long long max);
+long long
+strsuftollx(const char *desc, const char *val,
+    long long min, long long max, char *ebuf, size_t ebuflen);
+
+
+/*
+ * Convert an expression of the following forms to a (u)int64_t.
+ * 	1) A positive decimal number.
+ *	2) A positive decimal number followed by a b (mult by 512).
+ *	3) A positive decimal number followed by a k (mult by 1024).
+ *	4) A positive decimal number followed by a m (mult by 1048576).
+ *	5) A positive decimal number followed by a g (mult by 1073741824).
+ *	6) A positive decimal number followed by a t (mult by 1099511627776).
+ *	7) A positive decimal number followed by a w (mult by sizeof int)
+ *	8) Two or more positive decimal numbers (with/without k,b or w).
+ *	   separated by x (also * for backwards compatibility), specifying
+ *	   the product of the indicated values.
+ * Returns the result upon successful conversion, or exits with an
+ * appropriate error.
+ * 
+ */
+/* LONGLONG */
+long long
+strsuftoll(const char *desc, const char *val,
+    long long min, long long max)
+{
+	long long result;
+	char	errbuf[100];
+
+	result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
+	if (*errbuf != '\0')
+		errx(1, "%s", errbuf);
+	return (result);
+}
+
+/*
+ * As strsuftoll(), but returns the error message into the provided buffer
+ * rather than exiting with it.
+ */
+/* LONGLONG */
+long long
+strsuftollx(const char *desc, const char *val,
+    long long min, long long max, char *ebuf, size_t ebuflen)
+{
+	long long num, t;
+	char	*expr;
+
+	errno = 0;
+	ebuf[0] = '\0';
+
+	while (isspace((unsigned char)*val))	/* Skip leading space */
+		val++;
+
+	num = strtoll(val, &expr, 10);
+	if (errno == ERANGE)
+		goto erange;			/* Overflow */
+
+	if (expr == val)			/* No digits */
+		goto badnum;
+
+	switch (*expr) {
+	case 'b':
+		t = num;
+		num *= 512;			/* 1 block */
+		if (t > num)
+			goto erange;
+		++expr;
+		break;
+	case 'k':
+		t = num;
+		num *= 1024;			/* 1 kibibyte */
+		if (t > num)
+			goto erange;
+		++expr;
+		break;
+	case 'm':
+		t = num;
+		num *= 1048576;			/* 1 mebibyte */
+		if (t > num)
+			goto erange;
+		++expr;
+		break;
+	case 'g':
+		t = num;
+		num *= 1073741824;		/* 1 gibibyte */
+		if (t > num)
+			goto erange;
+		++expr;
+		break;
+	case 't':
+		t = num;
+		num *= 1099511627776LL;		/* 1 tebibyte */
+		if (t > num)
+			goto erange;
+		++expr;
+		break;
+	case 'w':
+		t = num;
+		num *= sizeof(int);		/* 1 word */
+		if (t > num)
+			goto erange;
+		++expr;
+		break;
+	}
+
+	switch (*expr) {
+	case '\0':
+		break;
+	case '*':				/* Backward compatible */
+	case 'x':
+		t = num;
+		num *= strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen);
+		if (*ebuf != '\0')
+			return (0);
+		if (t > num) {
+ erange:	 	
+			snprintf(ebuf, ebuflen,
+			    "%s: %s", desc, strerror(ERANGE));
+			return (0);
+		}
+		break;
+	default:
+ badnum:	snprintf(ebuf, ebuflen,
+		    "%s `%s': illegal number", desc, val);
+		return (0);
+	}
+	if (num < min) {
+			/* LONGLONG */
+		snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
+		    desc, (long long)num, (long long)min);
+		return (0);
+	}
+	if (num > max) {
+			/* LONGLONG */
+		snprintf(ebuf, ebuflen,
+		    "%s %lld is greater than %lld.",
+		    desc, (long long)num, (long long)max);
+		return (0);
+	}
+	*ebuf = '\0';
+	return (num);
+}
+
+#endif /* !HAVE_STRSUFTOLL */


More information about the Midnightbsd-cvs mailing list