xref: /illumos-gate/usr/src/lib/libc/port/gen/ttyname.c (revision fb91dbc55f18cdb9bafb343e220451f008afa9fb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * ttyname(f): return "/dev/X" (where X is a relative pathname
32  * under /dev/), which is the name of the tty character special
33  * file associated with the file descriptor f, or NULL if the
34  * pathname cannot be found.
35  *
36  * Ttyname tries to find the tty file by matching major/minor
37  * device, file system ID, and inode numbers of the file
38  * descriptor parameter to those of files in the /dev/ directory.
39  *
40  * It attempts to find a match on major/minor device numbers,
41  * file system ID, and inode numbers, but failing to match on
42  * all three, settles for just a match on device numbers and
43  * file system ID.
44  *
45  * To achieve higher performance and more flexible functionality,
46  * ttyname first looks for the tty file in the directories specified
47  * in the configuration file /etc/ttysrch. Entries in /etc/ttysrch
48  * may be qualified to specify that a partial match may be acceptable.
49  * This further improves performance by allowing an entry which
50  * matches major/minor and file system ID, but not inode number
51  * without searching the entire /dev tree. If /etc/ttysrch does not
52  * exist, ttyname looks in a default list of directories.  If after
53  * looking in the most likely places, ttyname still cannot find the
54  * tty file, it recursively searches thru the rest of the /dev/
55  * directory.
56  *
57  * In addition to the public interfaces, ttyname() & ttyname_r(), which
58  * do the lookup based on an open file descriptor,
59  * the private interface _ttyname_dev() does the lookup based on a
60  * major/minor device.  It follows the same order of lookup rules and
61  * returns similar information, but matches on just the major/minor
62  * device numbers.
63  */
64 
65 #pragma weak _ttyname = ttyname
66 
67 #include "lint.h"
68 #include "mtlib.h"
69 #include "libc.h"
70 #include "_libc_gettext.h"
71 #include <sys/sysmacros.h>
72 #include <sys/types.h>
73 #include <dirent.h>
74 #include <fcntl.h>
75 #include <sys/stat.h>
76 #include <stdlib.h>
77 #include <ctype.h>
78 #include <string.h>
79 #include <stdio.h>
80 #include <unistd.h>
81 #include <thread.h>
82 #include <synch.h>
83 #include <errno.h>
84 #include <limits.h>
85 #include <sys/mkdev.h>
86 #include "tsd.h"
87 
88 typedef struct entry {
89 	char *name;
90 	int flags;
91 } entry_t;
92 
93 typedef struct special {
94 	const char *spcl_name;	/* device name */
95 	dev_t spcl_rdev;	/* matching major/minor devnum */
96 	dev_t spcl_fsdev;	/* devnum of containing FS */
97 	ino_t spcl_inum;	/* inum of entry in FS */
98 } spcl_t;
99 
100 static int srch_dir(const entry_t path, int match_mask, int depth,
101     const entry_t skip_dirs[], struct stat64 *fsb);
102 static const entry_t *get_pri_dirs(void);
103 static char *ispts(struct stat64 *fsb, int match_mask);
104 static char *ispty(struct stat64 *fsb, int match_mask);
105 static void itoa(int i, char *ptr);
106 static char *_ttyname_common(struct stat64 *fsp, char *buffer,
107     uint_t match_mask);
108 
109 
110 #define	MAX_DEV_PATH	TTYNAME_MAX
111 #define	MAX_SRCH_DEPTH	4
112 
113 #define	MATCH_MM	1
114 #define	MATCH_FS	2
115 #define	MATCH_INO	4
116 #define	MATCH_ALL	7
117 
118 #define	DEV		"/dev"
119 #define	TTYSRCH		"/etc/ttysrch"
120 #define	PTS		"/dev/pts"
121 
122 static const entry_t dev_dir =
123 	{ "/dev", MATCH_ALL };
124 
125 static const entry_t def_srch_dirs[] = {	/* default search list */
126 	{ "/dev/pts", MATCH_ALL },
127 	{ "/dev/vt", MATCH_ALL },
128 	{ "/dev/term", MATCH_ALL },
129 	{ "/dev/zcons", MATCH_ALL },
130 	{ NULL, 0 }
131 };
132 
133 static char *dir_buf;		/* directory buffer for ttysrch body */
134 static entry_t *dir_vec;	/* directory vector for ttysrch ptrs */
135 static size_t dir_size;		/* ttysrch file size */
136 static timestruc_t dir_mtim;	/* ttysrch file modification time */
137 static char rbuf[MAX_DEV_PATH]; /* perfect match file name */
138 static char dev_rbuf[MAX_DEV_PATH]; /* partial match file name */
139 static int dev_flag;		/* if set, dev + rdev match was found */
140 static spcl_t special_case[] = {
141 	"/dev/tty", 0, 0, 0,
142 	"/dev/console", 0, 0, 0,
143 	"/dev/conslog", 0, 0, 0,
144 	"/dev/systty", 0, 0, 0,
145 	"/dev/wscons", 0, 0, 0,
146 	"/dev/msglog", 0, 0, 0,
147 };
148 #define	NUMSPECIAL	(sizeof (special_case) / sizeof (spcl_t))
149 static spcl_t ptmspecial = {
150 	"/dev/ptmx", 0, 0, 0
151 };
152 static	dev_t	ptcdev = NODEV;
153 static	dev_t	ptsldev = NODEV;
154 
155 char *
_ttyname_dev(dev_t rdev,char * buffer,size_t buflen)156 _ttyname_dev(dev_t rdev, char *buffer, size_t buflen)
157 {
158 	struct stat64 fsb = { 0 };
159 
160 	fsb.st_rdev = rdev;
161 
162 	if (buflen < MAX_DEV_PATH) {
163 		errno = ERANGE;
164 		return (NULL);
165 	}
166 
167 	return (_ttyname_common(&fsb, buffer, MATCH_MM));
168 }
169 
170 /*
171  * POSIX.1c Draft-6 version of the function ttyname_r.
172  * It was implemented by Solaris 2.3.
173  */
174 char *
ttyname_r(int f,char * buffer,int buflen)175 ttyname_r(int f, char *buffer, int buflen)
176 {
177 	struct stat64 fsb;	/* what we are searching for */
178 	/*
179 	 * do we need to search anything at all? (is fildes a char special tty
180 	 * file?)
181 	 */
182 	if (fstat64(f, &fsb) < 0) {
183 		errno = EBADF;
184 		return (0);
185 	}
186 	if ((isatty(f) == 0) ||
187 	    ((fsb.st_mode & S_IFMT) != S_IFCHR)) {
188 		errno = ENOTTY;
189 		return (0);
190 	}
191 
192 	if (buflen < MAX_DEV_PATH) {
193 		errno = ERANGE;
194 		return (0);
195 	}
196 
197 	return (_ttyname_common(&fsb, buffer, MATCH_ALL));
198 }
199 
200 static char *
_ttyname_common(struct stat64 * fsp,char * buffer,uint_t match_mask)201 _ttyname_common(struct stat64 *fsp, char *buffer, uint_t match_mask)
202 {
203 	struct stat64 tfsb;
204 	const entry_t *srch_dirs;	/* priority directories */
205 	spcl_t *spclp;
206 	int i;
207 	int found = 0;
208 	int dirno = 0;
209 	int is_pts = 0;
210 	char *retval = NULL;
211 	char *pt = NULL;
212 
213 	/*
214 	 * We can't use lmutex_lock() here because we call malloc()/free()
215 	 * and _libc_gettext().  Use the brute-force callout_lock_enter().
216 	 */
217 	callout_lock_enter();
218 
219 	/*
220 	 * match special cases
221 	 */
222 
223 	for (spclp = special_case, i = 0; i < NUMSPECIAL; spclp++, i++) {
224 		if ((spclp->spcl_inum | spclp->spcl_fsdev |
225 		    spclp->spcl_rdev) == 0) {
226 			if (stat64(spclp->spcl_name, &tfsb) != 0)
227 				continue;
228 			spclp->spcl_rdev = tfsb.st_rdev;
229 			spclp->spcl_fsdev = tfsb.st_dev;
230 			spclp->spcl_inum = tfsb.st_ino;
231 		}
232 		if (match_mask == MATCH_MM) {
233 			if (spclp->spcl_rdev == fsp->st_rdev) {
234 				retval = strcpy(rbuf, spclp->spcl_name);
235 				goto out;
236 			}
237 		} else if (spclp->spcl_fsdev == fsp->st_dev &&
238 		    spclp->spcl_rdev == fsp->st_rdev &&
239 		    spclp->spcl_inum == fsp->st_ino) {
240 			retval = strcpy(rbuf, spclp->spcl_name);
241 			goto out;
242 		}
243 	}
244 	/*
245 	 * additional special case: ptm clone device
246 	 *   ptm devs have no entries in /dev
247 	 *   if major number matches, just short circuit any further lookup
248 	 *   NOTE: the minor number of /dev/ptmx is the ptm major number
249 	 */
250 	spclp = &ptmspecial;
251 	if ((spclp->spcl_inum | spclp->spcl_fsdev | spclp->spcl_rdev) == 0) {
252 		if (stat64(spclp->spcl_name, &tfsb) == 0) {
253 			spclp->spcl_rdev = tfsb.st_rdev;
254 			spclp->spcl_fsdev = tfsb.st_dev;
255 			spclp->spcl_inum = tfsb.st_ino;
256 		}
257 	}
258 	if ((spclp->spcl_rdev != 0) &&
259 	    (minor(spclp->spcl_rdev) == major(fsp->st_rdev)))
260 		goto out;
261 
262 	/*
263 	 * additional special case: pty dev
264 	 *  one of the known default pairs of /dev/ptyXX or /dev/ttyXX
265 	 */
266 	if ((retval = ispty(fsp, match_mask)) != NULL)
267 		goto out;
268 
269 	/*
270 	 * search the priority directories
271 	 */
272 
273 
274 	srch_dirs = get_pri_dirs();
275 	dev_flag = 0;
276 
277 	while ((!found) && (srch_dirs[dirno].name != NULL)) {
278 
279 		/*
280 		 * if /dev is one of the priority directories, only
281 		 * search its top level(set depth = MAX_SEARCH_DEPTH)
282 		 */
283 
284 		/*
285 		 * Is /dev/pts then just do a quick check. We don't have
286 		 * to stat the entire /dev/pts dir.
287 		 */
288 		if (strcmp(PTS, srch_dirs[dirno].name) == 0) {
289 			if ((pt = ispts(fsp, match_mask)) != NULL) {
290 				is_pts = 1;
291 				found = 1;
292 			}
293 		} else {
294 			found = srch_dir(srch_dirs[dirno], match_mask,
295 			    ((strcmp(srch_dirs[dirno].name, dev_dir.name)
296 			    == 0) ? MAX_SRCH_DEPTH : 1), 0, fsp);
297 		}
298 		dirno++;
299 	}
300 
301 	/*
302 	 * search the /dev/ directory, skipping priority directories
303 	 */
304 	if (!found)
305 		found = srch_dir(dev_dir, match_mask, 0, srch_dirs, fsp);
306 
307 
308 	/*
309 	 * return
310 	 */
311 
312 	if (found) {
313 		if (is_pts)
314 			retval = pt;
315 		else
316 			retval = rbuf;
317 	} else if (dev_flag)
318 		retval = dev_rbuf;
319 	else
320 		retval = NULL;
321 out:	retval = (retval ? strcpy(buffer, retval) : NULL);
322 	callout_lock_exit();
323 	return (retval);
324 }
325 
326 /*
327  * POSIX.1c standard version of the function ttyname_r.
328  * User gets it via static ttyname_r from the header file.
329  */
330 int
__posix_ttyname_r(int fildes,char * name,size_t namesize)331 __posix_ttyname_r(int fildes, char *name, size_t namesize)
332 {
333 	int nerrno = 0;
334 	int oerrno = errno;
335 	int namelen;
336 
337 	errno = 0;
338 
339 	if (namesize > INT_MAX)
340 		namelen = INT_MAX;
341 	else
342 		namelen = (int)namesize;
343 
344 	if (ttyname_r(fildes, name, namelen) == NULL) {
345 		if (errno == 0)
346 			nerrno = EINVAL;
347 		else
348 			nerrno = errno;
349 	}
350 	errno = oerrno;
351 	return (nerrno);
352 }
353 
354 /*
355  * Checks if the name is under /dev/pts directory
356  */
357 static char *
ispts(struct stat64 * fsb,int match_mask)358 ispts(struct stat64 *fsb, int match_mask)
359 {
360 	static  char	buf[MAX_DEV_PATH];
361 	struct	stat64	stb;
362 
363 	(void) strcpy(buf, "/dev/pts/");
364 	itoa(minor(fsb->st_rdev), buf+strlen(buf));
365 
366 	if (stat64(buf, &stb) != 0)
367 		return (NULL);
368 
369 	if (match_mask == MATCH_MM) {
370 		if (stb.st_rdev == fsb->st_rdev)
371 			return (buf);
372 	} else if (stb.st_rdev == fsb->st_rdev && stb.st_dev == fsb->st_dev &&
373 	    stb.st_ino == fsb->st_ino)
374 			return (buf);
375 
376 	return (NULL);
377 }
378 
379 /*
380  * Checks if the dev is a known pty master or slave device
381  */
382 #define	MAXDEFAULTPTY	48
383 
384 static char *
ispty(struct stat64 * fsb,int match_mask)385 ispty(struct stat64 *fsb, int match_mask)
386 {
387 	static char	buf[16];	/* big enough for "/dev/XtyXX" */
388 	struct	stat64	stb;
389 	minor_t dmin;
390 	char prefix;
391 
392 	if (ptsldev == NODEV && stat64("/dev/ttyp0", &stb) == 0)
393 		ptsldev = stb.st_rdev;
394 
395 	/*
396 	 * check for a ptsl dev (/dev/ttyXX)
397 	 */
398 	prefix = 't';
399 	if (major(fsb->st_rdev) != major(ptsldev)) {
400 		/*
401 		 * not a ptsl, check for a ptc
402 		 */
403 		if (ptcdev == NODEV && stat64("/dev/ptyp0", &stb) == 0)
404 			ptcdev = stb.st_rdev;
405 
406 		/*
407 		 * check for a ptc dev (/dev/ptyXX)
408 		 */
409 		prefix = 'p';
410 		if (major(fsb->st_rdev) != major(ptcdev))
411 			return (NULL);
412 	}
413 
414 	/*
415 	 * check if minor number is in the known range
416 	 */
417 	dmin = minor(fsb->st_rdev);
418 	if (dmin > MAXDEFAULTPTY)
419 		return (NULL);
420 
421 	/*
422 	 * modify name based on minor number
423 	 */
424 	(void) snprintf(buf, sizeof (buf), "/dev/%cty%c%c",
425 	    prefix, 'p' + dmin / 16, "0123456789abcdef"[dmin % 16]);
426 
427 	if (stat64(buf, &stb) != 0)
428 		return (NULL);
429 
430 	if (match_mask == MATCH_MM) {
431 		if (stb.st_rdev == fsb->st_rdev)
432 			return (buf);
433 	} else if (stb.st_rdev == fsb->st_rdev &&
434 	    stb.st_dev == fsb->st_dev &&
435 	    stb.st_ino == fsb->st_ino)
436 			return (buf);
437 
438 	return (NULL);
439 }
440 
441 
442 /*
443  * Converts a number to a string (null terminated).
444  */
445 static void
itoa(int i,char * ptr)446 itoa(int i, char *ptr)
447 {
448 	int dig = 0;
449 	int tempi;
450 
451 	tempi = i;
452 	do {
453 		dig++;
454 		tempi /= 10;
455 	} while (tempi);
456 
457 	ptr += dig;
458 	*ptr = '\0';
459 	while (--dig >= 0) {
460 		*(--ptr) = i % 10 + '0';
461 		i /= 10;
462 	}
463 }
464 
465 /*
466  * srch_dir() searches directory path and all directories under it up
467  * to depth directories deep for a file described by a stat structure
468  * fsb.  It puts the answer into rbuf.  If a match is found on device
469  * number only, the file name is put into dev_rbuf and dev_flag is set.
470  *
471  * srch_dir() returns 1 if a match (on device and inode) is found,
472  * or 0 otherwise.
473  *
474  */
475 
476 static int
srch_dir(const entry_t path,int match_mask,int depth,const entry_t skip_dirs[],struct stat64 * fsb)477 srch_dir(const entry_t path,	/* current path */
478     int match_mask,		/* flags mask */
479     int depth,			/* current depth (/dev = 0) */
480     const entry_t skip_dirs[],	/* directories not needing searching */
481     struct stat64 *fsb)		/* the file being searched for */
482 {
483 	DIR *dirp;
484 	struct dirent64 *direntp;
485 	struct stat64 tsb;
486 	char file_name[MAX_DEV_PATH];
487 	entry_t file;
488 	char *last_comp;
489 	int found = 0;
490 	int dirno = 0;
491 	size_t path_len;
492 
493 	file.name = file_name;
494 	file.flags = path.flags & match_mask;
495 	if (file.flags == 0)
496 		file.flags = match_mask;
497 
498 	/*
499 	 * do we need to search this directory? (always search /dev at depth 0)
500 	 */
501 	if ((skip_dirs != NULL) && (depth != 0))
502 		while (skip_dirs[dirno].name != NULL)
503 			if (strcmp(skip_dirs[dirno++].name, path.name) == 0)
504 				return (0);
505 
506 	/*
507 	 * open directory
508 	 */
509 	if ((dirp = opendir(path.name)) == NULL) {
510 		return (0);
511 	}
512 
513 	/*
514 	 * Note, we only make sure we can fit path.name + '/' into
515 	 * file_name here.
516 	 */
517 	path_len = strlcpy(file_name, path.name, sizeof (file_name));
518 	if (path_len + 1 >= sizeof (file_name)) {
519 		(void) closedir(dirp);
520 		return (0);
521 	}
522 	last_comp = file_name + path_len;
523 	*last_comp++ = '/';
524 
525 	/*
526 	 * read thru the directory
527 	 */
528 	while ((!found) && ((direntp = readdir64(dirp)) != NULL)) {
529 		/*
530 		 * skip "." and ".." entries, if present
531 		 */
532 		if (direntp->d_name[0] == '.' &&
533 		    (strcmp(direntp->d_name, ".") == 0 ||
534 		    strcmp(direntp->d_name, "..") == 0))
535 			continue;
536 
537 		/*
538 		 * if the file name (path + "/" + d_name + NULL) would be too
539 		 * long, skip it
540 		 */
541 		if ((path_len + strlen(direntp->d_name) + 2) > MAX_DEV_PATH)
542 			continue;
543 
544 		(void) strcpy(last_comp, direntp->d_name);
545 		if (stat64(file_name, &tsb) < 0)
546 			continue;
547 
548 		if (strcmp(file_name, "/dev/vt/active") == 0)
549 			continue;
550 
551 		/*
552 		 * skip "/dev/syscon" because it may be an invalid link after
553 		 * single user mode.
554 		 */
555 		if (strcmp(file_name, "/dev/syscon") == 0)
556 			continue;
557 
558 		/*
559 		 * if a file is a directory and we are not too deep, recurse
560 		 */
561 		if ((tsb.st_mode & S_IFMT) == S_IFDIR)
562 			if (depth < MAX_SRCH_DEPTH)
563 				found = srch_dir(file, match_mask, depth+1,
564 				    skip_dirs, fsb);
565 			else
566 				continue;
567 
568 		/*
569 		 * else if it is not a directory, is it a character special
570 		 * file?
571 		 */
572 		else if ((tsb.st_mode & S_IFMT) == S_IFCHR) {
573 			int flag = 0;
574 			if (match_mask & MATCH_FS &&
575 			    tsb.st_dev == fsb->st_dev)
576 				flag |= MATCH_FS;
577 			if (match_mask & MATCH_MM &&
578 			    tsb.st_rdev == fsb->st_rdev)
579 				flag |= MATCH_MM;
580 			if (match_mask & MATCH_INO &&
581 			    tsb.st_ino == fsb->st_ino)
582 				flag |= MATCH_INO;
583 
584 			if ((flag & file.flags) == file.flags) {
585 				(void) strcpy(rbuf, file.name);
586 				found = 1;
587 			} else if ((flag & (MATCH_MM | MATCH_FS)) ==
588 			    (MATCH_MM | MATCH_FS)) {
589 
590 				/*
591 				 * no (inodes do not match), but save the name
592 				 * for later
593 				 */
594 				(void) strcpy(dev_rbuf, file.name);
595 				dev_flag = 1;
596 			}
597 		}
598 	}
599 	(void) closedir(dirp);
600 	return (found);
601 }
602 
603 
604 /*
605  * get_pri_dirs() - returns a pointer to an array of strings, where each string
606  * is a priority directory name.  The end of the array is marked by a NULL
607  * pointer.  The priority directories' names are obtained from the file
608  * /etc/ttysrch if it exists and is readable, or if not, a default hard-coded
609  * list of directories.
610  *
611  * /etc/ttysrch, if used, is read in as a string of characters into memory and
612  * then parsed into strings of priority directory names, omitting comments and
613  * blank lines.
614  *
615  */
616 
617 #define	START_STATE	1
618 #define	COMMENT_STATE	2
619 #define	DIRNAME_STATE	3
620 #define	FLAG_STATE	4
621 #define	CHECK_STATE	5
622 
623 #define	COMMENT_CHAR	'#'
624 #define	EOLN_CHAR	'\n'
625 
626 static const entry_t *
get_pri_dirs(void)627 get_pri_dirs(void)
628 {
629 	int fd, state;
630 	size_t sz;
631 	ssize_t size;
632 	struct stat64 sb;
633 	char *buf, *ebuf;
634 	entry_t *vec;
635 
636 	/*
637 	 * if no /etc/ttysrch, use defaults
638 	 */
639 	if ((fd = open(TTYSRCH, 0)) < 0)
640 		return (def_srch_dirs);
641 
642 	if (fstat64(fd, &sb) < 0) {
643 		(void) close(fd);
644 		return (def_srch_dirs);
645 	}
646 
647 	sz = (size_t)sb.st_size;
648 	if (dir_vec != NULL && sz == dir_size &&
649 	    sb.st_mtim.tv_sec == dir_mtim.tv_sec &&
650 	    sb.st_mtim.tv_nsec == dir_mtim.tv_nsec) {
651 		/*
652 		 * size & modification time match
653 		 * no need to reread TTYSRCH
654 		 * just return old pointer
655 		 */
656 		(void) close(fd);
657 		return (dir_vec);
658 	}
659 	buf = realloc(dir_buf, sz + 1);
660 	if (buf != NULL) {
661 		dir_buf = buf;
662 		size = read(fd, dir_buf, sz);
663 	}
664 	(void) close(fd);
665 
666 	if (buf == NULL || size < 0) {
667 		if (dir_vec != NULL) {
668 			free(dir_vec);
669 			dir_vec = NULL;
670 		}
671 		return ((entry_t *)def_srch_dirs);
672 	}
673 	dir_size = sz;
674 	dir_mtim = sb.st_mtim;
675 
676 	/*
677 	 * ensure newline termination for buffer.  Add an extra
678 	 * entry to dir_vec for null terminator
679 	 */
680 	ebuf = &dir_buf[size];
681 	*ebuf++ = '\n';
682 	for (sz = 1, buf = dir_buf; buf < ebuf; ++buf)
683 		if (*buf == '\n')
684 			++sz;
685 
686 	sz *= sizeof (*dir_vec);
687 	vec = realloc(dir_vec, sz);
688 	if (vec == NULL) {
689 		if (dir_vec != NULL) {
690 			free(dir_vec);
691 			dir_vec = NULL;
692 		}
693 		return (def_srch_dirs);
694 	}
695 	dir_vec = vec;
696 	state = START_STATE;
697 	for (buf = dir_buf; buf < ebuf; ++buf) {
698 		switch (state) {
699 
700 		case START_STATE:
701 			if (*buf == COMMENT_CHAR) {
702 				state = COMMENT_STATE;
703 				break;
704 			}
705 			if (!isspace(*buf)) {	/* skip leading white space */
706 				state = DIRNAME_STATE;
707 				vec->name = buf;
708 				vec->flags = 0;
709 			}
710 			break;
711 
712 		case COMMENT_STATE:
713 			if (*buf == EOLN_CHAR)
714 				state = START_STATE;
715 			break;
716 
717 		case DIRNAME_STATE:
718 			if (*buf == EOLN_CHAR) {
719 				state = CHECK_STATE;
720 				*buf = '\0';
721 			} else if (isspace(*buf)) {
722 				/* skip trailing white space */
723 				state = FLAG_STATE;
724 				*buf = '\0';
725 			}
726 			break;
727 
728 		case FLAG_STATE:
729 			switch (*buf) {
730 				case 'M':
731 					vec->flags |= MATCH_MM;
732 					break;
733 				case 'F':
734 					vec->flags |= MATCH_FS;
735 					break;
736 				case 'I':
737 					vec->flags |= MATCH_INO;
738 					break;
739 				case EOLN_CHAR:
740 					state = CHECK_STATE;
741 					break;
742 			}
743 			break;
744 
745 		case CHECK_STATE:
746 			if (strncmp(vec->name, DEV, strlen(DEV)) != 0) {
747 				int tfd = open("/dev/console", O_WRONLY);
748 				if (tfd >= 0) {
749 					char buf[256];
750 					(void) snprintf(buf, sizeof (buf),
751 					    _libc_gettext(
752 "ERROR: Entry '%s' in /etc/ttysrch ignored.\n"), vec->name);
753 					(void) write(tfd, buf, strlen(buf));
754 					(void) close(tfd);
755 				}
756 			} else {
757 				char *slash;
758 				slash = vec->name + strlen(vec->name) - 1;
759 				while (*slash == '/')
760 					*slash-- = '\0';
761 				if (vec->flags == 0)
762 					vec->flags = MATCH_ALL;
763 				vec++;
764 			}
765 			state = START_STATE;
766 			/*
767 			 * This state does not consume a character, so
768 			 * reposition the pointer.
769 			 */
770 			buf--;
771 			break;
772 
773 		}
774 	}
775 	vec->name = NULL;
776 	return (dir_vec);
777 }
778 
779 
780 char *
ttyname(int f)781 ttyname(int f)
782 {
783 	char *ans = tsdalloc(_T_TTYNAME, MAX_DEV_PATH, NULL);
784 
785 	if (ans == NULL)
786 		return (NULL);
787 	return (ttyname_r(f, ans, MAX_DEV_PATH));
788 }
789