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