SCCS-vsn: usr.bin/mesg/mesg.c 5.1
#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%";
-/*
- * 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 <stdlib.h>
+#include <string.h>
+static void err __P((const char *fmt, ...));
+static void usage __P((void));
main(argc, argv)
int argc;
main(argc, argv)
int argc;
- 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");
- fputs("is n\n", stderr);
+ (void)fprintf(stderr, "is n\n");
exit(1);
}
#define OTHER_WRITE 020
exit(1);
}
#define OTHER_WRITE 020
- newmode(sbuf.st_mode | OTHER_WRITE);
+ if (chmod(tty, sbuf.st_mode | OTHER_WRITE) < 0)
+ err("%s: %s", strerror(errno));
- newmode(sbuf.st_mode &~ OTHER_WRITE);
+ if (chmod(tty, sbuf.st_mode &~ OTHER_WRITE) < 0)
+ err("%s: %s", strerror(errno));
- default:
- fputs("usage: mesg [y] [n]\n", stderr);
- exit(-1);
+ usage();
+ /* NOTREACHED */
- 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 */