ttyflags.c revision 1.7 1 /* $NetBSD: ttyflags.c,v 1.7 1996/03/17 19:28:21 christos 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.7 1996/03/17 19:28:21 christos 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 /*
158 * Actually do the work; find out what the new flags value should be,
159 * open the device, and change the flags.
160 */
161 int
162 change_ttyflags(tep)
163 struct ttyent *tep;
164 {
165 int fd, flags, rval, st, sep;
166 char path[PATH_MAX];
167 char strflags[256];
168
169 st = tep->ty_status;
170 sep = flags = rval = 0;
171 strflags[0] = '\0';
172
173
174 /* Convert ttyent.h flags into ioctl flags. */
175 if (st & TTY_LOCAL) {
176 flags |= TIOCFLAG_CLOCAL;
177 (void)strcat(strflags, "local");
178 sep++;
179 }
180 if (st & TTY_RTSCTS) {
181 flags |= TIOCFLAG_CRTSCTS;
182 if (sep++)
183 (void)strcat(strflags, "|");
184 (void)strcat(strflags, "rtscts");
185 }
186 if (st & TTY_SOFTCAR) {
187 flags |= TIOCFLAG_SOFTCAR;
188 if (sep++)
189 (void)strcat(strflags, "|");
190 (void)strcat(strflags, "softcar");
191 }
192 if (st & TTY_MDMBUF) {
193 flags |= TIOCFLAG_MDMBUF;
194 if (sep++)
195 (void)strcat(strflags, "|");
196 (void)strcat(strflags, "mdmbuf");
197 }
198
199 if (strflags[0] == '\0')
200 (void)strcpy(strflags, "none");
201
202 /* Find the full device path name. */
203 (void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
204
205 if (vflag)
206 warnx("setting flags on %s to %s", path, strflags);
207 if (nflag)
208 return (0);
209
210 /* Open the device NON-BLOCKING, set the flags, and close it. */
211 if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
212 if (!(errno == ENXIO ||
213 (errno == ENOENT && (st & TTY_ON) == 0)))
214 rval = 1;
215 if (rval || vflag)
216 warn("open %s", path);
217 return (rval);
218 }
219 if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
220 if (errno != ENOTTY || vflag) {
221 warn("TIOCSFLAGS on %s", path);
222 rval = (errno != ENOTTY);
223 }
224 if (close(fd) == -1) {
225 warn("close %s", path);
226 return (1);
227 }
228 return (rval);
229 }
230
231 /*
232 * Print usage information when a bogus set of arguments is given.
233 */
234 void
235 usage()
236 {
237 (void)fprintf(stderr, "usage: ttyflags [-v] [-a | tty ... ]\n");
238 exit(1);
239 }
240