ttyflags.c revision 1.12 1 /* $NetBSD: ttyflags.c,v 1.12 2000/06/14 17:25:29 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 for the
18 * NetBSD Project. See http://www.netbsd.org/ for
19 * information about NetBSD.
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 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT("@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
40 All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 __RCSID("$NetBSD: ttyflags.c,v 1.12 2000/06/14 17:25:29 cgd Exp $");
45 #endif /* not lint */
46
47 #include <sys/types.h>
48 #include <sys/cdefs.h>
49 #include <sys/ioctl.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <limits.h>
55 #include <paths.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <ttyent.h>
60 #include <unistd.h>
61
62 int change_all __P((void));
63 int change_ttyflags __P((struct ttyent *));
64 int change_ttys __P((char **));
65 int main __P((int, char *[]));
66 void usage __P((void));
67
68 int nflag, vflag;
69
70 /*
71 * Ttyflags sets the device-specific tty flags, based on the contents
72 * of /etc/ttys. It can either set all of the ttys' flags, or set
73 * the flags of the ttys specified on the command line.
74 */
75 int
76 main(argc, argv)
77 int argc;
78 char *argv[];
79 {
80 int aflag, ch, rval;
81
82 aflag = nflag = vflag = 0;
83 while ((ch = getopt(argc, argv, "anv")) != -1)
84 switch (ch) {
85 case 'a':
86 aflag = 1;
87 break;
88 case 'n': /* undocumented */
89 nflag = 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 rval = 0;
105
106 if (setttyent() == 0)
107 err(1, "setttyent");
108
109 if (aflag)
110 rval = change_all();
111 else
112 rval = change_ttys(argv);
113
114 if (endttyent() == 0)
115 warn("endttyent");
116
117 exit(rval);
118 }
119
120 /*
121 * Change all /etc/ttys entries' flags.
122 */
123 int
124 change_all()
125 {
126 struct ttyent *tep;
127 int rval;
128
129 rval = 0;
130 for (tep = getttyent(); tep != NULL; tep = getttyent())
131 if (change_ttyflags(tep))
132 rval = 1;
133 return (rval);
134 }
135
136 /*
137 * Change the specified ttys' flags.
138 */
139 int
140 change_ttys(ttylist)
141 char **ttylist;
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 (change_ttyflags(tep))
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 change_ttyflags(tep)
169 struct ttyent *tep;
170 {
171 int fd, flags, rval, st, sep;
172 char path[PATH_MAX];
173 char strflags[256];
174
175 st = tep->ty_status;
176 sep = flags = rval = 0;
177 strflags[0] = '\0';
178
179
180 /* Convert ttyent.h flags into ioctl flags. */
181 if (st & TTY_LOCAL) {
182 flags |= TIOCFLAG_CLOCAL;
183 (void)strcat(strflags, "local");
184 sep++;
185 }
186 if (st & TTY_RTSCTS) {
187 flags |= TIOCFLAG_CRTSCTS;
188 if (sep++)
189 (void)strcat(strflags, "|");
190 (void)strcat(strflags, "rtscts");
191 }
192 if (st & TTY_DTRCTS) {
193 flags |= TIOCFLAG_CDTRCTS;
194 if (sep++)
195 (void)strcat(strflags, "|");
196 (void)strcat(strflags, "dtrcts");
197 }
198 if (st & TTY_SOFTCAR) {
199 flags |= TIOCFLAG_SOFTCAR;
200 if (sep++)
201 (void)strcat(strflags, "|");
202 (void)strcat(strflags, "softcar");
203 }
204 if (st & TTY_MDMBUF) {
205 flags |= TIOCFLAG_MDMBUF;
206 if (sep++)
207 (void)strcat(strflags, "|");
208 (void)strcat(strflags, "mdmbuf");
209 }
210
211 if (strflags[0] == '\0')
212 (void)strcpy(strflags, "none");
213
214 /* Find the full device path name. */
215 (void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
216
217 if (vflag)
218 warnx("setting flags on %s to %s", path, strflags);
219 if (nflag)
220 return (0);
221
222 /* Open the device NON-BLOCKING, set the flags, and close it. */
223 if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
224 if (!(errno == ENXIO ||
225 (errno == ENOENT && (st & TTY_ON) == 0)))
226 rval = 1;
227 if (rval || vflag)
228 warn("open %s", path);
229 return (rval);
230 }
231 if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
232 if (errno != ENOTTY || vflag) {
233 warn("TIOCSFLAGS on %s", path);
234 rval = (errno != ENOTTY);
235 }
236 if (close(fd) == -1) {
237 warn("close %s", path);
238 return (1);
239 }
240 return (rval);
241 }
242
243 /*
244 * Print usage information when a bogus set of arguments is given.
245 */
246 void
247 usage()
248 {
249 (void)fprintf(stderr, "usage: ttyflags [-v] [-a | tty ... ]\n");
250 exit(1);
251 }
252