source: trunk/minix/lib/other/getttyent.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.5 KB
Line 
1/* getttyent(3) - get a ttytab entry Author: Kees J. Bot
2 * 28 Oct 1995
3 */
4#define nil 0
5#define open _open
6#define close _close
7#define fcntl _fcntl
8#define read _read
9#include <string.h>
10#include <sys/types.h>
11#include <ttyent.h>
12#include <unistd.h>
13#include <fcntl.h>
14
15#define arraysize(a) (sizeof(a) / sizeof((a)[0]))
16#define arraylimit(a) ((a) + arraysize(a))
17
18static char TTYTAB[]= "/etc/ttytab"; /* The table of terminal devices. */
19
20static char buf[512]; /* Read buffer. */
21static char ttline[256]; /* One line from the ttytab file. */
22static char *ttargv[32]; /* Compound arguments. */
23static struct ttyent entry; /* Entry to fill and return. */
24static int ttfd= -1; /* Filedescriptor to the file. */
25static char *bufptr; /* Place in buf. */
26static ssize_t buflen= 0; /* Remaining characters in buf. */
27static char *lineptr; /* Place in the line. */
28static char **argvptr; /* Place in word lists. */
29
30void endttyent(void)
31/* Close the ttytab file. */
32{
33 if (ttfd >= 0) {
34 (void) close(ttfd);
35 ttfd= -1;
36 buflen= 0;
37 }
38}
39
40int setttyent(void)
41/* Open the ttytab file. */
42{
43 if (ttfd >= 0) endttyent();
44
45 if ((ttfd= open(TTYTAB, O_RDONLY)) < 0) return -1;
46 (void) fcntl(ttfd, F_SETFD, fcntl(ttfd, F_GETFD) | FD_CLOEXEC);
47 return 0;
48}
49
50static int getline(void)
51/* Get one line from the ttytab file, return 0 if bad or EOF. */
52{
53 lineptr= ttline;
54 argvptr= ttargv;
55
56 do {
57 if (buflen == 0) {
58 if ((buflen= read(ttfd, buf, sizeof(buf))) <= 0)
59 return 0;
60 bufptr= buf;
61 }
62
63 if (lineptr == arraylimit(ttline)) return 0;
64 buflen--;
65 } while ((*lineptr++ = *bufptr++) != '\n');
66
67 lineptr= ttline;
68 return 1;
69}
70
71static int white(int c)
72/* Whitespace? */
73{
74 return c == ' ' || c == '\t';
75}
76
77static 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.
80 */
81{
82 char *field, *last;
83
84 while (white(*lineptr)) lineptr++;
85 if (!quoted && *lineptr == '#') return nil;
86
87 field= lineptr;
88 for (;;) {
89 last= lineptr;
90 if (*lineptr == 0) return nil;
91 if (*lineptr == '\n') break;
92 if (quoted && *lineptr == '"') return field;
93 if (white(*lineptr++)) break;
94 }
95 *last= 0;
96 return *field == 0 ? nil : field;
97}
98
99static char **scan_quoted(void)
100/* Read a field that may be a quoted list of words. */
101{
102 char *p, **field= argvptr;
103
104 while (white(*lineptr)) lineptr++;
105
106 if (*lineptr == '"') {
107 /* Quoted list of words. */
108 lineptr++;
109 while ((p= scan_white(1)) != nil && *p != '"') {
110 if (argvptr == arraylimit(ttargv)) return nil;
111 *argvptr++= p;
112 }
113 if (*lineptr == '"') *lineptr++= 0;
114 } else {
115 /* Just one word. */
116 if ((p= scan_white(0)) == nil) return nil;
117 if (argvptr == arraylimit(ttargv)) return nil;
118 *argvptr++= p;
119 }
120 if (argvptr == arraylimit(ttargv)) return nil;
121 *argvptr++= nil;
122 return field;
123}
124
125struct ttyent *getttyent(void)
126/* Read one entry from the ttytab file. */
127{
128 /* Open the file if not yet open. */
129 if (ttfd < 0 && setttyent() < 0) return nil;
130
131 /* Look for a line with something on it. */
132 for (;;) {
133 if (!getline()) return nil; /* EOF or corrupt. */
134
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();
139
140 return &entry;
141 }
142}
143
144struct ttyent *getttynam(const char *name)
145/* Return the ttytab file entry for a given tty. */
146{
147 struct ttyent *tty;
148
149 endttyent();
150 while ((tty= getttyent()) != nil && strcmp(tty->ty_name, name) != 0) {}
151 endttyent();
152 return tty;
153}
Note: See TracBrowser for help on using the repository browser.