note other reasons for pclose to return -1; wait(2) -> wait4(2)
[unix-history] / usr / src / libexec / comsat / comsat.c
index 241a154..c71fc77 100644 (file)
@@ -1,18 +1,29 @@
 /*
  * Copyright (c) 1980 Regents of the University of California.
 /*
  * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif /* !lint */
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)comsat.c   5.7 (Berkeley) %G%";
-#endif /* !lint */
+static char sccsid[] = "@(#)comsat.c   5.13 (Berkeley) %G%";
+#endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/socket.h>
 
 #include <sys/param.h>
 #include <sys/socket.h>
@@ -59,7 +70,7 @@ main(argc, argv)
        if (getsockname(0, &from, &fromlen) < 0) {
                fprintf(stderr, "%s: ", argv[0]);
                perror("getsockname");
        if (getsockname(0, &from, &fromlen) < 0) {
                fprintf(stderr, "%s: ", argv[0]);
                perror("getsockname");
-               _exit(1);
+               exit(1);
        }
        openlog("comsat", LOG_PID, LOG_DAEMON);
        if (chdir("/usr/spool/mail")) {
        }
        openlog("comsat", LOG_PID, LOG_DAEMON);
        if (chdir("/usr/spool/mail")) {
@@ -97,7 +108,7 @@ main(argc, argv)
 
 reapchildren()
 {
 
 reapchildren()
 {
-       while (wait3((struct wait *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
+       while (wait3((union wait *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
 }
 
 onalrm()
 }
 
 onalrm()
@@ -110,11 +121,9 @@ onalrm()
 
        if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE)
                exit(0);
 
        if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE)
                exit(0);
-       dsyslog(LOG_DEBUG, ".onalrm: alarm");
        (void)alarm((u_int)15);
        (void)fstat(uf, &statbf);
        if (statbf.st_mtime > utmpmtime) {
        (void)alarm((u_int)15);
        (void)fstat(uf, &statbf);
        if (statbf.st_mtime > utmpmtime) {
-               dsyslog(LOG_DEBUG, ".onalrm: changed\n");
                utmpmtime = statbf.st_mtime;
                if (statbf.st_size > utmpsize) {
                        utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
                utmpmtime = statbf.st_mtime;
                if (statbf.st_size > utmpsize) {
                        utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
@@ -123,15 +132,13 @@ onalrm()
                        else
                                utmp = (struct utmp *)malloc(utmpsize);
                        if (!utmp) {
                        else
                                utmp = (struct utmp *)malloc(utmpsize);
                        if (!utmp) {
-                               dsyslog(LOG_DEBUG, ".onalrm: malloc failed");
+                               syslog(LOG_ERR, "malloc failed");
                                exit(1);
                        }
                }
                (void)lseek(uf, 0L, L_SET);
                                exit(1);
                        }
                }
                (void)lseek(uf, 0L, L_SET);
-               nutmp = read(uf, utmp, statbf.st_size)/sizeof(struct utmp);
+               nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
        }
        }
-       else
-               dsyslog(LOG_DEBUG, ".onalrm: ok\n");
 }
 
 mailfor(name)
 }
 
 mailfor(name)
@@ -139,13 +146,10 @@ mailfor(name)
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
-       int offset;
+       off_t offset, atol();
 
 
-       dsyslog(LOG_DEBUG, ".mailfor: mailfor %s\n", name);
-       if (!(cp = index(name, '@'))) {
-               dsyslog(LOG_DEBUG, ".mailfor: bad format\n");
+       if (!(cp = index(name, '@')))
                return;
                return;
-       }
        *cp = '\0';
        offset = atoi(cp + 1);
        while (--utp >= utmp)
        *cp = '\0';
        offset = atoi(cp + 1);
        while (--utp >= utmp)
@@ -157,27 +161,27 @@ static char       *cr;
 
 notify(utp, offset)
        register struct utmp *utp;
 
 notify(utp, offset)
        register struct utmp *utp;
-       int offset;
+       off_t offset;
 {
 {
-       FILE *tp;
+       static char tty[20] = "/dev/";
        struct sgttyb gttybuf;
        struct sgttyb gttybuf;
-       char tty[20], name[sizeof (utmp[0].ut_name) + 1];
+       FILE *tp;
+       char name[sizeof (utmp[0].ut_name) + 1];
        struct stat stb;
 
        struct stat stb;
 
-       (void)strcpy(tty, "/dev/");
        (void)strncpy(tty + 5, utp->ut_line, sizeof(utp->ut_line));
        (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)) {
        if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
-               dsyslog(LOG_DEBUG, ".notify: wrong mode on tty");
+               dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
                return;
        }
                return;
        }
+       dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
        if (fork())
                return;
        (void)signal(SIGALRM, SIG_DFL);
        (void)alarm((u_int)30);
        if ((tp = fopen(tty, "w")) == NULL) {
        if (fork())
                return;
        (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);
+               dsyslog(LOG_ERR, "fopen of tty %s failed", tty);
+               _exit(-1);
        }
        (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
        cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r";
        }
        (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
        cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ? "" : "\r";
@@ -186,13 +190,14 @@ notify(utp, offset)
        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);
        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);
+       fclose(tp);
+       _exit(0);
 }
 
 jkfprintf(tp, name, offset)
        register FILE *tp;
        char name[];
 }
 
 jkfprintf(tp, name, offset)
        register FILE *tp;
        char name[];
-       int offset;
+       off_t offset;
 {
        register char *cp;
        register FILE *fi;
 {
        register char *cp;
        register FILE *fi;
@@ -200,13 +205,9 @@ jkfprintf(tp, name, offset)
        char line[BUFSIZ];
        off_t fseek();
 
        char line[BUFSIZ];
        off_t fseek();
 
-       dsyslog(LOG_DEBUG, ".jkfprint: HERE %s's mail starting at %d\n",
-           name, offset);
-       if ((fi = fopen(name, "r")) == NULL) {
-               dsyslog(LOG_DEBUG, ".jkfprintf: Can't read the mail\n");
+       if ((fi = fopen(name, "r")) == NULL)
                return;
                return;
-       }
-       (void)fseek(fi, (long)offset, L_SET);
+       (void)fseek(fi, 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
@@ -216,24 +217,25 @@ 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) {
-               if (strncmp(line, "From ", 5) == 0)
-                       continue;
                if (inheader) {
                if (inheader) {
-                       if (line[0] == ' ' || line[0] == '\t')
-                               continue;
-                       if (!(cp = strpbrk(line, ": ")) || *cp == ' ')
+                       if (line[0] == '\n') {
                                inheader = 0;
                                inheader = 0;
-                       else if (strncmp(line, "From:", 5) &&
+                               continue;
+                       }
+                       if (line[0] == ' ' || line[0] == '\t' ||
+                           strncmp(line, "From:", 5) &&
                            strncmp(line, "Subject:", 8))
                                continue;
                }
                            strncmp(line, "Subject:", 8))
                                continue;
                }
-               if (cp = index(line, '\n'))
-                       *cp = '\0';
-               fprintf(tp, "%s%s\n", line, cr);
-               if (--linecnt <= 0 || (charcnt -= strlen(line)) <= 0) {
+               if (linecnt <= 0 || charcnt <= 0) {
                        fprintf(tp, "...more...%s\n", cr);
                        return;
                }
                        fprintf(tp, "...more...%s\n", cr);
                        return;
                }
+               if (cp = index(line, '\n'))
+                       *cp = '\0';
+               fprintf(tp, "%s%s\n", line, cr);
+               charcnt -= strlen(line);
+               linecnt--;
        }
        fprintf(tp, "----%s\n", cr);
 }
        }
        fprintf(tp, "----%s\n", cr);
 }