From d1cfa0acd0bb31b88b8e75a98c2bea12ef84a542 Mon Sep 17 00:00:00 2001 From: Zachary Storer Date: Mon, 24 Feb 2014 13:48:43 -0700 Subject: [PATCH] Import NetBSD sleep command Replaces the 'sleep' functionality provided by commands/sleep. Change-Id: I8758bf027b26aaef7954848803d6da982c753bfa --- bin/Makefile | 2 +- bin/sleep/Makefile | 8 ++ bin/sleep/sleep.1 | 123 ++++++++++++++++++++++++++++ bin/sleep/sleep.c | 159 ++++++++++++++++++++++++++++++++++++ commands/Makefile | 2 +- commands/sleep/Makefile | 4 - commands/sleep/sleep.c | 34 -------- distrib/sets/lists/minix/mi | 2 +- man/man1/Makefile | 2 +- man/man1/sleep.1 | 26 ------ 10 files changed, 294 insertions(+), 68 deletions(-) create mode 100644 bin/sleep/Makefile create mode 100644 bin/sleep/sleep.1 create mode 100644 bin/sleep/sleep.c delete mode 100644 commands/sleep/Makefile delete mode 100644 commands/sleep/sleep.c delete mode 100644 man/man1/sleep.1 diff --git a/bin/Makefile b/bin/Makefile index fd20021cd..05c0c1a2e 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -3,6 +3,6 @@ SUBDIR= cat date df echo ed expr \ kill ksh ln ls mkdir pax pwd rm rmdir \ - stty sync test + sleep stty sync test .include diff --git a/bin/sleep/Makefile b/bin/sleep/Makefile new file mode 100644 index 000000000..917f95f3d --- /dev/null +++ b/bin/sleep/Makefile @@ -0,0 +1,8 @@ +# $NetBSD: Makefile,v 1.9 1997/08/04 01:13:07 perry Exp $ +# @(#)Makefile 8.1 (Berkeley) 5/31/93 + +PROG= sleep +LDADD+= -lm +DPADD+= ${LIBM} + +.include diff --git a/bin/sleep/sleep.1 b/bin/sleep/sleep.1 new file mode 100644 index 000000000..ca7fe20fd --- /dev/null +++ b/bin/sleep/sleep.1 @@ -0,0 +1,123 @@ +.\" $NetBSD: sleep.1,v 1.22 2011/08/15 14:45:36 wiz Exp $ +.\" +.\" Copyright (c) 1990, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the Institute of Electrical and Electronics Engineers, Inc. +.\" +.\" 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. +.\" +.\" @(#)sleep.1 8.3 (Berkeley) 4/18/94 +.\" +.Dd August 13, 2011 +.Dt SLEEP 1 +.Os +.Sh NAME +.Nm sleep +.Nd suspend execution for an interval of time +.Sh SYNOPSIS +.Nm +.Ar seconds +.Sh DESCRIPTION +The +.Nm +utility +suspends execution for a minimum of +.Ar seconds . +It is usually used to schedule the execution of other commands (see +.Sx EXAMPLES +below). +.Pp +Note: The +.Nx +.Nm +command will accept and honor a non-integer number of specified seconds. +This is a non-portable extension, and its use will nearly guarantee that +a shell script will not execute properly on another system. +.Pp +When the +.Dv SIGINFO +signal is received, the estimate of the amount of seconds left to +sleep is printed on the standard output. +.Sh EXIT STATUS +The +.Nm +utility exits with one of the following values: +.Bl -tag -width flag +.It Li \&0 +On successful completion, or if the signal +.Dv SIGALRM +was received. +.It Li \&\*[Gt]\&0 +An error occurred. +.El +.Sh EXAMPLES +To schedule the execution of a command for 1800 seconds later: +.Pp +.Dl (sleep 1800; sh command_file \*[Gt]\*[Am] errors)\*[Am] +.Pp +This incantation would wait half an hour before +running the script command_file. +(See the +.Xr at 1 +utility.) +.Pp +To reiteratively run a command (with +.Xr csh 1 ) : +.Pp +.Bd -literal -offset indent -compact +while (1) + if (! -r zzz.rawdata) then + sleep 300 + else + foreach i (*.rawdata) + sleep 70 + awk -f collapse_data $i \*[Gt]\*[Gt] results + end + break + endif +end +.Ed +.Pp +The scenario for a script such as this might be: a program currently +running is taking longer than expected to process a series of +files, and it would be nice to have +another program start processing the files created by the first +program as soon as it is finished (when zzz.rawdata is created). +The script checks every five minutes for the file zzz.rawdata, +when the file is found, then another portion processing +is done courteously by sleeping for 70 seconds in between each +awk job. +.Sh SEE ALSO +.Xr at 1 , +.Xr nanosleep 2 , +.Xr sleep 3 +.Sh STANDARDS +The +.Nm +command is expected to be +.St -p1003.2 +compatible. diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c new file mode 100644 index 000000000..4349af439 --- /dev/null +++ b/bin/sleep/sleep.c @@ -0,0 +1,159 @@ +/* $NetBSD: sleep.c,v 1.24 2011/08/29 14:51:19 joerg Exp $ */ + +/* + * Copyright (c) 1988, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. + */ + +#include +#ifndef lint +__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\ + The Regents of the University of California. All rights reserved."); +#endif /* not lint */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; +#else +__RCSID("$NetBSD: sleep.c,v 1.24 2011/08/29 14:51:19 joerg Exp $"); +#endif +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +__dead static void alarmhandle(int); +__dead static void usage(void); + +static volatile sig_atomic_t report_requested; +static void +report_request(int signo __unused) +{ + + report_requested = 1; +} + +int +main(int argc, char *argv[]) +{ + char *arg, *temp; + double fval, ival, val; + struct timespec ntime; + time_t original; + int ch, fracflag, rv; + + setprogname(argv[0]); + (void)setlocale(LC_ALL, ""); + + (void)signal(SIGALRM, alarmhandle); + + while ((ch = getopt(argc, argv, "")) != -1) + switch(ch) { + default: + usage(); + } + argc -= optind; + argv += optind; + + if (argc != 1) + usage(); + + /* + * Okay, why not just use atof for everything? Why bother + * checking if there is a fraction in use? Because the old + * sleep handled the full range of integers, that's why, and a + * double can't handle a large long. This is fairly useless + * given how large a number a double can hold on most + * machines, but now we won't ever have trouble. If you want + * 1000000000.9 seconds of sleep, well, that's your + * problem. Why use an isdigit() check instead of checking for + * a period? Because doing it this way means locales will be + * handled transparently by the atof code. + */ + fracflag = 0; + arg = *argv; + for (temp = arg; *temp != '\0'; temp++) + if (!isdigit((unsigned char)*temp)) + fracflag++; + + if (fracflag) { + val = atof(arg); + if (val <= 0) + usage(); + ival = floor(val); + fval = (1000000000 * (val-ival)); + ntime.tv_sec = ival; + ntime.tv_nsec = fval; + } + else { + ntime.tv_sec = atol(arg); + if (ntime.tv_sec <= 0) + return EXIT_SUCCESS; + ntime.tv_nsec = 0; + } + + original = ntime.tv_sec; + signal(SIGINFO, report_request); + while ((rv = nanosleep(&ntime, &ntime)) != 0) { + if (report_requested) { + /* Reporting does not bother with nanoseconds. */ + warnx("about %d second(s) left out of the original %d", + (int)ntime.tv_sec, (int)original); + report_requested = 0; + } else + break; + } + + if (rv == -1) + err(EXIT_FAILURE, "nanosleep failed"); + + return EXIT_SUCCESS; + /* NOTREACHED */ +} + +static void +usage(void) +{ + (void)fprintf(stderr, "usage: %s seconds\n", getprogname()); + exit(EXIT_FAILURE); + /* NOTREACHED */ +} + +/* ARGSUSED */ +static void +alarmhandle(int i) +{ + _exit(EXIT_SUCCESS); + /* NOTREACHED */ +} diff --git a/commands/Makefile b/commands/Makefile index 99de1e54b..05bf989eb 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -23,7 +23,7 @@ SUBDIR= add_route arp ash at backup btrace \ ramdisk rarpd rawspeed rcp readclock \ remsync rget rlogin \ rotate rsh rshd service setup shar \ - sleep slip spell sprofalyze sprofdiff srccrc \ + slip spell sprofalyze sprofdiff srccrc \ svclog svrctl swifi synctree sysenv \ syslogd tail tcpd tcpdp tcpstat telnet \ telnetd term termcap tget time touch \ diff --git a/commands/sleep/Makefile b/commands/sleep/Makefile deleted file mode 100644 index fdbf1267f..000000000 --- a/commands/sleep/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -PROG= sleep -MAN= - -.include diff --git a/commands/sleep/sleep.c b/commands/sleep/sleep.c deleted file mode 100644 index b168de839..000000000 --- a/commands/sleep/sleep.c +++ /dev/null @@ -1,34 +0,0 @@ -/* sleep - suspend a process for x sec Author: Andy Tanenbaum */ - -#include -#include -#include -#include - -int main(int argc, char **argv); - -int main(argc, argv) -int argc; -char *argv[]; -{ - register int seconds; - register char c; - - seconds = 0; - - if (argc != 2) { - std_err("Usage: sleep time\n"); - exit(1); - } - while ((c = *(argv[1])++)) { - if (c < '0' || c > '9') { - std_err("sleep: bad arg\n"); - exit(1); - } - seconds = 10 * seconds + (c - '0'); - } - - /* Now sleep. */ - sleep(seconds); - return(0); -} diff --git a/distrib/sets/lists/minix/mi b/distrib/sets/lists/minix/mi index b5474148b..7ed6c8657 100644 --- a/distrib/sets/lists/minix/mi +++ b/distrib/sets/lists/minix/mi @@ -44,6 +44,7 @@ ./bin/service minix-sys ./bin/setup minix-sys ./bin/sh minix-sys +./bin/sleep minix-sys ./bin/stty minix-sys ./bin/sync minix-sys ./bin/sysenv minix-sys @@ -453,7 +454,6 @@ ./usr/bin/shlock minix-sys ./usr/bin/shuffle minix-sys ./usr/bin/size minix-sys binutils -./usr/bin/sleep minix-sys ./usr/bin/slip minix-sys ./usr/bin/soelim minix-sys ./usr/bin/sort minix-sys diff --git a/man/man1/Makefile b/man/man1/Makefile index 394021eb9..155f2f830 100644 --- a/man/man1/Makefile +++ b/man/man1/Makefile @@ -15,7 +15,7 @@ MAN= ash.1 at.1 \ ping.1 playwave.1 prep.1 \ profile.1 ps.1 rcp.1 recwave.1 \ remsync.1 rget.1 rlogin.1 rsh.1 rz.1 \ - shar.1 sleep.1 spell.1 \ + shar.1 spell.1 \ svc.1 svrctl.1 \ synctree.1 sysenv.1 sz.1 tail.1 telnet.1 template.1 \ term.1 termcap.1 tget.1 time.1 \ diff --git a/man/man1/sleep.1 b/man/man1/sleep.1 deleted file mode 100644 index e4b1afeaa..000000000 --- a/man/man1/sleep.1 +++ /dev/null @@ -1,26 +0,0 @@ -.TH SLEEP 1 -.SH NAME -sleep \- suspend execution for a given number of seconds -.SH SYNOPSIS -\fBsleep \fIseconds\fR -.br -.de FL -.TP -\\fB\\$1\\fR -\\$2 -.. -.de EX -.TP 20 -\\fB\\$1\\fR -# \\$2 -.. -.SH EXAMPLES -.TP 20 -.B sleep 10 -# Suspend execution for 10 sec. -.SH DESCRIPTION -.PP -The caller is suspended for the indicated number of seconds. -This command is typically used in shell scripts. -.SH "SEE ALSO" -.BR sleep (3). -- 2.44.0