biff.c revision 100614
1164426Ssam/*
2164426Ssam * Copyright (c) 1980, 1993
3164426Ssam *	The Regents of the University of California.  All rights reserved.
4164426Ssam *
5164426Ssam * Redistribution and use in source and binary forms, with or without
6164426Ssam * modification, are permitted provided that the following conditions
7164426Ssam * are met:
8164426Ssam * 1. Redistributions of source code must retain the above copyright
9164426Ssam *    notice, this list of conditions and the following disclaimer.
10164426Ssam * 2. Redistributions in binary form must reproduce the above copyright
11164426Ssam *    notice, this list of conditions and the following disclaimer in the
12164426Ssam *    documentation and/or other materials provided with the distribution.
13164426Ssam * 3. All advertising materials mentioning features or use of this software
14164426Ssam *    must display the following acknowledgement:
15164426Ssam *	This product includes software developed by the University of
16164426Ssam *	California, Berkeley and its contributors.
17164426Ssam * 4. Neither the name of the University nor the names of its contributors
18164426Ssam *    may be used to endorse or promote products derived from this software
19164426Ssam *    without specific prior written permission.
20164426Ssam *
21164426Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22164426Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23164426Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24164426Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25164426Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26164426Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27164426Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28164426Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29164426Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30164426Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31164426Ssam * SUCH DAMAGE.
32164426Ssam */
33164426Ssam
34164426Ssam#ifndef lint
35164426Ssamstatic const char copyright[] =
36164426Ssam"@(#) Copyright (c) 1980, 1993\n\
37164426Ssam	The Regents of the University of California.  All rights reserved.\n";
38164426Ssam#endif
39164426Ssam
40164426Ssam#if 0
41164426Ssam#ifndef lint
42164426Ssamstatic char sccsid[] = "@(#)biff.c	8.1 (Berkeley) 6/6/93";
43164426Ssam#endif
44164426Ssam#endif
45164426Ssam
46164426Ssam#include <sys/cdefs.h>
47164426Ssam__FBSDID("$FreeBSD: head/usr.bin/biff/biff.c 100614 2002-07-24 15:48:17Z robert $");
48164426Ssam
49164426Ssam#include <sys/stat.h>
50164426Ssam
51164426Ssam#include <err.h>
52164426Ssam#include <stdio.h>
53164426Ssam#include <stdlib.h>
54164426Ssam#include <unistd.h>
55164426Ssam
56164426Ssamstatic void usage(void);
57164426Ssam
58164426Ssamint
59164426Ssammain(argc, argv)
60164426Ssam	int argc;
61164426Ssam	char *argv[];
62164426Ssam{
63164426Ssam	struct stat sb;
64164426Ssam	int ch;
65164426Ssam	char *name;
66164426Ssam
67186352Ssam
68236987Simp	while ((ch = getopt(argc, argv, "")) != -1)
69164426Ssam		switch(ch) {
70164426Ssam		case '?':
71186352Ssam		default:
72164426Ssam			usage();
73164426Ssam		}
74164426Ssam	argc -= optind;
75164426Ssam	argv += optind;
76186352Ssam
77186352Ssam	if ((name = ttyname(STDIN_FILENO)) == NULL &&
78186352Ssam	    (name = ttyname(STDOUT_FILENO)) == NULL &&
79188088Ssam	    (name = ttyname(STDERR_FILENO)) == NULL)
80188088Ssam		err(2, "unknown tty");
81188088Ssam
82188088Ssam	if (stat(name, &sb))
83164426Ssam		err(2, "stat");
84164426Ssam
85186352Ssam	if (*argv == NULL) {
86164426Ssam		(void)printf("is %s\n",
87164426Ssam		    sb.st_mode & S_IXUSR ? "y" :
88186352Ssam		    sb.st_mode & S_IXGRP ? "b" : "n");
89186352Ssam		return(sb.st_mode & (S_IXUSR | S_IXGRP) ? 0 : 1);
90164426Ssam
91194654Ssam	}
92194654Ssam
93194654Ssam	switch(argv[0][0]) {
94194654Ssam	case 'n':
95186352Ssam		if (chmod(name, sb.st_mode & ~(S_IXUSR | S_IXGRP)) < 0)
96189463Ssam			err(2, "%s", name);
97186352Ssam		break;
98189463Ssam	case 'y':
99189463Ssam		if (chmod(name, (sb.st_mode & ~(S_IXUSR | S_IXGRP)) | S_IXUSR) < 0)
100189463Ssam			err(2, "%s", name);
101189463Ssam		break;
102189463Ssam	case 'b':
103189463Ssam		if (chmod(name, (sb.st_mode & ~(S_IXUSR | S_IXGRP)) | S_IXGRP) < 0)
104189463Ssam			err(2, "%s", name);
105186352Ssam		break;
106186352Ssam	default:
107189463Ssam		usage();
108189463Ssam	}
109186352Ssam	return(sb.st_mode & (S_IXUSR | S_IXGRP) ? 0 : 1);
110186352Ssam}
111189463Ssam
112186352Ssamstatic void
113186352Ssamusage()
114186352Ssam{
115186352Ssam	(void)fprintf(stderr, "usage: biff [n | y | b]\n");
116186352Ssam	exit(2);
117186352Ssam}
118186352Ssam