getutmpentry.c revision 92906
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#include <utmp.h>
1922347Spst
2022347Spst#if DOUTMPX
2122347Spst#include <utmpx.h>
2229964Sache#define setutent setutxent
2322347Spst#define getutline(x) getutxline(x)
2422347Spst#define utmp utmpx
2522347Spst#endif /* DOUTMPX */
2622347Spst
2722347Spst#if HAVE_STRING_H
2822347Spst#include <string.h>
2922347Spst#endif /* HAVE_STRING_H */
3022347Spst
3122347Spst#if DEBUG
3222347Spst#include <syslog.h>
3322347Spst#endif /* DEBUG */
3422347Spst#include "opie.h"
3522347Spst
3629964Sache#if !HAVE_GETUTLINE && !DOUTMPX
3722347Spststruct utmp *getutline __P((struct utmp *));
3829964Sache#endif /* HAVE_GETUTLINE && !DOUTMPX */
3922347Spst
4029964Sachestatic struct utmp u;
4129964Sache
4222347Spstint __opiegetutmpentry FUNCTION((line, utmp), char *line AND struct utmp *utmp)
4322347Spst{
4429964Sache  struct utmp *pu;
4522347Spst
4629964Sache  if (u.ut_line[0]) {
4729964Sache    pu = &u;
4829964Sache    goto gotit;
4929964Sache  };
5029964Sache
5122347Spst  memset(&u, 0, sizeof(u));
5222347Spst
5322347Spst  if (!strncmp(line, "/dev/", 5)) {
5422347Spst    strncpy(u.ut_line, line + 5, sizeof(u.ut_line));
5529964Sache    setutent();
5622347Spst    if ((pu = getutline(&u)))
5722347Spst      goto gotit;
5822347Spst
5922347Spst#ifdef hpux
6022347Spst    strcpy(u.ut_line, "pty/");
6122347Spst    strncpy(u.ut_line + 4, line + 5, sizeof(u.ut_line) - 4);
6229964Sache    setutent();
6322347Spst    if ((pu = getutline(&u)))
6422347Spst      goto gotit;
6522347Spst#endif /* hpux */
6622347Spst  }
6722347Spst
6822347Spst  strncpy(u.ut_line, line, sizeof(u.ut_line));
6929964Sache  setutent();
7022347Spst  if ((pu = getutline(&u)))
7122347Spst    goto gotit;
7222347Spst
7322347Spst#if DEBUG
7422347Spst  syslog(LOG_DEBUG, "__opiegetutmpentry: failed to find entry for line %s", line);
7522347Spst#endif /* DEBUG */
7622347Spst  return -1;
7722347Spst
7822347Spstgotit:
7922347Spst#if DEBUG
8022347Spst  syslog(LOG_DEBUG, "__opiegetutmpentry: succeeded with line %s", pu->ut_line);
8122347Spst#endif /* DEBUG */
8222347Spst  memcpy(utmp, pu, sizeof(struct utmp));
8322347Spst  return 0;
8422347Spst}
85