Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/std/mesg.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1989-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* mesg.c
23
* Written by David Korn
24
* Thu Aug 15 15:25:29 EDT 1996
25
*/
26
27
static const char usage[] =
28
"[-?\n@(#)$Id: mesg (AT&T Research) 1999-04-28 $\n]"
29
USAGE_LICENSE
30
"[+NAME?mesg - permit or deny messages to the terminal]"
31
"[+DESCRIPTION?\bmesg\b controls whether other users are allowed to send"
32
" messages via \bwrite\b(1), \btalk\b(1) or other commands to the"
33
" terminal device. The terminal device affected is determined by"
34
" searching for the first terminal in the sequence of devices"
35
" associated with standard input, standard output and standard error,"
36
" respectively. With no arguments, \bmesg\b reports the current state"
37
" without changing it. Processes with appropriate privileges may be"
38
" able to send messages to the terminal independent of the"
39
" current state.]"
40
41
"\n"
42
"\n[ y | n ]\n"
43
"\n"
44
45
"[+OPERANDS?The following operands are supported:]{"
46
" [+y?Grant permission to other users to send messages to the terminal.]"
47
" [+n?Deny permission to other users to send messages to the terminal.]"
48
"}"
49
"[+SEE ALSO?\btalk\b(1), \bwrite\b(1)]"
50
;
51
52
#include <cmd.h>
53
#include <ls.h>
54
55
static int mesg(int mode)
56
{
57
struct stat statb;
58
char *tty = ttyname(2);
59
if(!tty)
60
tty = ttyname(0);
61
if(!tty)
62
tty = ttyname(1);
63
if(!tty)
64
error(ERROR_exit(1),"cannot find terminal");
65
if(stat(tty,&statb)<0)
66
error(ERROR_system(1),"%s: cannot stat",tty);
67
switch(mode)
68
{
69
case 'n':
70
case 'y':
71
if(mode=='y')
72
statb.st_mode |= S_IWGRP;
73
else
74
statb.st_mode &= ~S_IWGRP;
75
if(chmod(tty, statb.st_mode) < 0)
76
error(ERROR_system(1),"%s: cannot stat",tty);
77
break;
78
case 0:
79
sfprintf(sfstdout,"%c\n",(statb.st_mode&S_IWGRP)?'y':'n');
80
}
81
return((statb.st_mode&S_IWGRP)==0);
82
}
83
84
int
85
main(int argc, char *argv[])
86
{
87
register int n;
88
89
NoP(argc);
90
error_info.id = "mesg";
91
for (;;)
92
{
93
switch (optget(argv, usage))
94
{
95
case ':':
96
error(2, "%s", opt_info.arg);
97
break;
98
case '?':
99
error(ERROR_usage(2), "%s", opt_info.arg);
100
break;
101
}
102
break;
103
}
104
argv += opt_info.index;
105
n = 0;
106
if(error_info.errors || (*argv && (n= **argv) !='y' && n!='n'))
107
error(ERROR_usage(2), "%s", optusage(NiL));
108
return mesg(n);
109
}
110
111