xref: /illumos-gate/usr/src/lib/libc/port/gen/ttyname.c (revision 5d17020e)
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 	path_len = strlen(path.name);
514 	(void) strcpy(file_name, path.name);
515 	last_comp = file_name + path_len;
516 	*last_comp++ = '/';
517 
518 	/*
519 	 * read thru the directory
520 	 */
521 	while ((!found) && ((direntp = readdir64(dirp)) != NULL)) {
522 		/*
523 		 * skip "." and ".." entries, if present
524 		 */
525 		if (direntp->d_name[0] == '.' &&
526 		    (strcmp(direntp->d_name, ".") == 0 ||
527 		    strcmp(direntp->d_name, "..") == 0))
528 			continue;
529 
530 		/*
531 		 * if the file name (path + "/" + d_name + NULL) would be too
532 		 * long, skip it
533 		 */
534 		if ((path_len + strlen(direntp->d_name) + 2) > MAX_DEV_PATH)
535 			continue;
536 
537 		(void) strcpy(last_comp, direntp->d_name);
538 		if (stat64(file_name, &tsb) < 0)
539 			continue;
540 
541 		if (strcmp(file_name, "/dev/vt/active") == 0)
542 			continue;
543 
544 		/*
545 		 * skip "/dev/syscon" because it may be an invalid link after
546 		 * single user mode.
547 		 */
548 		if (strcmp(file_name, "/dev/syscon") == 0)
549 			continue;
550 
551 		/*
552 		 * if a file is a directory and we are not too deep, recurse
553 		 */
554 		if ((tsb.st_mode & S_IFMT) == S_IFDIR)
555 			if (depth < MAX_SRCH_DEPTH)
556 				found = srch_dir(file, match_mask, depth+1,
557 				    skip_dirs, fsb);
558 			else
559 				continue;
560 
561 		/*
562 		 * else if it is not a directory, is it a character special
563 		 * file?
564 		 */
565 		else if ((tsb.st_mode & S_IFMT) == S_IFCHR) {
566 			int flag = 0;
567 			if (match_mask & MATCH_FS &&
568 			    tsb.st_dev == fsb->st_dev)
569 				flag |= MATCH_FS;
570 			if (match_mask & MATCH_MM &&
571 			    tsb.st_rdev == fsb->st_rdev)
572 				flag |= MATCH_MM;
573 			if (match_mask & MATCH_INO &&
574 			    tsb.st_ino == fsb->st_ino)
575 				flag |= MATCH_INO;
576 
577 			if ((flag & file.flags) == file.flags) {
578 				(void) strcpy(rbuf, file.name);
579 				found = 1;
580 			} else if ((flag & (MATCH_MM | MATCH_FS)) ==
581 			    (MATCH_MM | MATCH_FS)) {
582 
583 				/*
584 				 * no (inodes do not match), but save the name
585 				 * for later
586 				 */
587 				(void) strcpy(dev_rbuf, file.name);
588 				dev_flag = 1;
589 			}
590 		}
591 	}
592 	(void) closedir(dirp);
593 	return (found);
594 }
595 
596 
597 /*
598  * get_pri_dirs() - returns a pointer to an array of strings, where each string
599  * is a priority directory name.  The end of the array is marked by a NULL
600  * pointer.  The priority directories' names are obtained from the file
601  * /etc/ttysrch if it exists and is readable, or if not, a default hard-coded
602  * list of directories.
603  *
604  * /etc/ttysrch, if used, is read in as a string of characters into memory and
605  * then parsed into strings of priority directory names, omitting comments and
606  * blank lines.
607  *
608  */
609 
610 #define	START_STATE	1
611 #define	COMMENT_STATE	2
612 #define	DIRNAME_STATE	3
613 #define	FLAG_STATE	4
614 #define	CHECK_STATE	5
615 
616 #define	COMMENT_CHAR	'#'
617 #define	EOLN_CHAR	'\n'
618 
619 static const entry_t *
get_pri_dirs(void)620 get_pri_dirs(void)
621 {
622 	int fd, state;
623 	size_t sz;
624 	ssize_t size;
625 	struct stat64 sb;
626 	char *buf, *ebuf;
627 	entry_t *vec;
628 
629 	/*
630 	 * if no /etc/ttysrch, use defaults
631 	 */
632 	if ((fd = open(TTYSRCH, 0)) < 0)
633 		return (def_srch_dirs);
634 
635 	if (fstat64(fd, &sb) < 0) {
636 		(void) close(fd);
637 		return (def_srch_dirs);
638 	}
639 
640 	sz = (size_t)sb.st_size;
641 	if (dir_vec != NULL && sz == dir_size &&
642 	    sb.st_mtim.tv_sec == dir_mtim.tv_sec &&
643 	    sb.st_mtim.tv_nsec == dir_mtim.tv_nsec) {
644 		/*
645 		 * size & modification time match
646 		 * no need to reread TTYSRCH
647 		 * just return old pointer
648 		 */
649 		(void) close(fd);
650 		return (dir_vec);
651 	}
652 	buf = realloc(dir_buf, sz + 1);
653 	if (buf != NULL) {
654 		dir_buf = buf;
655 		size = read(fd, dir_buf, sz);
656 	}
657 	(void) close(fd);
658 
659 	if (buf == NULL || size < 0) {
660 		if (dir_vec != NULL) {
661 			free(dir_vec);
662 			dir_vec = NULL;
663 		}
664 		return ((entry_t *)def_srch_dirs);
665 	}
666 	dir_size = sz;
667 	dir_mtim = sb.st_mtim;
668 
669 	/*
670 	 * ensure newline termination for buffer.  Add an extra
671 	 * entry to dir_vec for null terminator
672 	 */
673 	ebuf = &dir_buf[size];
674 	*ebuf++ = '\n';
675 	for (sz = 1, buf = dir_buf; buf < ebuf; ++buf)
676 		if (*buf == '\n')
677 			++sz;
678 
679 	sz *= sizeof (*dir_vec);
680 	vec = realloc(dir_vec, sz);
681 	if (vec == NULL) {
682 		if (dir_vec != NULL) {
683 			free(dir_vec);
684 			dir_vec = NULL;
685 		}
686 		return (def_srch_dirs);
687 	}
688 	dir_vec = vec;
689 	state = START_STATE;
690 	for (buf = dir_buf; buf < ebuf; ++buf) {
691 		switch (state) {
692 
693 		case START_STATE:
694 			if (*buf == COMMENT_CHAR) {
695 				state = COMMENT_STATE;
696 				break;
697 			}
698 			if (!isspace(*buf)) {	/* skip leading white space */
699 				state = DIRNAME_STATE;
700 				vec->name = buf;
701 				vec->flags = 0;
702 			}
703 			break;
704 
705 		case COMMENT_STATE:
706 			if (*buf == EOLN_CHAR)
707 				state = START_STATE;
708 			break;
709 
710 		case DIRNAME_STATE:
711 			if (*buf == EOLN_CHAR) {
712 				state = CHECK_STATE;
713 				*buf = '\0';
714 			} else if (isspace(*buf)) {
715 				/* skip trailing white space */
716 				state = FLAG_STATE;
717 				*buf = '\0';
718 			}
719 			break;
720 
721 		case FLAG_STATE:
722 			switch (*buf) {
723 				case 'M':
724 					vec->flags |= MATCH_MM;
725 					break;
726 				case 'F':
727 					vec->flags |= MATCH_FS;
728 					break;
729 				case 'I':
730 					vec->flags |= MATCH_INO;
731 					break;
732 				case EOLN_CHAR:
733 					state = CHECK_STATE;
734 					break;
735 			}
736 			break;
737 
738 		case CHECK_STATE:
739 			if (strncmp(vec->name, DEV, strlen(DEV)) != 0) {
740 				int tfd = open("/dev/console", O_WRONLY);
741 				if (tfd >= 0) {
742 					char buf[256];
743 					(void) snprintf(buf, sizeof (buf),
744 					    _libc_gettext(
745 "ERROR: Entry '%s' in /etc/ttysrch ignored.\n"), vec->name);
746 					(void) write(tfd, buf, strlen(buf));
747 					(void) close(tfd);
748 				}
749 			} else {
750 				char *slash;
751 				slash = vec->name + strlen(vec->name) - 1;
752 				while (*slash == '/')
753 					*slash-- = '\0';
754 				if (vec->flags == 0)
755 					vec->flags = MATCH_ALL;
756 				vec++;
757 			}
758 			state = START_STATE;
759 			/*
760 			 * This state does not consume a character, so
761 			 * reposition the pointer.
762 			 */
763 			buf--;
764 			break;
765 
766 		}
767 	}
768 	vec->name = NULL;
769 	return (dir_vec);
770 }
771 
772 
773 char *
ttyname(int f)774 ttyname(int f)
775 {
776 	char *ans = tsdalloc(_T_TTYNAME, MAX_DEV_PATH, NULL);
777 
778 	if (ans == NULL)
779 		return (NULL);
780 	return (ttyname_r(f, ans, MAX_DEV_PATH));
781 }
782