redeclaration of err()
[unix-history] / usr / src / usr.bin / mesg / mesg.c
CommitLineData
56b5a10f
KB
1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
f15db449 5 * %sccs.include.redist.c%
56b5a10f
KB
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1987 Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
9127bcc6 15static char sccsid[] = "@(#)mesg.c 5.2 (Berkeley) %G%";
56b5a10f
KB
16#endif /* not lint */
17
69d8f2f4
BJ
18#include <sys/types.h>
19#include <sys/stat.h>
b8432c0e
KB
20#include <errno.h>
21#include <unistd.h>
56b5a10f 22#include <stdio.h>
b8432c0e
KB
23#include <stdlib.h>
24#include <string.h>
69d8f2f4 25
9127bcc6
KB
26void err __P((const char *fmt, ...));
27void usage __P((void));
69d8f2f4 28
b8432c0e 29int
69d8f2f4 30main(argc, argv)
56b5a10f 31 int argc;
b8432c0e 32 char *argv[];
69d8f2f4 33{
56b5a10f 34 struct stat sbuf;
b8432c0e
KB
35 char *tty;
36 int ch;
69d8f2f4 37
b8432c0e
KB
38 while ((ch = getopt(argc, argv, "")) != EOF)
39 switch(ch) {
40 case '?':
41 default:
42 usage();
43 }
44 argc -= optind;
45 argv += optind;
46
47 if (!(tty = ttyname(2)))
48 err("ttyname: %s", strerror(errno));
49 if (stat(tty, &sbuf) < 0)
50 err("%s: %s", strerror(errno));
51
52 if (*argv == NULL) {
56b5a10f 53 if (sbuf.st_mode & 020) {
b8432c0e 54 (void)fprintf(stderr, "is y\n");
56b5a10f 55 exit(0);
69d8f2f4 56 }
b8432c0e 57 (void)fprintf(stderr, "is n\n");
56b5a10f
KB
58 exit(1);
59 }
60#define OTHER_WRITE 020
b8432c0e 61 switch(*argv[0]) {
56b5a10f 62 case 'y':
b8432c0e
KB
63 if (chmod(tty, sbuf.st_mode | OTHER_WRITE) < 0)
64 err("%s: %s", strerror(errno));
56b5a10f
KB
65 exit(0);
66 case 'n':
b8432c0e
KB
67 if (chmod(tty, sbuf.st_mode &~ OTHER_WRITE) < 0)
68 err("%s: %s", strerror(errno));
56b5a10f 69 exit(1);
56b5a10f 70 }
b8432c0e
KB
71 usage();
72 /* NOTREACHED */
69d8f2f4
BJ
73}
74
9127bcc6 75void
b8432c0e 76usage()
69d8f2f4 77{
b8432c0e
KB
78 (void)fprintf(stderr, "usage: mesg [y | n]\n");
79 exit(2);
80}
81
82#if __STDC__
83#include <stdarg.h>
84#else
85#include <varargs.h>
86#endif
87
88void
89#if __STDC__
90err(const char *fmt, ...)
91#else
92err(fmt, va_alist)
93 char *fmt;
94 va_dcl
95#endif
96{
97 va_list ap;
98#if __STDC__
99 va_start(ap, fmt);
100#else
101 va_start(ap);
102#endif
103 (void)fprintf(stderr, "mesg: ");
104 (void)vfprintf(stderr, fmt, ap);
105 va_end(ap);
106 (void)fprintf(stderr, "\n");
107 exit(2);
108 /* NOTREACHED */
69d8f2f4 109}