File: mesg.c

package info (click to toggle)
sysvinit 2.75-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 552 kB
  • ctags: 423
  • sloc: ansic: 4,469; sh: 816; makefile: 115
file content (77 lines) | stat: -rw-r--r-- 1,699 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * mesg.c	The "mesg" utility. Gives / restrict access to
 *		your terminal by others.
 *
 * Usage:	mesg [y|n].
 *		Without arguments prints out the current settings.
 *
 *		This file is part of the sysvinit suite,
 *		Copyright 1991-1998 Miquel van Smoorenburg.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <grp.h>

char *Version = "@(#) mesg 2.74 08-Apr-1998 miquels@cistron.nl";

/*
 *	Here we check if this system has a "tty" group
 *	for the tty device. If it does, we set the modes
 *	to -rw--w--- instead if -rw--w--w.
 */
int hasttygrp(struct stat *st)
{
	struct group *gr;

	if ((gr = getgrgid(st->st_gid)) == NULL)
		return 0;
	if (strcmp(gr->gr_name, "tty") != 0)
		return 0;

	return 1;
}

int main(int argc, char **argv)
{
	struct stat st;
	unsigned int ttymode;

	if (!isatty(0)) {
		/* Or should we look in /var/run/utmp? */
		fprintf(stderr, "stdin: is not a tty\n");
		return(1);
	}

	if (fstat(0, &st) < 0) {
		perror("fstat");
		return(1);
	}

	ttymode = hasttygrp(&st) ? 020 : 022;

	if (argc < 2) {
		printf("is %s\n",
			((st.st_mode & ttymode) == ttymode) ? "y" : "n");
		return 0;
	}
	if (argc > 2 || (argv[1][0] != 'y' && argv[1][0] != 'n')) {
		fprintf(stderr, "Usage: mesg [y|n]\n");
		return 1;
	}
	if (argv[1][0] == 'y')
		st.st_mode |= ttymode;
	else
		st.st_mode &= ~(ttymode);
	fchmod(0, st.st_mode);

	return 0;
}