date and time created 84/04/27 10:25:32 by ralph
authorRalph Campbell <ralph@ucbvax.Berkeley.EDU>
Sat, 28 Apr 1984 01:25:32 +0000 (17:25 -0800)
committerRalph Campbell <ralph@ucbvax.Berkeley.EDU>
Sat, 28 Apr 1984 01:25:32 +0000 (17:25 -0800)
SCCS-vsn: lib/libc/gen/getttyent.c 4.1

usr/src/lib/libc/gen/getttyent.c [new file with mode: 0644]

diff --git a/usr/src/lib/libc/gen/getttyent.c b/usr/src/lib/libc/gen/getttyent.c
new file mode 100644 (file)
index 0000000..670e80f
--- /dev/null
@@ -0,0 +1,84 @@
+/* @(#)getttyent.c     4.1 (Berkeley) %G% */
+
+#include <stdio.h>
+#include <ttyent.h>
+
+static char TTYFILE[] = "/etc/ttys";
+static char EMPTY[] = "";
+static FILE *tf = NULL;
+static char line[BUFSIZ+1];
+static struct ttyent tty;
+
+setttyent()
+{
+       if (tf == NULL)
+               tf = fopen(TTYFILE, "r");
+       else
+               rewind(tf);
+}
+
+endttyent()
+{
+       if (tf != NULL) {
+               (void) fclose(tf);
+               tf = NULL;
+       }
+}
+
+static char *
+skip(p)
+register char *p;
+{
+       register int c;
+
+       while ((c = *p) != '\0') {
+               if (c == '#') {
+                       *p = '\0';
+                       break;
+               }
+               if (c == '\t' || c == ' ' || c == '\n') {
+                       *p++ = '\0';
+                       while ((c = *p) == '\t' || c == ' ' || c == '\n')
+                               p++;
+                       break;
+               }
+               p++;
+       }
+       return (p);
+}
+
+struct ttyent *
+getttyent()
+{
+       register char *p, *cp;
+       register int c;
+
+       if (tf == NULL) {
+               if ((tf = fopen(TTYFILE, "r")) == NULL)
+                       return (NULL);
+       }
+       do {
+               p = fgets(line, BUFSIZ, tf);
+               if (p == NULL)
+                       return (NULL);
+               while ((c = *p) == '\t' || c == ' ' || c == '\n')
+                       p++;
+       } while (c == '\0' || c == '#');
+       tty.ty_name = p;
+       p = skip(p);
+       tty.ty_getty = p;
+       p = skip(p);
+       tty.ty_type = p;
+       tty.ty_status = 0;
+       for (p = skip(p); *p; p = cp) {
+               cp = skip(p);
+               if (strcmp(p, "on") == 0)
+                       tty.ty_status |= TTY_ON;
+               else if (strcmp(p, "off") == 0)
+                       tty.ty_status &= ~TTY_ON;
+               else if (strcmp(p, "secure") == 0)
+                       tty.ty_status |= TTY_SECURE;
+       }
+       tty.ty_comment = EMPTY;
+       return (&tty);
+}