1 /* $NetBSD: ttyflags.c,v 1.16 2007/01/17 21:59:49 hubertf Exp $ */
4 * Copyright (c) 1994 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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>
39 __COPYRIGHT("@(#) Copyright (c) 1994\
40 Christopher G. Demetriou. All rights reserved.");
44 __RCSID("$NetBSD: ttyflags.c,v 1.16 2007/01/17 21:59:49 hubertf Exp $");
47 #include <sys/types.h>
48 #include <sys/ioctl.h>
62 int change_ttyflags(struct ttyent
*);
63 int change_ttys(char **);
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.
74 main(int argc
, char *argv
[])
78 aflag
= nflag
= vflag
= 0;
79 while ((ch
= getopt(argc
, argv
, "anv")) != -1)
84 case 'n': /* undocumented */
97 if (aflag
&& argc
!= 0)
102 if (setttyent() == 0)
108 rval
= change_ttys(argv
);
110 if (endttyent() == 0)
117 * Change all /etc/ttys entries' flags.
126 for (tep
= getttyent(); tep
!= NULL
; tep
= getttyent())
127 if (change_ttyflags(tep
))
133 * Change the specified ttys' flags.
136 change_ttys(char **ttylist
)
142 for (; *ttylist
!= NULL
; ttylist
++) {
143 tep
= getttynam(*ttylist
);
145 warnx("couldn't find an entry in %s for \"%s\"",
146 _PATH_TTYS
, *ttylist
);
151 if (change_ttyflags(tep
))
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
;
170 sep
= flags
= rval
= 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
));
180 if (st
& TTY_RTSCTS
) {
181 flags
|= TIOCFLAG_CRTSCTS
;
183 (void)strlcat(strflags
, "|", sizeof(strflags
));
184 (void)strlcat(strflags
, "rtscts", sizeof(strflags
));
186 if (st
& TTY_DTRCTS
) {
187 flags
|= TIOCFLAG_CDTRCTS
;
189 (void)strlcat(strflags
, "|", sizeof(strflags
));
190 (void)strlcat(strflags
, "dtrcts", sizeof(strflags
));
192 if (st
& TTY_SOFTCAR
) {
193 flags
|= TIOCFLAG_SOFTCAR
;
195 (void)strlcat(strflags
, "|", sizeof(strflags
));
196 (void)strlcat(strflags
, "softcar", sizeof(strflags
));
198 if (st
& TTY_MDMBUF
) {
199 flags
|= TIOCFLAG_MDMBUF
;
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
);
212 warnx("setting flags on %s to %s", path
, strflags
);
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)))
222 warn("open %s", path
);
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
);
238 * Print usage information when a bogus set of arguments is given.
243 (void)fprintf(stderr
, "usage: ttyflags [-v] [-a | tty ... ]\n");