[9] | 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 |
|
---|
| 18 | static char TTYTAB[]= "/etc/ttytab"; /* The table of terminal devices. */
|
---|
| 19 |
|
---|
| 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. */
|
---|
| 29 |
|
---|
| 30 | void 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 |
|
---|
| 40 | int 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 |
|
---|
| 50 | static 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 |
|
---|
| 71 | static int white(int c)
|
---|
| 72 | /* Whitespace? */
|
---|
| 73 | {
|
---|
| 74 | return c == ' ' || c == '\t';
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 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.
|
---|
| 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 |
|
---|
| 99 | static 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 |
|
---|
| 125 | struct 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 |
|
---|
| 144 | struct 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 | }
|
---|