Initial import from FreeBSD RELENG_4:
[games.git] / contrib / opie / libopie / getutmpentry.c
1 /* getutmpentry.c: The __opiegetutmpentry() library function.
2
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you didn't get a copy, you may request one from <license@inner.net>.
8
9         History:
10
11         Modified by cmetz for OPIE 2.31. Cache result.
12         Created by cmetz for OPIE 2.3 (re-write).
13 */
14
15 #include "opie_cfg.h"
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <utmp.h>
19
20 #if DOUTMPX
21 #include <utmpx.h>
22 #define setutent setutxent
23 #define getutline(x) getutxline(x)
24 #define utmp utmpx
25 #endif /* DOUTMPX */
26
27 #if HAVE_STRING_H
28 #include <string.h>
29 #endif /* HAVE_STRING_H */
30
31 #if DEBUG
32 #include <syslog.h>
33 #endif /* DEBUG */
34 #include "opie.h"
35
36 #if !HAVE_GETUTLINE && !DOUTMPX
37 struct utmp *getutline __P((struct utmp *));
38 #endif /* HAVE_GETUTLINE && !DOUTMPX */
39
40 static struct utmp u;
41
42 int __opiegetutmpentry FUNCTION((line, utmp), char *line AND struct utmp *utmp)
43 {
44   struct utmp *pu;
45
46   if (u.ut_line[0]) {
47     pu = &u;
48     goto gotit;
49   };
50
51   memset(&u, 0, sizeof(u));
52
53   if (!strncmp(line, "/dev/", 5)) {
54     strncpy(u.ut_line, line + 5, sizeof(u.ut_line));
55     setutent();
56     if ((pu = getutline(&u)))
57       goto gotit;
58
59 #ifdef hpux
60     strcpy(u.ut_line, "pty/");
61     strncpy(u.ut_line + 4, line + 5, sizeof(u.ut_line) - 4);
62     setutent();
63     if ((pu = getutline(&u)))
64       goto gotit;
65 #endif /* hpux */
66   }
67
68   strncpy(u.ut_line, line, sizeof(u.ut_line));
69   setutent();
70   if ((pu = getutline(&u)))
71     goto gotit;
72
73 #if DEBUG
74   syslog(LOG_DEBUG, "__opiegetutmpentry: failed to find entry for line %s", line);
75 #endif /* DEBUG */
76   return -1;
77
78 gotit:
79 #if DEBUG
80   syslog(LOG_DEBUG, "__opiegetutmpentry: succeeded with line %s", pu->ut_line);
81 #endif /* DEBUG */
82   memcpy(utmp, pu, sizeof(struct utmp));
83   return 0;
84 }