openat: don’t close (-1)
[gnulib.git] / posix-modules
blob2d40e6a012a910b2466d61c8d01101e4dc92bd2d
1 #!/bin/sh
3 # Copyright (C) 2002-2008, 2010-2024 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
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.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 progname=$0
20 package=gnulib
22 # func_exit STATUS
23 # exits with a given status.
24 # This function needs to be used, rather than 'exit', when a 'trap' handler is
25 # in effect that refers to $?.
26 func_exit ()
28 (exit $1); exit $1
31 # func_fatal_error message
32 # outputs to stderr a fatal error message, and terminates the program.
33 # Input:
34 # - progname name of this program
35 func_fatal_error ()
37 echo "$progname: *** $1" 1>&2
38 echo "$progname: *** Stop." 1>&2
39 func_exit 1
42 # func_readlink SYMLINK
43 # outputs the target of the given symlink.
44 if (type readlink) > /dev/null 2>&1; then
45 func_readlink ()
47 # Use the readlink program from GNU coreutils.
48 readlink "$1"
50 else
51 func_readlink ()
53 # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
54 # would do the wrong thing if the link target contains " -> ".
55 LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
59 # func_gnulib_dir
60 # locates the directory where the gnulib repository lives
61 # Input:
62 # - progname name of this program
63 # Sets variables
64 # - self_abspathname absolute pathname of this program
65 # - gnulib_dir absolute pathname of gnulib repository
66 func_gnulib_dir ()
68 case "$progname" in
69 /* | ?:*) self_abspathname="$progname" ;;
70 */*) self_abspathname=`pwd`/"$progname" ;;
72 # Look in $PATH.
73 # Iterate through the elements of $PATH.
74 # We use IFS=: instead of
75 # for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`
76 # because the latter does not work when some PATH element contains spaces.
77 # We use a canonicalized $pathx instead of $PATH, because empty PATH
78 # elements are by definition equivalent to '.', however field splitting
79 # according to IFS=: loses empty fields in many shells:
80 # - /bin/sh on OSF/1 and Solaris loses all empty fields (at the
81 # beginning, at the end, and in the middle),
82 # - /bin/sh on IRIX and /bin/ksh on IRIX and OSF/1 lose empty fields
83 # at the beginning and at the end,
84 # - GNU bash, /bin/sh on AIX and HP-UX, and /bin/ksh on AIX, HP-UX,
85 # Solaris lose empty fields at the end.
86 # The 'case' statement is an optimization, to avoid evaluating the
87 # explicit canonicalization command when $PATH contains no empty fields.
88 self_abspathname=
89 if test "${PATH_SEPARATOR+set}" != set; then
90 # Determine PATH_SEPARATOR by trying to find /bin/sh in a PATH which
91 # contains only /bin. Note that ksh looks also at the FPATH variable,
92 # so we have to set that as well for the test.
93 PATH_SEPARATOR=:
94 (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
95 && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
96 || PATH_SEPARATOR=';'
99 if test "${PATH_SEPARATOR+set}" != set; then
100 # Determine PATH_SEPARATOR by trying to find /bin/sh in a PATH which
101 # contains only /bin. Note that ksh looks also at the FPATH variable,
102 # so we have to set that as well for the test.
103 PATH_SEPARATOR=:
104 (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
105 && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
106 || PATH_SEPARATOR=';'
109 if test "$PATH_SEPARATOR" = ";"; then
110 # On Windows, programs are searched in "." before $PATH.
111 pathx=".;$PATH"
112 else
113 # On Unix, we have to convert empty PATH elements to ".".
114 pathx="$PATH"
115 case :$PATH: in
116 *::*)
117 pathx=`echo ":$PATH:" | sed -e 's/:::*/:.:/g' -e 's/^://' -e 's/:\$//'`
119 esac
121 saved_IFS="$IFS"
122 IFS="$PATH_SEPARATOR"
123 for d in $pathx; do
124 IFS="$saved_IFS"
125 test -z "$d" && d=.
126 if test -x "$d/$progname" && test ! -d "$d/$progname"; then
127 self_abspathname="$d/$progname"
128 break
130 done
131 IFS="$saved_IFS"
132 if test -z "$self_abspathname"; then
133 func_fatal_error "could not locate the posix-modules program - how did you invoke it?"
136 esac
137 while test -h "$self_abspathname"; do
138 # Resolve symbolic link.
139 linkval=`func_readlink "$self_abspathname"`
140 test -n "$linkval" || break
141 case "$linkval" in
142 /* | ?:* ) self_abspathname="$linkval" ;;
143 * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
144 esac
145 done
146 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
149 # func_tmpdir
150 # creates a temporary directory.
151 # Input:
152 # - progname name of this program
153 # Sets variable
154 # - tmp pathname of freshly created temporary directory
155 func_tmpdir ()
157 # Use the environment variable TMPDIR, falling back to /tmp. This allows
158 # users to specify a different temporary directory, for example, if their
159 # /tmp is filled up or too small.
160 : "${TMPDIR=/tmp}"
162 # Use the mktemp program if available. If not available, hide the error
163 # message.
164 tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
165 test -n "$tmp" && test -d "$tmp"
166 } ||
168 # Use a simple mkdir command. It is guaranteed to fail if the directory
169 # already exists. $RANDOM is bash specific and expands to empty in shells
170 # other than bash, ksh and zsh. Its use does not increase security;
171 # rather, it minimizes the probability of failure in a very cluttered /tmp
172 # directory.
173 tmp=$TMPDIR/gl$$-$RANDOM
174 (umask 077 && mkdir "$tmp")
175 } ||
177 echo "$progname: cannot create a temporary directory in $TMPDIR" >&2
178 func_exit 1
182 # func_usage
183 # outputs to stdout the --help usage message.
184 func_usage ()
186 echo "\
187 Usage: posix-modules [option]
189 Lists the gnulib modules that implement POSIX interfaces.
191 Options:
193 --for-mingw list only modules that work on mingw
194 --for-msvc list only modules that work on MSVC
196 Report bugs to <bug-gnulib@gnu.org>."
199 # func_version
200 # outputs to stdout the --version message.
201 func_version ()
203 func_gnulib_dir
204 if test -d "$gnulib_dir"/.git \
205 && (git --version) >/dev/null 2>/dev/null \
206 && (date --version) >/dev/null 2>/dev/null; then
207 # gnulib checked out from git.
208 sed_extract_first_date='/^Date/{
209 s/^Date:[ ]*//p
212 date=`cd "$gnulib_dir" && git log -n 1 --format=medium --date=iso ChangeLog | sed -n -e "$sed_extract_first_date"`
213 # Turn "Fri Mar 21 07:16:51 2008 -0600" into "Mar 21 2008 07:16:51 -0600".
214 sed_year_before_time='s/^[^ ]* \([^ ]*\) \([0-9]*\) \([0-9:]*\) \([0-9]*\) /\1 \2 \4 \3 /'
215 date=`echo "$date" | sed -e "$sed_year_before_time"`
216 # Use GNU date to compute the time in GMT.
217 date=`date -d "$date" -u +"%Y-%m-%d %H:%M:%S"`
218 version=' '`cd "$gnulib_dir" && ./build-aux/git-version-gen /dev/null | sed -e 's/-dirty/-modified/'`
219 else
220 # gnulib copy without versioning information.
221 date=`sed -e 's/ .*//;q' "$gnulib_dir"/ChangeLog`
222 version=
224 year=`"$gnulib_dir"/build-aux/mdate-sh "$self_abspathname" | sed 's,^.* ,,'`
225 echo "\
226 posix-modules (GNU $package $date)$version
227 Copyright (C) $year Free Software Foundation, Inc.
228 License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
229 This is free software: you are free to change and redistribute it.
230 There is NO WARRANTY, to the extent permitted by law.
232 Written by" "Bruno Haible"
235 # Excludes for mingw.
236 exclude_for_mingw=
237 # <grp.h> does not exist.
238 exclude_for_mingw="$exclude_for_mingw pt_chown grantpt posix_openpt-tests posix_openpt"
239 # The functions getuid, getgid, geteuid, getegid don't exist.
240 exclude_for_mingw="$exclude_for_mingw faccessat"
241 exclude_for_mingw="$exclude_for_mingw fchownat-tests fchownat"
243 # Excludes for MSVC.
244 exclude_for_msvc="$exclude_for_mingw"
246 # Command-line option processing.
247 exclude=
248 while test $# -gt 0; do
249 case "$1" in
250 --for-mingw | --for-ming | --for-min | --for-mi )
251 exclude="$exclude $exclude_for_mingw"
252 shift ;;
253 --for-msvc | --for-msv | --for-ms )
254 exclude="$exclude $exclude_for_msvc"
255 shift ;;
256 --help | --hel | --he | --h )
257 func_usage
258 exit $? ;;
259 --version | --versio | --versi | --vers | --ver | --ve | --v )
260 func_version
261 exit $? ;;
262 -* )
263 echo "posix-modules: unknown option $1" 1>&2
264 echo "Try 'posix-modules --help' for more information." 1>&2
265 exit 1 ;;
267 echo "posix-modules: too many arguments" 1>&2
268 echo "Try 'posix-modules --help' for more information." 1>&2
269 exit 1 ;;
270 esac
271 done
273 func_gnulib_dir
274 func_tmpdir
275 trap 'exit_status=$?
276 if test "$signal" != EXIT; then
277 echo "caught signal SIG$signal" >&2
279 rm -rf "$tmp"
280 exit $exit_status' EXIT
281 for signal in HUP INT QUIT PIPE TERM; do
282 trap '{ signal='$signal'; func_exit 1; }' $signal
283 done
284 signal=EXIT
287 # Get the header modules.
288 LC_ALL=C grep -h '^Gnulib module: ' "$gnulib_dir"/doc/posix-headers/*.texi 2>/dev/null \
289 | sed -e 's,^Gnulib module: ,,'
290 # Get the function modules.
291 LC_ALL=C grep -h '^Gnulib module: ' "$gnulib_dir"/doc/posix-functions/*.texi 2>/dev/null \
292 | sed -e 's,^Gnulib module: ,,'
293 # Then filter out the words "---", ",", "and", "or" and remove *-gnu modules.
294 ) | sed -e 's/,/ /g' | LC_ALL=C sort | LC_ALL=C uniq \
295 | { # Then filter out the words "---", "and", "or" and remove *-gnu modules.
296 tr ' ' '\012' | sed -e '/^---$/d' -e '/^and$/d' -e '/^or$/d' -e '/-gnu$/d'
298 | LC_ALL=C sort | LC_ALL=C uniq \
299 | { # Then filter out the excludes.
300 if test -n "$exclude"; then
301 for m in $exclude; do echo $m; done | LC_ALL=C sort -u > "$tmp"/excludes
302 LC_ALL=C join -v 1 - "$tmp"/excludes
303 rm -f "$tmp"/excludes
304 else
309 # Local Variables:
310 # indent-tabs-mode: nil
311 # whitespace-check-buffer-indent: nil
312 # End: