122347Spst/* getutline.c: A replacement for the getutline() 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
1159118Skris	Modified by cmetz for OPIE 2.32. Fixed check for fread() return
1259118Skris		value.
1329964Sache	Modified by cmetz for OPIE 2.31. If the OS won't tell us where
1429964Sache		_PATH_UTMP is, play the SVID game, then use
1529964Sache		Autoconf-discovered values.
1622347Spst	Created by cmetz for OPIE 2.3.
1722347Spst*/
1822347Spst
1922347Spst#include "opie_cfg.h"
2022347Spst#include <stdio.h>
2122347Spst#include <utmp.h>
2222347Spst#include "opie.h"
2322347Spst
2422347Spststatic struct utmp u;
2522347Spst
2629964Sache#ifndef _PATH_UTMP
2729964Sache#ifdef UTMP_FILE
2829964Sache#define _PATH_UTMP UTMP_FILE
2929964Sache#else /* UTMP_FILE */
3029964Sache#define _PATH_UTMP PATH_UTMP_AC
3129964Sache#endif /* UTMP_FILE */
3229964Sache#endif /* _PATH_UTMP */
3329964Sache
3422347Spststruct utmp *getutline FUNCTION((utmp), struct utmp *utmp)
3522347Spst{
3622347Spst  FILE *f;
3722347Spst  int i;
3822347Spst
3922347Spst  if (!(f = __opieopen(_PATH_UTMP, 0, 0644)))
4022347Spst    return 0;
4122347Spst
4222347Spst#if HAVE_TTYSLOT
4322347Spst  if (i = ttyslot()) {
4422347Spst    if (fseek(f, i * sizeof(struct utmp), SEEK_SET) < 0)
4522347Spst      goto ret;
4659118Skris    if (fread(&u, sizeof(struct utmp), 1, f) != 1)
4722347Spst      goto ret;
4822347Spst    fclose(f);
4922347Spst    return &u;
5022347Spst  }
5122347Spst#endif /* HAVE_TTYSLOT */
5222347Spst
5359118Skris  while(fread(&u, sizeof(struct utmp), 1, f) == 1) {
5422347Spst    if (!strncmp(utmp->ut_line, u.ut_line, sizeof(u.ut_line) - 1)) {
5522347Spst      fclose(f);
5622347Spst      return &u;
5722347Spst    }
5822347Spst  }
5922347Spst
6022347Spstret:
6122347Spst  fclose(f);
6222347Spst  return NULL;
6322347Spst}
64