]>
Commit | Line | Data |
---|---|---|
fe5dd171 JB |
1 | diff -Nur coreutils-4.5.9.orig/lib/readutmp.c coreutils-4.5.9/lib/readutmp.c |
2 | --- coreutils-4.5.9.orig/lib/readutmp.c Sat Nov 17 14:29:42 2001 | |
3 | +++ coreutils-4.5.9/lib/readutmp.c Sun Oct 27 21:58:30 2002 | |
40ce7301 JB |
4 | @@ -95,40 +96,31 @@ |
5 | } | |
6 | ||
7 | #else | |
8 | +#define UTMP_READ_INCR 128 | |
9 | ||
10 | int | |
11 | read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf) | |
12 | { | |
13 | - FILE *utmp; | |
14 | - struct stat file_stats; | |
15 | - size_t n_read; | |
16 | - size_t size; | |
17 | - STRUCT_UTMP *buf; | |
18 | - | |
19 | - utmp = fopen (filename, "r"); | |
20 | - if (utmp == NULL) | |
21 | - return 1; | |
22 | - | |
23 | - fstat (fileno (utmp), &file_stats); | |
24 | - size = file_stats.st_size; | |
25 | - if (size > 0) | |
26 | - buf = (STRUCT_UTMP *) xmalloc (size); | |
27 | - else | |
28 | - { | |
29 | - fclose (utmp); | |
30 | - return 1; | |
31 | - } | |
32 | - | |
33 | - /* Use < instead of != in case the utmp just grew. */ | |
34 | - n_read = fread (buf, 1, size, utmp); | |
35 | - if (ferror (utmp) || fclose (utmp) == EOF | |
36 | - || n_read < size) | |
37 | - return 1; | |
38 | + int n_read; | |
39 | + STRUCT_UTMP *uptr; | |
40 | ||
41 | - *n_entries = size / sizeof (STRUCT_UTMP); | |
42 | - *utmp_buf = buf; | |
43 | - | |
44 | - return 0; | |
45 | + utmpname(filename); | |
46 | + setutent(); | |
47 | + n_read = 0; | |
48 | + utmp_contents = (STRUCT_UTMP *) xmalloc(UTMP_READ_INCR*sizeof(STRUCT_UTMP)); | |
49 | + while ((uptr=getutent())) { | |
50 | + if (NULL == utmp_contents) | |
51 | + break; /* we do not have more memory here */ | |
52 | + memcpy(&utmp_contents[n_read], uptr, sizeof(STRUCT_UTMP)); | |
53 | + n_read++; | |
54 | + if (0 == ( n_read % UTMP_READ_INCR )) | |
55 | + utmp_contents = (STRUCT_UTMP *) | |
56 | + xrealloc(utmp_contents, | |
57 | + (n_read + UTMP_READ_INCR) * sizeof(STRUCT_UTMP)); | |
58 | + } | |
59 | + | |
60 | + endutent(); | |
61 | + return n_read; | |
62 | } | |
63 | ||
64 | #endif |