Mercurial > hg > octave-kai > gnulib-hg
annotate lib/readutmp.c @ 6259:96c32553b4c6
Use a consistent style for including <config.h>.
* __fpending.c, acl.c, argmatch.c,
argp-help.c, argp-parse.c,
argp-pvh.c, backupfile.c, basename.c, c-stack.c,
calloc.c, check-version.c, cloexec.c, closeout.c, copy-file.c,
creat-safer.c, cycle-check.c, dirfd.c, dirname.c, dup-safer.c,
dup2.c, euidaccess.c, exclude.c, exitfail.c, fatal-signal.c,
fd-safer.c, file-type.c, fileblocks.c, filemode.c,
filenamecat.c, findprog.c, fnmatch.c, fopen-safer.c, free.c,
fsusage.c, ftruncate.c, full-write.c, fwriteerror.c,
getaddrinfo.c, getcwd.c, getdelim.c, getline.c, getlogin_r.c,
getndelim2.c, getnline.c, getopt1.c, getpass.c, group-member.c,
hard-locale.c, hash-pjw.c, hash.c, human.c, idcache.c,
inet_ntop.c, isdir.c, long-options.c, malloc.c, memcasecmp.c,
memcmp.c, memcoll.c, memcpy.c, memmove.c, mkdir-p.c,
modechange.c, mountlist.c, open-safer.c, physmem.c,
pipe-safer.c, pipe.c, poll.c, posixver.c, progname.c,
progreloc.c, putenv.c, quote.c, quotearg.c, readline.c,
readlink.c, realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c,
safe-read.c, same.c, save-cwd.c, savedir.c, sig2str.c,
strcspn.c, strerror.c, stripslash.c, strncasecmp.c, strndup.c,
strnlen.c, strnlen1.c, strsep.c, strstr.c, strtod.c,
strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c,
userspec.c, utimecmp.c, version-etc-fsf.c,
version-etc.c, wait-process.c, xalloc-die.c, xgetcwd.c,
xmalloc.c, xmemcoll.c, xnanosleep.c, xreadlink.c, xsetenv.c,
xstrndup.c, xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c:
Standardize inclusion of config.h.
* __fpending.h, dirfd.h, getdate.h, human.h,
inttostr.h: Removed inclusion of config.h from header files.
* inttostr.c: Adjusted in-tree users.
* timespec.h: Remove superfluous warning to include config.h.
* atexit.c, chdir-long.c chown.c, fchown-stub.c, getgroups.c,
gettimeofday.c, lchown.c, lstat.c, mkdir.c, mkstemp.c,
nanosleep.c, openat.c, raise.c, readtokens0.c, readutmp.c,
unlinkdir.c: Guard inclusion of config.h with HAVE_CONFIG_H.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 19 Sep 2005 17:28:14 +0000 |
parents | 673d533b0778 |
children | 8a1a9361108c |
rev | line source |
---|---|
981 | 1 /* GNU's read utmp module. |
5813 | 2 Copyright (C) 1992-2001, 2003, 2004, 2005 Free Software Foundation, Inc. |
981 | 3 |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 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
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
981 | 17 |
18 /* Written by jla; revised by djm */ | |
19 | |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5931
diff
changeset
|
20 #ifdef HAVE_CONFIG_H |
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5931
diff
changeset
|
21 # include <config.h> |
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5931
diff
changeset
|
22 #endif |
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 |