Mercurial > hg > octave-kai > gnulib-hg
annotate lib/readutmp.c @ 9084:2932e92d6e31
* lib/version-etc.c (version_etc_va): Default to GPLv3+.
* NEWS: Document this change.
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Tue, 10 Jul 2007 12:22:36 +0000 |
parents | 02a028c7f4ba |
children | bbbbbf4cd1c5 |
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 { | |
7523
02a028c7f4ba
* lib/readutmp.c (desirable_utmp_entry): Use "bool" as the
Jim Meyering <jim@meyering.net>
parents:
7520
diff
changeset
|
72 bool user_proc = IS_USER_PROCESS (u); |
02a028c7f4ba
* lib/readutmp.c (desirable_utmp_entry): Use "bool" as the
Jim Meyering <jim@meyering.net>
parents:
7520
diff
changeset
|
73 if ((options & READ_UTMP_USER_PROCESS) && !user_proc) |
7520
ff9fa1a14d98
(desirable_utmp_entry): Implement new flag: READ_UTMP_USER_PROCESS.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
7302
diff
changeset
|
74 return false; |
ff9fa1a14d98
(desirable_utmp_entry): Implement new flag: READ_UTMP_USER_PROCESS.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
7302
diff
changeset
|
75 if ((options & READ_UTMP_CHECK_PIDS) |
7523
02a028c7f4ba
* lib/readutmp.c (desirable_utmp_entry): Use "bool" as the
Jim Meyering <jim@meyering.net>
parents:
7520
diff
changeset
|
76 && user_proc |
7520
ff9fa1a14d98
(desirable_utmp_entry): Implement new flag: READ_UTMP_USER_PROCESS.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
7302
diff
changeset
|
77 && (UT_PID (u) <= 0 |
ff9fa1a14d98
(desirable_utmp_entry): Implement new flag: READ_UTMP_USER_PROCESS.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
7302
diff
changeset
|
78 || (kill (UT_PID (u), 0) < 0 && errno == ESRCH))) |
ff9fa1a14d98
(desirable_utmp_entry): Implement new flag: READ_UTMP_USER_PROCESS.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
7302
diff
changeset
|
79 return false; |
ff9fa1a14d98
(desirable_utmp_entry): Implement new flag: READ_UTMP_USER_PROCESS.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
7302
diff
changeset
|
80 return true; |
5813 | 81 } |
82 | |
5907 | 83 /* 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
|
84 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
|
85 the number of entries, and return zero. If there is any error, |
5813 | 86 return -1, setting errno, and don't modify the parameters. |
87 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose | |
88 process-IDs do not currently exist. */ | |
981 | 89 |
2246
7805669f5f18
(read_utmp): Guard with `#ifdef UTMP_NAME_FUNCTION',
Jim Meyering <jim@meyering.net>
parents:
2227
diff
changeset
|
90 #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
|
91 |
981 | 92 int |
5907 | 93 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf, |
5813 | 94 int options) |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
95 { |
5813 | 96 size_t n_read = 0; |
97 size_t n_alloc = 0; | |
98 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
|
99 STRUCT_UTMP *u; |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
100 |
1823
93ecfb08bb9c
(read_utmp): Ignore the return value from utmpname.
Jim Meyering <jim@meyering.net>
parents:
1718
diff
changeset
|
101 /* Ignore the return value for now. |
93ecfb08bb9c
(read_utmp): Ignore the return value from utmpname.
Jim Meyering <jim@meyering.net>
parents:
1718
diff
changeset
|
102 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
|
103 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
|
104 versions are actually void. */ |
5907 | 105 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
|
106 |
1830
8139ee8d995b
(read_utmp): Use the new definitions.
Jim Meyering <jim@meyering.net>
parents:
1823
diff
changeset
|
107 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
|
108 |
1830
8139ee8d995b
(read_utmp): Use the new definitions.
Jim Meyering <jim@meyering.net>
parents:
1823
diff
changeset
|
109 while ((u = GET_UTMP_ENT ()) != NULL) |
5813 | 110 if (desirable_utmp_entry (u, options)) |
111 { | |
112 if (n_read == n_alloc) | |
113 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp); | |
114 | |
115 utmp[n_read++] = *u; | |
116 } | |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
117 |
1830
8139ee8d995b
(read_utmp): Use the new definitions.
Jim Meyering <jim@meyering.net>
parents:
1823
diff
changeset
|
118 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
|
119 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
120 *n_entries = n_read; |
1718
db73263fa4b3
(read_utmp) [HAVE_UTMPNAME]: Rewrite.
Jim Meyering <jim@meyering.net>
parents:
1710
diff
changeset
|
121 *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
|
122 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
123 return 0; |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
124 } |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
125 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
126 #else |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
127 |
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
128 int |
5907 | 129 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf, |
5813 | 130 int options) |
981 | 131 { |
5813 | 132 size_t n_read = 0; |
133 size_t n_alloc = 0; | |
134 STRUCT_UTMP *utmp = NULL; | |
135 int saved_errno; | |
5907 | 136 FILE *f = fopen (file, "r"); |
981 | 137 |
5813 | 138 if (! f) |
5159 | 139 return -1; |
981 | 140 |
5813 | 141 for (;;) |
981 | 142 { |
5813 | 143 if (n_read == n_alloc) |
144 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp); | |
145 if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0) | |
146 break; | |
147 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
|
148 } |
5813 | 149 |
150 saved_errno = ferror (f) ? errno : 0; | |
151 if (fclose (f) != 0) | |
152 saved_errno = errno; | |
153 if (saved_errno != 0) | |
4711
37bbd4e80866
Don't trash errno when a read fails.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4398
diff
changeset
|
154 { |
5813 | 155 free (utmp); |
156 errno = saved_errno; | |
5159 | 157 return -1; |
981 | 158 } |
159 | |
4711
37bbd4e80866
Don't trash errno when a read fails.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4398
diff
changeset
|
160 *n_entries = n_read; |
5813 | 161 *utmp_buf = utmp; |
982
ea65b688ec7b
(read_utmp): Take new params: count and buffer.
Jim Meyering <jim@meyering.net>
parents:
981
diff
changeset
|
162 |
ea65b688ec7b
(read_utmp): Take new params: count and buffer.
Jim Meyering <jim@meyering.net>
parents:
981
diff
changeset
|
163 return 0; |
981 | 164 } |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
165 |
2402
41c51e2e9170
Include sys/types.h before sys/stat.h.
Jim Meyering <jim@meyering.net>
parents:
2246
diff
changeset
|
166 #endif |