Import NetBSD sleep command

Replaces the 'sleep' functionality provided by commands/sleep.

Change-Id: I8758bf027b26aaef7954848803d6da982c753bfa
This commit is contained in:
Zachary Storer 2014-02-24 13:48:43 -07:00 committed by Lionel Sambuc
parent e54d075f6f
commit d1cfa0acd0
10 changed files with 294 additions and 68 deletions

View file

@ -3,6 +3,6 @@
SUBDIR= cat date df echo ed expr \ SUBDIR= cat date df echo ed expr \
kill ksh ln ls mkdir pax pwd rm rmdir \ kill ksh ln ls mkdir pax pwd rm rmdir \
stty sync test sleep stty sync test
.include <bsd.subdir.mk> .include <bsd.subdir.mk>

8
bin/sleep/Makefile Normal file
View file

@ -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 <bsd.prog.mk>

123
bin/sleep/sleep.1 Normal file
View file

@ -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.

159
bin/sleep/sleep.c Normal file
View file

@ -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 <sys/cdefs.h>
#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 <ctype.h>
#include <err.h>
#include <locale.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
__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 */
}

View file

@ -23,7 +23,7 @@ SUBDIR= add_route arp ash at backup btrace \
ramdisk rarpd rawspeed rcp readclock \ ramdisk rarpd rawspeed rcp readclock \
remsync rget rlogin \ remsync rget rlogin \
rotate rsh rshd service setup shar \ rotate rsh rshd service setup shar \
sleep slip spell sprofalyze sprofdiff srccrc \ slip spell sprofalyze sprofdiff srccrc \
svclog svrctl swifi synctree sysenv \ svclog svrctl swifi synctree sysenv \
syslogd tail tcpd tcpdp tcpstat telnet \ syslogd tail tcpd tcpdp tcpstat telnet \
telnetd term termcap tget time touch \ telnetd term termcap tget time touch \

View file

@ -1,4 +0,0 @@
PROG= sleep
MAN=
.include <bsd.prog.mk>

View file

@ -1,34 +0,0 @@
/* sleep - suspend a process for x sec Author: Andy Tanenbaum */
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <minix/minlib.h>
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);
}

View file

@ -44,6 +44,7 @@
./bin/service minix-sys ./bin/service minix-sys
./bin/setup minix-sys ./bin/setup minix-sys
./bin/sh minix-sys ./bin/sh minix-sys
./bin/sleep minix-sys
./bin/stty minix-sys ./bin/stty minix-sys
./bin/sync minix-sys ./bin/sync minix-sys
./bin/sysenv minix-sys ./bin/sysenv minix-sys
@ -453,7 +454,6 @@
./usr/bin/shlock minix-sys ./usr/bin/shlock minix-sys
./usr/bin/shuffle minix-sys ./usr/bin/shuffle minix-sys
./usr/bin/size minix-sys binutils ./usr/bin/size minix-sys binutils
./usr/bin/sleep minix-sys
./usr/bin/slip minix-sys ./usr/bin/slip minix-sys
./usr/bin/soelim minix-sys ./usr/bin/soelim minix-sys
./usr/bin/sort minix-sys ./usr/bin/sort minix-sys

View file

@ -15,7 +15,7 @@ MAN= ash.1 at.1 \
ping.1 playwave.1 prep.1 \ ping.1 playwave.1 prep.1 \
profile.1 ps.1 rcp.1 recwave.1 \ profile.1 ps.1 rcp.1 recwave.1 \
remsync.1 rget.1 rlogin.1 rsh.1 rz.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 \ svc.1 svrctl.1 \
synctree.1 sysenv.1 sz.1 tail.1 telnet.1 template.1 \ synctree.1 sysenv.1 sz.1 tail.1 telnet.1 template.1 \
term.1 termcap.1 tget.1 time.1 \ term.1 termcap.1 tget.1 time.1 \

View file

@ -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).