if utmp empty drops core; bug report 4.3BSD/etc/98;
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Thu, 19 Nov 1987 03:22:05 +0000 (19:22 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Thu, 19 Nov 1987 03:22:05 +0000 (19:22 -0800)
make lint happy; redo header reading logic completely

SCCS-vsn: libexec/comsat/comsat.c 5.7

usr/src/libexec/comsat/comsat.c

index 99554ec..241a154 100644 (file)
@@ -8,17 +8,17 @@
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* !lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)comsat.c   5.6 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)comsat.c   5.7 (Berkeley) %G%";
+#endif /* !lint */
 
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
-#include <sys/wait.h>
 #include <sys/file.h>
 #include <sys/file.h>
+#include <sys/wait.h>
 
 #include <netinet/in.h>
 
 
 #include <netinet/in.h>
 
@@ -29,39 +29,30 @@ static char sccsid[] = "@(#)comsat.c        5.6 (Berkeley) %G%";
 #include <errno.h>
 #include <netdb.h>
 #include <syslog.h>
 #include <errno.h>
 #include <netdb.h>
 #include <syslog.h>
+#include <strings.h>
 
 /*
  * comsat
  */
 int    debug = 0;
 
 /*
  * comsat
  */
 int    debug = 0;
-#define        dsyslog     if (debug) syslog
+#define        dsyslog if (debug) syslog
 
 
-struct sockaddr_in sin = { AF_INET };
-extern errno;
+#define MAXIDLE        120
 
 
-char   hostname[32];
+char   hostname[MAXHOSTNAMELEN];
 struct utmp *utmp = NULL;
 struct utmp *utmp = NULL;
-int    nutmp;
-int    uf;
-unsigned utmpmtime = 0;                        /* last modification time for utmp */
-unsigned utmpsize = 0;                 /* last malloced size for utmp */
-int    onalrm();
-int    reapchildren();
-long   lastmsgtime;
-char   *malloc(), *realloc();
-
-#define        MAXIDLE 120
-#define NAMLEN (sizeof (uts[0].ut_name) + 1)
+time_t lastmsgtime, time();
+int    nutmp, uf;
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
-       char *argv[];
+       char **argv;
 {
 {
+       extern int errno;
        register int cc;
        register int cc;
-       char buf[BUFSIZ];
        char msgbuf[100];
        struct sockaddr_in from;
        char msgbuf[100];
        struct sockaddr_in from;
-       int fromlen;
+       int fromlen, reapchildren(), onalrm();
 
        /* verify proper invocation */
        fromlen = sizeof (from);
 
        /* verify proper invocation */
        fromlen = sizeof (from);
@@ -71,18 +62,21 @@ main(argc, argv)
                _exit(1);
        }
        openlog("comsat", LOG_PID, LOG_DAEMON);
                _exit(1);
        }
        openlog("comsat", LOG_PID, LOG_DAEMON);
-       chdir("/usr/spool/mail");
-       if ((uf = open("/etc/utmp",0)) < 0) {
+       if (chdir("/usr/spool/mail")) {
+               syslog(LOG_ERR, "chdir: /usr/spool/mail");
+               exit(1);
+       }
+       if ((uf = open("/etc/utmp", O_RDONLY, 0)) < 0) {
                syslog(LOG_ERR, ".main: /etc/utmp: %m");
                (void) recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
                exit(1);
        }
                syslog(LOG_ERR, ".main: /etc/utmp: %m");
                (void) recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
                exit(1);
        }
-       lastmsgtime = time(0);
-       gethostname(hostname, sizeof (hostname));
+       (void)time(&lastmsgtime);
+       (void)gethostname(hostname, sizeof (hostname));
        onalrm();
        onalrm();
