handle long lines; move defines into ttyent.h; move getttynam() into
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 20 Sep 1989 02:24:12 +0000 (18:24 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 20 Sep 1989 02:24:12 +0000 (18:24 -0800)
getttyent.c; make all fields null pointers if unspecified; make setttyent()
and endtttyent() return values; move field explanations into ttys.5;
document it all.  Add Berkeley specific copyright notice.

SCCS-vsn: lib/libc/gen/getttyent.3 6.5
SCCS-vsn: lib/libc/gen/getttyent.c 5.5

usr/src/lib/libc/gen/getttyent.3
usr/src/lib/libc/gen/getttyent.c

index 9e6f76c..f6bbc22 100644 (file)
@@ -1,8 +1,19 @@
-.\" Copyright (c) 1985 Regents of the University of California.
-.\" All rights reserved.  The Berkeley software License Agreement
-.\" specifies the terms and conditions for redistribution.
+.\" Copyright (c) 1989 The Regents of the University of California.
+.\" All rights reserved.
 .\"
 .\"
-.\"    @(#)getttyent.3 6.4 (Berkeley) %G%
+.\" Redistribution and use in source and binary forms are permitted
+.\" provided that the above copyright notice and this paragraph are
+.\" duplicated in all such forms and that any documentation,
+.\" advertising materials, and other materials related to such
+.\" distribution and use acknowledge that the software was developed
+.\" by the University of California, Berkeley.  The name of the
+.\" University may not be used to endorse or promote products derived
+.\" from this software without specific prior written permission.
+.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+.\"
+.\"    @(#)getttyent.3 6.5 (Berkeley) %G%
 .\"
 .TH GETTTYENT 3  ""
 .UC 6
 .\"
 .TH GETTTYENT 3  ""
 .UC 6
@@ -25,70 +36,77 @@ getttyent, getttynam, setttyent, endttyent \- get ttys file entry
 .I Getttyent,
 and
 .I getttynam
 .I Getttyent,
 and
 .I getttynam
-each return a pointer to an object with the
-following structure
-containing the broken-out
-fields of a line from the tty description file.
-.RS
-.PP
+each return a pointer to an object, with the following structure,
+containing the broken-out fields of a line from the tty description
+file.
+.sp
 .nf
 .nf
-.so /usr/include/ttyent.h
-.ft R
-.ad
+struct ttyent {
+       char    *ty_name;                       /* terminal device name */
+       char    *ty_getty;              /* command to execute */
+       char    *ty_type;                       /* terminal type */
+#define        TTY_ON          0x01    /* enable logins */
+#define        TTY_SECURE      0x02    /* allow uid of 0 to login */
+       int     ty_status;              /* flag values */
+       char    *ty_window;             /* command for window manager */
+       char    *ty_comment;            /* comment field */
+};
 .fi
 .fi
-.RE
 .PP
 .PP
-.IP ty_name \w'ty_comment'u+3
-is the name of the character-special file in the directory ``/dev".
-For various reasons, it must reside in the directory ``/dev".
-.IP ty_getty
-is the command (usually
-.IR getty (8))
-which is invoked by
-.I init
+The fields are as follows:
+.TP
+ty_name
+The name of the character-special file.
+.TP
+ty_getty
+The name of the command invoked by
+.IR init (8)
 to initialize tty line characteristics.
 to initialize tty line characteristics.
-In fact, any arbitrary command can be used;
-a typical use is to initiate a terminal emulator in a window system.
-.IP ty_type
-is the name of the default terminal type connected to this tty line. This
-is typically a name from the
-.IR termcap (5)
-data base.
-The environment variable `TERM' is initialized with this name by
-.IR getty (8)
-or
-.IR login (1).
-.IP ty_status
-is a mask of bit fields which indicate various actions to be allowed on this
-tty line. The following is a description of each flag.
-.RS
-.IP TTY_ON \w'TTY_SECURE'u+3
+.TP
+ty_type
+The name of the default terminal type connected to this tty line.
+.TP
+ty_status
+A mask of bit fields which indicate various actions allowed on this
+tty line.
+The possible flags are as follows:
+.IP
+TTY_ON
+.IP
 Enables logins (i.e.,
 .IR init (8)
 Enables logins (i.e.,
 .IR init (8)
-will start the specified ``getty" command
-on this entry).
-.IP TTY_SECURE
-Allows root to login on this terminal. Note that `TTY_ON' must be included
-for this to be useful.
-.RE
-.IP ty_window
-is the command to execute for a window system
-associated with the line.  The window system will be started before
-the command specified in the
+will start the command referenced by
 .I ty_getty
 .I ty_getty
-entry is executed.
-If none is specified, this will be null.
-.IP ty_comment
-is the trailing comment field, if any; a leading delimiter and white space
-will be removed.
+on this entry).
+.IP
+TTY_SECURE
+.IP
+Allow users with a uid of 0 to login on this terminal.
+.TP
+ty_window
+The command to execute for a window system associated with the line.
+.TP
+ty_comment
+Any trailing comment field, with any leading hash marks (``#'') or
+whitespace removed.
+.PP
+If any of the fields pointing to character strings are unspecified,
+they are returned as null pointers.
+The field
+.I ty_status
+will be zero if no flag values are specified.
+.PP
+See
+.IR ttys (5)
+for a more complete discussion of the meaning and usage of the
+fields.
 .PP
 .I Getttyent
 .PP
 .I Getttyent
-reads the next
-line from the ttys file, opening the file if necessary;
-.I setttyent
-rewinds the file;
-.I endttyent
-closes it.
+reads the next line from the ttys file, opening the file if necessary.
+.I Setttyent
+rewinds the file if open, or opens the file if it is unopened.
+.I Endttyent
+closes any open files.
 .PP
 .I Getttynam
 searches from the beginning of the file until a matching
 .PP
 .I Getttynam
 searches from the beginning of the file until a matching
@@ -98,12 +116,17 @@ is found
 .SH FILES
 /etc/ttys
 .SH "SEE ALSO"
 .SH FILES
 /etc/ttys
 .SH "SEE ALSO"
-login(1), ttyslot(3), ttys(5), gettytab(5), termcap(5), getty(8), init(8)
+login(1), ttyslot(3), gettytab(5), termcap(5), ttys(5), getty(8), init(8)
 .SH DIAGNOSTICS
 .SH DIAGNOSTICS
-Null pointer
-(0) returned on EOF or error.
+The routines
+.I getttyent
+and
+.I getttynam
+return a null pointer on EOF or error.
+.I Setttyent
+and
+.I endttyent
+return 0 on failure and 1 on success.
 .SH BUGS
 .SH BUGS
-All information
-is contained in a static area
-so it must be copied if it is
-to be saved.
+All information is contained in a static area so it must be copied if
+it is to be saved.
index 321b2f5..a7a99f3 100644 (file)
 /*
 /*
- * Copyright (c) 1985 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1989 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)getttyent.c        5.4 (Berkeley) %G%";
-#endif LIBC_SCCS and not lint
+static char sccsid[] = "@(#)getttyent.c        5.5 (Berkeley) %G%";
+#endif /* LIBC_SCCS and not lint */
 
 
+#include <ttyent.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <ctype.h>
 #include <strings.h>
 #include <strings.h>
-#include <ttyent.h>
 
 
-static char TTYFILE[] = "/etc/ttys";
 static char zapchar;
 static char zapchar;
-static FILE *tf = NULL;
-#define LINE 256
-static char line[LINE];
-static struct ttyent tty;
+static FILE *tf;
 
 
-setttyent()
+struct ttyent *
+getttynam(tty)
+       char *tty;
 {
 {
-       if (tf == NULL)
-               tf = fopen(TTYFILE, "r");
-       else
-               rewind(tf);
+       register struct ttyent *t;
+
+       setttyent();
+       while (t = getttyent())
+               if (!strcmp(tty, t->ty_name))
+                       break;
+       return(t);
 }
 
 }
 
-endttyent()
+struct ttyent *
+getttyent()
 {
 {
-       if (tf != NULL) {
-               (void) fclose(tf);
-               tf = NULL;
+       static struct ttyent tty;
+       register int c;
+       register char *p;
+#define        MAXLINELENGTH   100
+       static char line[MAXLINELENGTH];
+       char *skip(), *value();
+
+       if (!tf && !setttyent())
+               return(NULL);
+       do {
+               if (!fgets(line, sizeof(line), tf))
+                       return(NULL);
+               /* skip lines that are too big */
+               if (!index(line, '\n')) {
+                       while ((c = getc(tf)) != '\n' && c != EOF)
+                               ;
+                       continue;
+               }
+               for (p = line; isspace(*p); ++p)
+                       ;
+       } while (!*p || *p == '#');
+
+       zapchar = 0;
+       tty.ty_name = p;
+       p = skip(p);
+       if (!*(tty.ty_getty = p))
+               tty.ty_getty = tty.ty_type = NULL;
+       else {
+               p = skip(p);
+               if (!*(tty.ty_type = p))
+                       tty.ty_type = NULL;
+               else
+                       p = skip(p);
        }
        }
+       tty.ty_status = 0;
+       tty.ty_window = NULL;
+
+#define        scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
+#define        vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
+       for (; *p; p = skip(p)) {
+               if (scmp(_TTYS_OFF))
+                       tty.ty_status &= ~TTY_ON;
+               else if (scmp(_TTYS_ON))
+                       tty.ty_status |= TTY_ON;
+               else if (scmp(_TTYS_SECURE))
+                       tty.ty_status |= TTY_SECURE;
+               else if (vcmp(_TTYS_WINDOW))
+                       tty.ty_window = value(p);
+               else
+                       break;
+       }
+
+       if (zapchar == '#' || *p == '#')
+               while ((c = *++p) == ' ' || c == '\t')
+                       ;
+       tty.ty_comment = p;
+       if (*p == 0)
+               tty.ty_comment = 0;
+       if (p = index(p, '\n'))
+               *p = '\0';
+       return(&tty);
 }
 
 }
 
-#define QUOTED 1
+#define        QUOTED  1
 
 /*
 
 /*
- * Skip over the current field, removing quotes,
- * and return a pointer to the next field.
+ * Skip over the current field, removing quotes, and return a pointer to
+ * the next field.
  */
 static char *
 skip(p)
        register char *p;
 {
  */
 static char *
 skip(p)
        register char *p;
 {
-       register char *t = p;
-       register int c;
-       register int q = 0;
+       register char *t;
+       register int c, q;
 
 
-       for (; (c = *p) != '\0'; p++) {
+       for (q = 0, t = p; (c = *p) != '\0'; p++) {
                if (c == '"') {
                        q ^= QUOTED;    /* obscure, but nice */
                        continue;
                if (c == '"') {
                        q ^= QUOTED;    /* obscure, but nice */
                        continue;
@@ -73,65 +143,34 @@ skip(p)
                }
        }
        *--t = '\0';
                }
        }
        *--t = '\0';
-       return (p);
+       return(p);
 }
 
 static char *
 value(p)
        register char *p;
 {
 }
 
 static char *
 value(p)
        register char *p;
 {
-       if ((p = index(p,'=')) == 0)
-               return(NULL);
-       p++;                    /* get past the = sign */
-       return(p);
+       return((p = index(p, '=')) ? ++p : NULL);
 }
 
 }
 
-struct ttyent *
-getttyent()
+setttyent()
 {
 {
-       register char *p;
-       register int c;
+       if (tf) {
+               (void)rewind(tf);
+               return(1);
+       } else if (tf = fopen(_PATH_TTYS, "r"))
+               return(1);
+       return(0);
+}
 
 
-       if (tf == NULL) {
-               if ((tf = fopen(TTYFILE, "r")) == NULL)
-                       return (NULL);
-       }
-       do {
-               p = fgets(line, LINE, tf);
-               if (p == NULL)
-                       return (NULL);
-               while ((c = *p) == '\t' || c == ' ' || c == '\n')
-                       p++;
-       } while (c == '\0' || c == '#');
-       zapchar = 0;
-       tty.ty_name = p;
-       p = skip(p);
-       tty.ty_getty = p;
-       p = skip(p);
-       tty.ty_type = p;
-       p = skip(p);
-       tty.ty_status = 0;
-       tty.ty_window = NULL;
-       for (; *p; p = skip(p)) {
-#define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n')
-               if (strncmp(p, "on", 2) == 0 && space(2))
-                       tty.ty_status |= TTY_ON;
-               else if (strncmp(p, "off", 3) == 0 && space(3))
-                       tty.ty_status &= ~TTY_ON;
-               else if (strncmp(p, "secure", 6) == 0 && space(6))
-                       tty.ty_status |= TTY_SECURE;
-               else if (strncmp(p, "window=", 7) == 0)
-                       tty.ty_window = value(p);
-               else
-                       break;
+endttyent()
+{
+       int rval;
+
+       if (tf) {
+               rval = !(fclose(tf) == EOF);
+               tf = NULL;
+               return(rval);
        }
        }
-       if (zapchar == '#' || *p == '#')
-               while ((c = *++p) == ' ' || c == '\t')
-                       ;
-       tty.ty_comment = p;
-       if (*p == 0)
-               tty.ty_comment = 0;
-       if (p = index(p, '\n'))
-               *p = '\0';
-       return(&tty);
+       return(1);
 }
 }