Mercurial > hg > octave-kai > gnulib-hg
annotate lib/readutmp.c @ 7302:8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
longer worry about uses that don't define HAVE_CONFIG_H.
* acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c:
* basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c:
* canonicalize.c, chdir-long.c, chdir-safer.c, chown.c:
* cloexec.c, close-stream.c, closeout.c, creat-safer.c:
* cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c:
* dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c:
* exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c:
* file-type.c, fileblocks.c, filemode.c, filenamecat.c:
* fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c:
* ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c:
* getcwd.c, getdate.y, getdomainname.c, getgroups.c:
* gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c:
* getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c:
* gettime.c, gettimeofday.c, getugroups.c, getusershell.c:
* glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c:
* human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c:
* isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c:
* malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c:
* memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c:
* mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c:
* mountlist.c, nanosleep.c, obstack.c, open-safer.c:
* openat-die.c, openat.c, pagealign_alloc.c, physmem.c:
* pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c:
* quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c:
* realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c:
* same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c:
* sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c:
* stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c:
* strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c:
* timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c:
* utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c:
* xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c:
* xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c:
* xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c:
Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 13 Sep 2006 22:38:14 +0000 |
parents | 96c32553b4c6 |
children | ff9fa1a14d98 |
rev | line source |
---|---|
981 | 1 /* GNU's read utmp module. |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
2 |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
3 Copyright (C) 1992-2001, 2003, 2004, 2005, 2006 Free Software |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
4 Foundation, Inc. |
981 | 5 |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5813
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
981 | 19 |
20 /* Written by jla; revised by djm */ | |
21 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
22 #include <config.h> |
981 | 23 |
5501
96318a40c410
Include readutmp.h first.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
24 #include "readutmp.h" |
96318a40c410
Include readutmp.h first.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
25 |
96318a40c410
Include readutmp.h first.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
26 #include <errno.h> |
1710 | 27 #include <stdio.h> |
28 | |
2402
41c51e2e9170
Include sys/types.h before sys/stat.h.
Jim Meyering <jim@meyering.net>
parents:
2246
diff
changeset
|
29 #include <sys/types.h> |
981 | 30 #include <sys/stat.h> |
5813 | 31 #include <signal.h> |
32 #include <stdbool.h> | |
4398
616adf27c415
Merge changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
33 #include <string.h> |
616adf27c415
Merge changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
34 #include <stdlib.h> |
981 | 35 |
4398
616adf27c415
Merge changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
36 #include "xalloc.h" |
981 | 37 |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
38 #if USE_UNLOCKED_IO |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
39 # include "unlocked-io.h" |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
40 #endif |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
41 |
5813 | 42 #ifndef SIZE_MAX |
43 # define SIZE_MAX ((size_t) -1) | |
44 #endif | |
45 | |
981 | 46 /* Copy UT->ut_name into storage obtained from malloc. Then remove any |
47 trailing spaces from the copy, NUL terminate it, and return the copy. */ | |
48 | |
49 char * | |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
50 extract_trimmed_name (const STRUCT_UTMP *ut) |
981 | 51 { |
52 char *p, *trimmed_name; | |
53 | |
2227
509768b4e550
(extract_trimmed_name): Use UT_USER instead of hard-coding
Jim Meyering <jim@meyering.net>
parents:
1830
diff
changeset
|
54 trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1); |
509768b4e550
(extract_trimmed_name): Use UT_USER instead of hard-coding
Jim Meyering <jim@meyering.net>
parents:
1830
diff
changeset
|
55 strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut))); |
5159 | 56 /* Append a trailing NUL. Some systems pad names shorter than the |
57 maximum with spaces, others pad with NULs. Remove any trailing | |
58 spaces. */ | |
59 trimmed_name[sizeof (UT_USER (ut))] = '\0'; | |
60 for (p = trimmed_name + strlen (trimmed_name); | |
61 trimmed_name < p && p[-1] == ' '; | |
62 *--p = '\0') | |
63 continue; | |
981 | 64 return trimmed_name; |
65 } | |
66 | |
5813 | 67 /* Is the utmp entry U desired by the user who asked for OPTIONS? */ |
68 | |
69 static inline bool | |
70 desirable_utmp_entry (STRUCT_UTMP const *u, int options) | |
71 { | |
72 return ! (options & READ_UTMP_CHECK_PIDS | |
5931
673d533b0778
(desirable_utmp_entry): Fix bug where "who -b" and "who -r"
Paul Eggert <eggert@cs.ucla.edu>
parents:
5907
diff
changeset
|
73 && IS_USER_PROCESS (u) |
5813 | 74 && (UT_PID (u) <= 0 |
75 || (kill (UT_PID (u), 0) < 0 && errno == ESRCH))); | |
76 } | |
77 | |
5907 | 78 /* Read the utmp entries corresponding to file FILE into freshly- |
1718
db73263fa4b3
(read_utmp) [HAVE_UTMPNAME]: Rewrite.
Jim Meyering <jim@meyering.net>
parents:
1710
diff
changeset
|
79 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to |
db73263fa4b3
(read_utmp) [HAVE_UTMPNAME]: Rewrite.
Jim Meyering <jim@meyering.net>
parents:
1710
diff
changeset
|
80 the number of entries, and return zero. If there is any error, |
5813 | 81 return -1, setting errno, and don't modify the parameters. |
82 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose | |
83 process-IDs do not currently exist. */ | |
981 | 84 |
2246
7805669f5f18
(read_utmp): Guard with `#ifdef UTMP_NAME_FUNCTION',
Jim Meyering <jim@meyering.net>
parents:
2227
diff
changeset
|
85 #ifdef UTMP_NAME_FUNCTION |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
86 |
981 | 87 int |
5907 | 88 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf, |
5813 | 89 int options) |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
90 { |
5813 | 91 size_t n_read = 0; |
92 size_t n_alloc = 0; | |
93 STRUCT_UTMP *utmp = NULL; | |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
94 STRUCT_UTMP *u; |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
95 |
1823
93ecfb08bb9c
(read_utmp): Ignore the return value from utmpname.
Jim Meyering <jim@meyering.net>
parents:
1718
diff
changeset
|
96 /* Ignore the return value for now. |
93ecfb08bb9c
(read_utmp): Ignore the return value from utmpname.
Jim Meyering <jim@meyering.net>
parents:
1718
diff
changeset
|
97 Solaris' utmpname returns 1 upon success -- which is contrary |
93ecfb08bb9c
(read_utmp): Ignore the return value from utmpname.
Jim Meyering <jim@meyering.net>
parents:
1718
diff
changeset
|
98 to what the GNU libc version does. In addition, older GNU libc |
93ecfb08bb9c
(read_utmp): Ignore the return value from utmpname.
Jim Meyering <jim@meyering.net>
parents:
1718
diff
changeset
|
99 versions are actually void. */ |
5907 | 100 UTMP_NAME_FUNCTION (file); |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
101 |
1830
8139ee8d995b
(read_utmp): Use the new definitions.
Jim Meyering <jim@meyering.net>
parents:
1823
diff
changeset
|
102 SET_UTMP_ENT (); |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
103 |
1830
8139ee8d995b
(read_utmp): Use the new definitions.
Jim Meyering <jim@meyering.net>
parents:
1823
diff
changeset
|
104 while ((u = GET_UTMP_ENT ()) != NULL) |
5813 | 105 if (desirable_utmp_entry (u, options)) |
106 { | |
107 if (n_read == n_alloc) | |
108 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp); | |
109 | |
110 utmp[n_read++] = *u; | |
111 } | |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
112 |
1830
8139ee8d995b
(read_utmp): Use the new definitions.
Jim Meyering <jim@meyering.net>
parents:
1823
diff
changeset
|
113 END_UTMP_ENT (); |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
114 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
115 *n_entries = n_read; |
1718
db73263fa4b3
(read_utmp) [HAVE_UTMPNAME]: Rewrite.
Jim Meyering <jim@meyering.net>
parents:
1710
diff
changeset
|
116 *utmp_buf = utmp; |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
117 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
118 return 0; |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
119 } |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
120 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
121 #else |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
122 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
123 int |
5907 | 124 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf, |
5813 | 125 int options) |
981 | 126 { |
5813 | 127 size_t n_read = 0; |
128 size_t n_alloc = 0; | |
129 STRUCT_UTMP *utmp = NULL; | |
130 int saved_errno; | |
5907 | 131 FILE *f = fopen (file, "r"); |
981 | 132 |
5813 | 133 if (! f) |
5159 | 134 return -1; |
981 | 135 |
5813 | 136 for (;;) |
981 | 137 { |
5813 | 138 if (n_read == n_alloc) |
139 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp); | |
140 if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0) | |
141 break; | |
142 n_read += desirable_utmp_entry (&utmp[n_read], options); | |
4711
37bbd4e80866
Don't trash errno when a read fails.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4398
diff
changeset
|
143 } |
5813 | 144 |
145 saved_errno = ferror (f) ? errno : 0; | |
146 if (fclose (f) != 0) | |
147 saved_errno = errno; | |
148 if (saved_errno != 0) | |
4711
37bbd4e80866
Don't trash errno when a read fails.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4398
diff
changeset
|
149 { |
5813 | 150 free (utmp); |
151 errno = saved_errno; | |
5159 | 152 return -1; |
981 | 153 } |
154 | |
4711
37bbd4e80866
Don't trash errno when a read fails.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4398
diff
changeset
|
155 *n_entries = n_read; |
5813 | 156 *utmp_buf = utmp; |
982
ea65b688ec7b
(read_utmp): Take new params: count and buffer.
Jim Meyering <jim@meyering.net>
parents:
981
diff
changeset
|
157 |
ea65b688ec7b
(read_utmp): Take new params: count and buffer.
Jim Meyering <jim@meyering.net>
parents:
981
diff
changeset
|
158 return 0; |
981 | 159 } |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
160 |
2402
41c51e2e9170
Include sys/types.h before sys/stat.h.
Jim Meyering <jim@meyering.net>
parents:
2246
diff
changeset
|
161 #endif |