diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1996-08-30 00:53:27 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1996-08-30 00:53:27 +0000 |
commit | c85c48ce5c7db7f6c194b278020cfa6c143b39cf (patch) | |
tree | 0c867cd1bf161955d5481a792e59a2b4d134b09b /sbin/ttyflags/ttyflags.c | |
parent | 9649052792b6a12bc1a4553ffd60a88df5147b56 (diff) |
add -p for printing; cleanup error handling
Diffstat (limited to 'sbin/ttyflags/ttyflags.c')
-rw-r--r-- | sbin/ttyflags/ttyflags.c | 141 |
1 files changed, 84 insertions, 57 deletions
diff --git a/sbin/ttyflags/ttyflags.c b/sbin/ttyflags/ttyflags.c index 921b2aa6f5c..b9af52a8cd7 100644 --- a/sbin/ttyflags/ttyflags.c +++ b/sbin/ttyflags/ttyflags.c @@ -1,7 +1,8 @@ -/* $OpenBSD: ttyflags.c,v 1.3 1996/06/23 14:32:48 deraadt Exp $ */ +/* $OpenBSD: ttyflags.c,v 1.4 1996/08/30 00:53:26 deraadt Exp $ */ /* $NetBSD: ttyflags.c,v 1.8 1996/04/09 05:20:30 cgd Exp $ */ /* + * Copyright (c) 1996 Theo de Raadt * Copyright (c) 1994 Christopher G. Demetriou * All rights reserved. * @@ -38,7 +39,7 @@ char copyright[] = #endif /* not lint */ #ifndef lint -static char rcsid[] = "$OpenBSD: ttyflags.c,v 1.3 1996/06/23 14:32:48 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: ttyflags.c,v 1.4 1996/08/30 00:53:26 deraadt Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -56,9 +57,9 @@ static char rcsid[] = "$OpenBSD: ttyflags.c,v 1.3 1996/06/23 14:32:48 deraadt Ex #include <ttyent.h> #include <unistd.h> -int change_all __P((void)); -int change_ttyflags __P((struct ttyent *)); -int change_ttys __P((char **)); +int all __P((int)); +int ttys __P((char **, int)); +int ttyflags __P((struct ttyent *, int)); void usage __P((void)); int nflag, vflag; @@ -73,10 +74,10 @@ main(argc, argv) int argc; char *argv[]; { - int aflag, ch, rval; + int aflag, ch, rval, pflag = 0; aflag = nflag = vflag = 0; - while ((ch = getopt(argc, argv, "anv")) != EOF) + while ((ch = getopt(argc, argv, "panv")) != EOF) switch (ch) { case 'a': aflag = 1; @@ -84,6 +85,9 @@ main(argc, argv) case 'n': /* undocumented */ nflag = 1; break; + case 'p': + pflag = 1; + break; case 'v': vflag = 1; break; @@ -97,15 +101,13 @@ main(argc, argv) if (aflag && argc != 0) usage(); - rval = 0; - if (setttyent() == 0) err(1, "setttyent"); if (aflag) - rval = change_all(); + rval = all(pflag); else - rval = change_ttys(argv); + rval = ttys(argv, pflag); if (endttyent() == 0) warn("endttyent"); @@ -117,14 +119,15 @@ main(argc, argv) * Change all /etc/ttys entries' flags. */ int -change_all() +all(print) + int print; { struct ttyent *tep; int rval; rval = 0; for (tep = getttyent(); tep != NULL; tep = getttyent()) - if (change_ttyflags(tep)) + if (ttyflags(tep, print)) rval = 1; return (rval); } @@ -133,8 +136,9 @@ change_all() * Change the specified ttys' flags. */ int -change_ttys(ttylist) +ttys(ttylist, print) char **ttylist; + int print; { struct ttyent *tep; int rval; @@ -149,7 +153,7 @@ change_ttys(ttylist) continue; } - if (change_ttyflags(tep)) + if (ttyflags(tep, print)) rval = 1; } return (rval); @@ -161,51 +165,46 @@ change_ttys(ttylist) * open the device, and change the flags. */ int -change_ttyflags(tep) +ttyflags(tep, print) struct ttyent *tep; + int print; { - int fd, flags, rval, st, sep; + int fd, flags = 0, rval, st = 0, sep = 0; char path[PATH_MAX]; - char strflags[256]; - - st = tep->ty_status; - sep = flags = rval = 0; - strflags[0] = '\0'; - + char strflags[256] = ""; - /* Convert ttyent.h flags into ioctl flags. */ - if (st & TTY_LOCAL) { - flags |= TIOCFLAG_CLOCAL; - (void)strcat(strflags, "local"); - sep++; - } - if (st & TTY_RTSCTS) { - flags |= TIOCFLAG_CRTSCTS; - if (sep++) - (void)strcat(strflags, "|"); - (void)strcat(strflags, "rtscts"); - } - if (st & TTY_SOFTCAR) { - flags |= TIOCFLAG_SOFTCAR; - if (sep++) - (void)strcat(strflags, "|"); - (void)strcat(strflags, "softcar"); - } - if (st & TTY_MDMBUF) { - flags |= TIOCFLAG_MDMBUF; - if (sep++) - (void)strcat(strflags, "|"); - (void)strcat(strflags, "mdmbuf"); + if (print == 0) { + /* Convert ttyent.h flags into ioctl flags. */ + if (st & TTY_LOCAL) { + flags |= TIOCFLAG_CLOCAL; + (void)strcat(strflags, "local"); + sep++; + } + if (st & TTY_RTSCTS) { + flags |= TIOCFLAG_CRTSCTS; + if (sep++) + (void)strcat(strflags, "|"); + (void)strcat(strflags, "rtscts"); + } + if (st & TTY_SOFTCAR) { + flags |= TIOCFLAG_SOFTCAR; + if (sep++) + (void)strcat(strflags, "|"); + (void)strcat(strflags, "softcar"); + } + if (st & TTY_MDMBUF) { + flags |= TIOCFLAG_MDMBUF; + if (sep++) + (void)strcat(strflags, "|"); + (void)strcat(strflags, "mdmbuf"); + } + if (vflag) + printf("%s setting flags to: %s\n", path, strflags); } - if (strflags[0] == '\0') - (void)strcpy(strflags, "none"); - /* Find the full device path name. */ (void)snprintf(path, sizeof path, "%s%s", _PATH_DEV, tep->ty_name); - if (vflag) - warnx("setting flags on %s to %s", path, strflags); if (nflag) return (0); @@ -214,15 +213,43 @@ change_ttyflags(tep) if (!(errno == ENXIO || (errno == ENOENT && (st & TTY_ON) == 0))) rval = 1; - if (rval || vflag) + if (vflag) warn("open %s", path); return (rval); } - if (ioctl(fd, TIOCSFLAGS, &flags) == -1) - if (errno != ENOTTY || vflag) { - warn("TIOCSFLAGS on %s", path); - rval = (errno != ENOTTY); + if (print == 0) { + if (ioctl(fd, TIOCSFLAGS, &flags) == -1) + if (errno != ENOTTY || vflag) { + warn("TIOCSFLAGS on %s", path); + rval = (errno != ENOTTY); + } + } else { + if (ioctl(fd, TIOCGFLAGS, &flags) == -1) + if (errno != ENOTTY || vflag) { + warn("TIOCSFLAGS on %s", path); + rval = (errno != ENOTTY); + } + if (flags & TIOCFLAG_CLOCAL) { + (void)strcat(strflags, "local"); + sep++; } + if (flags & TIOCFLAG_CRTSCTS) { + if (sep++) + (void)strcat(strflags, "|"); + (void)strcat(strflags, "rtscts"); + } + if (flags & TIOCFLAG_SOFTCAR) { + if (sep++) + (void)strcat(strflags, "|"); + (void)strcat(strflags, "softcar"); + } + if (flags & TIOCFLAG_MDMBUF) { + if (sep++) + (void)strcat(strflags, "|"); + (void)strcat(strflags, "mdmbuf"); + } + printf("%s flags are: %s\n", path, strflags); + } if (close(fd) == -1) { warn("close %s", path); return (1); @@ -236,6 +263,6 @@ change_ttyflags(tep) void usage() { - (void)fprintf(stderr, "usage: ttyflags [-v] [-a | tty ... ]\n"); + (void)fprintf(stderr, "usage: ttyflags [-v] [-p] [-a | tty ... ]\n"); exit(1); } |