A few changes to improve readability. Remove lots of casts. Casting printf is
just noise. Casting signal() is also uncommon in our tree. I kept a casts for
functions like write() where we would normally expect to check the error. (For
that matter, why do we ignore failure to write failedlogin?)

Also returning after open() returns -1 instead of wrapping the entire function
in an if drops us down a level of indentation.

Bonus fact: main() declares 40 variables. Maybe something can be done about
that too.

Index: failedlogin.c
===================================================================
RCS file: /cvs/src/usr.bin/login/failedlogin.c,v
retrieving revision 1.17
diff -u -p -r1.17 failedlogin.c
--- failedlogin.c       16 Jan 2015 06:40:09 -0000      1.17
+++ failedlogin.c       24 Jun 2016 01:18:41 -0000
@@ -55,30 +55,30 @@ log_failedlogin(uid_t uid, char *host, c
        int fd;
 
        /* Add O_CREAT if you want to create failedlogin if it doesn't exist */
-       if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, S_IRUSR|S_IWUSR)) >= 0) {
-               (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
-
-               /* Read in last bad login so can get the count */
-               if (read(fd, (char *)&failedlogin, sizeof(failedlogin)) !=
-                       sizeof(failedlogin) || failedlogin.bl_time == 0)
-                       memset((void *)&failedlogin, 0, sizeof(failedlogin));
-
-               (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
-               /* Increment count of bad logins */
-               ++failedlogin.count;
-               (void)time(&failedlogin.bl_time);
-               strncpy(failedlogin.bl_line, tty, sizeof(failedlogin.bl_line));
-               if (host)
-                       strncpy(failedlogin.bl_host, host, 
sizeof(failedlogin.bl_host));
-               else
-                       *failedlogin.bl_host = '\0';    /* NULL host field */
-               if (name)
-                       strncpy(failedlogin.bl_name, name, 
sizeof(failedlogin.bl_name));
-               else
-                       *failedlogin.bl_name = '\0';    /* NULL name field */
-               (void)write(fd, (char *)&failedlogin, sizeof(failedlogin));
-               (void)close(fd);
-       }
+       if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, S_IRUSR|S_IWUSR)) == -1)
+               return;
+       (void)lseek(fd, uid * sizeof(failedlogin), SEEK_SET);
+
+       /* Read in last bad login so can get the count */
+       if (read(fd, &failedlogin, sizeof(failedlogin)) !=
+           sizeof(failedlogin) || failedlogin.bl_time == 0)
+               memset(&failedlogin, 0, sizeof(failedlogin));
+
+       (void)lseek(fd, uid * sizeof(failedlogin), SEEK_SET);
+       /* Increment count of bad logins */
+       ++failedlogin.count;
+       time(&failedlogin.bl_time);
+       strncpy(failedlogin.bl_line, tty, sizeof(failedlogin.bl_line));
+       if (host)
+               strncpy(failedlogin.bl_host, host, sizeof(failedlogin.bl_host));
+       else
+               *failedlogin.bl_host = '\0';    /* NULL host field */
+       if (name)
+               strncpy(failedlogin.bl_name, name, sizeof(failedlogin.bl_name));
+       else
+               *failedlogin.bl_name = '\0';    /* NULL name field */
+       (void)write(fd, &failedlogin, sizeof(failedlogin));
+       close(fd);
 }
 
 /*
@@ -93,45 +93,44 @@ check_failedlogin(uid_t uid)
        struct badlogin failedlogin;
        int fd, was_bad = 0;
 
-       (void)memset((void *)&failedlogin, 0, sizeof(failedlogin));
+       memset(&failedlogin, 0, sizeof(failedlogin));
 
-       if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, 0)) >= 0) {
-               (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
-               if (read(fd, (char *)&failedlogin, sizeof(failedlogin)) ==
-                   sizeof(failedlogin) && failedlogin.count > 0 ) {
-                       /* There was a bad login */
-                       was_bad = 1;
-                       if (failedlogin.count > 1)
-                               (void)printf("There have been %lu unsuccessful "
-                                   "login attempts to your account.\n",
-                                   (u_long)failedlogin.count);
-                       (void)printf("Last unsuccessful login: %.*s", 24-5,
-                               (char *)ctime(&failedlogin.bl_time));
-                       (void)printf(" on %.*s",
-                           (int)sizeof(failedlogin.bl_line),
-                           failedlogin.bl_line);
-                       if (*failedlogin.bl_host != '\0') {
-                               if (*failedlogin.bl_name != '\0')
-                                       (void)printf(" from %.*s@%.*s",
-                                           (int)sizeof(failedlogin.bl_name),
-                                           failedlogin.bl_name,
-                                           (int)sizeof(failedlogin.bl_host),
-                                           failedlogin.bl_host);
-                               else
-                                       (void)printf(" from %.*s",
-                                           (int)sizeof(failedlogin.bl_host),
-                                           failedlogin.bl_host);
-                       }
-                       (void)putchar('\n');
-
-                       /* Reset since this is a good login and write record */
-                       failedlogin.count = 0;
-                       (void)lseek(fd, (off_t)uid * sizeof(failedlogin),
-                           SEEK_SET);
-                       (void)write(fd, (char *)&failedlogin,
-                           sizeof(failedlogin));
+       if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, 0)) == -1)
+               return was_bad;
+
+       (void)lseek(fd, uid * sizeof(failedlogin), SEEK_SET);
+       if (read(fd, &failedlogin, sizeof(failedlogin)) ==
+           sizeof(failedlogin) && failedlogin.count > 0 ) {
+               /* There was a bad login */
+               was_bad = 1;
+               if (failedlogin.count > 1)
+                       printf("There have been %lu unsuccessful "
+                           "login attempts to your account.\n",
+                           (u_long)failedlogin.count);
+               printf("Last unsuccessful login: %.*s", 24-5,
+                   ctime(&failedlogin.bl_time));
+               printf(" on %.*s",
+                   (int)sizeof(failedlogin.bl_line),
+                   failedlogin.bl_line);
+               if (*failedlogin.bl_host != '\0') {
+                       if (*failedlogin.bl_name != '\0')
+                               printf(" from %.*s@%.*s",
+                                   (int)sizeof(failedlogin.bl_name),
+                                   failedlogin.bl_name,
+                                   (int)sizeof(failedlogin.bl_host),
+                                   failedlogin.bl_host);
+                       else
+                               printf(" from %.*s",
+                                   (int)sizeof(failedlogin.bl_host),
+                                   failedlogin.bl_host);
                }
-               (void)close(fd);
+               putchar('\n');
+
+               /* Reset since this is a good login and write record */
+               failedlogin.count = 0;
+               (void)lseek(fd, uid * sizeof(failedlogin), SEEK_SET);
+               (void)write(fd, &failedlogin, sizeof(failedlogin));
        }
