xref: /original-bsd/usr.bin/biff/biff.c (revision 2cb1372a)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)biff.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 static void usage __P((void));
27 static void err __P((char *));
28 
29 main(argc, argv)
30 	int argc;
31 	char *argv[];
32 {
33 	struct stat sb;
34 	int ch;
35 	char *name;
36 
37 
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 ((name = ttyname(STDERR_FILENO)) == NULL) {
48 		(void)fprintf(stderr, "biff: unknown tty\n");
49 		exit(2);
50 	}
51 
52 	if (stat(name, &sb))
53 		err(name);
54 
55 	if (*argv == NULL) {
56 		(void)printf("is %s\n", sb.st_mode&0100 ? "y" : "n");
57 		exit(sb.st_mode & 0100 ? 0 : 1);
58 	}
59 
60 	switch(argv[0][0]) {
61 	case 'n':
62 		if (chmod(name, sb.st_mode & ~0100) < 0)
63 			err(name);
64 		break;
65 	case 'y':
66 		if (chmod(name, sb.st_mode | 0100) < 0)
67 			err(name);
68 		break;
69 	default:
70 		usage();
71 	}
72 	exit(sb.st_mode & 0100 ? 0 : 1);
73 }
74 
75 static void
76 err(name)
77 	char *name;
78 {
79 	(void)fprintf(stderr, "biff: %s: %s\n", name, strerror(errno));
80 	exit(2);
81 }
82 
83 static void
84 usage()
85 {
86 	(void)fprintf(stderr, "usage: biff [y | n]\n");
87 	exit(2);
88 }
89