getutline.c revision 174679
154359Sroberto/* getutline.c: A replacement for the getutline() function
254359Sroberto
354359Sroberto%%% copyright-cmetz-96
454359SrobertoThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
554359SrobertoThe Inner Net License Version 3 applies to this software.
654359SrobertoYou should have received a copy of the license with this software. If
754359Srobertoyou didn't get a copy, you may request one from <license@inner.net>.
854359Sroberto
954359Sroberto        History:
1054359Sroberto
1154359Sroberto	Modified by cmetz for OPIE 2.32. Fixed check for fread() return
1254359Sroberto		value.
1354359Sroberto	Modified by cmetz for OPIE 2.31. If the OS won't tell us where
14182007Sroberto		_PATH_UTMP is, play the SVID game, then use
1554359Sroberto		Autoconf-discovered values.
1654359Sroberto	Created by cmetz for OPIE 2.3.
1754359Sroberto*/
1854359Sroberto
19182007Sroberto#include "opie_cfg.h"
2054359Sroberto#include <stdio.h>
21182007Sroberto#include <utmp.h>
22182007Sroberto#include "opie.h"
23182007Sroberto
24182007Srobertostatic struct utmp u;
25182007Sroberto
26182007Sroberto#ifndef _PATH_UTMP
2754359Sroberto#ifdef UTMP_FILE
2854359Sroberto#define _PATH_UTMP UTMP_FILE
29182007Sroberto#else /* UTMP_FILE */
30182007Sroberto#define _PATH_UTMP PATH_UTMP_AC
3154359Sroberto#endif /* UTMP_FILE */
32182007Sroberto#endif /* _PATH_UTMP */
33182007Sroberto
3454359Srobertostruct utmp *getutline FUNCTION((utmp), struct utmp *utmp)
3554359Sroberto{
3654359Sroberto  FILE *f;
37285612Sdelphij  int i;
3854359Sroberto
3954359Sroberto  if (!(f = __opieopen(_PATH_UTMP, 0, 0644)))
4054359Sroberto    return 0;
4154359Sroberto
4254359Sroberto#if HAVE_TTYSLOT
4354359Sroberto  if (i = ttyslot()) {
4454359Sroberto    if (fseek(f, i * sizeof(struct utmp), SEEK_SET) < 0)
45182007Sroberto      goto ret;
46285612Sdelphij    if (fread(&u, sizeof(struct utmp), 1, f) != 1)
4754359Sroberto      goto ret;
4854359Sroberto    fclose(f);
4954359Sroberto    return &u;
5054359Sroberto  }
5154359Sroberto#endif /* HAVE_TTYSLOT */
5254359Sroberto
5354359Sroberto  while(fread(&u, sizeof(struct utmp), 1, f) == 1) {
5454359Sroberto    if (!strncmp(utmp->ut_line, u.ut_line, sizeof(u.ut_line) - 1)) {
5554359Sroberto      fclose(f);
56285612Sdelphij      return &u;
5754359Sroberto    }
5854359Sroberto  }
5954359Sroberto
6054359Srobertoret:
6154359Sroberto  fclose(f);
62132451Sroberto  return NULL;
63132451Sroberto}
64132451Sroberto