122347Spst/* getutmpentry.c: The __opiegetutmpentry() library function.
222347Spst
329964Sache%%% copyright-cmetz-96
492906SmarkmThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
592906SmarkmThe Inner Net License Version 3 applies to this software.
622347SpstYou should have received a copy of the license with this software. If
722347Spstyou didn't get a copy, you may request one from <license@inner.net>.
822347Spst
922347Spst        History:
1022347Spst
1129964Sache	Modified by cmetz for OPIE 2.31. Cache result.
1222347Spst        Created by cmetz for OPIE 2.3 (re-write).
1322347Spst*/
1422347Spst
1522347Spst#include "opie_cfg.h"
1622347Spst#include <stdio.h>
1722347Spst#include <sys/types.h>
1822347Spst
1922347Spst#if DOUTMPX
2022347Spst#include <utmpx.h>
2129964Sache#define setutent setutxent
2222347Spst#define getutline(x) getutxline(x)
2322347Spst#define utmp utmpx
24202086Sed#else
25202086Sed#include <utmp.h>
2622347Spst#endif /* DOUTMPX */
2722347Spst
2822347Spst#if HAVE_STRING_H
2922347Spst#include <string.h>
3022347Spst#endif /* HAVE_STRING_H */
3122347Spst
3222347Spst#if DEBUG
3322347Spst#include <syslog.h>
3422347Spst#endif /* DEBUG */
3522347Spst#include "opie.h"
3622347Spst
3729964Sache#if !HAVE_GETUTLINE && !DOUTMPX
3822347Spststruct utmp *getutline __P((struct utmp *));
3929964Sache#endif /* HAVE_GETUTLINE && !DOUTMPX */
4022347Spst
4129964Sachestatic struct utmp u;
4229964Sache
4322347Spstint __opiegetutmpentry FUNCTION((line, utmp), char *line AND struct utmp *utmp)
4422347Spst{
4529964Sache  struct utmp *pu;
4622347Spst
4729964Sache  if (u.ut_line[0]) {
4829964Sache    pu = &u;
4929964Sache    goto gotit;
5029964Sache  };
5129964Sache
5222347Spst  memset(&u, 0, sizeof(u));
5322347Spst
5422347Spst  if (!strncmp(line, "/dev/", 5)) {
5522347Spst    strncpy(u.ut_line, line + 5, sizeof(u.ut_line));
5629964Sache    setutent();
5722347Spst    if ((pu = getutline(&u)))
5822347Spst      goto gotit;
5922347Spst
6022347Spst#ifdef hpux
6122347Spst    strcpy(u.ut_line, "pty/");
6222347Spst    strncpy(u.ut_line + 4, line + 5, sizeof(u.ut_line) - 4);
6329964Sache    setutent();
6422347Spst    if ((pu = getutline(&u)))
6522347Spst      goto gotit;
6622347Spst#endif /* hpux */
6722347Spst  }
6822347Spst
6922347Spst  strncpy(u.ut_line, line, sizeof(u.ut_line));
7029964Sache  setutent();
7122347Spst  if ((pu = getutline(&u)))
7222347Spst    goto gotit;
7322347Spst
7422347Spst#if DEBUG
7522347Spst  syslog(LOG_DEBUG, "__opiegetutmpentry: failed to find entry for line %s", line);
7622347Spst#endif /* DEBUG */
7722347Spst  return -1;
7822347Spst
7922347Spstgotit:
8022347Spst#if DEBUG
8122347Spst  syslog(LOG_DEBUG, "__opiegetutmpentry: succeeded with line %s", pu->ut_line);
8222347Spst#endif /* DEBUG */
8322347Spst  memcpy(utmp, pu, sizeof(struct utmp));
8422347Spst  return 0;
8522347Spst}
86