+       close(fd);
        return(was_bad);
 }
Index: login.c
===================================================================
RCS file: /cvs/src/usr.bin/login/login.c,v
retrieving revision 1.67
diff -u -p -r1.67 login.c
--- login.c     26 Dec 2015 20:51:35 -0000      1.67
+++ login.c     24 Jun 2016 01:27:12 -0000
@@ -109,7 +109,6 @@ void         sighup(int);
 void    sleepexit(int);
 char   *stypeof(char *);
 void    timedout(int);
-int     main(int, char **);
 
 extern int check_failedlogin(uid_t);
 extern void log_failedlogin(uid_t, char *, char *, char *);
@@ -184,7 +183,7 @@ main(int argc, char *argv[])
         */
        fflag = pflag = 0;
        uid = getuid();
-       while ((ch = getopt(argc, argv, "fh:pu:L:R:")) != -1)
+       while ((ch = getopt(argc, argv, "fh:pu:L:R:")) != -1) {
                switch (ch) {
                case 'f':
                        fflag = 1;
@@ -253,11 +252,12 @@ main(int argc, char *argv[])
                default:
                        if (!uid)
                                syslog(LOG_ERR, "invalid flag %c", ch);
-                       (void)fprintf(stderr,
+                       fprintf(stderr,
                            "usage: login [-fp] [-h hostname] [-L local-addr] "
                            "[-R remote-addr] [-u username]\n\t[user]\n");
                        quickexit(1);
                }
+       }
        argc -= optind;
        argv += optind;
 
@@ -292,7 +292,7 @@ main(int argc, char *argv[])
 
        ttyn = ttyname(STDIN_FILENO);
        if (ttyn == NULL || *ttyn == '\0') {
-               (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
+               snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
                ttyn = tname;
        }
        if ((tty = strrchr(ttyn, '/')))
@@ -313,16 +313,16 @@ main(int argc, char *argv[])
                scds.rlim_cur = scds.rlim_max = QUAD_MIN;
        }
 
-       (void)signal(SIGALRM, timedout);
+       signal(SIGALRM, timedout);
        if (argc > 1) {
                needto = 0;
-               (void)alarm(timeout);
+               alarm(timeout);
        } else
                needto = 1;
-       (void)signal(SIGQUIT, SIG_IGN);
-       (void)signal(SIGINT, SIG_IGN);
-       (void)signal(SIGHUP, SIG_IGN);
-       (void)setpriority(PRIO_PROCESS, 0, 0);
+       signal(SIGQUIT, SIG_IGN);
+       signal(SIGINT, SIG_IGN);
+       signal(SIGHUP, SIG_IGN);
+       setpriority(PRIO_PROCESS, 0, 0);
 
        /* get the default login class */
        if ((lc = login_getclass(0)) == NULL) { /* get the default class */
@@ -340,8 +340,8 @@ main(int argc, char *argv[])
                }
                shell = strrchr(script, '/') + 1;
                auth_setstate(as, AUTH_OKAY);
-               auth_call(as, script, shell,
-                   fflag ? "-f" : username, fflag ? username : 0, (char *)0);
+               auth_call(as, script, shell, fflag ? "-f" : username,
+                   fflag ? username : 0, (char *)NULL);
                if (!(auth_getstate(as) & AUTH_ALLOW))
                        quickexit(1);
                auth_setenv(as);
@@ -422,7 +422,7 @@ main(int argc, char *argv[])
                                badlogin(tbuf);
                        failures = 0;
                }
