No empty .Rs/.Re
[netbsd-mini2440.git] / sbin / ttyflags / ttyflags.c
blob1dcba08555d0dd2340cd907e946e99ab0f6d281a
1 /* $NetBSD: ttyflags.c,v 1.16 2007/01/17 21:59:49 hubertf Exp $ */
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.
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.
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT("@(#) Copyright (c) 1994\
40 Christopher G. Demetriou. All rights reserved.");
41 #endif /* not lint */
43 #ifndef lint
44 __RCSID("$NetBSD: ttyflags.c,v 1.16 2007/01/17 21:59:49 hubertf Exp $");
45 #endif /* not lint */
47 #include <sys/types.h>
48 #include <sys/ioctl.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <limits.h>
54 #include <paths.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <ttyent.h>
59 #include <unistd.h>
61 int change_all(void);
62 int change_ttyflags(struct ttyent *);
63 int change_ttys(char **);
64 void usage(void);
66 int nflag, vflag;
69 * Ttyflags sets the device-specific tty flags, based on the contents
70 * of /etc/ttys. It can either set all of the ttys' flags, or set
71 * the flags of the ttys specified on the command line.
73 int
74 main(int argc, char *argv[])
76 int aflag, ch, rval;
78 aflag = nflag = vflag = 0;
79 while ((ch = getopt(argc, argv, "anv")) != -1)
80 switch (ch) {
81 case 'a':
82 aflag = 1;
83 break;
84 case 'n': /* undocumented */
85 nflag = 1;
86 break;
87 case 'v':
88 vflag = 1;
89 break;
90 case '?':
91 default:
92 usage();
94 argc -= optind;
95 argv += optind;
97 if (aflag && argc != 0)
98 usage();
100 rval = 0;
102 if (setttyent() == 0)
103 err(1, "setttyent");
105 if (aflag)
106 rval = change_all();
107 else
108 rval = change_ttys(argv);
110 if (endttyent() == 0)
111 warn("endttyent");
113 exit(rval);
117 * Change all /etc/ttys entries' flags.
120 change_all(void)
122 struct ttyent *tep;
123 int rval;
125 rval = 0;
126 for (tep = getttyent(); tep != NULL; tep = getttyent())
127 if (change_ttyflags(tep))
128 rval = 1;
129 return (rval);
133 * Change the specified ttys' flags.
136 change_ttys(char **ttylist)
138 struct ttyent *tep;
139 int rval;
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;
151 if (change_ttyflags(tep))
152 rval = 1;
154 return (rval);
159 * Actually do the work; find out what the new flags value should be,
160 * open the device, and change the flags.
163 change_ttyflags(struct ttyent *tep)
165 int fd, flags, rval, st, sep;
166 char path[PATH_MAX];
167 char strflags[256];
169 st = tep->ty_status;
170 sep = flags = rval = 0;
171 strflags[0] = '\0';
174 /* Convert ttyent.h flags into ioctl flags. */
175 if (st & TTY_LOCAL) {
176 flags |= TIOCFLAG_CLOCAL;
177 (void)strlcat(strflags, "local", sizeof(strflags));
178 sep++;
180 if (st & TTY_RTSCTS) {
181 flags |= TIOCFLAG_CRTSCTS;
182 if (sep++)
183 (void)strlcat(strflags, "|", sizeof(strflags));
184 (void)strlcat(strflags, "rtscts", sizeof(strflags));
186 if (st & TTY_DTRCTS) {
187 flags |= TIOCFLAG_CDTRCTS;
188 if (sep++)
189 (void)strlcat(strflags, "|", sizeof(strflags));
190 (void)strlcat(strflags, "dtrcts", sizeof(strflags));
192 if (st & TTY_SOFTCAR) {
193 flags |= TIOCFLAG_SOFTCAR;
194 if (sep++)
195 (void)strlcat(strflags, "|", sizeof(strflags));
196 (void)strlcat(strflags, "softcar", sizeof(strflags));
198 if (st & TTY_MDMBUF) {
199 flags |= TIOCFLAG_MDMBUF;
200 if (sep++)
201 (void)strlcat(strflags, "|", sizeof(strflags));
202 (void)strlcat(strflags, "mdmbuf", sizeof(strflags));
205 if (strflags[0] == '\0')
206 (void)strlcpy(strflags, "none", sizeof(strflags));
208 /* Find the full device path name. */
209 (void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
211 if (vflag)
212 warnx("setting flags on %s to %s", path, strflags);
213 if (nflag)
214 return (0);
216 /* Open the device NON-BLOCKING, set the flags, and close it. */
217 if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
218 if (!(errno == ENXIO ||
219 (errno == ENOENT && (st & TTY_ON) == 0)))
220 rval = 1;
221 if (rval || vflag)
222 warn("open %s", path);
223 return (rval);
225 if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
226 if (errno != ENOTTY || vflag) {
227 warn("TIOCSFLAGS on %s", path);
228 rval = (errno != ENOTTY);
230 if (close(fd) == -1) {
231 warn("close %s", path);
232 return (1);
234 return (rval);
238 * Print usage information when a bogus set of arguments is given.
240 void
241 usage(void)
243 (void)fprintf(stderr, "usage: ttyflags [-v] [-a | tty ... ]\n");
244 exit(1);