getutline.c revision 22347
1/* getutline.c: A replacement for the getutline() function 2 3%%% copyright-cmetz 4This software is Copyright 1996 by Craig Metz, All Rights Reserved. 5The Inner Net License Version 2 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 Created by cmetz for OPIE 2.3. 12*/ 13 14#include "opie_cfg.h" 15#include <stdio.h> 16#include <utmp.h> 17#include "opie.h" 18 19static struct utmp u; 20 21struct utmp *getutline FUNCTION((utmp), struct utmp *utmp) 22{ 23 FILE *f; 24 int i; 25 26 if (!(f = __opieopen(_PATH_UTMP, 0, 0644))) 27 return 0; 28 29#if HAVE_TTYSLOT 30 if (i = ttyslot()) { 31 if (fseek(f, i * sizeof(struct utmp), SEEK_SET) < 0) 32 goto ret; 33 if (fread(&u, sizeof(struct utmp), 1, f) != sizeof(struct utmp)) 34 goto ret; 35 fclose(f); 36 return &u; 37 } 38#endif /* HAVE_TTYSLOT */ 39 40 while(fread(&u, sizeof(struct utmp), 1, f) == sizeof(struct utmp)) { 41 if (!strncmp(utmp->ut_line, u.ut_line, sizeof(u.ut_line) - 1)) { 42 fclose(f); 43 return &u; 44 } 45 } 46 47ret: 48 fclose(f); 49 return NULL; 50} 51