xref: /titanic_50/usr/src/lib/libc/port/gen/ttyname.c (revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5)
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 *
156 _ttyname_dev(dev_t rdev, char *buffer, size_t buflen)
157 {
158 	struct stat64 fsb;
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 *
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 *
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) == NULL) {
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
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 *
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 *
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
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
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 (tsb.st_dev == fsb->st_dev)
568 				flag |= MATCH_FS;
569 			if (tsb.st_rdev == fsb->st_rdev)
570 				flag |= MATCH_MM;
571 			if (tsb.st_ino == fsb->st_ino)
572 				flag |= MATCH_INO;
573 
574 			if ((flag & file.flags) == file.flags) {
575 				(void) strcpy(rbuf, file.name);
576 				found = 1;
577 			} else if ((flag & (MATCH_MM | MATCH_FS)) ==
578 			    (MATCH_MM | MATCH_FS)) {
579 
580 				/*
581 				 * no (inodes do not match), but save the name
582 				 * for later
583 				 */
584 				(void) strcpy(dev_rbuf, file.name);
585 				dev_flag = 1;
586 			}
587 		}
588 	}
589 	(void) closedir(dirp);
590 	return (found);
591 }
592 
593 
594 /*
595  * get_pri_dirs() - returns a pointer to an array of strings, where each string
596  * is a priority directory name.  The end of the array is marked by a NULL
597  * pointer.  The priority directories' names are obtained from the file
598  * /etc/ttysrch if it exists and is readable, or if not, a default hard-coded
599  * list of directories.
600  *
601  * /etc/ttysrch, if used, is read in as a string of characters into memory and
602  * then parsed into strings of priority directory names, omitting comments and
603  * blank lines.
604  *
605  */
606 
607 #define	START_STATE	1
608 #define	COMMENT_STATE	2
609 #define	DIRNAME_STATE	3
610 #define	FLAG_STATE	4
611 #define	CHECK_STATE	5
612 
613 #define	COMMENT_CHAR	'#'
614 #define	EOLN_CHAR	'\n'
615 
616 static const entry_t *
617 get_pri_dirs(void)
618 {
619 	int fd, state;
620 	size_t sz;
621 	ssize_t size;
622 	struct stat64 sb;
623 	char *buf, *ebuf;
624 	entry_t *vec;
625 
626 	/*
627 	 * if no /etc/ttysrch, use defaults
628 	 */
629 	if ((fd = open(TTYSRCH, 0)) < 0)
630 		return (def_srch_dirs);
631 
632 	if (fstat64(fd, &sb) < 0) {
633 		(void) close(fd);
634 		return (def_srch_dirs);
635 	}
636 
637 	sz = (size_t)sb.st_size;
638 	if (dir_vec != NULL && sz == dir_size &&
639 	    sb.st_mtim.tv_sec == dir_mtim.tv_sec &&
640 	    sb.st_mtim.tv_nsec == dir_mtim.tv_nsec) {
641 		/*
642 		 * size & modification time match
643 		 * no need to reread TTYSRCH
644 		 * just return old pointer
645 		 */
646 		(void) close(fd);
647 		return (dir_vec);
648 	}
649 	buf = realloc(dir_buf, sz + 1);
650 	if (buf != NULL) {
651 		dir_buf = buf;
652 		size = read(fd, dir_buf, sz);
653 	}
654 	(void) close(fd);
655 
656 	if (buf == NULL || size < 0) {
657 		if (dir_vec != NULL) {
658 			free(dir_vec);
659 			dir_vec = NULL;
660 		}
661 		return ((entry_t *)def_srch_dirs);
662 	}
663 	dir_size = sz;
664 	dir_mtim = sb.st_mtim;
665 
666 	/*
667 	 * ensure newline termination for buffer.  Add an extra
668 	 * entry to dir_vec for null terminator
669 	 */
670 	ebuf = &dir_buf[size];
671 	*ebuf++ = '\n';
672 	for (sz = 1, buf = dir_buf; buf < ebuf; ++buf)
673 		if (*buf == '\n')
674 			++sz;
675 
676 	sz *= sizeof (*dir_vec);
677 	vec = realloc(dir_vec, sz);
678 	if (vec == NULL) {
679 		if (dir_vec != NULL) {
680 			free(dir_vec);
681 			dir_vec = NULL;
682 		}
683 		return (def_srch_dirs);
684 	}
685 	dir_vec = vec;
686 	state = START_STATE;
687 	for (buf = dir_buf; buf < ebuf; ++buf) {
688 		switch (state) {
689 
690 		case START_STATE:
691 			if (*buf == COMMENT_CHAR) {
692 				state = COMMENT_STATE;
693 				break;
694 			}
695 			if (!isspace(*buf))	/* skip leading white space */
696 				state = DIRNAME_STATE;
697 				vec->name = buf;
698 				vec->flags = 0;
699 			break;
700 
701 		case COMMENT_STATE:
702 			if (*buf == EOLN_CHAR)
703 				state = START_STATE;
704 			break;
705 
706 		case DIRNAME_STATE:
707 			if (*buf == EOLN_CHAR) {
708 				state = CHECK_STATE;
709 				*buf = '\0';
710 			} else if (isspace(*buf)) {
711 				/* skip trailing white space */
712 				state = FLAG_STATE;
713 				*buf = '\0';
714 			}
715 			break;
716 
717 		case FLAG_STATE:
718 			switch (*buf) {
719 				case 'M':
720 					vec->flags |= MATCH_MM;
721 					break;
722 				case 'F':
723 					vec->flags |= MATCH_FS;
724 					break;
725 				case 'I':
726 					vec->flags |= MATCH_INO;
727 					break;
728 				case EOLN_CHAR:
729 					state = CHECK_STATE;
730 					break;
731 			}
732 			break;
733 
734 		case CHECK_STATE:
735 			if (strncmp(vec->name, DEV, strlen(DEV)) != 0) {
736 				int tfd = open("/dev/console", O_WRONLY);
737 				if (tfd >= 0) {
738 					char buf[256];
739 					(void) snprintf(buf, sizeof (buf),
740 					    _libc_gettext(
741 "ERROR: Entry '%s' in /etc/ttysrch ignored.\n"), vec->name);
742 					(void) write(tfd, buf, strlen(buf));
743 					(void) close(tfd);
744 				}
745 			} else {
746 				char *slash;
747 				slash = vec->name + strlen(vec->name) - 1;
748 				while (*slash == '/')
749 					*slash-- = '\0';
750 				if (vec->flags == 0)
751 					vec->flags = MATCH_ALL;
752 				vec++;
753 			}
754 			state = START_STATE;
755 			/*
756 			 * This state does not consume a character, so
757 			 * reposition the pointer.
758 			 */
759 			buf--;
760 			break;
761 
762 		}
763 	}
764 	vec->name = NULL;
765 	return (dir_vec);
766 }
767 
768 
769 char *
770 ttyname(int f)
771 {
772 	char *ans = tsdalloc(_T_TTYNAME, MAX_DEV_PATH, NULL);
773 
774 	if (ans == NULL)
775 		return (NULL);
776 	return (ttyname_r(f, ans, MAX_DEV_PATH));
777 }
778