Welcome to that ANSI thang!
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sun, 18 Aug 1991 03:30:38 +0000 (19:30 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Sun, 18 Aug 1991 03:30:38 +0000 (19:30 -0800)
SCCS-vsn: usr.bin/mesg/mesg.c 5.1

usr/src/usr.bin/mesg/mesg.c

index 1bad3d3..54bf460 100644 (file)
@@ -12,67 +12,98 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)mesg.c     4.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)mesg.c     5.1 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-/*
- * mesg -- set current tty to accept or
- *     forbid write permission.
- *
- *     mesg [y] [n]
- *             y allow messages
- *             n forbid messages
- */
-
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 
-static char *tty;
+static void err __P((const char *fmt, ...));
+static void usage __P((void));
 
 
+int
 main(argc, argv)
        int argc;
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
        struct stat sbuf;
 {
        struct stat sbuf;
-       char *ttyname();
+       char *tty;
+       int ch;
 
 
-       if (!(tty = ttyname(2))) {
-               fputs("mesg: not a device in /dev.\n", stderr);
-               exit(-1);
-       }
-       if (stat(tty, &sbuf) < 0) {
-               perror("mesg");
-               exit(-1);
-       }
-       if (argc < 2) {
+       while ((ch = getopt(argc, argv, "")) != EOF)
+               switch(ch) {
+               case '?':
+               default:
+                       usage();
+               }
+       argc -= optind;
+       argv += optind;
+
+       if (!(tty = ttyname(2)))
+               err("ttyname: %s", strerror(errno));
+       if (stat(tty, &sbuf) < 0)
+               err("%s: %s", strerror(errno));
+
+       if (*argv == NULL) {
                if (sbuf.st_mode & 020) {
                if (sbuf.st_mode & 020) {
-                       fputs("is y\n", stderr);
+                       (void)fprintf(stderr, "is y\n");
                        exit(0);
                }
                        exit(0);
                }
-               fputs("is n\n", stderr);
+               (void)fprintf(stderr, "is n\n");
                exit(1);
        }
 #define        OTHER_WRITE     020
                exit(1);
        }
 #define        OTHER_WRITE     020
-       switch(*argv[1]) {
+       switch(*argv[0]) {
        case 'y':
        case 'y':
-               newmode(sbuf.st_mode | OTHER_WRITE);
+               if (chmod(tty, sbuf.st_mode | OTHER_WRITE) < 0)
+                       err("%s: %s", strerror(errno));
                exit(0);
        case 'n':
                exit(0);
        case 'n':
-               newmode(sbuf.st_mode &~ OTHER_WRITE);
+               if (chmod(tty, sbuf.st_mode &~ OTHER_WRITE) < 0)
+                       err("%s: %s", strerror(errno));
                exit(1);
                exit(1);
-       default:
-               fputs("usage: mesg [y] [n]\n", stderr);
-               exit(-1);
        }
        }
-       /*NOTREACHED*/
+       usage();
+       /* NOTREACHED */
 }
 
 }
 
-newmode(m)
-       u_short m;
+static void
+usage()
 {
 {
-       if (chmod(tty, m) < 0) {
-               perror("mesg");
-               exit(-1);
-       }
+       (void)fprintf(stderr, "usage: mesg [y | n]\n");
+       exit(2);
+}
+
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+void
+#if __STDC__
+err(const char *fmt, ...)
+#else
+err(fmt, va_alist)
+       char *fmt;
+       va_dcl
+#endif
+{
+       va_list ap;
+#if __STDC__
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "mesg: ");
+       (void)vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       (void)fprintf(stderr, "\n");
+       exit(2);
+       /* NOTREACHED */
 }
 }