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