mirror of
https://github.com/openbsd/src.git
synced 2025-01-03 06:45:37 -08:00
sleep(1): style(9) and other dusting
- Sort includes alphabetically - Sort prototypes alphabetically - Sort stack variables by size - Add missing braces to the getopt(3) loop - Be explicit: there is *one* argument, so use argv[0], not *argv - If nanosleep(2) somehow fails, say that "nanosleep" failed when we err(3) - Remove extra parentheses from the return statement - De-(void) the obvious fprintf(3) in usage() - __progname -> getprogname(3) - POSIX 1003.2 has long since become POSIX.1 - Remove an ARGUSED linter comment - stdio(3) flushing is not the only potential issue with an exit(3) from a signal handler. Just note that exit(3) isn't safe and leave it at that.
This commit is contained in:
parent
603a52d71c
commit
cb8c274b68
@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: sleep.c,v 1.28 2019/07/01 00:01:34 cheloha Exp $ */
|
/* $OpenBSD: sleep.c,v 1.29 2020/02/25 15:46:15 cheloha Exp $ */
|
||||||
/* $NetBSD: sleep.c,v 1.8 1995/03/21 09:11:11 cgd Exp $ */
|
/* $NetBSD: sleep.c,v 1.8 1995/03/21 09:11:11 cgd Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -33,37 +33,35 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <err.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <err.h>
|
|
||||||
|
|
||||||
extern char *__progname;
|
|
||||||
|
|
||||||
void usage(void);
|
|
||||||
void alarmh(int);
|
void alarmh(int);
|
||||||
|
void usage(void);
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ch;
|
struct timespec rqtp;
|
||||||
time_t t;
|
time_t t;
|
||||||
char *cp;
|
char *cp;
|
||||||
struct timespec rqtp;
|
int ch, i;
|
||||||
int i;
|
|
||||||
|
|
||||||
if (pledge("stdio", NULL) == -1)
|
if (pledge("stdio", NULL) == -1)
|
||||||
err(1, "pledge");
|
err(1, "pledge");
|
||||||
|
|
||||||
signal(SIGALRM, alarmh);
|
signal(SIGALRM, alarmh);
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "")) != -1)
|
while ((ch = getopt(argc, argv, "")) != -1) {
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
@ -73,12 +71,12 @@ main(int argc, char *argv[])
|
|||||||
timespecclear(&rqtp);
|
timespecclear(&rqtp);
|
||||||
|
|
||||||
/* Handle whole seconds. */
|
/* Handle whole seconds. */
|
||||||
for (cp = *argv; *cp != '\0' && *cp != '.'; cp++) {
|
for (cp = argv[0]; *cp != '\0' && *cp != '.'; cp++) {
|
||||||
if (!isdigit((unsigned char)*cp))
|
if (!isdigit((unsigned char)*cp))
|
||||||
errx(1, "seconds is invalid: %s", *argv);
|
errx(1, "seconds is invalid: %s", argv[0]);
|
||||||
t = (rqtp.tv_sec * 10) + (*cp - '0');
|
t = (rqtp.tv_sec * 10) + (*cp - '0');
|
||||||
if (t / 10 != rqtp.tv_sec) /* overflow */
|
if (t / 10 != rqtp.tv_sec) /* overflow */
|
||||||
errx(1, "seconds is too large: %s", *argv);
|
errx(1, "seconds is too large: %s", argv[0]);
|
||||||
rqtp.tv_sec = t;
|
rqtp.tv_sec = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +89,7 @@ main(int argc, char *argv[])
|
|||||||
i = 100000000;
|
i = 100000000;
|
||||||
for (cp++; *cp != '\0'; cp++) {
|
for (cp++; *cp != '\0'; cp++) {
|
||||||
if (!isdigit((unsigned char)*cp))
|
if (!isdigit((unsigned char)*cp))
|
||||||
errx(1, "seconds is invalid: %s", *argv);
|
errx(1, "seconds is invalid: %s", argv[0]);
|
||||||
rqtp.tv_nsec += (*cp - '0') * i;
|
rqtp.tv_nsec += (*cp - '0') * i;
|
||||||
i /= 10;
|
i /= 10;
|
||||||
}
|
}
|
||||||
@ -99,30 +97,29 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (timespecisset(&rqtp)) {
|
if (timespecisset(&rqtp)) {
|
||||||
if (nanosleep(&rqtp, NULL) == -1)
|
if (nanosleep(&rqtp, NULL) == -1)
|
||||||
err(1, NULL);
|
err(1, "nanosleep");
|
||||||
}
|
}
|
||||||
|
|
||||||
return (0);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
(void)fprintf(stderr, "usage: %s seconds\n", __progname);
|
fprintf(stderr, "usage: %s seconds\n", getprogname());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* POSIX 1003.2 says sleep should exit with 0 return code on reception
|
* POSIX.1 says sleep(1) may exit with status zero upon receipt
|
||||||
* of SIGALRM.
|
* of SIGALRM.
|
||||||
*/
|
*/
|
||||||
/* ARGSUSED */
|
|
||||||
void
|
void
|
||||||
alarmh(int signo)
|
alarmh(int signo)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* exit() flushes stdio buffers, which is not legal in a signal
|
* Always _exit(2) from signal handlers: exit(3) is not
|
||||||
* handler. Use _exit().
|
* generally signal-safe.
|
||||||
*/
|
*/
|
||||||
_exit(0);
|
_exit(0);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user