Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/gen It is silly to have to enumerate all the ptys i...



details:   https://anonhg.NetBSD.org/src/rev/33915c05f0b9
branches:  trunk
changeset: 571092:33915c05f0b9
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Nov 11 00:01:38 2004 +0000

description:
It is silly to have to enumerate all the ptys in /etc/ttys in order to
get a valid ttyslot for them. Instead if and entry is not found and we
are a pty, allocate the n + 1 + minor(pty) slot.

diffstat:

 lib/libc/gen/ttyname.3 |  15 ++++++++++--
 lib/libc/gen/ttyslot.c |  55 +++++++++++++++++++++++++++++++++----------------
 2 files changed, 49 insertions(+), 21 deletions(-)

diffs (125 lines):

diff -r 886d47e704bf -r 33915c05f0b9 lib/libc/gen/ttyname.3
--- a/lib/libc/gen/ttyname.3    Thu Nov 11 00:00:15 2004 +0000
+++ b/lib/libc/gen/ttyname.3    Thu Nov 11 00:01:38 2004 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: ttyname.3,v 1.14 2003/08/07 16:42:58 agc Exp $
+.\"    $NetBSD: ttyname.3,v 1.15 2004/11/11 00:01:38 christos Exp $
 .\"
 .\" Copyright (c) 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -60,9 +60,11 @@
 .Pa /dev/tty Ns Em xx
 and for which an entry exists
 in the initialization file
-.Pa /etc/ttys .
+.Pa /etc/ttys 
 (See
-.Xr ttys 5 . )
+.Xr ttys 5 ),
+or for pseudo terminal devices created in ptyfs and named
+.Pa /dev/pts/ Ns Em n .
 .Pp
 The
 .Fn isatty
@@ -86,6 +88,13 @@
 fetches the current process' control terminal number from the
 .Xr ttys 5
 file entry.
+If the terminal is a pseudo terminal, and there is no special entry
+in the
+.Xr ttys 5
+file for it, the slot number returned is 1 + (last slot number) + minor(tty).
+This will return a consistent and unique number of each pseudo terminal
+device without requiring one to enumerate all of them in
+Xr ttys 5 .
 .Sh RETURN VALUES
 The
 .Fn ttyname
diff -r 886d47e704bf -r 33915c05f0b9 lib/libc/gen/ttyslot.c
--- a/lib/libc/gen/ttyslot.c    Thu Nov 11 00:00:15 2004 +0000
+++ b/lib/libc/gen/ttyslot.c    Thu Nov 11 00:01:38 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ttyslot.c,v 1.11 2003/08/07 16:42:58 agc Exp $ */
+/*     $NetBSD: ttyslot.c,v 1.12 2004/11/11 00:01:38 christos Exp $    */
 
 /*
  * Copyright (c) 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)ttyslot.c  8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: ttyslot.c,v 1.11 2003/08/07 16:42:58 agc Exp $");
+__RCSID("$NetBSD: ttyslot.c,v 1.12 2004/11/11 00:01:38 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -44,34 +44,53 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
 
 #ifdef __weak_alias
 __weak_alias(ttyslot,_ttyslot)
 #endif
 
 int
-ttyslot()
+ttyslot(void)
 {
        struct ttyent *ttyp;
-       int slot;
+       int slot = 0, ispty = 0;
        char *p;
        int cnt;
        char *name;
+       struct ptmget ptm;
 
        setttyent();
-       for (cnt = 0; cnt < 3; ++cnt) 
-               if ((name = ttyname(cnt)) != NULL) {
-                       if ((p = strrchr(name, '/')) != NULL)
-                               ++p;
-                       else
-                               p = name;
-                       for (slot = 1; (ttyp = getttyent()) != NULL; ++slot)
-                               if (!strcmp(ttyp->ty_name, p)) {
-                                       endttyent();
-                                       return(slot);
-                               }
-                       break;
-               }
+       for (cnt = 0; cnt < 3; ++cnt) {
+               if (ioctl(cnt, TIOCPTSNAME, &ptm) != -1) {
+                       ispty = 1;
+                       name = ptm.sn;
+               } else if ((name = ttyname(cnt)) != NULL) {
+                       ispty = 0;
+               } else
+                       continue;
+
+               if ((p = strstr(name, "/pts/")) != NULL)
+                       ++p;
+               else if ((p = strrchr(name, '/')) != NULL)
+                       ++p;
+               else
+                       p = name;
+
+               for (slot = 1; (ttyp = getttyent()) != NULL; ++slot)
+                       if (!strcmp(ttyp->ty_name, p)) {
+                               endttyent();
+                               return slot;
+                       }
+               break;
+       }
        endttyent();
-       return(0);
+       if (ispty) {
+               struct stat st;
+               if (fstat(cnt, &st) == -1)
+                       return 0;
+               return slot + minor(st.st_rdev) + 1;
+       }
+       return 0;
 }



Home | Main Index | Thread Index | Old Index