ttyslot.c revision 202274
1177997Skib/*
2177997Skib * Copyright (c) 1988, 1993
3177997Skib *	The Regents of the University of California.  All rights reserved.
4177997Skib *
5177997Skib * Redistribution and use in source and binary forms, with or without
6177997Skib * modification, are permitted provided that the following conditions
7177997Skib * are met:
8177997Skib * 1. Redistributions of source code must retain the above copyright
9177997Skib *    notice, this list of conditions and the following disclaimer.
10177997Skib * 2. Redistributions in binary form must reproduce the above copyright
11177997Skib *    notice, this list of conditions and the following disclaimer in the
12177997Skib *    documentation and/or other materials provided with the distribution.
13177997Skib * 4. Neither the name of the University nor the names of its contributors
14177997Skib *    may be used to endorse or promote products derived from this software
15177997Skib *    without specific prior written permission.
16177997Skib *
17177997Skib * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18177997Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19177997Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20177997Skib * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21177997Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22177997Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23177997Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24177997Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25177997Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26177997Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27177997Skib * SUCH DAMAGE.
28177997Skib */
29177997Skib
30177997Skib#if defined(LIBC_SCCS) && !defined(lint)
31177997Skibstatic char sccsid[] = "@(#)ttyslot.c	8.1 (Berkeley) 6/4/93";
32177997Skib#endif /* LIBC_SCCS and not lint */
33177997Skib#include <sys/cdefs.h>
34227693Sed__FBSDID("$FreeBSD: head/lib/libc/gen/ttyslot.c 202274 2010-01-14 05:35:32Z ed $");
35177997Skib
36227693Sed#include <paths.h>
37177997Skib#include <ttyent.h>
38246085Sjhb#include <stdio.h>
39246085Sjhb#include <string.h>
40246085Sjhb#include <unistd.h>
41246085Sjhb
42246085Sjhbint
43246085Sjhb__ttyslot(void)
44246085Sjhb{
45246085Sjhb	struct ttyent *ttyp;
46246085Sjhb	int slot;
47246085Sjhb	int cnt;
48246085Sjhb	char *name;
49246085Sjhb
50246085Sjhb	setttyent();
51246085Sjhb	for (cnt = 0; cnt < 3; ++cnt)
52246085Sjhb		if ( (name = ttyname(cnt)) ) {
53246085Sjhb			if (strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1) != 0)
54246085Sjhb				break;
55246085Sjhb			name += sizeof _PATH_DEV - 1;
56246085Sjhb			for (slot = 1; (ttyp = getttyent()); ++slot)
57293541Sdchagin				if (!strcmp(ttyp->ty_name, name)) {
58293541Sdchagin					endttyent();
59293541Sdchagin					return(slot);
60293541Sdchagin				}
61293541Sdchagin			break;
62293541Sdchagin		}
63293541Sdchagin	endttyent();
64293541Sdchagin	return(0);
65293541Sdchagin}
66293541Sdchagin
67293541Sdchagin__sym_compat(ttyslot, __ttyslot, FBSD_1.0);
68293541Sdchagin