Merge branch 'vendor/BINUTILS221'
[dragonfly.git] / contrib / opie / libmissing / getutline.c
1 /* getutline.c: A replacement for the getutline() function
2
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you didn't get a copy, you may request one from <license@inner.net>.
8
9         History:
10
11         Modified by cmetz for OPIE 2.32. Fixed check for fread() return
12                 value.
13         Modified by cmetz for OPIE 2.31. If the OS won't tell us where
14                 _PATH_UTMP is, play the SVID game, then use
15                 Autoconf-discovered values.
16         Created by cmetz for OPIE 2.3.
17 */
18
19 #include "opie_cfg.h"
20 #include <stdio.h>
21 #include <utmp.h>
22 #include "opie.h"
23
24 static struct utmp u;
25
26 #ifndef _PATH_UTMP
27 #ifdef UTMP_FILE
28 #define _PATH_UTMP UTMP_FILE
29 #else /* UTMP_FILE */
30 #define _PATH_UTMP PATH_UTMP_AC
31 #endif /* UTMP_FILE */
32 #endif /* _PATH_UTMP */
33
34 struct utmp *getutline FUNCTION((utmp), struct utmp *utmp)
35 {
36   FILE *f;
37   int i;
38
39   if (!(f = __opieopen(_PATH_UTMP, 0, 0644)))
40     return 0;
41
42 #if HAVE_TTYSLOT
43   if (i = ttyslot()) {
44     if (fseek(f, i * sizeof(struct utmp), SEEK_SET) < 0)
45       goto ret;
46     if (fread(&u, sizeof(struct utmp), 1, f) != 1)
47       goto ret;
48     fclose(f);
49     return &u;
50   }
51 #endif /* HAVE_TTYSLOT */
52
53   while(fread(&u, sizeof(struct utmp), 1, f) == 1) {
54     if (!strncmp(utmp->ut_line, u.ut_line, sizeof(u.ut_line) - 1)) {
55       fclose(f);
56       return &u;
57     }
58   }
59
60 ret:
61   fclose(f);
62   return NULL;
63 }