2005-01-19 23:48:13 +03:00
|
|
|
/* $NetBSD: ttyflags.c,v 1.15 2005/01/19 20:48:13 xtraeme Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1994-03-30 13:29:16 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Christopher G. Demetriou
|
|
|
|
* All rights reserved.
|
2000-06-14 21:24:02 +04:00
|
|
|
*
|
1994-03-30 13:29:16 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
2000-06-14 21:24:02 +04:00
|
|
|
* This product includes software developed for the
|
2003-02-14 19:21:47 +03:00
|
|
|
* NetBSD Project. See http://www.NetBSD.org/ for
|
2000-06-14 21:24:02 +04:00
|
|
|
* information about NetBSD.
|
1994-03-30 13:29:16 +04:00
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
2000-06-14 21:24:02 +04:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
1994-03-30 13:29:16 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2000-06-14 21:24:02 +04:00
|
|
|
*
|
|
|
|
* <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
|
1994-03-30 13:29:16 +04:00
|
|
|
*/
|
|
|
|
|
1997-09-15 15:24:41 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-03-30 13:29:16 +04:00
|
|
|
#ifndef lint
|
1997-09-15 15:24:41 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
|
|
|
|
All rights reserved.\n");
|
1994-03-30 13:29:16 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2005-01-19 23:48:13 +03:00
|
|
|
__RCSID("$NetBSD: ttyflags.c,v 1.15 2005/01/19 20:48:13 xtraeme Exp $");
|
1994-03-30 13:29:16 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1996-04-09 09:20:30 +04:00
|
|
|
#include <string.h>
|
1994-03-30 13:29:16 +04:00
|
|
|
#include <ttyent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2005-01-19 23:48:13 +03:00
|
|
|
int change_all(void);
|
|
|
|
int change_ttyflags(struct ttyent *);
|
|
|
|
int change_ttys(char **);
|
|
|
|
void usage(void);
|
1994-03-30 13:29:16 +04:00
|
|
|
|
|
|
|
int nflag, vflag;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ttyflags sets the device-specific tty flags, based on the contents
|
|
|
|
* of /etc/ttys. It can either set all of the ttys' flags, or set
|
|
|
|
* the flags of the ttys specified on the command line.
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:48:13 +03:00
|
|
|
main(int argc, char *argv[])
|
1994-03-30 13:29:16 +04:00
|
|
|
{
|
|
|
|
int aflag, ch, rval;
|
|
|
|
|
|
|
|
aflag = nflag = vflag = 0;
|
1997-09-15 15:24:41 +04:00
|
|
|
while ((ch = getopt(argc, argv, "anv")) != -1)
|
1994-03-30 13:29:16 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'n': /* undocumented */
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (aflag && argc != 0)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
rval = 0;
|
|
|
|
|
|
|
|
if (setttyent() == 0)
|
|
|
|
err(1, "setttyent");
|
|
|
|
|
|
|
|
if (aflag)
|
|
|
|
rval = change_all();
|
|
|
|
else
|
|
|
|
rval = change_ttys(argv);
|
|
|
|
|
|
|
|
if (endttyent() == 0)
|
|
|
|
warn("endttyent");
|
|
|
|
|
|
|
|
exit(rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change all /etc/ttys entries' flags.
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:48:13 +03:00
|
|
|
change_all(void)
|
1994-03-30 13:29:16 +04:00
|
|
|
{
|
|
|
|
struct ttyent *tep;
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
rval = 0;
|
|
|
|
for (tep = getttyent(); tep != NULL; tep = getttyent())
|
|
|
|
if (change_ttyflags(tep))
|
|
|
|
rval = 1;
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change the specified ttys' flags.
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:48:13 +03:00
|
|
|
change_ttys(char **ttylist)
|
1994-03-30 13:29:16 +04:00
|
|
|
{
|
|
|
|
struct ttyent *tep;
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
rval = 0;
|
|
|
|
for (; *ttylist != NULL; ttylist++) {
|
|
|
|
tep = getttynam(*ttylist);
|
|
|
|
if (tep == NULL) {
|
|
|
|
warnx("couldn't find an entry in %s for \"%s\"",
|
|
|
|
_PATH_TTYS, *ttylist);
|
|
|
|
rval = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change_ttyflags(tep))
|
|
|
|
rval = 1;
|
|
|
|
}
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
1996-03-17 22:28:21 +03:00
|
|
|
|
1994-03-30 13:29:16 +04:00
|
|
|
/*
|
1996-03-17 22:28:21 +03:00
|
|
|
* Actually do the work; find out what the new flags value should be,
|
1994-03-30 13:29:16 +04:00
|
|
|
* open the device, and change the flags.
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:48:13 +03:00
|
|
|
change_ttyflags(struct ttyent *tep)
|
1994-03-30 13:29:16 +04:00
|
|
|
{
|
1996-03-17 22:28:21 +03:00
|
|
|
int fd, flags, rval, st, sep;
|
1994-03-30 13:29:16 +04:00
|
|
|
char path[PATH_MAX];
|
1996-03-17 22:28:21 +03:00
|
|
|
char strflags[256];
|
1994-03-30 13:29:16 +04:00
|
|
|
|
|
|
|
st = tep->ty_status;
|
1996-03-17 22:28:21 +03:00
|
|
|
sep = flags = rval = 0;
|
|
|
|
strflags[0] = '\0';
|
1994-03-30 13:29:16 +04:00
|
|
|
|
1996-03-17 22:28:21 +03:00
|
|
|
|
1994-03-30 13:29:16 +04:00
|
|
|
/* Convert ttyent.h flags into ioctl flags. */
|
1996-03-17 22:28:21 +03:00
|
|
|
if (st & TTY_LOCAL) {
|
1994-03-30 13:29:16 +04:00
|
|
|
flags |= TIOCFLAG_CLOCAL;
|
2003-07-12 18:33:55 +04:00
|
|
|
(void)strlcat(strflags, "local", sizeof(strflags));
|
1996-03-17 22:28:21 +03:00
|
|
|
sep++;
|
|
|
|
}
|
|
|
|
if (st & TTY_RTSCTS) {
|
1994-03-30 13:29:16 +04:00
|
|
|
flags |= TIOCFLAG_CRTSCTS;
|
1996-03-17 22:28:21 +03:00
|
|
|
if (sep++)
|
2003-07-12 18:33:55 +04:00
|
|
|
(void)strlcat(strflags, "|", sizeof(strflags));
|
|
|
|
(void)strlcat(strflags, "rtscts", sizeof(strflags));
|
1996-03-17 22:28:21 +03:00
|
|
|
}
|
1997-10-20 12:07:47 +04:00
|
|
|
if (st & TTY_DTRCTS) {
|
|
|
|
flags |= TIOCFLAG_CDTRCTS;
|
|
|
|
if (sep++)
|
2003-07-12 18:33:55 +04:00
|
|
|
(void)strlcat(strflags, "|", sizeof(strflags));
|
|
|
|
(void)strlcat(strflags, "dtrcts", sizeof(strflags));
|
1997-10-20 12:07:47 +04:00
|
|
|
}
|
1996-03-17 22:28:21 +03:00
|
|
|
if (st & TTY_SOFTCAR) {
|
1994-03-30 13:29:16 +04:00
|
|
|
flags |= TIOCFLAG_SOFTCAR;
|
1996-03-17 22:28:21 +03:00
|
|
|
if (sep++)
|
2003-07-12 18:33:55 +04:00
|
|
|
(void)strlcat(strflags, "|", sizeof(strflags));
|
|
|
|
(void)strlcat(strflags, "softcar", sizeof(strflags));
|
1996-03-17 22:28:21 +03:00
|
|
|
}
|
|
|
|
if (st & TTY_MDMBUF) {
|
1994-03-30 13:29:16 +04:00
|
|
|
flags |= TIOCFLAG_MDMBUF;
|
1996-03-17 22:28:21 +03:00
|
|
|
if (sep++)
|
2003-07-12 18:33:55 +04:00
|
|
|
(void)strlcat(strflags, "|", sizeof(strflags));
|
|
|
|
(void)strlcat(strflags, "mdmbuf", sizeof(strflags));
|
1996-03-17 22:28:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strflags[0] == '\0')
|
2003-07-12 18:33:55 +04:00
|
|
|
(void)strlcpy(strflags, "none", sizeof(strflags));
|
1994-03-30 13:29:16 +04:00
|
|
|
|
|
|
|
/* Find the full device path name. */
|
|
|
|
(void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name);
|
|
|
|
|
|
|
|
if (vflag)
|
1996-03-17 22:28:21 +03:00
|
|
|
warnx("setting flags on %s to %s", path, strflags);
|
1994-03-30 13:29:16 +04:00
|
|
|
if (nflag)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* Open the device NON-BLOCKING, set the flags, and close it. */
|
|
|
|
if ((fd = open(path, O_RDONLY | O_NONBLOCK, 0)) == -1) {
|
1995-08-13 09:24:03 +04:00
|
|
|
if (!(errno == ENXIO ||
|
|
|
|
(errno == ENOENT && (st & TTY_ON) == 0)))
|
1994-03-30 13:53:07 +04:00
|
|
|
rval = 1;
|
|
|
|
if (rval || vflag)
|
|
|
|
warn("open %s", path);
|
|
|
|
return (rval);
|
1994-03-30 13:29:16 +04:00
|
|
|
}
|
1994-03-30 13:53:07 +04:00
|
|
|
if (ioctl(fd, TIOCSFLAGS, &flags) == -1)
|
|
|
|
if (errno != ENOTTY || vflag) {
|
|
|
|
warn("TIOCSFLAGS on %s", path);
|
|
|
|
rval = (errno != ENOTTY);
|
|
|
|
}
|
1994-03-30 13:29:16 +04:00
|
|
|
if (close(fd) == -1) {
|
|
|
|
warn("close %s", path);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print usage information when a bogus set of arguments is given.
|
|
|
|
*/
|
|
|
|
void
|
2005-01-19 23:48:13 +03:00
|
|
|
usage(void)
|
1994-03-30 13:29:16 +04:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr, "usage: ttyflags [-v] [-a | tty ... ]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|