Home | History | Annotate | Line # | Download | only in ttyflags
ttyflags.c revision 1.9
      1 /*	$NetBSD: ttyflags.c,v 1.9 1997/09/15 11:24:41 lukem 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 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __COPYRIGHT("@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
     36 	All rights reserved.\n");
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 __RCSID("$NetBSD: ttyflags.c,v 1.9 1997/09/15 11:24:41 lukem 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 <string.h>
     55 #include <ttyent.h>
     56 #include <unistd.h>
     57 
     58 int change_all __P((void));
     59 int change_ttyflags __P((struct ttyent *));
     60 int change_ttys __P((char **));
     61 int main __P((int, char *[]));
     62 void usage __P((void));
     63 
     64 int nflag, vflag;
     65 
     66 /*
     67  * Ttyflags sets the device-specific tty flags, based on the contents
     68  * of /etc/ttys.  It can either set all of the ttys' flags, or set
     69  * the flags of the ttys specified on the command line.
     70  */
     71 int
     72 main(argc, argv)
     73 	int argc;
     74 	char *argv[];
     75 {
     76 	int aflag, ch, rval;
     77 
     78 	aflag = nflag = vflag = 0;
     79 	while ((ch = getopt(argc, argv, "anv")) != -1)
     80 		switch (ch) {
     81 		case 'a':
     82 			aflag = 1;
     83 			break;
     84 		case 'n':		/* undocumented */
     85 			nflag = 1;
     86 			break;
     87 		case 'v':
     88 			vflag = 1;
     89 			break;
     90 		case '?':
     91 		default:
     92 			usage();
     93 		}
     94 	argc -= optind;
     95 	argv += optind;
     96 
     97 	if (aflag && argc != 0)
     98 		usage();
     99 
    100 	rval = 0;
    101 
    102 	if (setttyent() == 0)
    103 		err(1, "setttyent");
    104 
    105 	if (aflag)
    106 		rval = change_all();
    107 	else
    108 		rval = change_ttys(argv);
    109 
    110 	if (endttyent() == 0)
    111 		warn("endttyent");
    112 
    113 	exit(rval);
    114 }
    115 
    116 /*
    117  * Change all /etc/ttys entries' flags.
    118  */
    119 int
    120 change_all()
    121 {
    122 	struct ttyent *tep;
    123 	int rval;
    124 
    125 	rval = 0;
    126 	for (tep = getttyent(); tep != NULL; tep = getttyent())
    127 		if (change_ttyflags(tep))
    128 			rval = 1;
    129 	return (rval);
    130 }
    131 
    132 /*
    133  * Change the specified ttys' flags.
    134  */
    135 int
    136 change_ttys(ttylist)
    137 	char **ttylist;
    138 {
    139 	struct ttyent *tep;
    140 	int rval;
    141 
    142 	rval = 0;
    143 	for (; *ttylist != NULL; ttylist++) {
    144 		tep = getttynam(*ttylist);
    145 		if (tep == NULL) {
    146 			warnx("couldn't find an entry in %s for \"%s\"",
    147 			    _PATH_TTYS, *ttylist);
    148 			rval = 1;
    149 			continue;
    150 		}
    151 
    152 		if (change_ttyflags(tep))
    153 			rval = 1;
    154 	}
    155 	return (rval);
    156 }
    157 
    158 
    159 /*
    160  * Actually do the work; find out what the new flags value should be,
    161  * open the device, and change the flags.
    162  */
    163 int
    164 change_ttyflags(tep)
    165 	struct ttyent *tep;
    166 {
    167 	int fd, flags, rval, st, sep;
    168 	char path[PATH_MAX];
    169 	char strflags[256];
    170 
    171 	st = tep->ty_status;
    172 	sep = flags = rval = 0;
    173 	strflags[0] = '\0';
    174 
    175 
    176 	/* Convert ttyent.h flags into ioctl flags. */
    177 	if (st & TTY_LOCAL) {
    178 		flags |= TIOCFLAG_CLOCAL;
    179 		(void)strcat(strflags, "local");
    180 		sep++;
    181 	}
    182 	if (st & TTY_RTSCTS) {
    183 		flags |= TIOCFLAG_CRTSCTS;
    184 		if (sep++)
    185 			(void)strcat(strflags, "|");
    186 		(void)strcat(strflags, "rtscts");
    187 	}
    188 	if (st & TTY_SOFTCAR) {
    189 		flags |= TIOCFLAG_SOFTCAR;
    190 		if (sep++)
    191 			(void)strcat(strflags, "|");
    192 		(void)strcat(strflags, "softcar");
    193 	}
    194 	if (st & TTY_MDMBUF) {
    195 		flags |= TIOCFLAG_MDMBUF;
    196 		if (sep++)
    197 			(void)strcat(strflags, "|");
    198 		(void)strcat(strflags, "mdmbuf");
    199 	}
    200 
    201 	if (strflags[0] == '\0')
    202 		(void)strcpy(strflags, "none");
    203 
    204 	/* Find the full device path name. */
    205 	(void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
    206 
    207 	if (vflag)
    208 		warnx("setting flags on %s to %s", path, strflags);
    209 	if (nflag)
    210 		return (0);
    211 
    212 	/* Open the device NON-BLOCKING, set the flags, and close it. */
    213 	if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
    214 		if (!(errno == ENXIO ||
    215 		      (errno == ENOENT && (st & TTY_ON) == 0)))
    216 			rval = 1;
    217 		if (rval || vflag)
    218 			warn("open %s", path);
    219 		return (rval);
    220 	}
    221 	if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
    222 		if (errno != ENOTTY || vflag) {
    223 			warn("TIOCSFLAGS on %s", path);
    224 			rval = (errno != ENOTTY);
    225 		}
    226 	if (close(fd) == -1) {
    227 		warn("close %s", path);
    228 		return (1);
    229 	}
    230 	return (rval);
    231 }
    232 
    233 /*
    234  * Print usage information when a bogus set of arguments is given.
    235  */
    236 void
    237 usage()
    238 {
    239 	(void)fprintf(stderr, "usage: ttyflags [-v] [-a | tty ... ]\n");
    240 	exit(1);
    241 }
    242