Add define for Kirk Smith's USR Courier driver. Change default baud
[unix-history] / usr / src / usr.bin / write / write.c
index 87ee682..c0ddb19 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef        lint
 #ifndef        lint
-static char *sccsid = "@(#)write.c     4.9 %G%";
+static char *sccsid = "@(#)write.c     4.13 %G%";
 #endif
 /*
  * write to another user
 #endif
 /*
  * write to another user
@@ -24,6 +24,7 @@ char  me[NMAX + 1]    = "???";
 char   *him;
 char   *mytty;
 char   histty[32];
 char   *him;
 char   *mytty;
 char   histty[32];
+char   ttybuf[32];
 char   *histtya;
 char   *ttyname();
 char   *rindex();
 char   *histtya;
 char   *ttyname();
 char   *rindex();
@@ -42,6 +43,8 @@ main(argc, argv)
        register FILE *uf;
        int c1, c2;
        long clock = time(0);
        register FILE *uf;
        int c1, c2;
        long clock = time(0);
+       int suser = getuid() == 0;
+       int nomesg = 0;
        struct tm *localtime();
        struct tm *localclock = localtime( &clock );
 
        struct tm *localtime();
        struct tm *localclock = localtime( &clock );
 
@@ -54,6 +57,8 @@ main(argc, argv)
                histtya = argv[2];
        if ((uf = fopen("/etc/utmp", "r")) == NULL) {
                perror("write: Can't open /etc/utmp");
                histtya = argv[2];
        if ((uf = fopen("/etc/utmp", "r")) == NULL) {
                perror("write: Can't open /etc/utmp");
+               if (histtya == 0)
+                       exit(10);
                goto cont;
        }
        mytty = ttyname(2);
                goto cont;
        }
        mytty = ttyname(2);
@@ -65,10 +70,11 @@ main(argc, argv)
                perror("write: Can't stat your tty");
                exit(1);
        }
                perror("write: Can't stat your tty");
                exit(1);
        }
-       if ((stbuf.st_mode&02) == 0) {
+       if ((stbuf.st_mode&020) == 0) {
                fprintf(stderr,
                        "write: You have write permission turned off\n");
                fprintf(stderr,
                        "write: You have write permission turned off\n");
-               exit(1);
+               if (!suser)
+                       exit(1);
        }
        mytty = rindex(mytty, '/') + 1;
        if (histtya) {
        }
        mytty = rindex(mytty, '/') + 1;
        if (histtya) {
@@ -99,46 +105,49 @@ main(argc, argv)
                        if (c1 != c2)
                                goto nomat;
                }
                        if (c1 != c2)
                                goto nomat;
                }
+               if (histtya && strncmp(histtya, ubuf.ut_line,
+                   sizeof(ubuf.ut_line)))
+                       continue;
                logcnt++;
                logcnt++;
-               if (histty[0]==0) {
-                       strcpy(histty, "/dev/");
-                       strcat(histty, ubuf.ut_line);
+               if (histty[0]==0 || nomesg && histtya == 0) {
+                       strcpy(ttybuf, "/dev/");
+                       strcat(ttybuf, ubuf.ut_line);
+                       if (histty[0]==0)
+                               strcpy(histty, ttybuf);
+                       if (access(ttybuf, 0) < 0 || stat(ttybuf, &stbuf) < 0 ||
+                           (stbuf.st_mode&020) == 0)
+                               nomesg++;
+                       else {
+                               strcpy(histty, ttybuf);
+                               nomesg = 0;
+                       }
                }
        nomat:
                ;
        }
                }
        nomat:
                ;
        }
-cont:
-       if (logcnt==0 && histty[0]=='\0') {
-               fprintf(stderr, "write: %s not logged in\n", him);
+       fclose(uf);
+       if (logcnt==0) {
+               fprintf(stderr, "write: %s not logged in%s\n", him,
+                       histtya ? " on that tty" : "");
                exit(1);
        }
                exit(1);
        }
-       if (uf != NULL)
-               fclose(uf);
        if (histtya==0 && logcnt > 1) {
                fprintf(stderr,
                "write: %s logged in more than once ... writing to %s\n",
                        him, histty+5);
        }
        if (histtya==0 && logcnt > 1) {
                fprintf(stderr,
                "write: %s logged in more than once ... writing to %s\n",
                        him, histty+5);
        }
-       if (histty[0] == 0) {
-               printf(him);
-               if (logcnt)
-                       printf(" not on that tty\n"); else
-                       printf(" not logged in\n");
-               exit(1);
-       }
+cont:
        if (access(histty, 0) < 0) {
                fprintf(stderr, "write: No such tty\n");
                exit(1);
        }
        signal(SIGALRM, timout);
        alarm(5);
        if (access(histty, 0) < 0) {
                fprintf(stderr, "write: No such tty\n");
                exit(1);
        }
        signal(SIGALRM, timout);
        alarm(5);
-       if ((tf = fopen(histty, "w")) == NULL)
-               goto perm;
+       if ((tf = fopen(histty, "w")) == NULL) {
+               fprintf(stderr, "write: Permission denied\n");
+               exit(1);
+       }
        alarm(0);
        alarm(0);
-       if (fstat(fileno(tf), &stbuf) < 0)
-               goto perm;
-       if ((stbuf.st_mode&02) == 0)
-               goto perm;
        sigs(eof);
        { char hostname[32];
          gethostname(hostname, sizeof (hostname));
        sigs(eof);
        { char hostname[32];
          gethostname(hostname, sizeof (hostname));
@@ -186,9 +195,6 @@ cont:
                        }
                }
        }
                        }
                }
        }
-perm:
-       fprintf(stderr, "write: Permission denied\n");
-       exit(1);
 }
 
 timout()
 }
 
 timout()
@@ -217,6 +223,8 @@ ex(bp)
                goto out;
        }
        if (i == 0) {
                goto out;
        }
        if (i == 0) {
+               fclose(tf);             /* Close his terminal */
+               setgid(getgid());       /* Give up effective group privs */
                sigs((int (*)())0);
                execl(getenv("SHELL") ?
                    getenv("SHELL") : "/bin/sh", "sh", "-c", bp+1, 0);
                sigs((int (*)())0);
                execl(getenv("SHELL") ?
                    getenv("SHELL") : "/bin/sh", "sh", "-c", bp+1, 0);