1 1.8 lukem /* $NetBSD: mesg.c,v 1.8 2008/07/21 14:19:24 lukem Exp $ */ 2 1.4 jtc 3 1.3 cgd /* 4 1.4 jtc * Copyright (c) 1987, 1993 5 1.4 jtc * The Regents of the University of California. All rights reserved. 6 1.3 cgd * (c) UNIX System Laboratories, Inc. 7 1.3 cgd * All or some portions of this file are derived from material licensed 8 1.3 cgd * to the University of California by American Telephone and Telegraph 9 1.3 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 1.3 cgd * the permission of UNIX System Laboratories, Inc. 11 1.3 cgd * 12 1.3 cgd * Redistribution and use in source and binary forms, with or without 13 1.3 cgd * modification, are permitted provided that the following conditions 14 1.3 cgd * are met: 15 1.3 cgd * 1. Redistributions of source code must retain the above copyright 16 1.3 cgd * notice, this list of conditions and the following disclaimer. 17 1.3 cgd * 2. Redistributions in binary form must reproduce the above copyright 18 1.3 cgd * notice, this list of conditions and the following disclaimer in the 19 1.3 cgd * documentation and/or other materials provided with the distribution. 20 1.6 agc * 3. Neither the name of the University nor the names of its contributors 21 1.3 cgd * may be used to endorse or promote products derived from this software 22 1.3 cgd * without specific prior written permission. 23 1.3 cgd * 24 1.3 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 1.3 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 1.3 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 1.3 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 1.3 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 1.3 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 1.3 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 1.3 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 1.3 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 1.3 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 1.3 cgd * SUCH DAMAGE. 35 1.3 cgd */ 36 1.3 cgd 37 1.5 mikel #include <sys/cdefs.h> 38 1.5 mikel 39 1.3 cgd #ifndef lint 40 1.8 lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993\ 41 1.8 lukem The Regents of the University of California. All rights reserved."); 42 1.3 cgd #endif /* not lint */ 43 1.3 cgd 44 1.3 cgd #ifndef lint 45 1.4 jtc #if 0 46 1.4 jtc static char sccsid[] = "@(#)mesg.c 8.2 (Berkeley) 1/21/94"; 47 1.4 jtc #endif 48 1.8 lukem __RCSID("$NetBSD: mesg.c,v 1.8 2008/07/21 14:19:24 lukem Exp $"); 49 1.3 cgd #endif /* not lint */ 50 1.3 cgd 51 1.3 cgd #include <sys/types.h> 52 1.3 cgd #include <sys/stat.h> 53 1.4 jtc 54 1.4 jtc #include <err.h> 55 1.4 jtc #include <errno.h> 56 1.3 cgd #include <stdio.h> 57 1.4 jtc #include <stdlib.h> 58 1.4 jtc #include <string.h> 59 1.4 jtc #include <unistd.h> 60 1.3 cgd 61 1.4 jtc int 62 1.7 christos main(int argc, char *argv[]) 63 1.3 cgd { 64 1.4 jtc struct stat sb; 65 1.4 jtc char *tty; 66 1.4 jtc int ch; 67 1.4 jtc 68 1.7 christos setprogname(*argv); 69 1.7 christos 70 1.5 mikel while ((ch = getopt(argc, argv, "")) != -1) 71 1.4 jtc switch (ch) { 72 1.4 jtc case '?': 73 1.4 jtc default: 74 1.4 jtc goto usage; 75 1.4 jtc } 76 1.4 jtc argc -= optind; 77 1.4 jtc argv += optind; 78 1.3 cgd 79 1.7 christos if ((tty = ttyname(STDIN_FILENO)) == NULL && 80 1.7 christos (tty = ttyname(STDOUT_FILENO)) == NULL && 81 1.7 christos (tty = ttyname(STDERR_FILENO)) == NULL) 82 1.5 mikel err(2, "ttyname"); 83 1.7 christos if (stat(tty, &sb) == -1) 84 1.5 mikel err(2, "%s", tty); 85 1.4 jtc 86 1.4 jtc if (*argv == NULL) { 87 1.4 jtc if (sb.st_mode & S_IWGRP) { 88 1.4 jtc (void)fprintf(stderr, "is y\n"); 89 1.7 christos return 0; 90 1.3 cgd } 91 1.4 jtc (void)fprintf(stderr, "is n\n"); 92 1.7 christos return 1; 93 1.3 cgd } 94 1.4 jtc 95 1.4 jtc switch (*argv[0]) { 96 1.3 cgd case 'y': 97 1.7 christos if (chmod(tty, sb.st_mode | S_IWGRP) == -1) 98 1.5 mikel err(2, "%s", tty); 99 1.7 christos return 0; 100 1.3 cgd case 'n': 101 1.7 christos if (chmod(tty, sb.st_mode & ~S_IWGRP) == -1) 102 1.5 mikel err(2, "%s", tty); 103 1.7 christos return 1; 104 1.3 cgd } 105 1.3 cgd 106 1.7 christos usage: (void)fprintf(stderr, "Usage: %s [y | n]\n", getprogname()); 107 1.7 christos return 2; 108 1.3 cgd } 109