- Check stdin, stdout, and stderr for a tty. From Liam Foy for SUSv3

compliance.
- While here, delint, prototypes, getsetprogname, warns=3
This commit is contained in:
christos 2005-07-30 16:14:39 +00:00
parent 86741d79ab
commit d8213a2a2a
2 changed files with 19 additions and 18 deletions

View File

@ -1,6 +1,7 @@
# $NetBSD: Makefile,v 1.5 1997/10/19 04:23:04 lukem Exp $
# $NetBSD: Makefile,v 1.6 2005/07/30 16:14:39 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
WARNS=3
PROG= mesg
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: mesg.c,v 1.6 2003/08/07 11:15:12 agc Exp $ */
/* $NetBSD: mesg.c,v 1.7 2005/07/30 16:14:39 christos Exp $ */
/*
* Copyright (c) 1987, 1993
@ -45,7 +45,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
#if 0
static char sccsid[] = "@(#)mesg.c 8.2 (Berkeley) 1/21/94";
#endif
__RCSID("$NetBSD: mesg.c,v 1.6 2003/08/07 11:15:12 agc Exp $");
__RCSID("$NetBSD: mesg.c,v 1.7 2005/07/30 16:14:39 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -58,17 +58,15 @@ __RCSID("$NetBSD: mesg.c,v 1.6 2003/08/07 11:15:12 agc Exp $");
#include <string.h>
#include <unistd.h>
int main __P((int, char **));
int
main(argc, argv)
int argc;
char *argv[];
main(int argc, char *argv[])
{
struct stat sb;
char *tty;
int ch;
setprogname(*argv);
while ((ch = getopt(argc, argv, "")) != -1)
switch (ch) {
case '?':
@ -78,31 +76,33 @@ main(argc, argv)
argc -= optind;
argv += optind;
if ((tty = ttyname(STDERR_FILENO)) == NULL)
if ((tty = ttyname(STDIN_FILENO)) == NULL &&
(tty = ttyname(STDOUT_FILENO)) == NULL &&
(tty = ttyname(STDERR_FILENO)) == NULL)
err(2, "ttyname");
if (stat(tty, &sb) < 0)
if (stat(tty, &sb) == -1)
err(2, "%s", tty);
if (*argv == NULL) {
if (sb.st_mode & S_IWGRP) {
(void)fprintf(stderr, "is y\n");
exit(0);
return 0;
}
(void)fprintf(stderr, "is n\n");
exit(1);
return 1;
}
switch (*argv[0]) {
case 'y':
if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
if (chmod(tty, sb.st_mode | S_IWGRP) == -1)
err(2, "%s", tty);
exit(0);
return 0;
case 'n':
if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0)
if (chmod(tty, sb.st_mode & ~S_IWGRP) == -1)
err(2, "%s", tty);
exit(1);
return 1;
}
usage: (void)fprintf(stderr, "usage: mesg [y | n]\n");
exit(2);
usage: (void)fprintf(stderr, "Usage: %s [y | n]\n", getprogname());
return 2;
}