#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <libintl.h>
#include <sys/types.h>
#include <sys/stat.h>
static void error(const char *s);
static void newmode(mode_t m);
static void usage(void);
static char *tty;
int
main(int argc, char *argv[])
{
int i, c, r = 0;
int action = 0;
struct stat sbuf;
extern int optind;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
for (i = 0; i <= 2; i++) {
if ((tty = ttyname(i)) != NULL)
break;
}
if (stat(tty, &sbuf) < 0)
error("cannot stat");
if (argc < 2) {
if (sbuf.st_mode & (S_IWGRP | S_IWOTH)) {
(void) printf("is y\n");
} else {
r = 1;
(void) printf("is n\n");
}
exit(r);
}
while ((c = getopt(argc, argv, "yn")) != EOF) {
switch (c) {
case 'y':
if (action > 0)
usage();
newmode(S_IRUSR | S_IWUSR | S_IWGRP);
action++;
break;
case 'n':
if (action > 0)
usage();
newmode(S_IRUSR | S_IWUSR);
r = 1;
action++;
break;
case '?':
usage();
break;
}
}
if (argc > optind) {
if (action > 0)
usage();
switch (*argv[optind]) {
case 'y':
newmode(S_IRUSR | S_IWUSR | S_IWGRP);
break;
case 'n':
newmode(S_IRUSR | S_IWUSR);
r = 1;
break;
default:
usage();
break;
}
}
return (r);
}
void
error(const char *s)
{
(void) fprintf(stderr, "mesg: ");
(void) fprintf(stderr, "%s\n", s);
exit(2);
}
void
newmode(mode_t m)
{
if (chmod(tty, m) < 0)
error("cannot change mode");
}
void
usage(void)
{
(void) fprintf(stderr, gettext("usage: mesg [-y | -n | y | n]\n"));
exit(2);
}