1 /*
2  *  $Id: ttyfdslot.c,v 1.4 2002/06/22 13:26:21 hiroo Exp $
3  */
4 
5 /*
6  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7  * This file is part of FreeWnn.
8  *
9  * Copyright Kyoto University Research Institute for Mathematical Sciences
10  *                 1987, 1988, 1989, 1990, 1991, 1992
11  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
12  * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
13  * Copyright FreeWnn Project 1999, 2000, 2002
14  *
15  * Maintainer:  FreeWnn Project   <freewnn@tomo.gr.jp>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30  */
31 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif
34 
35 #include <stdio.h>
36 #if STDC_HEADERS
37 #  include <string.h>
38 #else
39 #  if HAVE_MEMORY_H
40 #    include <memory.h>
41 #  endif
42 #  if HAVE_STRINGS_H
43 #    include <strings.h>
44 #  endif
45 #endif /* STDC_HEADERS */
46 #if HAVE_UNISTD_H
47 #  include <unistd.h>
48 #endif
49 
50 #include "commonhd.h"
51 #include "wnn_os.h"
52 
53 /*
54         BSD42
55  */
56 #if defined(BSD42) && (! defined(BSD43)) || defined(linux)
57 
58 #define SLOTSIZ 32
59 
60 int
ttyfdslot(int fd)61 ttyfdslot (int fd)
62 {
63   char eachslot[SLOTSIZ];
64   register char *fullnamp;
65   register char *ttynamp;
66   register FILE *ttysfp;
67   register char *p;
68   register int slotnum;
69 
70   if ((fullnamp = ttyname (fd)) == NULL)
71     return NULL;
72   if ((ttynamp = rindex (fullnamp, '/')) == NULL)
73     {
74       ttynamp = fullnamp;
75     }
76   else
77     {
78       ttynamp++;
79     }
80   if ((ttysfp = fopen ("/etc/ttys", "r")) == NULL)
81     return NULL;
82   for (slotnum = 0; fgets (eachslot, SLOTSIZ, ttysfp);)
83     {
84       p = eachslot + strlen (eachslot) - 1;
85       if (*p == '\n')
86         *p = '\0';
87       slotnum++;
88       if (strcmp (ttynamp, &eachslot[2]) == 0)
89         {
90           fclose (ttysfp);
91           return slotnum;
92         }
93     }
94   fclose (ttysfp);
95   return NULL;
96 }
97 #endif /* defined(BSD42) && (! defined(BSD43)) || defined(linux) */
98 
99 
100 /*
101         BSD43
102  */
103 #if defined(BSD43) && !defined(linux)
104 
105 #include <ttyent.h>
106 
107 int
ttyfdslot(int fd)108 ttyfdslot (int fd)
109 {
110   register char *fullnamp;
111   register char *ttynamp;
112   register int slotnum;
113   register struct ttyent *te;
114   if ((fullnamp = ttyname (fd)) == NULL)
115     return NULL;
116   if ((ttynamp = rindex (fullnamp, '/')) == NULL)
117     {
118       ttynamp = fullnamp;
119     }
120   else
121     {
122       ttynamp++;
123     }
124   for (slotnum = 1; (te = getttyent ()) != NULL; slotnum++)
125     {
126       if (strcmp (te->ty_name, ttynamp) == 0)
127         {
128           endttyent ();
129           return slotnum;
130         }
131     }
132   endttyent ();
133   return NULL;
134 }
135 
136 #endif /* defined(BSD43) && !defined(linux) */
137