getutmpentry.c revision 302408
1/* getutmpentry.c: The __opiegetutmpentry() library function.
2
3%%% copyright-cmetz-96
4This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5The Inner Net License Version 3 applies to this software.
6You should have received a copy of the license with this software. If
7you 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
19#if DOUTMPX
20#include <utmpx.h>
21#define setutent setutxent
22#define getutline(x) getutxline(x)
23#define utmp utmpx
24#else
25#include <utmp.h>
26#endif /* DOUTMPX */
27
28#if HAVE_STRING_H
29#include <string.h>
30#endif /* HAVE_STRING_H */
31
32#if DEBUG
33#include <syslog.h>
34#endif /* DEBUG */
35#include "opie.h"
36
37#if !HAVE_GETUTLINE && !DOUTMPX
38struct utmp *getutline __P((struct utmp *));
39#endif /* HAVE_GETUTLINE && !DOUTMPX */
40
41static struct utmp u;
42
43int __opiegetutmpentry FUNCTION((line, utmp), char *line AND struct utmp *utmp)
44{
45  struct utmp *pu;
46
47  if (u.ut_line[0]) {
48    pu = &u;
49    goto gotit;
50  };
51
52  memset(&u, 0, sizeof(u));
53
54  if (!strncmp(line, "/dev/", 5)) {
55    strncpy(u.ut_line, line + 5, sizeof(u.ut_line));
56    setutent();
57    if ((pu = getutline(&u)))
58      goto gotit;
59
60#ifdef hpux
61    strcpy(u.ut_line, "pty/");
62    strncpy(u.ut_line + 4, line + 5, sizeof(u.ut_line) - 4);
63    setutent();
64    if ((pu = getutline(&u)))
65      goto gotit;
66#endif /* hpux */
67  }
68
69  strncpy(u.ut_line, line, sizeof(u.ut_line));
70  setutent();
71  if ((pu = getutline(&u)))
72    goto gotit;
73
74#if DEBUG
75  syslog(LOG_DEBUG, "__opiegetutmpentry: failed to find entry for line %s", line);
76#endif /* DEBUG */
77  return -1;
78
79gotit:
80#if DEBUG
81  syslog(LOG_DEBUG, "__opiegetutmpentry: succeeded with line %s", pu->ut_line);
82#endif /* DEBUG */
83  memcpy(utmp, pu, sizeof(struct utmp));
84  return 0;
85}
86