sleep.c revision 210696
1139969Simp/*-
21556Srgrimes * Copyright (c) 1988, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
30114433Sobrien#if 0
311556Srgrimes#ifndef lint
3220423Sstevestatic char const copyright[] =
331556Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\
341556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351556Srgrimes#endif /* not lint */
361556Srgrimes
371556Srgrimes#ifndef lint
3836152Scharnierstatic char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
39114433Sobrien#endif /* not lint */
4036152Scharnier#endif
4199110Sobrien#include <sys/cdefs.h>
4299110Sobrien__FBSDID("$FreeBSD: head/bin/sleep/sleep.c 210696 2010-07-31 17:41:58Z kib $");
431556Srgrimes
4451835Sru#include <ctype.h>
45210679Skib#include <err.h>
4651835Sru#include <limits.h>
47210679Skib#include <signal.h>
48210696Skib#include <stdint.h>
49114575Smarkm#include <stdio.h>
501556Srgrimes#include <stdlib.h>
5151835Sru#include <time.h>
521556Srgrimes#include <unistd.h>
531556Srgrimes
54210679Skibstatic void usage(void);
551556Srgrimes
56210679Skibstatic volatile sig_atomic_t report_requested;
57210679Skibstatic void
58210679Skibreport_request(int signo __unused)
59210679Skib{
60210679Skib
61210679Skib	report_requested = 1;
62210679Skib}
63210679Skib
641556Srgrimesint
6590111Simpmain(int argc, char *argv[])
661556Srgrimes{
6751835Sru	struct timespec time_to_sleep;
68210679Skib	long l, original;
69106883Snjl	int neg;
7051835Sru	char *p;
711556Srgrimes
72210679Skib	if (argc != 2)
731556Srgrimes		usage();
741556Srgrimes
75106883Snjl	p = argv[1];
7651835Sru
7751835Sru	/* Skip over leading whitespaces. */
78106882Snjl	while (isspace((unsigned char)*p))
7951835Sru		++p;
8051835Sru
81106882Snjl	/* Check for optional `+' or `-' sign. */
82106882Snjl	neg = 0;
83106882Snjl	if (*p == '-') {
84106882Snjl		neg = 1;
85106882Snjl		++p;
86210679Skib		if (!isdigit((unsigned char)*p) && *p != '.')
87106883Snjl			usage();
8851835Sru	}
89106882Snjl	else if (*p == '+')
90106882Snjl		++p;
9151835Sru
9251835Sru	/* Calculate seconds. */
93106882Snjl	if (isdigit((unsigned char)*p)) {
94106882Snjl		l = strtol(p, &p, 10);
95210679Skib
96210679Skib		/*
97210679Skib		 * Avoid overflow when `seconds' is huge.  This assumes
98210679Skib		 * that the maximum value for a time_t is <= INT_MAX.
99210679Skib		 */
100210679Skib		if (l > INT_MAX)
10151835Sru			l = INT_MAX;
102106882Snjl	} else
103106882Snjl		l = 0;
10451835Sru	time_to_sleep.tv_sec = (time_t)l;
10551835Sru
10651835Sru	/* Calculate nanoseconds. */
10751835Sru	time_to_sleep.tv_nsec = 0;
10851835Sru
10951835Sru	if (*p == '.') {		/* Decimal point. */
11051835Sru		l = 100000000L;
11151835Sru		do {
112106882Snjl			if (isdigit((unsigned char)*++p))
11351835Sru				time_to_sleep.tv_nsec += (*p - '0') * l;
11451835Sru			else
11551835Sru				break;
116114575Smarkm			l /= 10;
117114575Smarkm		} while (l);
11851835Sru	}
11951835Sru
120210679Skib	/* Skip over the trailing whitespace. */
121210679Skib	while (isspace((unsigned char)*p))
122210679Skib		++p;
123210679Skib	if (*p != '\0')
124210679Skib		usage();
12551835Sru
126210679Skib	signal(SIGINFO, report_request);
127210679Skib	if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) {
128210679Skib		original = time_to_sleep.tv_sec;
129210679Skib		while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) {
130210679Skib			if (report_requested) {
131210679Skib				/*
132210679Skib				 * Reporting does not bother with
133210679Skib				 * fractions of a second...
134210679Skib				 */
135210696Skib				warnx("about %jd second(s) left"
136210696Skib				    " out of the original %jd",
137210696Skib				    (intmax_t)time_to_sleep.tv_sec,
138210696Skib				    (intmax_t)original);
139210679Skib				report_requested = 0;
140210679Skib			} else
141210679Skib				break;
142210679Skib		}
143210679Skib	}
144210679Skib
145210679Skib	return (0);
1461556Srgrimes}
1471556Srgrimes
148210679Skibstatic void
14990111Simpusage(void)
1501556Srgrimes{
151210679Skib	static const char msg[] = "usage: sleep seconds\n";
1521556Srgrimes
153114575Smarkm	write(STDERR_FILENO, msg, sizeof(msg) - 1);
154210679Skib	exit(1);
1551556Srgrimes}
156