diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c index 6d305c6aa58..a2a86dcc5f8 100644 --- a/bin/sleep/sleep.c +++ b/bin/sleep/sleep.c @@ -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 $ */ /* @@ -33,37 +33,35 @@ #include #include +#include #include #include #include #include #include -#include -extern char *__progname; - -void usage(void); void alarmh(int); +void usage(void); int main(int argc, char *argv[]) { - int ch; + struct timespec rqtp; time_t t; char *cp; - struct timespec rqtp; - int i; + int ch, i; if (pledge("stdio", NULL) == -1) err(1, "pledge"); signal(SIGALRM, alarmh); - while ((ch = getopt(argc, argv, "")) != -1) + while ((ch = getopt(argc, argv, "")) != -1) { switch(ch) { default: usage(); } + } argc -= optind; argv += optind; @@ -73,12 +71,12 @@ main(int argc, char *argv[]) timespecclear(&rqtp); /* Handle whole seconds. */ - for (cp = *argv; *cp != '\0' && *cp != '.'; cp++) { + for (cp = argv[0]; *cp != '\0' && *cp != '.'; 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'); 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; } @@ -91,7 +89,7 @@ main(int argc, char *argv[]) i = 100000000; for (cp++; *cp != '\0'; 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; i /= 10; } @@ -99,30 +97,29 @@ main(int argc, char *argv[]) if (timespecisset(&rqtp)) { if (nanosleep(&rqtp, NULL) == -1) - err(1, NULL); + err(1, "nanosleep"); } - return (0); + return 0; } void usage(void) { - (void)fprintf(stderr, "usage: %s seconds\n", __progname); + fprintf(stderr, "usage: %s seconds\n", getprogname()); 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. */ -/* ARGSUSED */ void alarmh(int signo) { /* - * exit() flushes stdio buffers, which is not legal in a signal - * handler. Use _exit(). + * Always _exit(2) from signal handlers: exit(3) is not + * generally signal-safe. */ _exit(0); }