1 /* getttyent(3) - get a ttytab entry Author: Kees J. Bot
10 #include <sys/types.h>
15 #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
16 #define arraylimit(a) ((a) + arraysize(a))
18 static char TTYTAB
[]= "/etc/ttytab"; /* The table of terminal devices. */
20 static char buf
[512]; /* Read buffer. */
21 static char ttline
[256]; /* One line from the ttytab file. */
22 static char *ttargv
[32]; /* Compound arguments. */
23 static struct ttyent entry
; /* Entry to fill and return. */
24 static int ttfd
= -1; /* Filedescriptor to the file. */
25 static char *bufptr
; /* Place in buf. */
26 static ssize_t buflen
= 0; /* Remaining characters in buf. */
27 static char *lineptr
; /* Place in the line. */
28 static char **argvptr
; /* Place in word lists. */
31 /* Close the ttytab file. */
41 /* Open the ttytab file. */
43 if (ttfd
>= 0) endttyent();
45 if ((ttfd
= open(TTYTAB
, O_RDONLY
)) < 0) return -1;
46 (void) fcntl(ttfd
, F_SETFD
, fcntl(ttfd
, F_GETFD
) | FD_CLOEXEC
);
50 static int getline(void)
51 /* Get one line from the ttytab file, return 0 if bad or EOF. */
58 if ((buflen
= read(ttfd
, buf
, sizeof(buf
))) <= 0)
63 if (lineptr
== arraylimit(ttline
)) return 0;
65 } while ((*lineptr
++ = *bufptr
++) != '\n');
71 static int white(int c
)
74 return c
== ' ' || c
== '\t';
77 static char *scan_white(int quoted
)
78 /* Scan for a field separator in a line, return the start of the field.
79 * "quoted" is set if we have to watch out for double quotes.
84 while (white(*lineptr
)) lineptr
++;
85 if (!quoted
&& *lineptr
== '#') return nil
;
90 if (*lineptr
== 0) return nil
;
91 if (*lineptr
== '\n') break;
92 if (quoted
&& *lineptr
== '"') return field
;
93 if (white(*lineptr
++)) break;
96 return *field
== 0 ? nil
: field
;
99 static char **scan_quoted(void)
100 /* Read a field that may be a quoted list of words. */
102 char *p
, **field
= argvptr
;
104 while (white(*lineptr
)) lineptr
++;
106 if (*lineptr
== '"') {
107 /* Quoted list of words. */
109 while ((p
= scan_white(1)) != nil
&& *p
!= '"') {
110 if (argvptr
== arraylimit(ttargv
)) return nil
;
113 if (*lineptr
== '"') *lineptr
++= 0;
116 if ((p
= scan_white(0)) == nil
) return nil
;
117 if (argvptr
== arraylimit(ttargv
)) return nil
;
120 if (argvptr
== arraylimit(ttargv
)) return nil
;
125 struct ttyent
*getttyent(void)
126 /* Read one entry from the ttytab file. */
128 /* Open the file if not yet open. */
129 if (ttfd
< 0 && setttyent() < 0) return nil
;
131 /* Look for a line with something on it. */
133 if (!getline()) return nil
; /* EOF or corrupt. */
135 if ((entry
.ty_name
= scan_white(0)) == nil
) continue;
136 entry
.ty_type
= scan_white(0);
137 entry
.ty_getty
= scan_quoted();
138 entry
.ty_init
= scan_quoted();
144 struct ttyent
*getttynam(const char *name
)
145 /* Return the ttytab file entry for a given tty. */
150 while ((tty
= getttyent()) != nil
&& strcmp(tty
->ty_name
, name
) != 0) {}