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