-       signal(SIGALRM, onalrm);
-       signal(SIGTTOU, SIG_IGN);
-       signal(SIGCHLD, reapchildren);
+       (void)signal(SIGALRM, onalrm);
+       (void)signal(SIGTTOU, SIG_IGN);
+       (void)signal(SIGCHLD, reapchildren);
        for (;;) {
                cc = recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
                if (cc <= 0) {
        for (;;) {
                cc = recv(0, msgbuf, sizeof (msgbuf) - 1, 0);
                if (cc <= 0) {
@@ -91,47 +85,52 @@ main(argc, argv)
                        errno = 0;
                        continue;
                }
                        errno = 0;
                        continue;
                }
+               if (!nutmp)             /* no one has logged in yet */
+                       continue;
                sigblock(sigmask(SIGALRM));
                msgbuf[cc] = 0;
                sigblock(sigmask(SIGALRM));
                msgbuf[cc] = 0;
-               lastmsgtime = time(0);
+               (void)time(&lastmsgtime);
                mailfor(msgbuf);
                mailfor(msgbuf);
-               sigsetmask(0);
+               sigsetmask(0L);
        }
 }
 
 reapchildren()
 {
        }
 }
 
 reapchildren()
 {
-
-       while (wait3((struct wait *)0, WNOHANG, (struct rusage *)0) > 0)
-               ;
+       while (wait3((struct wait *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
 }
 
 onalrm()
 {
 }
 
 onalrm()
 {
+       static u_int utmpsize;          /* last malloced size for utmp */
+       static u_int utmpmtime;         /* last modification time for utmp */
        struct stat statbf;
        struct stat statbf;
+       off_t lseek();
+       char *malloc(), *realloc();
 
 
-       if (time(0) - lastmsgtime >= MAXIDLE)
+       if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE)
                exit(0);
                exit(0);
-       dsyslog(LOG_DEBUG,".onalrm: alarm");
-       alarm(15);
-       fstat(uf, &statbf);
+       dsyslog(LOG_DEBUG, ".onalrm: alarm");
+       (void)alarm((u_int)15);
+       (void)fstat(uf, &statbf);
        if (statbf.st_mtime > utmpmtime) {
        if (statbf.st_mtime > utmpmtime) {
-               dsyslog(LOG_DEBUG,".onalrm: changed\n");
+               dsyslog(LOG_DEBUG, ".onalrm: changed\n");
                utmpmtime = statbf.st_mtime;
                if (statbf.st_size > utmpsize) {
                        utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
                        if (utmp)
                utmpmtime = statbf.st_mtime;
                if (statbf.st_size > utmpsize) {
                        utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
                        if (utmp)
-                               utmp = (struct utmp *)realloc(utmp, utmpsize);
+                               utmp = (struct utmp *)realloc((char *)utmp, utmpsize);
                        else
                                utmp = (struct utmp *)malloc(utmpsize);
                        else
                                utmp = (struct utmp *)malloc(utmpsize);
-                       if (! utmp) {
-                               dsyslog(LOG_DEBUG,".onalrm: malloc failed");
+                       if (!utmp) {
+                               dsyslog(LOG_DEBUG, ".onalrm: malloc failed");
                                exit(1);
                        }
                }
                                exit(1);
                        }
                }
-               lseek(uf, 0, 0);
-               nutmp = read(uf,utmp,statbf.st_size)/sizeof(struct utmp);
-       } else
+               (void)lseek(uf, 0L, L_SET);
+               nutmp = read(uf, utmp, statbf.st_size)/sizeof(struct utmp);
+       }
+       else
                dsyslog(LOG_DEBUG, ".onalrm: ok\n");
 }
 
                dsyslog(LOG_DEBUG, ".onalrm: ok\n");
 }
 
@@ -140,75 +139,74 @@ mailfor(name)
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
-       char *rindex();
        int offset;
 
        int offset;
 
-       dsyslog(LOG_DEBUG,".mailfor: mailfor %s\n", name);
-       cp = name;
-       while (*cp && *cp != '@')
-               cp++;
-       if (*cp == 0) {
-               dsyslog(LOG_DEBUG,".mailfor: bad format\n");
+       dsyslog(LOG_DEBUG, ".mailfor: mailfor %s\n", name);
+       if (!(cp = index(name, '@'))) {
+               dsyslog(LOG_DEBUG, ".mailfor: bad format\n");
                return;
        }
                return;
        }
-       *cp = 0;
-       offset = atoi(cp+1);
+       *cp = '\0';
+       offset = atoi(cp + 1);
        while (--utp >= utmp)
                if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
                        notify(utp, offset);
 }
 
        while (--utp >= utmp)
                if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
                        notify(utp, offset);
 }
 
-char   *cr;
+static char    *cr;
 
 notify(utp, offset)
        register struct utmp *utp;
 
 notify(utp, offset)
        register struct utmp *utp;
