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