]> granicus.if.org Git - shadow/blob - libmisc/ttytype.c
(failcheck): The failed argument is a bool.
[shadow] / libmisc / ttytype.c
1 /*
2  * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 1997, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the copyright holders or contributors may not be used to
16  *    endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <config.h>
33
34 #ident "$Id$"
35
36 #include <stdio.h>
37 #include "prototypes.h"
38 #include "defines.h"
39 #include "getdef.h"
40 /*
41  * ttytype - set ttytype from port to terminal type mapping database
42  */
43 void ttytype (const char *line)
44 {
45         FILE *fp;
46         char buf[BUFSIZ];
47         char *typefile;
48         char *cp;
49         char type[BUFSIZ];
50         char port[BUFSIZ];
51
52         if (getenv ("TERM"))
53                 return;
54         if ((typefile = getdef_str ("TTYTYPE_FILE")) == NULL)
55                 return;
56         if (access (typefile, F_OK))
57                 return;
58
59         if (!(fp = fopen (typefile, "r"))) {
60                 perror (typefile);
61                 return;
62         }
63         while (fgets (buf, sizeof buf, fp)) {
64                 if (buf[0] == '#')
65                         continue;
66
67                 if ((cp = strchr (buf, '\n')))
68                         *cp = '\0';
69
70                 if (sscanf (buf, "%s %s", type, port) == 2 &&
71                     strcmp (line, port) == 0)
72                         break;
73         }
74         if (!feof (fp) && !ferror (fp))
75                 addenv ("TERM", type);
76
77         fclose (fp);
78 }