From cb8c274b68edb6c497032f6e02c7e7e16d0c4d21 Mon Sep 17 00:00:00 2001 From: cheloha Date: Tue, 25 Feb 2020 15:46:15 +0000 Subject: [PATCH] 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. --- bin/sleep/sleep.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) 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); }