Home | History | Annotate | Line # | Download | only in ttyflags
ttyflags.c revision 1.3
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1994 Christopher G. Demetriou
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *      This product includes software developed by Christopher G. Demetriou.
     16  1.1  cgd  * 4. The name of the author may not be used to endorse or promote products
     17  1.1  cgd  *    derived from this software without specific prior written permission
     18  1.1  cgd  *
     19  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  cgd  */
     30  1.1  cgd 
     31  1.1  cgd #ifndef lint
     32  1.1  cgd char copyright[] =
     33  1.1  cgd "@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
     34  1.1  cgd  All rights reserved.\n";
     35  1.1  cgd #endif /* not lint */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.3  cgd static char rcsid[] = "$Id: ttyflags.c,v 1.3 1994/04/19 03:42:54 cgd Exp $";
     39  1.1  cgd #endif /* not lint */
     40  1.1  cgd 
     41  1.1  cgd #include <sys/types.h>
     42  1.1  cgd #include <sys/cdefs.h>
     43  1.1  cgd #include <sys/ioctl.h>
     44  1.1  cgd 
     45  1.1  cgd #include <err.h>
     46  1.1  cgd #include <errno.h>
     47  1.1  cgd #include <fcntl.h>
     48  1.1  cgd #include <limits.h>
     49  1.1  cgd #include <paths.h>
     50  1.1  cgd #include <stdio.h>
     51  1.1  cgd #include <stdlib.h>
     52  1.1  cgd #include <ttyent.h>
     53  1.1  cgd #include <unistd.h>
     54  1.1  cgd 
     55  1.1  cgd int change_all __P((void));
     56  1.1  cgd int change_ttyflags __P((struct ttyent *));
     57  1.1  cgd int change_ttys __P((char **));
     58  1.1  cgd void usage __P((void));
     59  1.1  cgd 
     60  1.1  cgd int nflag, vflag;
     61  1.1  cgd 
     62  1.1  cgd /*
     63  1.1  cgd  * Ttyflags sets the device-specific tty flags, based on the contents
     64  1.1  cgd  * of /etc/ttys.  It can either set all of the ttys' flags, or set
     65  1.1  cgd  * the flags of the ttys specified on the command line.
     66  1.1  cgd  */
     67  1.1  cgd int
     68  1.1  cgd main(argc, argv)
     69  1.1  cgd 	int argc;
     70  1.1  cgd 	char *argv[];
     71  1.1  cgd {
     72  1.1  cgd 	int aflag, ch, rval;
     73  1.1  cgd 
     74  1.1  cgd 	aflag = nflag = vflag = 0;
     75  1.1  cgd 	while ((ch = getopt(argc, argv, "anv")) != EOF)
     76  1.1  cgd 		switch (ch) {
     77  1.1  cgd 		case 'a':
     78  1.1  cgd 			aflag = 1;
     79  1.1  cgd 			break;
     80  1.1  cgd 		case 'n':		/* undocumented */
     81  1.1  cgd 			nflag = 1;
     82  1.1  cgd 			break;
     83  1.1  cgd 		case 'v':
     84  1.1  cgd 			vflag = 1;
     85  1.1  cgd 			break;
     86  1.1  cgd 		case '?':
     87  1.1  cgd 		default:
     88  1.1  cgd 			usage();
     89  1.1  cgd 		}
     90  1.1  cgd 	argc -= optind;
     91  1.1  cgd 	argv += optind;
     92  1.1  cgd 
     93  1.1  cgd 	if (aflag && argc != 0)
     94  1.1  cgd 		usage();
     95  1.1  cgd 
     96  1.1  cgd 	rval = 0;
     97  1.1  cgd 
     98  1.1  cgd 	if (setttyent() == 0)
     99  1.1  cgd 		err(1, "setttyent");
    100  1.1  cgd 
    101  1.1  cgd 	if (aflag)
    102  1.1  cgd 		rval = change_all();
    103  1.1  cgd 	else
    104  1.1  cgd 		rval = change_ttys(argv);
    105  1.1  cgd 
    106  1.1  cgd 	if (endttyent() == 0)
    107  1.1  cgd 		warn("endttyent");
    108  1.1  cgd 
    109  1.1  cgd 	exit(rval);
    110  1.1  cgd }
    111  1.1  cgd 
    112  1.1  cgd /*
    113  1.1  cgd  * Change all /etc/ttys entries' flags.
    114  1.1  cgd  */
    115  1.1  cgd int
    116  1.1  cgd change_all()
    117  1.1  cgd {
    118  1.1  cgd 	struct ttyent *tep;
    119  1.1  cgd 	int rval;
    120  1.1  cgd 
    121  1.1  cgd 	rval = 0;
    122  1.1  cgd 	for (tep = getttyent(); tep != NULL; tep = getttyent())
    123  1.1  cgd 		if (change_ttyflags(tep))
    124  1.1  cgd 			rval = 1;
    125  1.1  cgd 	return (rval);
    126  1.1  cgd }
    127  1.1  cgd 
    128  1.1  cgd /*
    129  1.1  cgd  * Change the specified ttys' flags.
    130  1.1  cgd  */
    131  1.1  cgd int
    132  1.1  cgd change_ttys(ttylist)
    133  1.1  cgd 	char **ttylist;
    134  1.1  cgd {
    135  1.1  cgd 	struct ttyent *tep;
    136  1.1  cgd 	int rval;
    137  1.1  cgd 
    138  1.1  cgd 	rval = 0;
    139  1.1  cgd 	for (; *ttylist != NULL; ttylist++) {
    140  1.1  cgd 		tep = getttynam(*ttylist);
    141  1.1  cgd 		if (tep == NULL) {
    142  1.1  cgd 			warnx("couldn't find an entry in %s for \"%s\"",
    143  1.1  cgd 			    _PATH_TTYS, *ttylist);
    144  1.1  cgd 			rval = 1;
    145  1.1  cgd 			continue;
    146  1.1  cgd 		}
    147  1.1  cgd 
    148  1.1  cgd 		if (change_ttyflags(tep))
    149  1.1  cgd 			rval = 1;
    150  1.1  cgd 	}
    151  1.1  cgd 	return (rval);
    152  1.1  cgd }
    153  1.1  cgd 
    154  1.1  cgd /*
    155  1.1  cgd  * Acutually do the work; find out what the new flags value should be,
    156  1.1  cgd  * open the device, and change the flags.
    157  1.1  cgd  */
    158  1.1  cgd int
    159  1.1  cgd change_ttyflags(tep)
    160  1.1  cgd 	struct ttyent *tep;
    161  1.1  cgd {
    162  1.1  cgd 	int fd, flags, rval, st;
    163  1.1  cgd 	char path[PATH_MAX];
    164  1.1  cgd 
    165  1.1  cgd 	st = tep->ty_status;
    166  1.1  cgd 	flags = rval = 0;
    167  1.1  cgd 
    168  1.1  cgd 	/* Convert ttyent.h flags into ioctl flags. */
    169  1.1  cgd 	if (st & TTY_LOCAL)
    170  1.1  cgd 		flags |= TIOCFLAG_CLOCAL;
    171  1.1  cgd 	if (st & TTY_RTSCTS)
    172  1.1  cgd 		flags |= TIOCFLAG_CRTSCTS;
    173  1.1  cgd 	if (st & TTY_SOFTCAR)
    174  1.1  cgd 		flags |= TIOCFLAG_SOFTCAR;
    175  1.1  cgd 	if (st & TTY_MDMBUF)
    176  1.1  cgd 		flags |= TIOCFLAG_MDMBUF;
    177  1.1  cgd 
    178  1.1  cgd 	/* Find the full device path name. */
    179  1.1  cgd 	(void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
    180  1.1  cgd 
    181  1.1  cgd 	if (vflag)
    182  1.1  cgd 		warnx("setting flags on %s to %0x", path, flags);
    183  1.1  cgd 	if (nflag)
    184  1.1  cgd 		return (0);
    185  1.1  cgd 
    186  1.1  cgd 	/* Open the device NON-BLOCKING, set the flags, and close it. */
    187  1.1  cgd 	if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
    188  1.3  cgd 		if ((errno != ENOENT && errno != ENXIO) || (st & TTY_ON) != 0)
    189  1.2  cgd 			rval = 1;
    190  1.2  cgd 		if (rval || vflag)
    191  1.2  cgd 			warn("open %s", path);
    192  1.2  cgd 		return (rval);
    193  1.1  cgd 	}
    194  1.2  cgd 	if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
    195  1.2  cgd 		if (errno != ENOTTY || vflag) {
    196  1.2  cgd 			warn("TIOCSFLAGS on %s", path);
    197  1.2  cgd 			rval = (errno != ENOTTY);
    198  1.2  cgd 		}
    199  1.1  cgd 	if (close(fd) == -1) {
    200  1.1  cgd 		warn("close %s", path);
    201  1.1  cgd 		return (1);
    202  1.1  cgd 	}
    203  1.1  cgd 	return (rval);
    204  1.1  cgd }
    205  1.1  cgd 
    206  1.1  cgd /*
    207  1.1  cgd  * Print usage information when a bogus set of arguments is given.
    208  1.1  cgd  */
    209  1.1  cgd void
    210  1.1  cgd usage()
    211  1.1  cgd {
    212  1.1  cgd 	(void)fprintf(stderr, "usage: ttyflags [-v] [-a | tty ... ]\n");
    213  1.1  cgd 	exit(1);
    214  1.1  cgd }
    215