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