Mercurial > hg > octave-kai > gnulib-hg
annotate lib/readutmp.c @ 17160:72f4bab621be
fts: introduce FTS_VERBATIM
This gives clients the option to disable stripping of trailing slashes
from input path names during fts_open initialization.
The recent change v0.0-7611-g3a9002d that made fts_open strip trailing
slashes from input path names had a negative impact on findutils that
relies on the old fts_open behavior to implement POSIX requirement that
each path operand of the find utility shall be evaluated unaltered as it
was provided, including all trailing slash characters.
* lib/fts_.h (FTS_VERBATIM): New bit flag.
(FTS_OPTIONMASK, FTS_NAMEONLY, FTS_STOP): Adjust.
* lib/fts.c (fts_open): Honor it.
author | Dmitry V. Levin <ldv@altlinux.org> |
---|---|
date | Sun, 18 Nov 2012 04:40:18 +0400 |
parents | c2bb4d88ad0d |
children | dcca7ac14066 |
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 |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
3 Copyright (C) 1992-2001, 2003-2006, 2009-2012 Free Software Foundation, Inc. |
981 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7523
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
981 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7523
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7523
diff
changeset
|
8 (at your option) any later version. |
981 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7523
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
981 | 17 |
18 /* Written by jla; revised by djm */ | |
19 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
20 #include <config.h> |
981 | 21 |
5501
96318a40c410
Include readutmp.h first.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
22 #include "readutmp.h" |
96318a40c410
Include readutmp.h first.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
23 |
96318a40c410
Include readutmp.h first.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
24 #include <errno.h> |
1710 | 25 #include <stdio.h> |
26 | |
2402
41c51e2e9170
Include sys/types.h before sys/stat.h.
Jim Meyering <jim@meyering.net>
parents:
2246
diff
changeset
|
27 #include <sys/types.h> |
981 | 28 #include <sys/stat.h> |
5813 | 29 #include <signal.h> |
30 #include <stdbool.h> | |
4398
616adf27c415
Merge changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
31 #include <string.h> |
616adf27c415
Merge changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
32 #include <stdlib.h> |
11395
5442b2430859
avoid gcc warnings about unused macro definitions
Jim Meyering <meyering@redhat.com>
parents:
9309
diff
changeset
|
33 #include <stdint.h> |
981 | 34 |
4398
616adf27c415
Merge changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
35 #include "xalloc.h" |
981 | 36 |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
37 #if USE_UNLOCKED_IO |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
38 # include "unlocked-io.h" |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
39 #endif |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
40 |
981 | 41 /* Copy UT->ut_name into storage obtained from malloc. Then remove any |
42 trailing spaces from the copy, NUL terminate it, and return the copy. */ | |
43 | |
44 char * | |
1362
e5c4dcf21602
(read_utmp): Add variant for systems that have the utmpname function.
Jim Meyering <jim@meyering.net>
parents:
984
diff
changeset
|
45 extract_trimmed_name (const STRUCT_UTMP *ut) |
981 | 46 { |
47 char *p, *trimmed_name; | |
48 | |
2227
509768b4e550
(extract_trimmed_name): Use UT_USER instead of hard-coding
Jim Meyering <jim@meyering.net>
parents:
1830
diff
changeset
|
49 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
|
50 strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut))); |
5159 | 51 /* Append a trailing NUL. Some systems pad names shorter than the |
52 maximum with spaces, others pad with NULs. Remove any trailing | |
53 spaces. */ | |
54 trimmed_name[sizeof (UT_USER (ut))] = '\0'; | |
55 for (p = trimmed_name + strlen (trimmed_name); | |
56 trimmed_name < p && p[-1] == ' '; | |
57 *--p = '\0') | |
58 continue; | |
981 | 59 return trimmed_name; |
60 } | |
61 | |
5813 | 62 /* Is the utmp entry U desired by the user who asked for OPTIONS? */ |
63 | |
64 static inline bool | |
65 desirable_utmp_entry (STRUCT_UTMP const *u, int options) | |
66 { | |
7523
02a028c7f4ba
* lib/readutmp.c (desirable_utmp_entry): Use "bool" as the
Jim Meyering <jim@meyering.net>
parents:
7520
diff
changeset
|
67 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
|
68 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
|
69 return false; |
ff9fa1a14d98
(desirable_utmp_entry): Implement new flag: READ_UTMP_USER_PROCESS.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
7302
diff
changeset
|
70 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
|
71 && user_proc |
17076
c2bb4d88ad0d
readutmp: fix non-portable UT_PID use
Mats Erik Andersson <mats.andersson@gisladisker.se>
parents:
16201
diff
changeset
|
72 && 0 < UT_PID (u) |
c2bb4d88ad0d
readutmp: fix non-portable UT_PID use
Mats Erik Andersson <mats.andersson@gisladisker.se>
parents:
16201
diff
changeset
|
73 && (kill (UT_PID (u), 0) < 0 && errno == ESRCH)) |
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 return true; |
5813 | 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, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11395
diff
changeset
|
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 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11395
diff
changeset
|
107 if (n_read == n_alloc) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11395
diff
changeset
|
108 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp); |
5813 | 109 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11395
diff
changeset
|
110 utmp[n_read++] = *u; |
5813 | 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, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11395
diff
changeset
|
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) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11395
diff
changeset
|
139 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp); |
5813 | 140 if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11395
diff
changeset
|
141 break; |
5813 | 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 |