+       int offset;
 {
        FILE *tp;
        struct sgttyb gttybuf;
        char tty[20], name[sizeof (utmp[0].ut_name) + 1];
        struct stat stb;
 
 {
        FILE *tp;
        struct sgttyb gttybuf;
        char tty[20], name[sizeof (utmp[0].ut_name) + 1];
        struct stat stb;
 
-       strcpy(tty, "/dev/");
-       strncat(tty, utp->ut_line, sizeof(utp->ut_line));
-       dsyslog(LOG_DEBUG,".notify: notify %s on %s\n", utp->ut_name, tty);
-       if (stat(tty, &stb) == 0 && (stb.st_mode & 0100) == 0) {
-               dsyslog(LOG_DEBUG,".notify: wrong mode on tty");
+       (void)strcpy(tty, "/dev/");
+       (void)strncpy(tty + 5, utp->ut_line, sizeof(utp->ut_line));
+       dsyslog(LOG_DEBUG, ".notify: notify %s on %s\n", utp->ut_name, tty);
+       if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
+               dsyslog(LOG_DEBUG, ".notify: wrong mode on tty");
                return;
        }
        if (fork())
                return;
                return;
        }
        if (fork())
                return;
-       signal(SIGALRM, SIG_DFL);
-       alarm(30);
-       if ((tp = fopen(tty,"w")) == 0) {
-               dsyslog(LOG_DEBUG,".notify: fopen of tty failed");
+       (void)signal(SIGALRM, SIG_DFL);
+       (void)alarm((u_int)30);
+       if ((tp = fopen(tty, "w")) == NULL) {
+               dsyslog(LOG_DEBUG, ".notify: fopen of tty failed");
                exit(-1);
        }
                exit(-1);
        }
-       ioctl(fileno(tp), TIOCGETP, &gttybuf);
+       (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
        cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r";
        cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r";
-       strncpy(name, utp->ut_name, sizeof (utp->ut_name));
+       (void)strncpy(name, utp->ut_name, sizeof (utp->ut_name));
        name[sizeof (name) - 1] = '\0';
        name[sizeof (name) - 1] = '\0';
-       fprintf(tp,"%s\n\007New mail for %s@%.*s\007 has arrived:%s\n",
-           cr, name, sizeof (hostname), hostname, cr);
-       fprintf(tp,"----%s\n", cr);
+       fprintf(tp, "%s\n\007New mail for %s@%.*s\007 has arrived:%s\n----%s\n",
+           cr, name, sizeof (hostname), hostname, cr, cr);
        jkfprintf(tp, name, offset);
        exit(0);
 }
 
 jkfprintf(tp, name, offset)
        register FILE *tp;
        jkfprintf(tp, name, offset);
        exit(0);
 }
 
 jkfprintf(tp, name, offset)
        register FILE *tp;
+       char name[];
+       int offset;
 {
 {
+       register char *cp;
        register FILE *fi;
        register FILE *fi;
-       register int linecnt, charcnt;
+       register int linecnt, charcnt, inheader;
        char line[BUFSIZ];
        char line[BUFSIZ];
-       int inheader;
+       off_t fseek();
 
 
-       dsyslog(LOG_DEBUG,".jkfprint: HERE %s's mail starting at %d\n",
+       dsyslog(LOG_DEBUG, ".jkfprint: HERE %s's mail starting at %d\n",
            name, offset);
            name, offset);
-       if ((fi = fopen(name,"r")) == NULL) {
-               dsyslog(LOG_DEBUG,".jkfprintf: Cant read the mail\n");
+       if ((fi = fopen(name, "r")) == NULL) {
+               dsyslog(LOG_DEBUG, ".jkfprintf: Can't read the mail\n");
                return;
        }
                return;
        }
-       fseek(fi, offset, L_SET);
+       (void)fseek(fi, (long)offset, L_SET);
        /* 
         * Print the first 7 lines or 560 characters of the new mail
         * (whichever comes first).  Skip header crap other than
        /* 
         * Print the first 7 lines or 560 characters of the new mail
         * (whichever comes first).  Skip header crap other than
@@ -218,34 +216,24 @@ jkfprintf(tp, name, offset)
        charcnt = 560;
        inheader = 1;
        while (fgets(line, sizeof (line), fi) != NULL) {
        charcnt = 560;
        inheader = 1;
        while (fgets(line, sizeof (line), fi) != NULL) {
-               register char *cp;
-               char *index();
-               int cnt;
-
-               if (linecnt <= 0 || charcnt <= 0) {  
-                       fprintf(tp,"...more...%s\n", cr);
-                       return;
-               }
                if (strncmp(line, "From ", 5) == 0)
                        continue;
                if (strncmp(line, "From ", 5) == 0)
                        continue;
-               if (inheader && (line[0] == ' ' || line[0] == '\t'))
-                       continue;
-               cp = index(line, ':');
-               if (cp == 0 || (index(line, ' ') && index(line, ' ') < cp))
-                       inheader = 0;
-               else
-                       cnt = cp - line;
-               if (inheader &&
-                   strncmp(line, "Date", cnt) &&
-                   strncmp(line, "From", cnt) &&
-                   strncmp(line, "Subject", cnt) &&
-                   strncmp(line, "To", cnt))
-                       continue;
-               cp = index(line, '\n');
-               if (cp)
+               if (inheader) {
+                       if (line[0] == ' ' || line[0] == '\t')
+                               continue;
+                       if (!(cp = strpbrk(line, ": ")) || *cp == ' ')
+                               inheader = 0;
+                       else if (strncmp(line, "From:", 5) &&
+                           strncmp(line, "Subject:", 8))
+                               continue;
+               }
+               if (cp = index(line, '\n'))
                        *cp = '\0';
                        *cp = '\0';
-               fprintf(tp,"%s%s\n", line, cr);
-               linecnt--, charcnt -= strlen(line);
+               fprintf(tp, "%s%s\n", line, cr);
+               if (--linecnt <= 0 || (charcnt -= strlen(line)) <= 0) {
+                       fprintf(tp, "...more...%s\n", cr);
+                       return;
+               }
        }
        }
-       fprintf(tp,"----%s\n", cr);
+       fprintf(tp, "----%s\n", cr);
 }
 }