-               (void)strlcpy(tbuf, username, sizeof(tbuf));
+               strlcpy(tbuf, username, sizeof(tbuf));
 
                if ((pwd = getpwnam(username)) != NULL &&
                    auth_setpwd(as, pwd) < 0) {
@@ -526,7 +526,7 @@ failed:
                } else {
                        if (!as || (p = auth_getvalue(as, "errormsg")) == NULL)
                                p = "Login incorrect";
-                       (void)printf("%s\n", p);
+                       printf("%s\n", p);
                }
                failures++;
                if (pwd)
@@ -545,7 +545,7 @@ failed:
        }
 
        /* committed to login -- turn off timeout */
-       (void)alarm(0);
+       alarm(0);
 
        endpwent();
 
@@ -581,8 +581,8 @@ failed:
                quickexit(1);
        }
        if (term[0] == '\0')
-               (void)strlcpy(term, stypeof(tty), sizeof(term));
-       (void)snprintf(mail, sizeof(mail), "%s/%s", _PATH_MAILDIR,
+               strlcpy(term, stypeof(tty), sizeof(term));
+       snprintf(mail, sizeof(mail), "%s/%s", _PATH_MAILDIR,
                pwd->pw_name);
        if (setenv("TERM", term, 0) == -1 ||
            setenv("LOGNAME", pwd->pw_name, 1) == -1 ||
@@ -620,7 +620,7 @@ failed:
        homeless = chdir(pwd->pw_dir);
        if (homeless) {
                if (login_getcapbool(lc, "requirehome", 0)) {
-                       (void)printf("No home directory %s!\n", pwd->pw_dir);
+                       printf("No home directory %s!\n", pwd->pw_dir);
                        quickexit(1);
                }
                if (chdir("/"))
@@ -635,11 +635,11 @@ failed:
        setegid(0);     /* XXX use a saved gid instead? */
 
        if ((p = auth_getvalue(as, "warnmsg")) != NULL)
-               (void)printf("WARNING: %s\n\n", p);
+               printf("WARNING: %s\n\n", p);
 
        expire = auth_check_expire(as);
        if (expire < 0) {
-               (void)printf("Sorry -- your account has expired.\n");
+               printf("Sorry -- your account has expired.\n");
                quickexit(1);
        } else if (expire > 0 && !quietlog) {
                warning = login_getcaptime(lc, "expire-warn",
@@ -650,13 +650,13 @@ failed:
        }
 
        /* Nothing else left to fail -- really log in. */
-       (void)signal(SIGHUP, SIG_DFL);
+       signal(SIGHUP, SIG_DFL);
        memset(&utmp, 0, sizeof(utmp));
-       (void)time(&utmp.ut_time);
-       (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
+       time(&utmp.ut_time);
+       strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
        if (hostname)
-               (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
-       (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
+               strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
+       strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
        login(&utmp);
 
        if (!quietlog)
@@ -684,26 +684,26 @@ failed:
                        auth_cat(copyright);
                motd();
                if (stat(mail, &st) == 0 && st.st_size != 0)
-                       (void)printf("You have %smail.\n",
+                       printf("You have %smail.\n",
                            (st.st_mtime > st.st_atime) ? "new " : "");
        }
 
-       (void)signal(SIGALRM, SIG_DFL);
-       (void)signal(SIGQUIT, SIG_DFL);
-       (void)signal(SIGHUP, SIG_DFL);
-       (void)signal(SIGINT, SIG_DFL);
-       (void)signal(SIGTSTP, SIG_IGN);
+       signal(SIGALRM, SIG_DFL);
+       signal(SIGQUIT, SIG_DFL);
+       signal(SIGHUP, SIG_DFL);
+       signal(SIGINT, SIG_DFL);
+       signal(SIGTSTP, SIG_IGN);
 
        tbuf[0] = '-';
-       (void)strlcpy(tbuf + 1, (p = strrchr(shell, '/')) ?
-           p + 1 : shell, sizeof(tbuf) - 1);
+       strlcpy(tbuf + 1, (p = strrchr(shell, '/')) ?  p + 1 : shell,
+           sizeof(tbuf) - 1);
 
        if ((scds.rlim_cur != QUAD_MIN || scds.rlim_max != QUAD_MIN) &&
            setrlimit(RLIMIT_CORE, &scds) < 0)
                syslog(LOG_ERR, "couldn't reset core dump size: %m");
 
        if (lastchance)
-               (void)printf("WARNING: Your password has expired."
+               printf("WARNING: Your password has expired."
                    "  You must change your password, now!\n");
 
        if (setusercontext(lc, pwd, rootlogin ? 0 : pwd->pw_uid,
@@ -713,16 +713,16 @@ failed:
        }
 
        if (homeless) {
-               (void)printf("No home directory %s!\n", pwd->pw_dir);
-               (void)printf("Logging in with home = \"/\".\n");
-               (void)setenv("HOME", "/", 1);
+               printf("No home directory %s!\n", pwd->pw_dir);
+               printf("Logging in with home = \"/\".\n");
+               setenv("HOME", "/", 1);
        }
 
        if (auth_approval(as, lc, NULL, "login") == 0) {
                if (auth_getstate(as) & AUTH_EXPIRED)
-                       (void)printf("Sorry -- your account has expired.\n");
+                       printf("Sorry -- your account has expired.\n");
                else
-                       (void)printf("approval failure\n");
+                       printf("approval failure\n");
                quickexit(1);
        }
 
@@ -756,7 +756,7 @@ getloginname(void)
        int ch;
 
        for (;;) {
-               (void)printf("login: ");
+               printf("login: ");
                for (p = nbuf; (ch = getchar()) != '\n'; ) {
                        if (ch == EOF) {
                                badlogin(username);
@@ -767,7 +767,7 @@ getloginname(void)
                }
                if (p > nbuf) {
                        if (nbuf[0] == '-')
-                               (void)fprintf(stderr,
+                               fprintf(stderr,
                                    "login names may not start with '-'.\n");
                        else {
                                *p = '\0';
@@ -803,25 +803,23 @@ motd(void)
        sa.sa_handler = sigint;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;                /* don't set SA_RESTART */
-       (void)sigaction(SIGINT, &sa, &osa);
+       sigaction(SIGINT, &sa, &osa);
 
        /* read and spew motd until EOF, error, or SIGINT */
        while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 &&
            write(STDOUT_FILENO, tbuf, nchars) == nchars)
-               ;
+               continue;
 
-       (void)sigaction(SIGINT, &osa, NULL);
-       (void)close(fd);
+       sigaction(SIGINT, &osa, NULL);
+       close(fd);
 }
 
-/* ARGSUSED */
 void
 sigint(int signo)
 {
        return;                 /* just interrupt syscall */
 }
 
-/* ARGSUSED */
 void
 timedout(int signo)
 {
@@ -842,31 +840,31 @@ dolastlog(int quiet)
        off_t pos;
        int fd;
 
-       if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
-               pos = (off_t)pwd->pw_uid * sizeof(ll);
-               if (!quiet) {
-                       if (pread(fd, &ll, sizeof(ll), pos) == sizeof(ll) &&
-                           ll.ll_time != 0) {
-                               (void)printf("Last login: %.*s ",
-                                   24-5, (char *)ctime(&ll.ll_time));
-                               (void)printf("on %.*s",
-                                   (int)sizeof(ll.ll_line),
-                                   ll.ll_line);
-                               if (*ll.ll_host != '\0')
-                                       (void)printf(" from %.*s",
-                                           (int)sizeof(ll.ll_host),
-                                           ll.ll_host);
-                               (void)putchar('\n');
-                       }
-               }
-               memset(&ll, 0, sizeof(ll));
-               (void)time(&ll.ll_time);
-               (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
-               if (hostname)
-                       (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
-               (void)pwrite(fd, &ll, sizeof(ll), pos);
-               (void)close(fd);
-       }
+       if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) == -1)
+               return;
+       pos = pwd->pw_uid * sizeof(ll);
+       if (!quiet) {
+               if (pread(fd, &ll, sizeof(ll), pos) == sizeof(ll) &&
+                   ll.ll_time != 0) {
+                       printf("Last login: %.*s ",
+                           24-5, (char *)ctime(&ll.ll_time));
+                       printf("on %.*s",
+                           (int)sizeof(ll.ll_line),
+                           ll.ll_line);
+                       if (*ll.ll_host != '\0')
+                               printf(" from %.*s",
+                                   (int)sizeof(ll.ll_host),
+                                   ll.ll_host);
+                       putchar('\n');
+               }
+       }
+       memset(&ll, 0, sizeof(ll));
+       time(&ll.ll_time);
+       strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
+       if (hostname)
+               strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
+       (void)pwrite(fd, &ll, sizeof(ll), pos);
+       close(fd);
 }
 
 void
@@ -912,7 +910,7 @@ void
 sleepexit(int eval)
 {
        auth_close(as);
-       (void)sleep(5);
+       sleep(5);
        exit(eval);
 }
 

Reply via email to