summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am38
-rw-r--r--lib/cloexec.c35
-rw-r--r--lib/cloexec.h2
-rw-r--r--lib/error.c18
-rw-r--r--lib/error.h2
-rw-r--r--lib/exit.h2
-rw-r--r--lib/exitfail.c4
-rw-r--r--lib/exitfail.h2
-rw-r--r--lib/fsusage.c45
-rw-r--r--lib/fsusage.h17
-rw-r--r--lib/full-read.c2
-rw-r--r--lib/full-read.h2
-rw-r--r--lib/full-write.c10
-rw-r--r--lib/full-write.h15
-rw-r--r--lib/getloadavg.c149
-rw-r--r--lib/getopt.c540
-rw-r--r--lib/getopt1.c71
-rw-r--r--lib/getopt_.h (renamed from lib/getopt.h)100
-rw-r--r--lib/getopt_int.h131
-rw-r--r--lib/gettext.h14
-rw-r--r--lib/malloc.c6
-rw-r--r--lib/mountlist.c114
-rw-r--r--lib/mountlist.h40
-rw-r--r--lib/realloc.c19
-rw-r--r--lib/safe-read.c46
-rw-r--r--lib/safe-read.h2
-rw-r--r--lib/safe-write.c2
-rw-r--r--lib/safe-write.h2
-rw-r--r--lib/stdbool_.h54
-rw-r--r--lib/strtod.c4
-rw-r--r--lib/unlocked-io.h203
-rw-r--r--lib/xalloc-die.c45
-rw-r--r--lib/xalloc.h42
-rw-r--r--lib/xmalloc.c70
34 files changed, 1041 insertions, 807 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 0612a75..0d69089 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -6,29 +6,28 @@ noinst_LIBRARIES = libcoreutils.a libnagiosplug.a
6libcoreutils_a_SOURCES = \ 6libcoreutils_a_SOURCES = \
7 cloexec.c cloexec.h \ 7 cloexec.c cloexec.h \
8 exit.h \ 8 exit.h \
9 exitfail.c exitfail.h \
10 fsusage.h \
11 full-read.c full-read.h \ 9 full-read.c full-read.h \
12 full-write.c full-write.h \ 10 full-write.c full-write.h \
13 getopt.c getopt.h getopt1.c \
14 gettext.h \ 11 gettext.h \
15 mountlist.h \
16 safe-read.c safe-read.h \ 12 safe-read.c safe-read.h \
17 safe-write.c safe-write.h \ 13 safe-write.c safe-write.h xalloc-die.c
18 unlocked-io.h \
19 xalloc.h \
20 xmalloc.c \
21 xstrdup.c
22 14
23other_coreutils_files = \ 15other_coreutils_files = \
24 error.c error.h \ 16 error.c error.h \
25 fsusage.c \ 17 fsusage.c \
26 getloadaavg.c \ 18 getloadavg.c \
27 malloc.c \ 19 malloc.c \
28 mountlist.c \ 20 mountlist.c \
29 realloc.c \ 21 realloc.c \
30 stdbool_.h \ 22 strtod.c \
31 strtod.c 23 exitfail.c exitfail.h \
24 fsusage.h \
25 getopt.c getopt1.c \
26 mountlist.h \
27 unlocked-io.h \
28 xalloc.h \
29 xmalloc.c
30
32 31
33libcoreutils_a_LIBADD = $(LIBOBJS) 32libcoreutils_a_LIBADD = $(LIBOBJS)
34libcoreutils_a_DEPENDENCIES = $(libcoreutils_a_LIBADD) 33libcoreutils_a_DEPENDENCIES = $(libcoreutils_a_LIBADD)
@@ -37,6 +36,10 @@ libnagiosplug_a_SOURCES = snprintf.c
37 36
38INCLUDES = -I$(srcdir) -I$(top_srcdir)/intl 37INCLUDES = -I$(srcdir) -I$(top_srcdir)/intl
39 38
39print_coreutil_files:
40 @echo $(libcoreutils_a_SOURCES) $(other_coreutils_files) $(EXTRA_DIST)
41
42# Below are from coreutil's lib/Makefile.am
40BUILT_SOURCES = $(STDBOOL_H) 43BUILT_SOURCES = $(STDBOOL_H)
41EXTRA_DIST = stdbool_.h 44EXTRA_DIST = stdbool_.h
42MOSTLYCLEANFILES = stdbool.h stdbool.ht 45MOSTLYCLEANFILES = stdbool.h stdbool.ht
@@ -45,3 +48,14 @@ stdbool.h: stdbool_.h
45 sed -e 's/@''HAVE__BOOL''@/$(HAVE__BOOL)/g' $(srcdir)/stdbool_.h > $@t 48 sed -e 's/@''HAVE__BOOL''@/$(HAVE__BOOL)/g' $(srcdir)/stdbool_.h > $@t
46 mv $@t $@ 49 mv $@t $@
47 50
51BUILT_SOURCES += $(GETOPT_H)
52EXTRA_DIST += getopt_.h getopt_int.h
53
54# We need the following in order to create an <getopt.h> when the system
55# doesn't have one that works with the given compiler.
56all-local $(lib_OBJECTS): $(GETOPT_H)
57getopt.h: getopt_.h
58 cp $(srcdir)/getopt_.h $@-t
59 mv $@-t $@
60MOSTLYCLEANFILES += getopt.h getopt.h-t
61
diff --git a/lib/cloexec.c b/lib/cloexec.c
index 20f30db..cf2308c 100644
--- a/lib/cloexec.c
+++ b/lib/cloexec.c
@@ -1,5 +1,5 @@
1/* closexec.c - set or clear the close-on-exec descriptor flag 1/* closexec.c - set or clear the close-on-exec descriptor flag
2 Copyright (C) 1991, 2004 Free Software Foundation, Inc. 2 Copyright (C) 1991, 2004, 2005 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
@@ -13,23 +13,18 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 The code is taken from glibc/manual/llio.texi */ 18 The code is taken from glibc/manual/llio.texi */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23 23
24#include "cloexec.h" 24#include "cloexec.h"
25 25
26#if HAVE_UNISTD_H 26#include <unistd.h>
27# include <unistd.h> 27#include <fcntl.h>
28#endif
29
30#if HAVE_FCNTL_H
31# include <fcntl.h>
32#endif
33 28
34#ifndef FD_CLOEXEC 29#ifndef FD_CLOEXEC
35# define FD_CLOEXEC 1 30# define FD_CLOEXEC 1
@@ -37,27 +32,29 @@
37 32
38/* Set the `FD_CLOEXEC' flag of DESC if VALUE is true, 33/* Set the `FD_CLOEXEC' flag of DESC if VALUE is true,
39 or clear the flag if VALUE is false. 34 or clear the flag if VALUE is false.
40 Return true on success, or false on error with `errno' set. */ 35 Return 0 on success, or -1 on error with `errno' set. */
41 36
42bool 37int
43set_cloexec_flag (int desc, bool value) 38set_cloexec_flag (int desc, bool value)
44{ 39{
45#if defined F_GETFD && defined F_SETFD 40#if defined F_GETFD && defined F_SETFD
46 41
47 int flags = fcntl (desc, F_GETFD, 0); 42 int flags = fcntl (desc, F_GETFD, 0);
48 int newflags;
49 43
50 if (flags < 0) 44 if (0 <= flags)
51 return false; 45 {
46 int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
52 47
53 newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC); 48 if (flags == newflags
49 || fcntl (desc, F_SETFD, newflags) != -1)
50 return 0;
51 }
54 52
55 return (flags == newflags 53 return -1;
56 || fcntl (desc, F_SETFD, newflags) != -1);
57 54
58#else 55#else
59 56
60 return true; 57 return 0;
61 58
62#endif 59#endif
63} 60}
diff --git a/lib/cloexec.h b/lib/cloexec.h
index ecad8d7..c25921d 100644
--- a/lib/cloexec.h
+++ b/lib/cloexec.h
@@ -1,2 +1,2 @@
1#include <stdbool.h> 1#include <stdbool.h>
2bool set_cloexec_flag (int desc, bool value); 2int set_cloexec_flag (int desc, bool value);
diff --git a/lib/error.c b/lib/error.c
index 1149235..45698be 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -1,5 +1,5 @@
1/* Error handler for noninteractive utilities 1/* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2002, 2003 Free Software Foundation, Inc. 2 Copyright (C) 1990-1998, 2000-2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
@@ -14,7 +14,7 @@
14 14
15 You should have received a copy of the GNU General Public License along 15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, 16 with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 18
19/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ 19/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 20
@@ -29,9 +29,7 @@
29#include <stdlib.h> 29#include <stdlib.h>
30#include <string.h> 30#include <string.h>
31 31
32#ifdef _LIBC 32#if !_LIBC && ENABLE_NLS
33# include <libintl.h>
34#else
35# include "gettext.h" 33# include "gettext.h"
36#endif 34#endif
37 35
@@ -40,7 +38,7 @@
40# define mbsrtowcs __mbsrtowcs 38# define mbsrtowcs __mbsrtowcs
41#endif 39#endif
42 40
43#if !_LIBC 41#if USE_UNLOCKED_IO
44# include "unlocked-io.h" 42# include "unlocked-io.h"
45#endif 43#endif
46 44
@@ -106,7 +104,7 @@ extern char *program_name;
106static void 104static void
107print_errno_message (int errnum) 105print_errno_message (int errnum)
108{ 106{
109 char const *s; 107 char const *s = NULL;
110 108
111#if defined HAVE_STRERROR_R || _LIBC 109#if defined HAVE_STRERROR_R || _LIBC
112 char errbuf[1024]; 110 char errbuf[1024];
@@ -115,15 +113,11 @@ print_errno_message (int errnum)
115# else 113# else
116 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0) 114 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
117 s = errbuf; 115 s = errbuf;
118 else
119 s = 0;
120# endif 116# endif
121#else
122 s = strerror (errnum);
123#endif 117#endif
124 118
125#if !_LIBC 119#if !_LIBC
126 if (! s) 120 if (! s && ! (s = strerror (errnum)))
127 s = _("Unknown system error"); 121 s = _("Unknown system error");
128#endif 122#endif
129 123
diff --git a/lib/error.h b/lib/error.h
index 8ed6359..e5220a2 100644
--- a/lib/error.h
+++ b/lib/error.h
@@ -14,7 +14,7 @@
14 14
15 You should have received a copy of the GNU General Public License along 15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, 16 with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 18
19#ifndef _ERROR_H 19#ifndef _ERROR_H
20#define _ERROR_H 1 20#define _ERROR_H 1
diff --git a/lib/exit.h b/lib/exit.h
index 4e8d465..9dbfb7c 100644
--- a/lib/exit.h
+++ b/lib/exit.h
@@ -13,7 +13,7 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18#ifndef _EXIT_H 18#ifndef _EXIT_H
19#define _EXIT_H 19#define _EXIT_H
diff --git a/lib/exitfail.c b/lib/exitfail.c
index 2ae5f69..27d38c3 100644
--- a/lib/exitfail.c
+++ b/lib/exitfail.c
@@ -15,9 +15,9 @@
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. 16 along with this program; see the file COPYING.
17 If not, write to the Free Software Foundation, 17 If not, write to the Free Software Foundation,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23 23
diff --git a/lib/exitfail.h b/lib/exitfail.h
index cf5ab71..e46cf9c 100644
--- a/lib/exitfail.h
+++ b/lib/exitfail.h
@@ -15,6 +15,6 @@
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. 16 along with this program; see the file COPYING.
17 If not, write to the Free Software Foundation, 17 If not, write to the Free Software Foundation,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20extern int volatile exit_failure; 20extern int volatile exit_failure;
diff --git a/lib/fsusage.c b/lib/fsusage.c
index d926029..b137790 100644
--- a/lib/fsusage.c
+++ b/lib/fsusage.c
@@ -1,7 +1,7 @@
1/* fsusage.c -- return space usage of mounted filesystems 1/* fsusage.c -- return space usage of mounted file systems
2 2
3 Copyright (C) 1991, 1992, 1996, 1998, 1999, 2002, 2003 Free 3 Copyright (C) 1991, 1992, 1996, 1998, 1999, 2002, 2003, 2004, 2005
4 Software Foundation, Inc. 4 Free Software Foundation, Inc.
5 5
6 This program is free software; you can redistribute it and/or modify 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 7 it under the terms of the GNU General Public License as published by
@@ -15,19 +15,19 @@
15 15
16 You should have received a copy of the GNU General Public License 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, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23 23
24#if HAVE_INTTYPES_H 24#if HAVE_INTTYPES_H
25# include <inttypes.h> 25# include <inttypes.h>
26#else
27# if HAVE_STDINT_H
28# include <stdint.h>
29# endif
30#endif 26#endif
27#if HAVE_STDINT_H
28# include <stdint.h>
29#endif
30#include <unistd.h>
31#ifndef UINTMAX_MAX 31#ifndef UINTMAX_MAX
32# define UINTMAX_MAX ((uintmax_t) -1) 32# define UINTMAX_MAX ((uintmax_t) -1)
33#endif 33#endif
@@ -58,9 +58,7 @@
58# include <sys/filsys.h> /* SVR2 */ 58# include <sys/filsys.h> /* SVR2 */
59#endif 59#endif
60 60
61#if HAVE_FCNTL_H 61#include <fcntl.h>
62# include <fcntl.h>
63#endif
64 62
65#if HAVE_SYS_STATFS_H 63#if HAVE_SYS_STATFS_H
66# include <sys/statfs.h> 64# include <sys/statfs.h>
@@ -72,7 +70,6 @@
72 70
73#if HAVE_SYS_STATVFS_H /* SVR4 */ 71#if HAVE_SYS_STATVFS_H /* SVR4 */
74# include <sys/statvfs.h> 72# include <sys/statvfs.h>
75int statvfs ();
76#endif 73#endif
77 74
78#include "full-read.h" 75#include "full-read.h"
@@ -102,20 +99,20 @@ int statvfs ();
102#define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1)) 99#define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1))
103 100
104/* Fill in the fields of FSP with information about space usage for 101/* Fill in the fields of FSP with information about space usage for
105 the filesystem on which PATH resides. 102 the file system on which FILE resides.
106 DISK is the device on which PATH is mounted, for space-getting 103 DISK is the device on which FILE is mounted, for space-getting
107 methods that need to know it. 104 methods that need to know it.
108 Return 0 if successful, -1 if not. When returning -1, ensure that 105 Return 0 if successful, -1 if not. When returning -1, ensure that
109 ERRNO is either a system error value, or zero if DISK is NULL 106 ERRNO is either a system error value, or zero if DISK is NULL
110 on a system that requires a non-NULL value. */ 107 on a system that requires a non-NULL value. */
111int 108int
112get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp) 109get_fs_usage (char const *file, char const *disk, struct fs_usage *fsp)
113{ 110{
114#ifdef STAT_STATFS3_OSF1 111#ifdef STAT_STATFS3_OSF1
115 112
116 struct statfs fsd; 113 struct statfs fsd;
117 114
118 if (statfs (path, &fsd, sizeof (struct statfs)) != 0) 115 if (statfs (file, &fsd, sizeof (struct statfs)) != 0)
119 return -1; 116 return -1;
120 117
121 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize); 118 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
@@ -126,7 +123,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp)
126 123
127 struct fs_data fsd; 124 struct fs_data fsd;
128 125
129 if (statfs (path, &fsd) != 1) 126 if (statfs (file, &fsd) != 1)
130 return -1; 127 return -1;
131 128
132 fsp->fsu_blocksize = 1024; 129 fsp->fsu_blocksize = 1024;
@@ -180,7 +177,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp)
180 177
181 struct statfs fsd; 178 struct statfs fsd;
182 179
183 if (statfs (path, &fsd) < 0) 180 if (statfs (file, &fsd) < 0)
184 return -1; 181 return -1;
185 182
186 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize); 183 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
@@ -206,7 +203,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp)
206 203
207 struct statfs fsd; 204 struct statfs fsd;
208 205
209 if (statfs (path, &fsd) < 0) 206 if (statfs (file, &fsd) < 0)
210 return -1; 207 return -1;
211 208
212 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize); 209 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
@@ -221,7 +218,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp)
221 218
222 struct statfs fsd; 219 struct statfs fsd;
223 220
224 if (statfs (path, &fsd, sizeof fsd, 0) < 0) 221 if (statfs (file, &fsd, sizeof fsd, 0) < 0)
225 return -1; 222 return -1;
226 223
227 /* Empirically, the block counts on most SVR3 and SVR3-derived 224 /* Empirically, the block counts on most SVR3 and SVR3-derived
@@ -239,7 +236,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp)
239 236
240 struct statvfs fsd; 237 struct statvfs fsd;
241 238
242 if (statvfs (path, &fsd) < 0) 239 if (statvfs (file, &fsd) < 0)
243 return -1; 240 return -1;
244 241
245 /* f_frsize isn't guaranteed to be supported. */ 242 /* f_frsize isn't guaranteed to be supported. */
@@ -268,12 +265,12 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp)
268/* AIX PS/2 does not supply statfs. */ 265/* AIX PS/2 does not supply statfs. */
269 266
270int 267int
271statfs (char *path, struct statfs *fsb) 268statfs (char *file, struct statfs *fsb)
272{ 269{
273 struct stat stats; 270 struct stat stats;
274 struct dustat fsd; 271 struct dustat fsd;
275 272
276 if (stat (path, &stats)) 273 if (stat (file, &stats) != 0)
277 return -1; 274 return -1;
278 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd))) 275 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
279 return -1; 276 return -1;
diff --git a/lib/fsusage.h b/lib/fsusage.h
index e2cbbf1..f3eda2d 100644
--- a/lib/fsusage.h
+++ b/lib/fsusage.h
@@ -1,6 +1,7 @@
1/* fsusage.h -- declarations for filesystem space usage info 1/* fsusage.h -- declarations for file system space usage info
2 2
3 Copyright (C) 1991, 1992, 1997, 2003 Free Software Foundation, Inc. 3 Copyright (C) 1991, 1992, 1997, 2003, 2004, 2005 Free Software
4 Foundation, Inc.
4 5
5 This program is free software; you can redistribute it and/or modify 6 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 it under the terms of the GNU General Public License as published by
@@ -14,24 +15,26 @@
14 15
15 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation, 17 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 19
19/* Space usage statistics for a filesystem. Blocks are 512-byte. */ 20/* Space usage statistics for a file system. Blocks are 512-byte. */
20 21
21#if !defined FSUSAGE_H_ 22#if !defined FSUSAGE_H_
22# define FSUSAGE_H_ 23# define FSUSAGE_H_
23 24
25# include <stdbool.h>
26
24struct fs_usage 27struct fs_usage
25{ 28{
26 int fsu_blocksize; /* Size of a block. */ 29 uintmax_t fsu_blocksize; /* Size of a block. */
27 uintmax_t fsu_blocks; /* Total blocks. */ 30 uintmax_t fsu_blocks; /* Total blocks. */
28 uintmax_t fsu_bfree; /* Free blocks available to superuser. */ 31 uintmax_t fsu_bfree; /* Free blocks available to superuser. */
29 uintmax_t fsu_bavail; /* Free blocks available to non-superuser. */ 32 uintmax_t fsu_bavail; /* Free blocks available to non-superuser. */
30 int fsu_bavail_top_bit_set; /* 1 if fsu_bavail represents a value < 0. */ 33 bool fsu_bavail_top_bit_set; /* 1 if fsu_bavail represents a value < 0. */
31 uintmax_t fsu_files; /* Total file nodes. */ 34 uintmax_t fsu_files; /* Total file nodes. */
32 uintmax_t fsu_ffree; /* Free file nodes. */ 35 uintmax_t fsu_ffree; /* Free file nodes. */
33}; 36};
34 37
35int get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp); 38int get_fs_usage (char const *file, char const *disk, struct fs_usage *fsp);
36 39
37#endif 40#endif
diff --git a/lib/full-read.c b/lib/full-read.c
index 1dc3e37..8c3472a 100644
--- a/lib/full-read.c
+++ b/lib/full-read.c
@@ -13,7 +13,7 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18#define FULL_READ 18#define FULL_READ
19#include "full-write.c" 19#include "full-write.c"
diff --git a/lib/full-read.h b/lib/full-read.h
index 71f19a3..05d83a7 100644
--- a/lib/full-read.h
+++ b/lib/full-read.h
@@ -14,7 +14,7 @@
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program; if not, read to the Free Software Foundation, 16 along with this program; if not, read to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 18
19#include <stddef.h> 19#include <stddef.h>
20 20
diff --git a/lib/full-write.c b/lib/full-write.c
index 1513705..106f3c7 100644
--- a/lib/full-write.c
+++ b/lib/full-write.c
@@ -1,6 +1,7 @@
1/* An interface to read and write that retries (if necessary) until complete. 1/* An interface to read and write that retries (if necessary) until complete.
2 2
3 Copyright (C) 1993, 1994, 1997-2003 Free Software Foundation, Inc. 3 Copyright (C) 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004 Free Software Foundation, Inc.
4 5
5 This program is free software; you can redistribute it and/or modify 6 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 it under the terms of the GNU General Public License as published by
@@ -14,9 +15,9 @@
14 15
15 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation, 17 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 19
19#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
20# include <config.h> 21# include <config.h>
21#endif 22#endif
22 23
@@ -28,9 +29,6 @@
28#endif 29#endif
29 30
30#include <errno.h> 31#include <errno.h>
31#ifndef errno
32extern int errno;
33#endif
34 32
35#ifdef FULL_READ 33#ifdef FULL_READ
36# include "safe-read.h" 34# include "safe-read.h"
diff --git a/lib/full-write.h b/lib/full-write.h
index 8cd2e81..d20d2fe 100644
--- a/lib/full-write.h
+++ b/lib/full-write.h
@@ -1,6 +1,6 @@
1/* An interface to write() that writes all it is asked to write. 1/* An interface to write() that writes all it is asked to write.
2 2
3 Copyright (C) 2002 Free Software Foundation, Inc. 3 Copyright (C) 2002-2003 Free Software Foundation, Inc.
4 4
5 This program is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published by
@@ -14,11 +14,22 @@
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation, 16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 18
19#include <stddef.h> 19#include <stddef.h>
20 20
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26
21/* Write COUNT bytes at BUF to descriptor FD, retrying if interrupted 27/* Write COUNT bytes at BUF to descriptor FD, retrying if interrupted
22 or if partial writes occur. Return the number of bytes successfully 28 or if partial writes occur. Return the number of bytes successfully
23 written, setting errno if that is less than COUNT. */ 29 written, setting errno if that is less than COUNT. */
24extern size_t full_write (int fd, const void *buf, size_t count); 30extern size_t full_write (int fd, const void *buf, size_t count);
31
32
33#ifdef __cplusplus
34}
35#endif
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index c6b6fee..2a50131 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -1,7 +1,7 @@
1/* Get the system load averages. 1/* Get the system load averages.
2 2
3 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, 3 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994,
4 1995, 1997, 1999, 2000, 2003, 2004 Free Software Foundation, Inc. 4 1995, 1997, 1999, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
5 5
6 NOTE: The canonical source of this file is maintained with gnulib. 6 NOTE: The canonical source of this file is maintained with gnulib.
7 Bugs can be reported to bug-gnulib@gnu.org. 7 Bugs can be reported to bug-gnulib@gnu.org.
@@ -18,7 +18,7 @@
18 18
19 You should have received a copy of the GNU General Public License 19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software 20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 USA. */ 22 USA. */
23 23
24/* Compile-time symbols that this file uses: 24/* Compile-time symbols that this file uses:
@@ -32,7 +32,7 @@
32 AC_CHECK_FUNCS(pstat_getdynamic) in your 32 AC_CHECK_FUNCS(pstat_getdynamic) in your
33 configure.in file. 33 configure.in file.
34 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist. 34 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
35 KERNEL_FILE Pathname of the kernel to nlist. 35 KERNEL_FILE Name of the kernel file to nlist.
36 LDAV_CVT() Scale the load average from the kernel. 36 LDAV_CVT() Scale the load average from the kernel.
37 Returns a double. 37 Returns a double.
38 LDAV_SYMBOL Name of kernel symbol giving load average. 38 LDAV_SYMBOL Name of kernel symbol giving load average.
@@ -47,9 +47,8 @@
47 the nlist n_name element is a pointer, 47 the nlist n_name element is a pointer,
48 not an array. 48 not an array.
49 HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'. 49 HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'.
50 LINUX_LDAV_FILE [__linux__]: File containing load averages. 50 LINUX_LDAV_FILE [__linux__, __CYGWIN__]: File containing
51 HAVE_LOCALE_H locale.h is available. 51 load averages.
52 HAVE_SETLOCALE The `setlocale' function is available.
53 52
54 Specific system predefines this file uses, aside from setting 53 Specific system predefines this file uses, aside from setting
55 default values if not emacs: 54 default values if not emacs:
@@ -70,9 +69,10 @@
70 UMAX4_3 69 UMAX4_3
71 VMS 70 VMS
72 WINDOWS32 No-op for Windows95/NT. 71 WINDOWS32 No-op for Windows95/NT.
73 __linux__ Linux: assumes /proc filesystem mounted. 72 __linux__ Linux: assumes /proc file system mounted.
74 Support from Michael K. Johnson. 73 Support from Michael K. Johnson.
75 __NetBSD__ NetBSD: assumes /kern filesystem mounted. 74 __CYGWIN__ Cygwin emulates linux /proc/loadavg.
75 __NetBSD__ NetBSD: assumes /kern file system mounted.
76 76
77 In addition, to avoid nesting many #ifdefs, we internally set 77 In addition, to avoid nesting many #ifdefs, we internally set
78 LDAV_DONE to indicate that the load average has been computed. 78 LDAV_DONE to indicate that the load average has been computed.
@@ -85,38 +85,29 @@
85# include <config.h> 85# include <config.h>
86#endif 86#endif
87 87
88#include <sys/types.h> 88#include <errno.h>
89 89#include <stdbool.h>
90/* Both the Emacs and non-Emacs sections want this. Some 90#include <stdio.h>
91 configuration files' definitions for the LOAD_AVE_CVT macro (like 91#include <stdlib.h>
92 sparc.h's) use macros like FSCALE, defined here. */
93#if defined (unix) || defined (__unix)
94# include <sys/param.h>
95#endif
96
97 92
98/* Exclude all the code except the test program at the end 93/* Exclude all the code except the test program at the end
99 if the system has its own `getloadavg' function. 94 if the system has its own `getloadavg' function. */
100 95
101 The declaration of `errno' is needed by the test program 96#ifndef HAVE_GETLOADAVG
102 as well as the function itself, so it comes first. */
103
104#include <errno.h>
105
106#ifndef errno
107extern int errno;
108#endif
109 97
110#ifdef HAVE_LOCALE_H 98# include <sys/types.h>
111# include <locale.h>
112#endif
113#ifndef HAVE_SETLOCALE
114# define setlocale(Category, Locale) /* empty */
115#endif
116 99
117#include "cloexec.h" 100/* Both the Emacs and non-Emacs sections want this. Some
101 configuration files' definitions for the LOAD_AVE_CVT macro (like
102 sparc.h's) use macros like FSCALE, defined here. */
103# if defined (unix) || defined (__unix)
104# include <sys/param.h>
105# endif
118 106
119#ifndef HAVE_GETLOADAVG 107# include "c-strtod.h"
108# include "cloexec.h"
109# include "intprops.h"
110# include "xalloc.h"
120 111
121/* The existing Emacs configuration files define a macro called 112/* The existing Emacs configuration files define a macro called
122 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and 113 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
@@ -357,11 +348,7 @@ extern int errno;
357# define LDAV_SYMBOL "avenrun" 348# define LDAV_SYMBOL "avenrun"
358# endif 349# endif
359 350
360# ifdef HAVE_UNISTD_H 351# include <unistd.h>
361# include <unistd.h>
362# endif
363
364# include <stdio.h>
365 352
366/* LOAD_AVE_TYPE should only get defined if we're going to use the 353/* LOAD_AVE_TYPE should only get defined if we're going to use the
367 nlist method. */ 354 nlist method. */
@@ -435,7 +422,6 @@ extern int errno;
435# endif /* sgi */ 422# endif /* sgi */
436 423
437# ifdef UMAX 424# ifdef UMAX
438# include <stdio.h>
439# include <signal.h> 425# include <signal.h>
440# include <sys/time.h> 426# include <sys/time.h>
441# include <sys/wait.h> 427# include <sys/wait.h>
@@ -461,17 +447,13 @@ extern int errno;
461# include <sys/dg_sys_info.h> 447# include <sys/dg_sys_info.h>
462# endif 448# endif
463 449
464# if defined (HAVE_FCNTL_H) || defined (_POSIX_VERSION) 450# include "fcntl--.h"
465# include <fcntl.h>
466# else
467# include <sys/file.h>
468# endif
469 451
470/* Avoid static vars inside a function since in HPUX they dump as pure. */ 452/* Avoid static vars inside a function since in HPUX they dump as pure. */
471 453
472# ifdef NeXT 454# ifdef NeXT
473static processor_set_t default_set; 455static processor_set_t default_set;
474static int getloadavg_initialized; 456static bool getloadavg_initialized;
475# endif /* NeXT */ 457# endif /* NeXT */
476 458
477# ifdef UMAX 459# ifdef UMAX
@@ -486,8 +468,8 @@ static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
486# if !defined (HAVE_LIBKSTAT) && defined (LOAD_AVE_TYPE) 468# if !defined (HAVE_LIBKSTAT) && defined (LOAD_AVE_TYPE)
487/* File descriptor open to /dev/kmem or VMS load ave driver. */ 469/* File descriptor open to /dev/kmem or VMS load ave driver. */
488static int channel; 470static int channel;
489/* Nonzero iff channel is valid. */ 471/* True iff channel is valid. */
490static int getloadavg_initialized; 472static bool getloadavg_initialized;
491/* Offset in kmem to seek to read load average, or 0 means invalid. */ 473/* Offset in kmem to seek to read load average, or 0 means invalid. */
492static long offset; 474static long offset;
493 475
@@ -583,7 +565,7 @@ getloadavg (double loadavg[], int nelem)
583 565
584# endif /* hpux && HAVE_PSTAT_GETDYNAMIC */ 566# endif /* hpux && HAVE_PSTAT_GETDYNAMIC */
585 567
586# if !defined (LDAV_DONE) && defined (__linux__) 568# if !defined (LDAV_DONE) && (defined (__linux__) || defined (__CYGWIN__))
587# define LDAV_DONE 569# define LDAV_DONE
588# undef LOAD_AVE_TYPE 570# undef LOAD_AVE_TYPE
589 571
@@ -591,32 +573,36 @@ getloadavg (double loadavg[], int nelem)
591# define LINUX_LDAV_FILE "/proc/loadavg" 573# define LINUX_LDAV_FILE "/proc/loadavg"
592# endif 574# endif
593 575
594 char ldavgbuf[40]; 576 char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
595 double load_ave[3]; 577 char const *ptr = ldavgbuf;
596 int fd, count; 578 int fd, count;
597 579
598 fd = open (LINUX_LDAV_FILE, O_RDONLY); 580 fd = open (LINUX_LDAV_FILE, O_RDONLY);
599 if (fd == -1) 581 if (fd == -1)
600 return -1; 582 return -1;
601 count = read (fd, ldavgbuf, 40); 583 count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
602 (void) close (fd); 584 (void) close (fd);
603 if (count <= 0) 585 if (count <= 0)
604 return -1; 586 return -1;
587 ldavgbuf[count] = '\0';
605 588
606 /* The following sscanf must use the C locale. */ 589 for (elem = 0; elem < nelem; elem++)
607 setlocale (LC_NUMERIC, "C"); 590 {
608 count = sscanf (ldavgbuf, "%lf %lf %lf", 591 char *endptr;
609 &load_ave[0], &load_ave[1], &load_ave[2]); 592 double d = c_strtod (ptr, &endptr);
610 setlocale (LC_NUMERIC, ""); 593 if (ptr == endptr)
611 if (count < 1) 594 {
612 return -1; 595 if (elem == 0)
613 596 return -1;
614 for (elem = 0; elem < nelem && elem < count; elem++) 597 break;
615 loadavg[elem] = load_ave[elem]; 598 }
599 loadavg[elem] = d;
600 ptr = endptr;
601 }
616 602
617 return elem; 603 return elem;
618 604
619# endif /* __linux__ */ 605# endif /* __linux__ || __CYGWIN__ */
620 606
621# if !defined (LDAV_DONE) && defined (__NetBSD__) 607# if !defined (LDAV_DONE) && defined (__NetBSD__)
622# define LDAV_DONE 608# define LDAV_DONE
@@ -653,7 +639,7 @@ getloadavg (double loadavg[], int nelem)
653 639
654 host_t host; 640 host_t host;
655 struct processor_set_basic_info info; 641 struct processor_set_basic_info info;
656 unsigned info_count; 642 unsigned int info_count;
657 643
658 /* We only know how to get the 1-minute average for this system, 644 /* We only know how to get the 1-minute average for this system,
659 so even if the caller asks for more than 1, we only return 1. */ 645 so even if the caller asks for more than 1, we only return 1. */
@@ -661,7 +647,7 @@ getloadavg (double loadavg[], int nelem)
661 if (!getloadavg_initialized) 647 if (!getloadavg_initialized)
662 { 648 {
663 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS) 649 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
664 getloadavg_initialized = 1; 650 getloadavg_initialized = true;
665 } 651 }
666 652
667 if (getloadavg_initialized) 653 if (getloadavg_initialized)
@@ -670,7 +656,7 @@ getloadavg (double loadavg[], int nelem)
670 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host, 656 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
671 (processor_set_info_t) &info, &info_count) 657 (processor_set_info_t) &info, &info_count)
672 != KERN_SUCCESS) 658 != KERN_SUCCESS)
673 getloadavg_initialized = 0; 659 getloadavg_initialized = false;
674 else 660 else
675 { 661 {
676 if (nelem > 0) 662 if (nelem > 0)
@@ -831,7 +817,7 @@ getloadavg (double loadavg[], int nelem)
831 /* VMS specific code -- read from the Load Ave driver. */ 817 /* VMS specific code -- read from the Load Ave driver. */
832 818
833 LOAD_AVE_TYPE load_ave[3]; 819 LOAD_AVE_TYPE load_ave[3];
834 static int getloadavg_initialized = 0; 820 static bool getloadavg_initialized;
835# ifdef eunice 821# ifdef eunice
836 struct 822 struct
837 { 823 {
@@ -851,7 +837,7 @@ getloadavg (double loadavg[], int nelem)
851 $DESCRIPTOR (descriptor, "LAV0:"); 837 $DESCRIPTOR (descriptor, "LAV0:");
852# endif 838# endif
853 if (sys$assign (&descriptor, &channel, 0, 0) & 1) 839 if (sys$assign (&descriptor, &channel, 0, 0) & 1)
854 getloadavg_initialized = 1; 840 getloadavg_initialized = true;
855 } 841 }
856 842
857 /* Read the load average vector. */ 843 /* Read the load average vector. */
@@ -860,7 +846,7 @@ getloadavg (double loadavg[], int nelem)
860 load_ave, 12, 0, 0, 0, 0) & 1)) 846 load_ave, 12, 0, 0, 0, 0) & 1))
861 { 847 {
862 sys$dassgn (channel); 848 sys$dassgn (channel);
863 getloadavg_initialized = 0; 849 getloadavg_initialized = false;
864 } 850 }
865 851
866 if (!getloadavg_initialized) 852 if (!getloadavg_initialized)
@@ -913,7 +899,7 @@ getloadavg (double loadavg[], int nelem)
913 899
914 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN); 900 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
915 if (ldav_off != -1) 901 if (ldav_off != -1)
916 offset = (long) ldav_off & 0x7fffffff; 902 offset = (long int) ldav_off & 0x7fffffff;
917# endif /* sgi */ 903# endif /* sgi */
918 } 904 }
919 905
@@ -921,13 +907,13 @@ getloadavg (double loadavg[], int nelem)
921 if (!getloadavg_initialized) 907 if (!getloadavg_initialized)
922 { 908 {
923# ifndef SUNOS_5 909# ifndef SUNOS_5
924 channel = open ("/dev/kmem", 0); 910 channel = open ("/dev/kmem", O_RDONLY);
925 if (channel >= 0) 911 if (channel >= 0)
926 { 912 {
927 /* Set the channel to close on exec, so it does not 913 /* Set the channel to close on exec, so it does not
928 litter any child's descriptor table. */ 914 litter any child's descriptor table. */
929 set_cloexec_flag (channel, true); 915 set_cloexec_flag (channel, true);
930 getloadavg_initialized = 1; 916 getloadavg_initialized = true;
931 } 917 }
932# else /* SUNOS_5 */ 918# else /* SUNOS_5 */
933 /* We pass 0 for the kernel, corefile, and swapfile names 919 /* We pass 0 for the kernel, corefile, and swapfile names
@@ -938,7 +924,7 @@ getloadavg (double loadavg[], int nelem)
938 /* nlist the currently running kernel. */ 924 /* nlist the currently running kernel. */
939 kvm_nlist (kd, nl); 925 kvm_nlist (kd, nl);
940 offset = nl[0].n_value; 926 offset = nl[0].n_value;
941 getloadavg_initialized = 1; 927 getloadavg_initialized = true;
942 } 928 }
943# endif /* SUNOS_5 */ 929# endif /* SUNOS_5 */
944 } 930 }
@@ -953,14 +939,14 @@ getloadavg (double loadavg[], int nelem)
953 != sizeof (load_ave)) 939 != sizeof (load_ave))
954 { 940 {
955 close (channel); 941 close (channel);
956 getloadavg_initialized = 0; 942 getloadavg_initialized = false;
957 } 943 }
958# else /* SUNOS_5 */ 944# else /* SUNOS_5 */
959 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave)) 945 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
960 != sizeof (load_ave)) 946 != sizeof (load_ave))
961 { 947 {
962 kvm_close (kd); 948 kvm_close (kd);
963 getloadavg_initialized = 0; 949 getloadavg_initialized = false;
964 } 950 }
965# endif /* SUNOS_5 */ 951# endif /* SUNOS_5 */
966 } 952 }
@@ -980,20 +966,19 @@ getloadavg (double loadavg[], int nelem)
980# define LDAV_DONE 966# define LDAV_DONE
981# endif /* !LDAV_DONE && LOAD_AVE_TYPE */ 967# endif /* !LDAV_DONE && LOAD_AVE_TYPE */
982 968
983# ifdef LDAV_DONE 969# if !defined LDAV_DONE
984 return elem;
985# else
986 /* Set errno to zero to indicate that there was no particular error; 970 /* Set errno to zero to indicate that there was no particular error;
987 this function just can't work at all on this system. */ 971 this function just can't work at all on this system. */
988 errno = 0; 972 errno = 0;
989 return -1; 973 elem = -1;
990# endif 974# endif
975 return elem;
991} 976}
992 977
993#endif /* ! HAVE_GETLOADAVG */ 978#endif /* ! HAVE_GETLOADAVG */
994 979
995#ifdef TEST 980#ifdef TEST
996void 981int
997main (int argc, char **argv) 982main (int argc, char **argv)
998{ 983{
999 int naptime = 0; 984 int naptime = 0;
@@ -1011,7 +996,7 @@ main (int argc, char **argv)
1011 if (loads == -1) 996 if (loads == -1)
1012 { 997 {
1013 perror ("Error getting load average"); 998 perror ("Error getting load average");
1014 exit (1); 999 return EXIT_FAILURE;
1015 } 1000 }
1016 if (loads > 0) 1001 if (loads > 0)
1017 printf ("1-minute: %f ", avg[0]); 1002 printf ("1-minute: %f ", avg[0]);
@@ -1027,6 +1012,6 @@ main (int argc, char **argv)
1027 sleep (naptime); 1012 sleep (naptime);
1028 } 1013 }
1029 1014
1030 exit (0); 1015 return EXIT_SUCCESS;
1031} 1016}
1032#endif /* TEST */ 1017#endif /* TEST */
diff --git a/lib/getopt.c b/lib/getopt.c
index 6dcdbeb..bcb81c8 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -2,11 +2,8 @@
2 NOTE: getopt is now part of the C library, so if you don't know what 2 NOTE: getopt is now part of the C library, so if you don't know what
3 "Keep this file name-space clean" means, talk to drepper@gnu.org 3 "Keep this file name-space clean" means, talk to drepper@gnu.org
4 before changing it! 4 before changing it!
5 5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
6 Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 6 Free Software Foundation, Inc.
7 1996, 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
8 Inc.
9
10 This file is part of the GNU C Library. 7 This file is part of the GNU C Library.
11 8
12 This program is free software; you can redistribute it and/or modify 9 This program is free software; you can redistribute it and/or modify
@@ -21,7 +18,7 @@
21 18
22 You should have received a copy of the GNU General Public License along 19 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, 20 with this program; if not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 21 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 22
26/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 23/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
27 Ditto for AIX 3.2 and <stdlib.h>. */ 24 Ditto for AIX 3.2 and <stdlib.h>. */
@@ -35,25 +32,6 @@
35 32
36#include <stdio.h> 33#include <stdio.h>
37 34
38/* Comment out all this code if we are using the GNU C Library, and are not
39 actually compiling the library itself. This code is part of the GNU C
40 Library, but also included in many other GNU distributions. Compiling
41 and linking in this code is a waste when using the GNU C library
42 (especially if it is a shared library). Rather than having every GNU
43 program understand `configure --with-gnu-libc' and omit the object files,
44 it is simpler to just do this in the source for each such file. */
45
46#define GETOPT_INTERFACE_VERSION 2
47#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
48# include <gnu-versions.h>
49# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
50# define ELIDE_CODE
51# endif
52#endif
53
54#ifndef ELIDE_CODE
55
56
57/* This needs to come after some library #include 35/* This needs to come after some library #include
58 to get __GNU_LIBRARY__ defined. */ 36 to get __GNU_LIBRARY__ defined. */
59#ifdef __GNU_LIBRARY__ 37#ifdef __GNU_LIBRARY__
@@ -72,10 +50,9 @@
72#ifdef _LIBC 50#ifdef _LIBC
73# include <libintl.h> 51# include <libintl.h>
74#else 52#else
75/* This is for other GNU distributions with internationalized messages. */
76# include "gettext.h" 53# include "gettext.h"
54# define _(msgid) gettext (msgid)
77#endif 55#endif
78#define _(msgid) gettext (msgid)
79 56
80#if defined _LIBC && defined USE_IN_LIBIO 57#if defined _LIBC && defined USE_IN_LIBIO
81# include <wchar.h> 58# include <wchar.h>
@@ -85,21 +62,22 @@
85# define attribute_hidden 62# define attribute_hidden
86#endif 63#endif
87 64
88/* This version of `getopt' appears to the caller like standard Unix `getopt' 65/* Unlike standard Unix `getopt', functions like `getopt_long'
89 but it behaves differently for the user, since it allows the user 66 let the user intersperse the options with the other arguments.
90 to intersperse the options with the other arguments.
91 67
92 As `getopt' works, it permutes the elements of ARGV so that, 68 As `getopt_long' works, it permutes the elements of ARGV so that,
93 when it is done, all the options precede everything else. Thus 69 when it is done, all the options precede everything else. Thus
94 all application programs are extended to handle flexible argument order. 70 all application programs are extended to handle flexible argument order.
95 71
96 Setting the environment variable POSIXLY_CORRECT disables permutation. 72 Using `getopt' or setting the environment variable POSIXLY_CORRECT
97 Then the behavior is completely standard. 73 disables permutation.
74 Then the application's behavior is completely standard.
98 75
99 GNU application programs can use a third alternative mode in which 76 GNU application programs can use a third alternative mode in which
100 they can distinguish the relative order of options and other arguments. */ 77 they can distinguish the relative order of options and other arguments. */
101 78
102#include "getopt.h" 79#include "getopt.h"
80#include "getopt_int.h"
103 81
104/* For communication from `getopt' to the caller. 82/* For communication from `getopt' to the caller.
105 When `getopt' finds an option that takes an argument, 83 When `getopt' finds an option that takes an argument,
@@ -124,21 +102,6 @@ char *optarg;
124/* 1003.2 says this must be 1 before any call. */ 102/* 1003.2 says this must be 1 before any call. */
125int optind = 1; 103int optind = 1;
126 104
127/* Formerly, initialization of getopt depended on optind==0, which
128 causes problems with re-calling getopt as programs generally don't
129 know that. */
130
131int __getopt_initialized attribute_hidden;
132
133/* The next char to be scanned in the option-element
134 in which the last option character we returned was found.
135 This allows us to pick up the scan where we left off.
136
137 If this is zero, or a null string, it means resume the scan
138 by advancing to the next ARGV-element. */
139
140static char *nextchar;
141
142/* Callers store zero here to inhibit the error message 105/* Callers store zero here to inhibit the error message
143 for unrecognized options. */ 106 for unrecognized options. */
144 107
@@ -150,44 +113,12 @@ int opterr = 1;
150 113
151int optopt = '?'; 114int optopt = '?';
152 115
153/* Describe how to deal with options that follow non-option ARGV-elements. 116/* Keep a global copy of all internal members of getopt_data. */
154
155 If the caller did not specify anything,
156 the default is REQUIRE_ORDER if the environment variable
157 POSIXLY_CORRECT is defined, PERMUTE otherwise.
158
159 REQUIRE_ORDER means don't recognize them as options;
160 stop option processing when the first non-option is seen.
161 This is what Unix does.
162 This mode of operation is selected by either setting the environment
163 variable POSIXLY_CORRECT, or using `+' as the first character
164 of the list of option characters.
165
166 PERMUTE is the default. We permute the contents of ARGV as we scan,
167 so that eventually all the non-options are at the end. This allows options
168 to be given in any order, even with programs that were not written to
169 expect this.
170
171 RETURN_IN_ORDER is an option available to programs that were written
172 to expect options and other ARGV-elements in any order and that care about
173 the ordering of the two. We describe each non-option ARGV-element
174 as if it were the argument of an option with character code 1.
175 Using `-' as the first character of the list of option characters
176 selects this mode of operation.
177 117
178 The special argument `--' forces an end of option-scanning regardless 118static struct _getopt_data getopt_data;
179 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
180 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
181 119
182static enum
183{
184 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
185} ordering;
186
187/* Value of POSIXLY_CORRECT environment variable. */
188static char *posixly_correct;
189 120
190#ifndef __GNU_LIBRARY__ 121#ifndef __GNU_LIBRARY__
191 122
192/* Avoid depending on library functions or files 123/* Avoid depending on library functions or files
193 whose names are inconsistent. */ 124 whose names are inconsistent. */
@@ -198,15 +129,6 @@ extern char *getenv ();
198 129
199#endif /* not __GNU_LIBRARY__ */ 130#endif /* not __GNU_LIBRARY__ */
200 131
201/* Handle permutation of arguments. */
202
203/* Describe the part of ARGV that contains non-options that have
204 been skipped. `first_nonopt' is the index in ARGV of the first of them;
205 `last_nonopt' is the index after the last of them. */
206
207static int first_nonopt;
208static int last_nonopt;
209
210#ifdef _LIBC 132#ifdef _LIBC
211/* Stored original parameters. 133/* Stored original parameters.
212 XXX This is no good solution. We should rather copy the args so 134 XXX This is no good solution. We should rather copy the args so
@@ -220,14 +142,11 @@ extern char **__libc_argv;
220# ifdef USE_NONOPTION_FLAGS 142# ifdef USE_NONOPTION_FLAGS
221/* Defined in getopt_init.c */ 143/* Defined in getopt_init.c */
222extern char *__getopt_nonoption_flags; 144extern char *__getopt_nonoption_flags;
223
224static int nonoption_flags_max_len;
225static int nonoption_flags_len;
226# endif 145# endif
227 146
228# ifdef USE_NONOPTION_FLAGS 147# ifdef USE_NONOPTION_FLAGS
229# define SWAP_FLAGS(ch1, ch2) \ 148# define SWAP_FLAGS(ch1, ch2) \
230 if (nonoption_flags_len > 0) \ 149 if (d->__nonoption_flags_len > 0) \
231 { \ 150 { \
232 char __tmp = __getopt_nonoption_flags[ch1]; \ 151 char __tmp = __getopt_nonoption_flags[ch1]; \
233 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 152 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
@@ -250,11 +169,11 @@ static int nonoption_flags_len;
250 the new indices of the non-options in ARGV after they are moved. */ 169 the new indices of the non-options in ARGV after they are moved. */
251 170
252static void 171static void
253exchange (char **argv) 172exchange (char **argv, struct _getopt_data *d)
254{ 173{
255 int bottom = first_nonopt; 174 int bottom = d->__first_nonopt;
256 int middle = last_nonopt; 175 int middle = d->__last_nonopt;
257 int top = optind; 176 int top = d->optind;
258 char *tem; 177 char *tem;
259 178
260 /* Exchange the shorter segment with the far end of the longer segment. 179 /* Exchange the shorter segment with the far end of the longer segment.
@@ -266,19 +185,19 @@ exchange (char **argv)
266 /* First make sure the handling of the `__getopt_nonoption_flags' 185 /* First make sure the handling of the `__getopt_nonoption_flags'
267 string can work normally. Our top argument must be in the range 186 string can work normally. Our top argument must be in the range
268 of the string. */ 187 of the string. */
269 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 188 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
270 { 189 {
271 /* We must extend the array. The user plays games with us and 190 /* We must extend the array. The user plays games with us and
272 presents new arguments. */ 191 presents new arguments. */
273 char *new_str = malloc (top + 1); 192 char *new_str = malloc (top + 1);
274 if (new_str == NULL) 193 if (new_str == NULL)
275 nonoption_flags_len = nonoption_flags_max_len = 0; 194 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
276 else 195 else
277 { 196 {
278 memset (__mempcpy (new_str, __getopt_nonoption_flags, 197 memset (__mempcpy (new_str, __getopt_nonoption_flags,
279 nonoption_flags_max_len), 198 d->__nonoption_flags_max_len),
280 '\0', top + 1 - nonoption_flags_max_len); 199 '\0', top + 1 - d->__nonoption_flags_max_len);
281 nonoption_flags_max_len = top + 1; 200 d->__nonoption_flags_max_len = top + 1;
282 __getopt_nonoption_flags = new_str; 201 __getopt_nonoption_flags = new_str;
283 } 202 }
284 } 203 }
@@ -324,70 +243,71 @@ exchange (char **argv)
324 243
325 /* Update records for the slots the non-options now occupy. */ 244 /* Update records for the slots the non-options now occupy. */
326 245
327 first_nonopt += (optind - last_nonopt); 246 d->__first_nonopt += (d->optind - d->__last_nonopt);
328 last_nonopt = optind; 247 d->__last_nonopt = d->optind;
329} 248}
330 249
331/* Initialize the internal data when the first call is made. */ 250/* Initialize the internal data when the first call is made. */
332 251
333static const char * 252static const char *
334_getopt_initialize (int argc, char *const *argv, const char *optstring) 253_getopt_initialize (int argc, char **argv, const char *optstring,
254 int posixly_correct, struct _getopt_data *d)
335{ 255{
336 /* Start processing options with ARGV-element 1 (since ARGV-element 0 256 /* Start processing options with ARGV-element 1 (since ARGV-element 0
337 is the program name); the sequence of previously skipped 257 is the program name); the sequence of previously skipped
338 non-option ARGV-elements is empty. */ 258 non-option ARGV-elements is empty. */
339 259
340 first_nonopt = last_nonopt = optind; 260 d->__first_nonopt = d->__last_nonopt = d->optind;
341 261
342 nextchar = NULL; 262 d->__nextchar = NULL;
343 263
344 posixly_correct = getenv ("POSIXLY_CORRECT"); 264 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
345 265
346 /* Determine how to handle the ordering of options and nonoptions. */ 266 /* Determine how to handle the ordering of options and nonoptions. */
347 267
348 if (optstring[0] == '-') 268 if (optstring[0] == '-')
349 { 269 {
350 ordering = RETURN_IN_ORDER; 270 d->__ordering = RETURN_IN_ORDER;
351 ++optstring; 271 ++optstring;
352 } 272 }
353 else if (optstring[0] == '+') 273 else if (optstring[0] == '+')
354 { 274 {
355 ordering = REQUIRE_ORDER; 275 d->__ordering = REQUIRE_ORDER;
356 ++optstring; 276 ++optstring;
357 } 277 }
358 else if (posixly_correct != NULL) 278 else if (d->__posixly_correct)
359 ordering = REQUIRE_ORDER; 279 d->__ordering = REQUIRE_ORDER;
360 else 280 else
361 ordering = PERMUTE; 281 d->__ordering = PERMUTE;
362 282
363#if defined _LIBC && defined USE_NONOPTION_FLAGS 283#if defined _LIBC && defined USE_NONOPTION_FLAGS
364 if (posixly_correct == NULL 284 if (!d->__posixly_correct
365 && argc == __libc_argc && argv == __libc_argv) 285 && argc == __libc_argc && argv == __libc_argv)
366 { 286 {
367 if (nonoption_flags_max_len == 0) 287 if (d->__nonoption_flags_max_len == 0)
368 { 288 {
369 if (__getopt_nonoption_flags == NULL 289 if (__getopt_nonoption_flags == NULL
370 || __getopt_nonoption_flags[0] == '\0') 290 || __getopt_nonoption_flags[0] == '\0')
371 nonoption_flags_max_len = -1; 291 d->__nonoption_flags_max_len = -1;
372 else 292 else
373 { 293 {
374 const char *orig_str = __getopt_nonoption_flags; 294 const char *orig_str = __getopt_nonoption_flags;
375 int len = nonoption_flags_max_len = strlen (orig_str); 295 int len = d->__nonoption_flags_max_len = strlen (orig_str);
376 if (nonoption_flags_max_len < argc) 296 if (d->__nonoption_flags_max_len < argc)
377 nonoption_flags_max_len = argc; 297 d->__nonoption_flags_max_len = argc;
378 __getopt_nonoption_flags = 298 __getopt_nonoption_flags =
379 (char *) malloc (nonoption_flags_max_len); 299 (char *) malloc (d->__nonoption_flags_max_len);
380 if (__getopt_nonoption_flags == NULL) 300 if (__getopt_nonoption_flags == NULL)
381 nonoption_flags_max_len = -1; 301 d->__nonoption_flags_max_len = -1;
382 else 302 else
383 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), 303 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
384 '\0', nonoption_flags_max_len - len); 304 '\0', d->__nonoption_flags_max_len - len);
385 } 305 }
386 } 306 }
387 nonoption_flags_len = nonoption_flags_max_len; 307 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
388 } 308 }
389 else 309 else
390 nonoption_flags_len = 0; 310 d->__nonoption_flags_len = 0;
391#endif 311#endif
392 312
393 return optstring; 313 return optstring;
@@ -435,10 +355,6 @@ _getopt_initialize (int argc, char *const *argv, const char *optstring)
435 `flag' field is nonzero, the value of the option's `val' field 355 `flag' field is nonzero, the value of the option's `val' field
436 if the `flag' field is zero. 356 if the `flag' field is zero.
437 357
438 The elements of ARGV aren't really const, because we permute them.
439 But we pretend they're const in the prototype to be compatible
440 with other systems.
441
442 LONGOPTS is a vector of `struct option' terminated by an 358 LONGOPTS is a vector of `struct option' terminated by an
443 element containing a name which is zero. 359 element containing a name which is zero.
444 360
@@ -447,28 +363,32 @@ _getopt_initialize (int argc, char *const *argv, const char *optstring)
447 recent call. 363 recent call.
448 364
449 If LONG_ONLY is nonzero, '-' as well as '--' can introduce 365 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
450 long-named options. */ 366 long-named options.
367
368 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
369 environment variable were set. */
451 370
452int 371int
453_getopt_internal (int argc, char *const *argv, 372_getopt_internal_r (int argc, char **argv, const char *optstring,
454 const char *optstring, const struct option *longopts, 373 const struct option *longopts, int *longind,
455 int *longind, int long_only) 374 int long_only, int posixly_correct, struct _getopt_data *d)
456{ 375{
457 int print_errors = opterr; 376 int print_errors = d->opterr;
458 if (optstring[0] == ':') 377 if (optstring[0] == ':')
459 print_errors = 0; 378 print_errors = 0;
460 379
461 if (argc < 1) 380 if (argc < 1)
462 return -1; 381 return -1;
463 382
464 optarg = NULL; 383 d->optarg = NULL;
465 384
466 if (optind == 0 || !__getopt_initialized) 385 if (d->optind == 0 || !d->__initialized)
467 { 386 {
468 if (optind == 0) 387 if (d->optind == 0)
469 optind = 1; /* Don't scan ARGV[0], the program name. */ 388 d->optind = 1; /* Don't scan ARGV[0], the program name. */
470 optstring = _getopt_initialize (argc, argv, optstring); 389 optstring = _getopt_initialize (argc, argv, optstring,
471 __getopt_initialized = 1; 390 posixly_correct, d);
391 d->__initialized = 1;
472 } 392 }
473 393
474 /* Test whether ARGV[optind] points to a non-option argument. 394 /* Test whether ARGV[optind] points to a non-option argument.
@@ -476,40 +396,41 @@ _getopt_internal (int argc, char *const *argv,
476 from the shell indicating it is not an option. The later information 396 from the shell indicating it is not an option. The later information
477 is only used when the used in the GNU libc. */ 397 is only used when the used in the GNU libc. */
478#if defined _LIBC && defined USE_NONOPTION_FLAGS 398#if defined _LIBC && defined USE_NONOPTION_FLAGS
479# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 399# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
480 || (optind < nonoption_flags_len \ 400 || (d->optind < d->__nonoption_flags_len \
481 && __getopt_nonoption_flags[optind] == '1')) 401 && __getopt_nonoption_flags[d->optind] == '1'))
482#else 402#else
483# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 403# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
484#endif 404#endif
485 405
486 if (nextchar == NULL || *nextchar == '\0') 406 if (d->__nextchar == NULL || *d->__nextchar == '\0')
487 { 407 {
488 /* Advance to the next ARGV-element. */ 408 /* Advance to the next ARGV-element. */
489 409
490 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 410 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
491 moved back by the user (who may also have changed the arguments). */ 411 moved back by the user (who may also have changed the arguments). */
492 if (last_nonopt > optind) 412 if (d->__last_nonopt > d->optind)
493 last_nonopt = optind; 413 d->__last_nonopt = d->optind;
494 if (first_nonopt > optind) 414 if (d->__first_nonopt > d->optind)
495 first_nonopt = optind; 415 d->__first_nonopt = d->optind;
496 416
497 if (ordering == PERMUTE) 417 if (d->__ordering == PERMUTE)
498 { 418 {
499 /* If we have just processed some options following some non-options, 419 /* If we have just processed some options following some non-options,
500 exchange them so that the options come first. */ 420 exchange them so that the options come first. */
501 421
502 if (first_nonopt != last_nonopt && last_nonopt != optind) 422 if (d->__first_nonopt != d->__last_nonopt
503 exchange ((char **) argv); 423 && d->__last_nonopt != d->optind)
504 else if (last_nonopt != optind) 424 exchange ((char **) argv, d);
505 first_nonopt = optind; 425 else if (d->__last_nonopt != d->optind)
426 d->__first_nonopt = d->optind;
506 427
507 /* Skip any additional non-options 428 /* Skip any additional non-options
508 and extend the range of non-options previously skipped. */ 429 and extend the range of non-options previously skipped. */
509 430
510 while (optind < argc && NONOPTION_P) 431 while (d->optind < argc && NONOPTION_P)
511 optind++; 432 d->optind++;
512 last_nonopt = optind; 433 d->__last_nonopt = d->optind;
513 } 434 }
514 435
515 /* The special ARGV-element `--' means premature end of options. 436 /* The special ARGV-element `--' means premature end of options.
@@ -517,28 +438,29 @@ _getopt_internal (int argc, char *const *argv,
517 then exchange with previous non-options as if it were an option, 438 then exchange with previous non-options as if it were an option,
518 then skip everything else like a non-option. */ 439 then skip everything else like a non-option. */
519 440
520 if (optind != argc && !strcmp (argv[optind], "--")) 441 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
521 { 442 {
522 optind++; 443 d->optind++;
523 444
524 if (first_nonopt != last_nonopt && last_nonopt != optind) 445 if (d->__first_nonopt != d->__last_nonopt
525 exchange ((char **) argv); 446 && d->__last_nonopt != d->optind)
526 else if (first_nonopt == last_nonopt) 447 exchange ((char **) argv, d);
527 first_nonopt = optind; 448 else if (d->__first_nonopt == d->__last_nonopt)
528 last_nonopt = argc; 449 d->__first_nonopt = d->optind;
450 d->__last_nonopt = argc;
529 451
530 optind = argc; 452 d->optind = argc;
531 } 453 }
532 454
533 /* If we have done all the ARGV-elements, stop the scan 455 /* If we have done all the ARGV-elements, stop the scan
534 and back over any non-options that we skipped and permuted. */ 456 and back over any non-options that we skipped and permuted. */
535 457
536 if (optind == argc) 458 if (d->optind == argc)
537 { 459 {
538 /* Set the next-arg-index to point at the non-options 460 /* Set the next-arg-index to point at the non-options
539 that we previously skipped, so the caller will digest them. */ 461 that we previously skipped, so the caller will digest them. */
540 if (first_nonopt != last_nonopt) 462 if (d->__first_nonopt != d->__last_nonopt)
541 optind = first_nonopt; 463 d->optind = d->__first_nonopt;
542 return -1; 464 return -1;
543 } 465 }
544 466
@@ -547,17 +469,17 @@ _getopt_internal (int argc, char *const *argv,
547 469
548 if (NONOPTION_P) 470 if (NONOPTION_P)
549 { 471 {
550 if (ordering == REQUIRE_ORDER) 472 if (d->__ordering == REQUIRE_ORDER)
551 return -1; 473 return -1;
552 optarg = argv[optind++]; 474 d->optarg = argv[d->optind++];
553 return 1; 475 return 1;
554 } 476 }
555 477
556 /* We have found another option-ARGV-element. 478 /* We have found another option-ARGV-element.
557 Skip the initial punctuation. */ 479 Skip the initial punctuation. */
558 480
559 nextchar = (argv[optind] + 1 481 d->__nextchar = (argv[d->optind] + 1
560 + (longopts != NULL && argv[optind][1] == '-')); 482 + (longopts != NULL && argv[d->optind][1] == '-'));
561 } 483 }
562 484
563 /* Decode the current option-ARGV-element. */ 485 /* Decode the current option-ARGV-element. */
@@ -576,9 +498,9 @@ _getopt_internal (int argc, char *const *argv,
576 This distinction seems to be the most useful approach. */ 498 This distinction seems to be the most useful approach. */
577 499
578 if (longopts != NULL 500 if (longopts != NULL
579 && (argv[optind][1] == '-' 501 && (argv[d->optind][1] == '-'
580 || (long_only 502 || (long_only && (argv[d->optind][2]
581 && (argv[optind][2] || !strchr (optstring, argv[optind][1]))))) 503 || !strchr (optstring, argv[d->optind][1])))))
582 { 504 {
583 char *nameend; 505 char *nameend;
584 const struct option *p; 506 const struct option *p;
@@ -588,15 +510,15 @@ _getopt_internal (int argc, char *const *argv,
588 int indfound = -1; 510 int indfound = -1;
589 int option_index; 511 int option_index;
590 512
591 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 513 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
592 /* Do nothing. */ ; 514 /* Do nothing. */ ;
593 515
594 /* Test all long options for either exact match 516 /* Test all long options for either exact match
595 or abbreviated matches. */ 517 or abbreviated matches. */
596 for (p = longopts, option_index = 0; p->name; p++, option_index++) 518 for (p = longopts, option_index = 0; p->name; p++, option_index++)
597 if (!strncmp (p->name, nextchar, nameend - nextchar)) 519 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
598 { 520 {
599 if ((unsigned int) (nameend - nextchar) 521 if ((unsigned int) (nameend - d->__nextchar)
600 == (unsigned int) strlen (p->name)) 522 == (unsigned int) strlen (p->name))
601 { 523 {
602 /* Exact match found. */ 524 /* Exact match found. */
@@ -627,37 +549,44 @@ _getopt_internal (int argc, char *const *argv,
627 char *buf; 549 char *buf;
628 550
629 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), 551 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
630 argv[0], argv[optind]) >= 0) 552 argv[0], argv[d->optind]) >= 0)
631 { 553 {
554 _IO_flockfile (stderr);
555
556 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
557 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
632 558
633 if (_IO_fwide (stderr, 0) > 0) 559 if (_IO_fwide (stderr, 0) > 0)
634 __fwprintf (stderr, L"%s", buf); 560 __fwprintf (stderr, L"%s", buf);
635 else 561 else
636 fputs (buf, stderr); 562 fputs (buf, stderr);
637 563
564 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
565 _IO_funlockfile (stderr);
566
638 free (buf); 567 free (buf);
639 } 568 }
640#else 569#else
641 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 570 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
642 argv[0], argv[optind]); 571 argv[0], argv[d->optind]);
643#endif 572#endif
644 } 573 }
645 nextchar += strlen (nextchar); 574 d->__nextchar += strlen (d->__nextchar);
646 optind++; 575 d->optind++;
647 optopt = 0; 576 d->optopt = 0;
648 return '?'; 577 return '?';
649 } 578 }
650 579
651 if (pfound != NULL) 580 if (pfound != NULL)
652 { 581 {
653 option_index = indfound; 582 option_index = indfound;
654 optind++; 583 d->optind++;
655 if (*nameend) 584 if (*nameend)
656 { 585 {
657 /* Don't test has_arg with >, because some C compilers don't 586 /* Don't test has_arg with >, because some C compilers don't
658 allow it to be used on enums. */ 587 allow it to be used on enums. */
659 if (pfound->has_arg) 588 if (pfound->has_arg)
660 optarg = nameend + 1; 589 d->optarg = nameend + 1;
661 else 590 else
662 { 591 {
663 if (print_errors) 592 if (print_errors)
@@ -667,7 +596,7 @@ _getopt_internal (int argc, char *const *argv,
667 int n; 596 int n;
668#endif 597#endif
669 598
670 if (argv[optind - 1][1] == '-') 599 if (argv[d->optind - 1][1] == '-')
671 { 600 {
672 /* --option */ 601 /* --option */
673#if defined _LIBC && defined USE_IN_LIBIO 602#if defined _LIBC && defined USE_IN_LIBIO
@@ -686,38 +615,48 @@ _getopt_internal (int argc, char *const *argv,
686#if defined _LIBC && defined USE_IN_LIBIO 615#if defined _LIBC && defined USE_IN_LIBIO
687 n = __asprintf (&buf, _("\ 616 n = __asprintf (&buf, _("\
688%s: option `%c%s' doesn't allow an argument\n"), 617%s: option `%c%s' doesn't allow an argument\n"),
689 argv[0], argv[optind - 1][0], 618 argv[0], argv[d->optind - 1][0],
690 pfound->name); 619 pfound->name);
691#else 620#else
692 fprintf (stderr, _("\ 621 fprintf (stderr, _("\
693%s: option `%c%s' doesn't allow an argument\n"), 622%s: option `%c%s' doesn't allow an argument\n"),
694 argv[0], argv[optind - 1][0], pfound->name); 623 argv[0], argv[d->optind - 1][0],
624 pfound->name);
695#endif 625#endif
696 } 626 }
697 627
698#if defined _LIBC && defined USE_IN_LIBIO 628#if defined _LIBC && defined USE_IN_LIBIO
699 if (n >= 0) 629 if (n >= 0)
700 { 630 {
631 _IO_flockfile (stderr);
632
633 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
634 ((_IO_FILE *) stderr)->_flags2
635 |= _IO_FLAGS2_NOTCANCEL;
636
701 if (_IO_fwide (stderr, 0) > 0) 637 if (_IO_fwide (stderr, 0) > 0)
702 __fwprintf (stderr, L"%s", buf); 638 __fwprintf (stderr, L"%s", buf);
703 else 639 else
704 fputs (buf, stderr); 640 fputs (buf, stderr);
705 641
642 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
643 _IO_funlockfile (stderr);
644
706 free (buf); 645 free (buf);
707 } 646 }
708#endif 647#endif
709 } 648 }
710 649
711 nextchar += strlen (nextchar); 650 d->__nextchar += strlen (d->__nextchar);
712 651
713 optopt = pfound->val; 652 d->optopt = pfound->val;
714 return '?'; 653 return '?';
715 } 654 }
716 } 655 }
717 else if (pfound->has_arg == 1) 656 else if (pfound->has_arg == 1)
718 { 657 {
719 if (optind < argc) 658 if (d->optind < argc)
720 optarg = argv[optind++]; 659 d->optarg = argv[d->optind++];
721 else 660 else
722 { 661 {
723 if (print_errors) 662 if (print_errors)
@@ -727,27 +666,36 @@ _getopt_internal (int argc, char *const *argv,
727 666
728 if (__asprintf (&buf, _("\ 667 if (__asprintf (&buf, _("\
729%s: option `%s' requires an argument\n"), 668%s: option `%s' requires an argument\n"),
730 argv[0], argv[optind - 1]) >= 0) 669 argv[0], argv[d->optind - 1]) >= 0)
731 { 670 {
671 _IO_flockfile (stderr);
672
673 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
674 ((_IO_FILE *) stderr)->_flags2
675 |= _IO_FLAGS2_NOTCANCEL;
676
732 if (_IO_fwide (stderr, 0) > 0) 677 if (_IO_fwide (stderr, 0) > 0)
733 __fwprintf (stderr, L"%s", buf); 678 __fwprintf (stderr, L"%s", buf);
734 else 679 else
735 fputs (buf, stderr); 680 fputs (buf, stderr);
736 681
682 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
683 _IO_funlockfile (stderr);
684
737 free (buf); 685 free (buf);
738 } 686 }
739#else 687#else
740 fprintf (stderr, 688 fprintf (stderr,
741 _("%s: option `%s' requires an argument\n"), 689 _("%s: option `%s' requires an argument\n"),
742 argv[0], argv[optind - 1]); 690 argv[0], argv[d->optind - 1]);
743#endif 691#endif
744 } 692 }
745 nextchar += strlen (nextchar); 693 d->__nextchar += strlen (d->__nextchar);
746 optopt = pfound->val; 694 d->optopt = pfound->val;
747 return optstring[0] == ':' ? ':' : '?'; 695 return optstring[0] == ':' ? ':' : '?';
748 } 696 }
749 } 697 }
750 nextchar += strlen (nextchar); 698 d->__nextchar += strlen (d->__nextchar);
751 if (longind != NULL) 699 if (longind != NULL)
752 *longind = option_index; 700 *longind = option_index;
753 if (pfound->flag) 701 if (pfound->flag)
@@ -762,8 +710,8 @@ _getopt_internal (int argc, char *const *argv,
762 or the option starts with '--' or is not a valid short 710 or the option starts with '--' or is not a valid short
763 option, then it's an error. 711 option, then it's an error.
764 Otherwise interpret it as a short option. */ 712 Otherwise interpret it as a short option. */
765 if (!long_only || argv[optind][1] == '-' 713 if (!long_only || argv[d->optind][1] == '-'
766 || strchr (optstring, *nextchar) == NULL) 714 || strchr (optstring, *d->__nextchar) == NULL)
767 { 715 {
768 if (print_errors) 716 if (print_errors)
769 { 717 {
@@ -772,15 +720,15 @@ _getopt_internal (int argc, char *const *argv,
772 int n; 720 int n;
773#endif 721#endif
774 722
775 if (argv[optind][1] == '-') 723 if (argv[d->optind][1] == '-')
776 { 724 {
777 /* --option */ 725 /* --option */
778#if defined _LIBC && defined USE_IN_LIBIO 726#if defined _LIBC && defined USE_IN_LIBIO
779 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), 727 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
780 argv[0], nextchar); 728 argv[0], d->__nextchar);
781#else 729#else
782 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 730 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
783 argv[0], nextchar); 731 argv[0], d->__nextchar);
784#endif 732#endif
785 } 733 }
786 else 734 else
@@ -788,28 +736,36 @@ _getopt_internal (int argc, char *const *argv,
788 /* +option or -option */ 736 /* +option or -option */
789#if defined _LIBC && defined USE_IN_LIBIO 737#if defined _LIBC && defined USE_IN_LIBIO
790 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), 738 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
791 argv[0], argv[optind][0], nextchar); 739 argv[0], argv[d->optind][0], d->__nextchar);
792#else 740#else
793 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 741 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
794 argv[0], argv[optind][0], nextchar); 742 argv[0], argv[d->optind][0], d->__nextchar);
795#endif 743#endif
796 } 744 }
797 745
798#if defined _LIBC && defined USE_IN_LIBIO 746#if defined _LIBC && defined USE_IN_LIBIO
799 if (n >= 0) 747 if (n >= 0)
800 { 748 {
749 _IO_flockfile (stderr);
750
751 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
752 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
753
801 if (_IO_fwide (stderr, 0) > 0) 754 if (_IO_fwide (stderr, 0) > 0)
802 __fwprintf (stderr, L"%s", buf); 755 __fwprintf (stderr, L"%s", buf);
803 else 756 else
804 fputs (buf, stderr); 757 fputs (buf, stderr);
805 758
759 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
760 _IO_funlockfile (stderr);
761
806 free (buf); 762 free (buf);
807 } 763 }
808#endif 764#endif
809 } 765 }
810 nextchar = (char *) ""; 766 d->__nextchar = (char *) "";
811 optind++; 767 d->optind++;
812 optopt = 0; 768 d->optopt = 0;
813 return '?'; 769 return '?';
814 } 770 }
815 } 771 }
@@ -817,12 +773,12 @@ _getopt_internal (int argc, char *const *argv,
817 /* Look at and handle the next short option-character. */ 773 /* Look at and handle the next short option-character. */
818 774
819 { 775 {
820 char c = *nextchar++; 776 char c = *d->__nextchar++;
821 char *temp = strchr (optstring, c); 777 char *temp = strchr (optstring, c);
822 778
823 /* Increment `optind' when we start to process its last character. */ 779 /* Increment `optind' when we start to process its last character. */
824 if (*nextchar == '\0') 780 if (*d->__nextchar == '\0')
825 ++optind; 781 ++d->optind;
826 782
827 if (temp == NULL || c == ':') 783 if (temp == NULL || c == ':')
828 { 784 {
@@ -833,7 +789,7 @@ _getopt_internal (int argc, char *const *argv,
833 int n; 789 int n;
834#endif 790#endif
835 791
836 if (posixly_correct) 792 if (d->__posixly_correct)
837 { 793 {
838 /* 1003.2 specifies the format of this message. */ 794 /* 1003.2 specifies the format of this message. */
839#if defined _LIBC && defined USE_IN_LIBIO 795#if defined _LIBC && defined USE_IN_LIBIO
@@ -856,16 +812,24 @@ _getopt_internal (int argc, char *const *argv,
856#if defined _LIBC && defined USE_IN_LIBIO 812#if defined _LIBC && defined USE_IN_LIBIO
857 if (n >= 0) 813 if (n >= 0)
858 { 814 {
815 _IO_flockfile (stderr);
816
817 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
818 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
819
859 if (_IO_fwide (stderr, 0) > 0) 820 if (_IO_fwide (stderr, 0) > 0)
860 __fwprintf (stderr, L"%s", buf); 821 __fwprintf (stderr, L"%s", buf);
861 else 822 else
862 fputs (buf, stderr); 823 fputs (buf, stderr);
863 824
825 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
826 _IO_funlockfile (stderr);
827
864 free (buf); 828 free (buf);
865 } 829 }
866#endif 830#endif
867 } 831 }
868 optopt = c; 832 d->optopt = c;
869 return '?'; 833 return '?';
870 } 834 }
871 /* Convenience. Treat POSIX -W foo same as long option --foo */ 835 /* Convenience. Treat POSIX -W foo same as long option --foo */
@@ -880,14 +844,14 @@ _getopt_internal (int argc, char *const *argv,
880 int option_index; 844 int option_index;
881 845
882 /* This is an option that requires an argument. */ 846 /* This is an option that requires an argument. */
883 if (*nextchar != '\0') 847 if (*d->__nextchar != '\0')
884 { 848 {
885 optarg = nextchar; 849 d->optarg = d->__nextchar;
886 /* If we end this ARGV-element by taking the rest as an arg, 850 /* If we end this ARGV-element by taking the rest as an arg,
887 we must advance to the next element now. */ 851 we must advance to the next element now. */
888 optind++; 852 d->optind++;
889 } 853 }
890 else if (optind == argc) 854 else if (d->optind == argc)
891 { 855 {
892 if (print_errors) 856 if (print_errors)
893 { 857 {
@@ -899,11 +863,19 @@ _getopt_internal (int argc, char *const *argv,
899 _("%s: option requires an argument -- %c\n"), 863 _("%s: option requires an argument -- %c\n"),
900 argv[0], c) >= 0) 864 argv[0], c) >= 0)
901 { 865 {
866 _IO_flockfile (stderr);
867
868 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
869 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
870
902 if (_IO_fwide (stderr, 0) > 0) 871 if (_IO_fwide (stderr, 0) > 0)
903 __fwprintf (stderr, L"%s", buf); 872 __fwprintf (stderr, L"%s", buf);
904 else 873 else
905 fputs (buf, stderr); 874 fputs (buf, stderr);
906 875
876 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
877 _IO_funlockfile (stderr);
878
907 free (buf); 879 free (buf);
908 } 880 }
909#else 881#else
@@ -911,7 +883,7 @@ _getopt_internal (int argc, char *const *argv,
911 argv[0], c); 883 argv[0], c);
912#endif 884#endif
913 } 885 }
914 optopt = c; 886 d->optopt = c;
915 if (optstring[0] == ':') 887 if (optstring[0] == ':')
916 c = ':'; 888 c = ':';
917 else 889 else
@@ -919,22 +891,23 @@ _getopt_internal (int argc, char *const *argv,
919 return c; 891 return c;
920 } 892 }
921 else 893 else
922 /* We already incremented `optind' once; 894 /* We already incremented `d->optind' once;
923 increment it again when taking next ARGV-elt as argument. */ 895 increment it again when taking next ARGV-elt as argument. */
924 optarg = argv[optind++]; 896 d->optarg = argv[d->optind++];
925 897
926 /* optarg is now the argument, see if it's in the 898 /* optarg is now the argument, see if it's in the
927 table of longopts. */ 899 table of longopts. */
928 900
929 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) 901 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
902 nameend++)
930 /* Do nothing. */ ; 903 /* Do nothing. */ ;
931 904
932 /* Test all long options for either exact match 905 /* Test all long options for either exact match
933 or abbreviated matches. */ 906 or abbreviated matches. */
934 for (p = longopts, option_index = 0; p->name; p++, option_index++) 907 for (p = longopts, option_index = 0; p->name; p++, option_index++)
935 if (!strncmp (p->name, nextchar, nameend - nextchar)) 908 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
936 { 909 {
937 if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 910 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
938 { 911 {
939 /* Exact match found. */ 912 /* Exact match found. */
940 pfound = p; 913 pfound = p;
@@ -960,22 +933,30 @@ _getopt_internal (int argc, char *const *argv,
960 char *buf; 933 char *buf;
961 934
962 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), 935 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
963 argv[0], argv[optind]) >= 0) 936 argv[0], argv[d->optind]) >= 0)
964 { 937 {
938 _IO_flockfile (stderr);
939
940 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
941 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
942
965 if (_IO_fwide (stderr, 0) > 0) 943 if (_IO_fwide (stderr, 0) > 0)
966 __fwprintf (stderr, L"%s", buf); 944 __fwprintf (stderr, L"%s", buf);
967 else 945 else
968 fputs (buf, stderr); 946 fputs (buf, stderr);
969 947
948 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
949 _IO_funlockfile (stderr);
950
970 free (buf); 951 free (buf);
971 } 952 }
972#else 953#else
973 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 954 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
974 argv[0], argv[optind]); 955 argv[0], argv[d->optind]);
975#endif 956#endif
976 } 957 }
977 nextchar += strlen (nextchar); 958 d->__nextchar += strlen (d->__nextchar);
978 optind++; 959 d->optind++;
979 return '?'; 960 return '?';
980 } 961 }
981 if (pfound != NULL) 962 if (pfound != NULL)
@@ -986,7 +967,7 @@ _getopt_internal (int argc, char *const *argv,
986 /* Don't test has_arg with >, because some C compilers don't 967 /* Don't test has_arg with >, because some C compilers don't
987 allow it to be used on enums. */ 968 allow it to be used on enums. */
988 if (pfound->has_arg) 969 if (pfound->has_arg)
989 optarg = nameend + 1; 970 d->optarg = nameend + 1;
990 else 971 else
991 { 972 {
992 if (print_errors) 973 if (print_errors)
@@ -998,11 +979,20 @@ _getopt_internal (int argc, char *const *argv,
998%s: option `-W %s' doesn't allow an argument\n"), 979%s: option `-W %s' doesn't allow an argument\n"),
999 argv[0], pfound->name) >= 0) 980 argv[0], pfound->name) >= 0)
1000 { 981 {
982 _IO_flockfile (stderr);
983
984 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
985 ((_IO_FILE *) stderr)->_flags2
986 |= _IO_FLAGS2_NOTCANCEL;
987
1001 if (_IO_fwide (stderr, 0) > 0) 988 if (_IO_fwide (stderr, 0) > 0)
1002 __fwprintf (stderr, L"%s", buf); 989 __fwprintf (stderr, L"%s", buf);
1003 else 990 else
1004 fputs (buf, stderr); 991 fputs (buf, stderr);
1005 992
993 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
994 _IO_funlockfile (stderr);
995
1006 free (buf); 996 free (buf);
1007 } 997 }
1008#else 998#else
@@ -1012,14 +1002,14 @@ _getopt_internal (int argc, char *const *argv,
1012#endif 1002#endif
1013 } 1003 }
1014 1004
1015 nextchar += strlen (nextchar); 1005 d->__nextchar += strlen (d->__nextchar);
1016 return '?'; 1006 return '?';
1017 } 1007 }
1018 } 1008 }
1019 else if (pfound->has_arg == 1) 1009 else if (pfound->has_arg == 1)
1020 { 1010 {
1021 if (optind < argc) 1011 if (d->optind < argc)
1022 optarg = argv[optind++]; 1012 d->optarg = argv[d->optind++];
1023 else 1013 else
1024 { 1014 {
1025 if (print_errors) 1015 if (print_errors)
@@ -1029,26 +1019,35 @@ _getopt_internal (int argc, char *const *argv,
1029 1019
1030 if (__asprintf (&buf, _("\ 1020 if (__asprintf (&buf, _("\
1031%s: option `%s' requires an argument\n"), 1021%s: option `%s' requires an argument\n"),
1032 argv[0], argv[optind - 1]) >= 0) 1022 argv[0], argv[d->optind - 1]) >= 0)
1033 { 1023 {
1024 _IO_flockfile (stderr);
1025
1026 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1027 ((_IO_FILE *) stderr)->_flags2
1028 |= _IO_FLAGS2_NOTCANCEL;
1029
1034 if (_IO_fwide (stderr, 0) > 0) 1030 if (_IO_fwide (stderr, 0) > 0)
1035 __fwprintf (stderr, L"%s", buf); 1031 __fwprintf (stderr, L"%s", buf);
1036 else 1032 else
1037 fputs (buf, stderr); 1033 fputs (buf, stderr);
1038 1034
1035 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1036 _IO_funlockfile (stderr);
1037
1039 free (buf); 1038 free (buf);
1040 } 1039 }
1041#else 1040#else
1042 fprintf (stderr, 1041 fprintf (stderr,
1043 _("%s: option `%s' requires an argument\n"), 1042 _("%s: option `%s' requires an argument\n"),
1044 argv[0], argv[optind - 1]); 1043 argv[0], argv[d->optind - 1]);
1045#endif 1044#endif
1046 } 1045 }
1047 nextchar += strlen (nextchar); 1046 d->__nextchar += strlen (d->__nextchar);
1048 return optstring[0] == ':' ? ':' : '?'; 1047 return optstring[0] == ':' ? ':' : '?';
1049 } 1048 }
1050 } 1049 }
1051 nextchar += strlen (nextchar); 1050 d->__nextchar += strlen (d->__nextchar);
1052 if (longind != NULL) 1051 if (longind != NULL)
1053 *longind = option_index; 1052 *longind = option_index;
1054 if (pfound->flag) 1053 if (pfound->flag)
@@ -1058,7 +1057,7 @@ _getopt_internal (int argc, char *const *argv,
1058 } 1057 }
1059 return pfound->val; 1058 return pfound->val;
1060 } 1059 }
1061 nextchar = NULL; 1060 d->__nextchar = NULL;
1062 return 'W'; /* Let the application handle it. */ 1061 return 'W'; /* Let the application handle it. */
1063 } 1062 }
1064 if (temp[1] == ':') 1063 if (temp[1] == ':')
@@ -1066,26 +1065,26 @@ _getopt_internal (int argc, char *const *argv,
1066 if (temp[2] == ':') 1065 if (temp[2] == ':')
1067 { 1066 {
1068 /* This is an option that accepts an argument optionally. */ 1067 /* This is an option that accepts an argument optionally. */
1069 if (*nextchar != '\0') 1068 if (*d->__nextchar != '\0')
1070 { 1069 {
1071 optarg = nextchar; 1070 d->optarg = d->__nextchar;
1072 optind++; 1071 d->optind++;
1073 } 1072 }
1074 else 1073 else
1075 optarg = NULL; 1074 d->optarg = NULL;
1076 nextchar = NULL; 1075 d->__nextchar = NULL;
1077 } 1076 }
1078 else 1077 else
1079 { 1078 {
1080 /* This is an option that requires an argument. */ 1079 /* This is an option that requires an argument. */
1081 if (*nextchar != '\0') 1080 if (*d->__nextchar != '\0')
1082 { 1081 {
1083 optarg = nextchar; 1082 d->optarg = d->__nextchar;
1084 /* If we end this ARGV-element by taking the rest as an arg, 1083 /* If we end this ARGV-element by taking the rest as an arg,
1085 we must advance to the next element now. */ 1084 we must advance to the next element now. */
1086 optind++; 1085 d->optind++;
1087 } 1086 }
1088 else if (optind == argc) 1087 else if (d->optind == argc)
1089 { 1088 {
1090 if (print_errors) 1089 if (print_errors)
1091 { 1090 {
@@ -1097,11 +1096,19 @@ _getopt_internal (int argc, char *const *argv,
1097%s: option requires an argument -- %c\n"), 1096%s: option requires an argument -- %c\n"),
1098 argv[0], c) >= 0) 1097 argv[0], c) >= 0)
1099 { 1098 {
1099 _IO_flockfile (stderr);
1100
1101 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1102 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1103
1100 if (_IO_fwide (stderr, 0) > 0) 1104 if (_IO_fwide (stderr, 0) > 0)
1101 __fwprintf (stderr, L"%s", buf); 1105 __fwprintf (stderr, L"%s", buf);
1102 else 1106 else
1103 fputs (buf, stderr); 1107 fputs (buf, stderr);
1104 1108
1109 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1110 _IO_funlockfile (stderr);
1111
1105 free (buf); 1112 free (buf);
1106 } 1113 }
1107#else 1114#else
@@ -1110,7 +1117,7 @@ _getopt_internal (int argc, char *const *argv,
1110 argv[0], c); 1117 argv[0], c);
1111#endif 1118#endif
1112 } 1119 }
1113 optopt = c; 1120 d->optopt = c;
1114 if (optstring[0] == ':') 1121 if (optstring[0] == ':')
1115 c = ':'; 1122 c = ':';
1116 else 1123 else
@@ -1119,8 +1126,8 @@ _getopt_internal (int argc, char *const *argv,
1119 else 1126 else
1120 /* We already incremented `optind' once; 1127 /* We already incremented `optind' once;
1121 increment it again when taking next ARGV-elt as argument. */ 1128 increment it again when taking next ARGV-elt as argument. */
1122 optarg = argv[optind++]; 1129 d->optarg = argv[d->optind++];
1123 nextchar = NULL; 1130 d->__nextchar = NULL;
1124 } 1131 }
1125 } 1132 }
1126 return c; 1133 return c;
@@ -1128,15 +1135,40 @@ _getopt_internal (int argc, char *const *argv,
1128} 1135}
1129 1136
1130int 1137int
1138_getopt_internal (int argc, char **argv, const char *optstring,
1139 const struct option *longopts, int *longind,
1140 int long_only, int posixly_correct)
1141{
1142 int result;
1143
1144 getopt_data.optind = optind;
1145 getopt_data.opterr = opterr;
1146
1147 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1148 long_only, posixly_correct, &getopt_data);
1149
1150 optind = getopt_data.optind;
1151 optarg = getopt_data.optarg;
1152 optopt = getopt_data.optopt;
1153
1154 return result;
1155}
1156
1157/* glibc gets a LSB-compliant getopt.
1158 Standalone applications get a POSIX-compliant getopt. */
1159#if _LIBC
1160enum { POSIXLY_CORRECT = 0 };
1161#else
1162enum { POSIXLY_CORRECT = 1 };
1163#endif
1164
1165int
1131getopt (int argc, char *const *argv, const char *optstring) 1166getopt (int argc, char *const *argv, const char *optstring)
1132{ 1167{
1133 return _getopt_internal (argc, argv, optstring, 1168 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1134 (const struct option *) 0, 1169 POSIXLY_CORRECT);
1135 (int *) 0,
1136 0);
1137} 1170}
1138 1171
1139#endif /* Not ELIDE_CODE. */
1140 1172
1141#ifdef TEST 1173#ifdef TEST
1142 1174
diff --git a/lib/getopt1.c b/lib/getopt1.c
index 3288c72..25d7926 100644
--- a/lib/getopt1.c
+++ b/lib/getopt1.c
@@ -1,8 +1,6 @@
1/* getopt_long and getopt_long_only entry points for GNU getopt. 1/* getopt_long and getopt_long_only entry points for GNU getopt.
2 2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004
3 Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 3 Free Software Foundation, Inc.
4 1997, 1998, 2003 Free Software Foundation, Inc.
5
6 This file is part of the GNU C Library. 4 This file is part of the GNU C Library.
7 5
8 This program is free software; you can redistribute it and/or modify 6 This program is free software; you can redistribute it and/or modify
@@ -17,10 +15,10 @@
17 15
18 You should have received a copy of the GNU General Public License along 16 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 19
22#ifdef HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
23#include <config.h> 21# include <config.h>
24#endif 22#endif
25 23
26#ifdef _LIBC 24#ifdef _LIBC
@@ -28,28 +26,10 @@
28#else 26#else
29# include "getopt.h" 27# include "getopt.h"
30#endif 28#endif
29#include "getopt_int.h"
31 30
32#include <stdio.h> 31#include <stdio.h>
33 32
34/* Comment out all this code if we are using the GNU C Library, and are not
35 actually compiling the library itself. This code is part of the GNU C
36 Library, but also included in many other GNU distributions. Compiling
37 and linking in this code is a waste when using the GNU C library
38 (especially if it is a shared library). Rather than having every GNU
39 program understand `configure --with-gnu-libc' and omit the object files,
40 it is simpler to just do this in the source for each such file. */
41
42#define GETOPT_INTERFACE_VERSION 2
43#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
44#include <gnu-versions.h>
45#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
46#define ELIDE_CODE
47#endif
48#endif
49
50#ifndef ELIDE_CODE
51
52
53/* This needs to come after some library #include 33/* This needs to come after some library #include
54 to get __GNU_LIBRARY__ defined. */ 34 to get __GNU_LIBRARY__ defined. */
55#ifdef __GNU_LIBRARY__ 35#ifdef __GNU_LIBRARY__
@@ -61,13 +41,20 @@
61#endif 41#endif
62 42
63int 43int
64getopt_long (int argc, 44getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
65 char *const *argv, 45 const struct option *long_options, int *opt_index)
66 const char *options,
67 const struct option *long_options,
68 int *opt_index)
69{ 46{
70 return _getopt_internal (argc, argv, options, long_options, opt_index, 0); 47 return _getopt_internal (argc, (char **) argv, options, long_options,
48 opt_index, 0, 0);
49}
50
51int
52_getopt_long_r (int argc, char **argv, const char *options,
53 const struct option *long_options, int *opt_index,
54 struct _getopt_data *d)
55{
56 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
57 0, 0, d);
71} 58}
72 59
73/* Like getopt_long, but '-' as well as '--' can indicate a long option. 60/* Like getopt_long, but '-' as well as '--' can indicate a long option.
@@ -76,21 +63,23 @@ getopt_long (int argc,
76 instead. */ 63 instead. */
77 64
78int 65int
79getopt_long_only (int argc, 66getopt_long_only (int argc, char *__getopt_argv_const *argv,
80 char *const *argv,
81 const char *options, 67 const char *options,
82 const struct option *long_options, 68 const struct option *long_options, int *opt_index)
83 int *opt_index)
84{ 69{
85 return _getopt_internal (argc, argv, options, long_options, opt_index, 1); 70 return _getopt_internal (argc, (char **) argv, options, long_options,
71 opt_index, 1, 0);
86} 72}
87 73
88# ifdef _LIBC 74int
89libc_hidden_def (getopt_long) 75_getopt_long_only_r (int argc, char **argv, const char *options,
90libc_hidden_def (getopt_long_only) 76 const struct option *long_options, int *opt_index,
91# endif 77 struct _getopt_data *d)
78{
79 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
80 1, 0, d);
81}
92 82
93#endif /* Not ELIDE_CODE. */
94 83
95#ifdef TEST 84#ifdef TEST
96 85
diff --git a/lib/getopt.h b/lib/getopt_.h
index 5e15191..3c406e5 100644
--- a/lib/getopt.h
+++ b/lib/getopt_.h
@@ -1,8 +1,6 @@
1/* Declarations for getopt. 1/* Declarations for getopt.
2 2 Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2005
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 3 Free Software Foundation, Inc.
4 1999, 2001, 2003 Free Software Foundation, Inc.
5
6 This file is part of the GNU C Library. 4 This file is part of the GNU C Library.
7 5
8 This program is free software; you can redistribute it and/or modify 6 This program is free software; you can redistribute it and/or modify
@@ -17,7 +15,7 @@
17 15
18 You should have received a copy of the GNU General Public License along 16 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 19
22#ifndef _GETOPT_H 20#ifndef _GETOPT_H
23 21
@@ -25,6 +23,62 @@
25# define _GETOPT_H 1 23# define _GETOPT_H 1
26#endif 24#endif
27 25
26/* Standalone applications should #define __GETOPT_PREFIX to an
27 identifier that prefixes the external functions and variables
28 defined in this header. When this happens, include the
29 headers that might declare getopt so that they will not cause
30 confusion if included after this file. Then systematically rename
31 identifiers so that they do not collide with the system functions
32 and variables. Renaming avoids problems with some compilers and
33 linkers. */
34#if defined __GETOPT_PREFIX && !defined __need_getopt
35# include <stdlib.h>
36# include <stdio.h>
37# include <unistd.h>
38# undef __need_getopt
39# undef getopt
40# undef getopt_long
41# undef getopt_long_only
42# undef optarg
43# undef opterr
44# undef optind
45# undef optopt
46# define __GETOPT_CONCAT(x, y) x ## y
47# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
48# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
49# define getopt __GETOPT_ID (getopt)
50# define getopt_long __GETOPT_ID (getopt_long)
51# define getopt_long_only __GETOPT_ID (getopt_long_only)
52# define optarg __GETOPT_ID (optarg)
53# define opterr __GETOPT_ID (opterr)
54# define optind __GETOPT_ID (optind)
55# define optopt __GETOPT_ID (optopt)
56#endif
57
58/* Standalone applications get correct prototypes for getopt_long and
59 getopt_long_only; they declare "char **argv". libc uses prototypes
60 with "char *const *argv" that are incorrect because getopt_long and
61 getopt_long_only can permute argv; this is required for backward
62 compatibility (e.g., for LSB 2.0.1).
63
64 This used to be `#if defined __GETOPT_PREFIX && !defined __need_getopt',
65 but it caused redefinition warnings if both unistd.h and getopt.h were
66 included, since unistd.h includes getopt.h having previously defined
67 __need_getopt.
68
69 The only place where __getopt_argv_const is used is in definitions
70 of getopt_long and getopt_long_only below, but these are visible
71 only if __need_getopt is not defined, so it is quite safe to rewrite
72 the conditional as follows:
73*/
74#if !defined __need_getopt
75# if defined __GETOPT_PREFIX
76# define __getopt_argv_const /* empty */
77# else
78# define __getopt_argv_const const
79# endif
80#endif
81
28/* If __GNU_LIBRARY__ is not already defined, either we are being used 82/* If __GNU_LIBRARY__ is not already defined, either we are being used
29 standalone, or this is the first header included in the source file. 83 standalone, or this is the first header included in the source file.
30 If we are being used with glibc, we need to include <features.h>, but 84 If we are being used with glibc, we need to include <features.h>, but
@@ -36,6 +90,17 @@
36# include <ctype.h> 90# include <ctype.h>
37#endif 91#endif
38 92
93#ifndef __THROW
94# ifndef __GNUC_PREREQ
95# define __GNUC_PREREQ(maj, min) (0)
96# endif
97# if defined __cplusplus && __GNUC_PREREQ (2,8)
98# define __THROW throw ()
99# else
100# define __THROW
101# endif
102#endif
103
39#ifdef __cplusplus 104#ifdef __cplusplus
40extern "C" { 105extern "C" {
41#endif 106#endif
@@ -135,28 +200,19 @@ struct option
135 arguments to the option '\0'. This behavior is specific to the GNU 200 arguments to the option '\0'. This behavior is specific to the GNU
136 `getopt'. */ 201 `getopt'. */
137 202
138#ifdef __GNU_LIBRARY__ 203extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
139/* Many other libraries have conflicting prototypes for getopt, with 204 __THROW;
140 differences in the consts, in stdlib.h. To avoid compilation
141 errors, only prototype getopt for the GNU C library. */
142extern int getopt (int ___argc, char *const *___argv, const char *__shortopts);
143#else /* not __GNU_LIBRARY__ */
144extern int getopt ();
145#endif /* __GNU_LIBRARY__ */
146 205
147#ifndef __need_getopt 206#ifndef __need_getopt
148extern int getopt_long (int ___argc, char *const *___argv, 207extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
149 const char *__shortopts, 208 const char *__shortopts,
150 const struct option *__longopts, int *__longind); 209 const struct option *__longopts, int *__longind)
151extern int getopt_long_only (int ___argc, char *const *___argv, 210 __THROW;
211extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
152 const char *__shortopts, 212 const char *__shortopts,
153 const struct option *__longopts, int *__longind); 213 const struct option *__longopts, int *__longind)
214 __THROW;
154 215
155/* Internal only. Users should not call this directly. */
156extern int _getopt_internal (int ___argc, char *const *___argv,
157 const char *__shortopts,
158 const struct option *__longopts, int *__longind,
159 int __long_only);
160#endif 216#endif
161 217
162#ifdef __cplusplus 218#ifdef __cplusplus
diff --git a/lib/getopt_int.h b/lib/getopt_int.h
new file mode 100644
index 0000000..401579f
--- /dev/null
+++ b/lib/getopt_int.h
@@ -0,0 +1,131 @@
1/* Internal declarations for getopt.
2 Copyright (C) 1989-1994,1996-1999,2001,2003,2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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 along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20#ifndef _GETOPT_INT_H
21#define _GETOPT_INT_H 1
22
23extern int _getopt_internal (int ___argc, char **___argv,
24 const char *__shortopts,
25 const struct option *__longopts, int *__longind,
26 int __long_only, int __posixly_correct);
27
28
29/* Reentrant versions which can handle parsing multiple argument
30 vectors at the same time. */
31
32/* Data type for reentrant functions. */
33struct _getopt_data
34{
35 /* These have exactly the same meaning as the corresponding global
36 variables, except that they are used for the reentrant
37 versions of getopt. */
38 int optind;
39 int opterr;
40 int optopt;
41 char *optarg;
42
43 /* Internal members. */
44
45 /* True if the internal members have been initialized. */
46 int __initialized;
47
48 /* The next char to be scanned in the option-element
49 in which the last option character we returned was found.
50 This allows us to pick up the scan where we left off.
51
52 If this is zero, or a null string, it means resume the scan
53 by advancing to the next ARGV-element. */
54 char *__nextchar;
55
56 /* Describe how to deal with options that follow non-option ARGV-elements.
57
58 If the caller did not specify anything,
59 the default is REQUIRE_ORDER if the environment variable
60 POSIXLY_CORRECT is defined, PERMUTE otherwise.
61
62 REQUIRE_ORDER means don't recognize them as options;
63 stop option processing when the first non-option is seen.
64 This is what Unix does.
65 This mode of operation is selected by either setting the environment
66 variable POSIXLY_CORRECT, or using `+' as the first character
67 of the list of option characters, or by calling getopt.
68
69 PERMUTE is the default. We permute the contents of ARGV as we
70 scan, so that eventually all the non-options are at the end.
71 This allows options to be given in any order, even with programs
72 that were not written to expect this.
73
74 RETURN_IN_ORDER is an option available to programs that were
75 written to expect options and other ARGV-elements in any order
76 and that care about the ordering of the two. We describe each
77 non-option ARGV-element as if it were the argument of an option
78 with character code 1. Using `-' as the first character of the
79 list of option characters selects this mode of operation.
80
81 The special argument `--' forces an end of option-scanning regardless
82 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
83 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
84
85 enum
86 {
87 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
88 } __ordering;
89
90 /* If the POSIXLY_CORRECT environment variable is set
91 or getopt was called. */
92 int __posixly_correct;
93
94
95 /* Handle permutation of arguments. */
96
97 /* Describe the part of ARGV that contains non-options that have
98 been skipped. `first_nonopt' is the index in ARGV of the first
99 of them; `last_nonopt' is the index after the last of them. */
100
101 int __first_nonopt;
102 int __last_nonopt;
103
104#if defined _LIBC && defined USE_NONOPTION_FLAGS
105 int __nonoption_flags_max_len;
106 int __nonoption_flags_len;
107# endif
108};
109
110/* The initializer is necessary to set OPTIND and OPTERR to their
111 default values and to clear the initialization flag. */
112#define _GETOPT_DATA_INITIALIZER { 1, 1 }
113
114extern int _getopt_internal_r (int ___argc, char **___argv,
115 const char *__shortopts,
116 const struct option *__longopts, int *__longind,
117 int __long_only, int __posixly_correct,
118 struct _getopt_data *__data);
119
120extern int _getopt_long_r (int ___argc, char **___argv,
121 const char *__shortopts,
122 const struct option *__longopts, int *__longind,
123 struct _getopt_data *__data);
124
125extern int _getopt_long_only_r (int ___argc, char **___argv,
126 const char *__shortopts,
127 const struct option *__longopts,
128 int *__longind,
129 struct _getopt_data *__data);
130
131#endif /* getopt_int.h */
diff --git a/lib/gettext.h b/lib/gettext.h
index 835732e..285cb31 100644
--- a/lib/gettext.h
+++ b/lib/gettext.h
@@ -1,5 +1,5 @@
1/* Convenience header for conditional use of GNU <libintl.h>. 1/* Convenience header for conditional use of GNU <libintl.h>.
2 Copyright (C) 1995-1998, 2000-2002 Free Software Foundation, Inc. 2 Copyright (C) 1995-1998, 2000-2002, 2004 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
@@ -13,7 +13,7 @@
13 13
14 You should have received a copy of the GNU General Public License along 14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, 15 with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18#ifndef _LIBGETTEXT_H 18#ifndef _LIBGETTEXT_H
19#define _LIBGETTEXT_H 1 19#define _LIBGETTEXT_H 1
@@ -36,6 +36,16 @@
36# include <locale.h> 36# include <locale.h>
37#endif 37#endif
38 38
39/* Many header files from the libstdc++ coming with g++ 3.3 or newer include
40 <libintl.h>, which chokes if dcgettext is defined as a macro. So include
41 it now, to make later inclusions of <libintl.h> a NOP. */
42#if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
43# include <cstdlib>
44# if (__GLIBC__ >= 2) || _GLIBCXX_HAVE_LIBINTL_H
45# include <libintl.h>
46# endif
47#endif
48
39/* Disabled NLS. 49/* Disabled NLS.
40 The casts to 'const char *' serve the purpose of producing warnings 50 The casts to 'const char *' serve the purpose of producing warnings
41 for invalid uses of the value returned from these functions. 51 for invalid uses of the value returned from these functions.
diff --git a/lib/malloc.c b/lib/malloc.c
index a43d169..58fa611 100644
--- a/lib/malloc.c
+++ b/lib/malloc.c
@@ -1,4 +1,4 @@
1/* Work around bug on some systems where malloc (0) fails. 1/* malloc() function that is glibc compatible.
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc. 2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
@@ -13,11 +13,11 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18/* written by Jim Meyering */ 18/* written by Jim Meyering */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23#undef malloc 23#undef malloc
diff --git a/lib/mountlist.c b/lib/mountlist.c
index 631be31..70d4edb 100644
--- a/lib/mountlist.c
+++ b/lib/mountlist.c
@@ -1,5 +1,7 @@
1/* mountlist.c -- return a list of mounted filesystems 1/* mountlist.c -- return a list of mounted file systems
2 Copyright (C) 1991, 1992, 1997-2004 Free Software Foundation, Inc. 2
3 Copyright (C) 1991, 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005 Free Software Foundation, Inc.
3 5
4 This program is free software; you can redistribute it and/or modify 6 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 7 it under the terms of the GNU General Public License as published by
@@ -13,39 +15,29 @@
13 15
14 You should have received a copy of the GNU General Public License 16 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, 17 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 19
18#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
19# include <config.h> 21# include <config.h>
20#endif 22#endif
21 23
24#include "mountlist.h"
25
22#include <stdio.h> 26#include <stdio.h>
23#include <sys/types.h>
24#include <stdlib.h> 27#include <stdlib.h>
25#include <string.h> 28#include <string.h>
26 29
27#include "xalloc.h" 30#include "xalloc.h"
28 31
29#ifndef SIZE_MAX
30# define SIZE_MAX ((size_t) -1)
31#endif
32
33#ifndef strstr 32#ifndef strstr
34char *strstr (); 33char *strstr ();
35#endif 34#endif
36 35
37#include <errno.h> 36#include <errno.h>
38#ifndef errno
39extern int errno;
40#endif
41 37
42#ifdef HAVE_FCNTL_H 38#include <fcntl.h>
43# include <fcntl.h>
44#endif
45 39
46#ifdef HAVE_UNISTD_H 40#include <unistd.h>
47# include <unistd.h>
48#endif
49 41
50#if HAVE_SYS_PARAM_H 42#if HAVE_SYS_PARAM_H
51# include <sys/param.h> 43# include <sys/param.h>
@@ -140,42 +132,39 @@ extern int errno;
140# define MNT_IGNORE(M) 0 132# define MNT_IGNORE(M) 0
141#endif 133#endif
142 134
143#include "mountlist.h" 135#if USE_UNLOCKED_IO
144#include "unlocked-io.h" 136# include "unlocked-io.h"
137#endif
145 138
146#ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ 139#ifndef SIZE_MAX
147/* Return the value of the hexadecimal number represented by CP. 140# define SIZE_MAX ((size_t) -1)
148 No prefix (like '0x') or suffix (like 'h') is expected to be 141#endif
149 part of CP. */
150/* FIXME: this can overflow */
151 142
152static int 143#ifndef ME_DUMMY
153xatoi (char *cp) 144# define ME_DUMMY(Fs_name, Fs_type) \
154{ 145 (strcmp (Fs_type, "autofs") == 0 \
155 int val; 146 || strcmp (Fs_type, "none") == 0 \
147 || strcmp (Fs_type, "proc") == 0 \
148 || strcmp (Fs_type, "subfs") == 0 \
149 /* for Irix 6.5 */ \
150 || strcmp (Fs_type, "ignore") == 0)
151#endif
156 152
157 val = 0; 153#ifndef ME_REMOTE
158 while (*cp) 154/* A file system is `remote' if its Fs_name contains a `:'
159 { 155 or if (it is of type smbfs and its Fs_name starts with `//'). */
160 if (*cp >= 'a' && *cp <= 'f') 156# define ME_REMOTE(Fs_name, Fs_type) \
161 val = val * 16 + *cp - 'a' + 10; 157 (strchr (Fs_name, ':') != 0 \
162 else if (*cp >= 'A' && *cp <= 'F') 158 || ((Fs_name)[0] == '/' \
163 val = val * 16 + *cp - 'A' + 10; 159 && (Fs_name)[1] == '/' \
164 else if (*cp >= '0' && *cp <= '9') 160 && strcmp (Fs_type, "smbfs") == 0))
165 val = val * 16 + *cp - '0'; 161#endif
166 else
167 break;
168 cp++;
169 }
170 return val;
171}
172#endif /* MOUNTED_GETMNTENT1. */
173 162
174#if MOUNTED_GETMNTINFO 163#if MOUNTED_GETMNTINFO
175 164
176# if ! HAVE_F_FSTYPENAME_IN_STATFS 165# if ! HAVE_F_FSTYPENAME_IN_STATFS
177static char * 166static char *
178fstype_to_string (short t) 167fstype_to_string (short int t)
179{ 168{
180 switch (t) 169 switch (t)
181 { 170 {
@@ -296,13 +285,13 @@ fstype_to_string (int t)
296} 285}
297#endif /* MOUNTED_VMOUNT */ 286#endif /* MOUNTED_VMOUNT */
298 287
299/* Return a list of the currently mounted filesystems, or NULL on error. 288/* Return a list of the currently mounted file systems, or NULL on error.
300 Add each entry to the tail of the list so that they stay in order. 289 Add each entry to the tail of the list so that they stay in order.
301 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in 290 If NEED_FS_TYPE is true, ensure that the file system type fields in
302 the returned list are valid. Otherwise, they might not be. */ 291 the returned list are valid. Otherwise, they might not be. */
303 292
304struct mount_entry * 293struct mount_entry *
305read_filesystem_list (int need_fs_type) 294read_file_system_list (bool need_fs_type)
306{ 295{
307 struct mount_entry *mount_list; 296 struct mount_entry *mount_list;
308 struct mount_entry *me; 297 struct mount_entry *me;
@@ -360,12 +349,7 @@ read_filesystem_list (int need_fs_type)
360 me->me_remote = ME_REMOTE (me->me_devname, me->me_type); 349 me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
361 devopt = strstr (mnt->mnt_opts, "dev="); 350 devopt = strstr (mnt->mnt_opts, "dev=");
362 if (devopt) 351 if (devopt)
363 { 352 me->me_dev = strtoul (devopt + 4, NULL, 16);
364 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
365 me->me_dev = xatoi (devopt + 6);
366 else
367 me->me_dev = xatoi (devopt + 4);
368 }
369 else 353 else
370 me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ 354 me->me_dev = (dev_t) -1; /* Magic; means not known yet. */
371 355
@@ -438,14 +422,14 @@ read_filesystem_list (int need_fs_type)
438#if defined MOUNTED_FS_STAT_DEV /* BeOS */ 422#if defined MOUNTED_FS_STAT_DEV /* BeOS */
439 { 423 {
440 /* The next_dev() and fs_stat_dev() system calls give the list of 424 /* The next_dev() and fs_stat_dev() system calls give the list of
441 all filesystems, including the information returned by statvfs() 425 all file systems, including the information returned by statvfs()
442 (fs type, total blocks, free blocks etc.), but without the mount 426 (fs type, total blocks, free blocks etc.), but without the mount
443 point. But on BeOS all filesystems except / are mounted in the 427 point. But on BeOS all file systems except / are mounted in the
444 rootfs, directly under /. 428 rootfs, directly under /.
445 The directory name of the mount point is often, but not always, 429 The directory name of the mount point is often, but not always,
446 identical to the volume name of the device. 430 identical to the volume name of the device.
447 We therefore get the list of subdirectories of /, and the list 431 We therefore get the list of subdirectories of /, and the list
448 of all filesystems, and match the two lists. */ 432 of all file systems, and match the two lists. */
449 433
450 DIR *dirp; 434 DIR *dirp;
451 struct rootdir_entry 435 struct rootdir_entry
@@ -744,12 +728,12 @@ read_filesystem_list (int need_fs_type)
744 int n_entries; 728 int n_entries;
745 int i; 729 int i;
746 730
747 /* Ask how many bytes to allocate for the mounted filesystem info. */ 731 /* Ask how many bytes to allocate for the mounted file system info. */
748 if (mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize) != 0) 732 if (mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize) != 0)
749 return NULL; 733 return NULL;
750 entries = xmalloc (bufsize); 734 entries = xmalloc (bufsize);
751 735
752 /* Get the list of mounted filesystems. */ 736 /* Get the list of mounted file systems. */
753 n_entries = mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries); 737 n_entries = mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
754 if (n_entries < 0) 738 if (n_entries < 0)
755 { 739 {
@@ -769,16 +753,16 @@ read_filesystem_list (int need_fs_type)
769 me = xmalloc (sizeof *me); 753 me = xmalloc (sizeof *me);
770 if (vmp->vmt_flags & MNT_REMOTE) 754 if (vmp->vmt_flags & MNT_REMOTE)
771 { 755 {
772 char *host, *path; 756 char *host, *dir;
773 757
774 me->me_remote = 1; 758 me->me_remote = 1;
775 /* Prepend the remote pathname. */ 759 /* Prepend the remote dirname. */
776 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off; 760 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
777 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off; 761 dir = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
778 me->me_devname = xmalloc (strlen (host) + strlen (path) + 2); 762 me->me_devname = xmalloc (strlen (host) + strlen (dir) + 2);
779 strcpy (me->me_devname, host); 763 strcpy (me->me_devname, host);
780 strcat (me->me_devname, ":"); 764 strcat (me->me_devname, ":");
781 strcat (me->me_devname, path); 765 strcat (me->me_devname, dir);
782 } 766 }
783 else 767 else
784 { 768 {
diff --git a/lib/mountlist.h b/lib/mountlist.h
index 9e550a0..7f5a6f7 100644
--- a/lib/mountlist.h
+++ b/lib/mountlist.h
@@ -1,7 +1,7 @@
1/* mountlist.h -- declarations for list of mounted filesystems 1/* mountlist.h -- declarations for list of mounted file systems
2 2
3 Copyright (C) 1991, 1992, 1998, 2000, 2001, 2002, 2003 Free 3 Copyright (C) 1991, 1992, 1998, 2000, 2001, 2002, 2003, 2004, 2005
4 Software Foundation, Inc. 4 Free Software Foundation, Inc.
5 5
6 This program is free software; you can redistribute it and/or modify 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 7 it under the terms of the GNU General Public License as published by
@@ -15,39 +15,27 @@
15 15
16 You should have received a copy of the GNU General Public License 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, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20#ifndef MOUNTLIST_H_
21# define MOUNTLIST_H_
22
23# include <stdbool.h>
24# include <sys/types.h>
19 25
20/* A mount table entry. */ 26/* A mount table entry. */
21struct mount_entry 27struct mount_entry
22{ 28{
23 char *me_devname; /* Device node pathname, including "/dev/". */ 29 char *me_devname; /* Device node name, including "/dev/". */
24 char *me_mountdir; /* Mount point directory pathname. */ 30 char *me_mountdir; /* Mount point directory name. */
25 char *me_type; /* "nfs", "4.2", etc. */ 31 char *me_type; /* "nfs", "4.2", etc. */
26 dev_t me_dev; /* Device number of me_mountdir. */ 32 dev_t me_dev; /* Device number of me_mountdir. */
27 unsigned int me_dummy : 1; /* Nonzero for dummy filesystems. */ 33 unsigned int me_dummy : 1; /* Nonzero for dummy file systems. */
28 unsigned int me_remote : 1; /* Nonzero for remote fileystems. */ 34 unsigned int me_remote : 1; /* Nonzero for remote fileystems. */
29 unsigned int me_type_malloced : 1; /* Nonzero if me_type was malloced. */ 35 unsigned int me_type_malloced : 1; /* Nonzero if me_type was malloced. */
30 struct mount_entry *me_next; 36 struct mount_entry *me_next;
31}; 37};
32 38
33struct mount_entry *read_filesystem_list (int need_fs_type); 39struct mount_entry *read_file_system_list (bool need_fs_type);
34
35#ifndef ME_DUMMY
36# define ME_DUMMY(Fs_name, Fs_type) \
37 (!strcmp (Fs_type, "autofs") \
38 /* for Irix 6.5 */ \
39 || !strcmp (Fs_type, "ignore"))
40#endif
41 40
42#undef STREQ
43#define STREQ(a, b) (strcmp ((a), (b)) == 0)
44
45#ifndef ME_REMOTE
46/* A file system is `remote' if its Fs_name contains a `:'
47 or if (it is of type smbfs and its Fs_name starts with `//'). */
48# define ME_REMOTE(Fs_name, Fs_type) \
49 (strchr ((Fs_name), ':') != 0 \
50 || ((Fs_name)[0] == '/' \
51 && (Fs_name)[1] == '/' \
52 && STREQ (Fs_type, "smbfs")))
53#endif 41#endif
diff --git a/lib/realloc.c b/lib/realloc.c
index ccbf991..fe94822 100644
--- a/lib/realloc.c
+++ b/lib/realloc.c
@@ -1,5 +1,5 @@
1/* Work around bug on some systems where realloc (NULL, 0) fails. 1/* realloc() function that is glibc compatible.
2 Copyright (C) 1997, 2003 Free Software Foundation, Inc. 2 Copyright (C) 1997, 2003, 2004 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
@@ -13,11 +13,11 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18/* written by Jim Meyering */ 18/* written by Jim Meyering */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23#undef realloc 23#undef realloc
@@ -32,8 +32,15 @@ void *
32rpl_realloc (void *p, size_t n) 32rpl_realloc (void *p, size_t n)
33{ 33{
34 if (n == 0) 34 if (n == 0)
35 n = 1; 35 {
36 if (p == 0) 36 n = 1;
37
38 /* In theory realloc might fail, so don't rely on it to free. */
39 free (p);
40 p = NULL;
41 }
42
43 if (p == NULL)
37 return malloc (n); 44 return malloc (n);
38 return realloc (p, n); 45 return realloc (p, n);
39} 46}
diff --git a/lib/safe-read.c b/lib/safe-read.c
index c21d1cf..9caf846 100644
--- a/lib/safe-read.c
+++ b/lib/safe-read.c
@@ -1,5 +1,7 @@
1/* An interface to read and write that retries after interrupts. 1/* An interface to read and write that retries after interrupts.
2 Copyright (C) 1993, 1994, 1998, 2002-2003 Free Software Foundation, Inc. 2
3 Copyright (C) 1993, 1994, 1998, 2002, 2003, 2004, 2005 Free Software
4 Foundation, Inc.
3 5
4 This program is free software; you can redistribute it and/or modify 6 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 7 it under the terms of the GNU General Public License as published by
@@ -13,9 +15,9 @@
13 15
14 You should have received a copy of the GNU General Public License 16 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, 17 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 19
18#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
19# include <config.h> 21# include <config.h>
20#endif 22#endif
21 23
@@ -28,14 +30,9 @@
28 30
29/* Get ssize_t. */ 31/* Get ssize_t. */
30#include <sys/types.h> 32#include <sys/types.h>
31#if HAVE_UNISTD_H 33#include <unistd.h>
32# include <unistd.h>
33#endif
34 34
35#include <errno.h> 35#include <errno.h>
36#ifndef errno
37extern int errno;
38#endif
39 36
40#ifdef EINTR 37#ifdef EINTR
41# define IS_EINTR(x) ((x) == EINTR) 38# define IS_EINTR(x) ((x) == EINTR)
@@ -61,22 +58,23 @@ extern int errno;
61size_t 58size_t
62safe_rw (int fd, void const *buf, size_t count) 59safe_rw (int fd, void const *buf, size_t count)
63{ 60{
64 ssize_t result; 61 /* Work around a bug in Tru64 5.1. Attempting to read more than
62 INT_MAX bytes fails with errno == EINVAL. See
63 <http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>.
64 When decreasing COUNT, keep it block-aligned. */
65 enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 };
65 66
66 /* POSIX limits COUNT to SSIZE_MAX, but we limit it further, requiring 67 for (;;)
67 that COUNT <= INT_MAX, to avoid triggering a bug in Tru64 5.1.
68 When decreasing COUNT, keep the file pointer block-aligned.
69 Note that in any case, read(write) may succeed, yet read(write)
70 fewer than COUNT bytes, so the caller must be prepared to handle
71 partial results. */
72 if (count > INT_MAX)
73 count = INT_MAX & ~8191;
74
75 do
76 { 68 {
77 result = rw (fd, buf, count); 69 ssize_t result = rw (fd, buf, count);
78 }
79 while (result < 0 && IS_EINTR (errno));
80 70
81 return (size_t) result; 71 if (0 <= result)
72 return result;
73 else if (IS_EINTR (errno))
74 continue;
75 else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count)
76 count = BUGGY_READ_MAXIMUM;
77 else
78 return result;
79 }
82} 80}
diff --git a/lib/safe-read.h b/lib/safe-read.h
index cbe6e0b..9f8a59b 100644
--- a/lib/safe-read.h
+++ b/lib/safe-read.h
@@ -13,7 +13,7 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18#include <stddef.h> 18#include <stddef.h>
19 19
diff --git a/lib/safe-write.c b/lib/safe-write.c
index fbafa7c..4c375a6 100644
--- a/lib/safe-write.c
+++ b/lib/safe-write.c
@@ -13,7 +13,7 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18#define SAFE_WRITE 18#define SAFE_WRITE
19#include "safe-read.c" 19#include "safe-read.c"
diff --git a/lib/safe-write.h b/lib/safe-write.h
index ab1f45b..c194636 100644
--- a/lib/safe-write.h
+++ b/lib/safe-write.h
@@ -13,7 +13,7 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18#include <stddef.h> 18#include <stddef.h>
19 19
diff --git a/lib/stdbool_.h b/lib/stdbool_.h
index e33715a..7cd2da8 100644
--- a/lib/stdbool_.h
+++ b/lib/stdbool_.h
@@ -1,4 +1,4 @@
1/* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. 1/* Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
2 Written by Bruno Haible <haible@clisp.cons.org>, 2001. 2 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
@@ -13,7 +13,7 @@
13 13
14 You should have received a copy of the GNU General Public License 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, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18#ifndef _STDBOOL_H 18#ifndef _STDBOOL_H
19#define _STDBOOL_H 19#define _STDBOOL_H
@@ -54,35 +54,47 @@
54/* 7.16. Boolean type and values */ 54/* 7.16. Boolean type and values */
55 55
56/* BeOS <sys/socket.h> already #defines false 0, true 1. We use the same 56/* BeOS <sys/socket.h> already #defines false 0, true 1. We use the same
57 definitions below, but temporarily we have to #undef them. */ 57 definitions below, which is OK. */
58#ifdef __BEOS__ 58#ifdef __BEOS__
59# include <OS.h> /* defines bool but not _Bool */ 59# include <OS.h> /* defines bool but not _Bool */
60# undef false
61# undef true
62#endif 60#endif
63 61
64/* For the sake of symbolic names in gdb, we define true and false as 62/* C++ and BeOS have a reliable bool (and _Bool, if it exists).
65 enum constants, not only as macros. 63 Otherwise, since this file is being compiled, the system
66 It is tempting to write 64 <stdbool.h> is not reliable so assume that the system _Bool is not
67 typedef enum { false = 0, true = 1 } _Bool; 65 reliable either. Under that assumption, it is tempting to write
68 so that gdb prints values of type 'bool' symbolically. But if we do 66
67 typedef enum { false, true } _Bool;
68
69 so that gdb prints values of type 'bool' symbolically. But if we do
69 this, values of type '_Bool' may promote to 'int' or 'unsigned int' 70 this, values of type '_Bool' may promote to 'int' or 'unsigned int'
70 (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int' 71 (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int'
71 (see ISO C 99 6.3.1.1.(2)). So we add a negative value to the 72 (see ISO C 99 6.3.1.1.(2)). We could instead try this:
72 enum; this ensures that '_Bool' promotes to 'int'. */ 73
73#if !(defined __cplusplus || defined __BEOS__) 74 typedef enum { _Bool_dummy = -1, false, true } _Bool;
75
76 as the negative value ensures that '_Bool' promotes to 'int'.
77 However, this runs into some other problems. First, Sun's C
78 compiler when (__SUNPRO_C < 0x550 || __STDC__ == 1) issues a stupid
79 "warning: _Bool is a keyword in ISO C99". Second, IBM's AIX cc
80 compiler 6.0.0.0 (and presumably other versions) mishandles
81 subscripts involving _Bool (effectively, _Bool promotes to unsigned
82 int in this case), and we need to redefine _Bool in that case.
83 Third, HP-UX 10.20's C compiler lacks <stdbool.h> but has _Bool and
84 mishandles comparisons of _Bool to int (it promotes _Bool to
85 unsigned int).
86
87 The simplest way to work around these problems is to ignore any
88 existing definition of _Bool and use our own. */
89
90#if defined __cplusplus || defined __BEOS__
74# if !@HAVE__BOOL@ 91# if !@HAVE__BOOL@
75# if defined __SUNPRO_C && (__SUNPRO_C < 0x550 || __STDC__ == 1) 92typedef bool _Bool;
76 /* Avoid stupid "warning: _Bool is a keyword in ISO C99". */
77# define _Bool signed char
78enum { false = 0, true = 1 };
79# else
80typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
81# endif
82# endif 93# endif
83#else 94#else
84typedef bool _Bool; 95# define _Bool signed char
85#endif 96#endif
97
86#define bool _Bool 98#define bool _Bool
87 99
88/* The other macros must be usable in preprocessor directives. */ 100/* The other macros must be usable in preprocessor directives. */
diff --git a/lib/strtod.c b/lib/strtod.c
index 161f97a..7b48754 100644
--- a/lib/strtod.c
+++ b/lib/strtod.c
@@ -12,9 +12,9 @@
12 12
13 You should have received a copy of the GNU General Public License 13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation, 14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 15 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
16 16
17#if HAVE_CONFIG_H 17#ifdef HAVE_CONFIG_H
18# include <config.h> 18# include <config.h>
19#endif 19#endif
20 20
diff --git a/lib/unlocked-io.h b/lib/unlocked-io.h
index 36a7a48..d009303 100644
--- a/lib/unlocked-io.h
+++ b/lib/unlocked-io.h
@@ -1,6 +1,6 @@
1/* Prefer faster, non-thread-safe stdio functions if available. 1/* Prefer faster, non-thread-safe stdio functions if available.
2 2
3 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. 3 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 4
5 This program is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published by
@@ -14,19 +14,13 @@
14 14
15 You should have received a copy of the GNU General Public License along 15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, 16 with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 18
19/* Written by Jim Meyering. */ 19/* Written by Jim Meyering. */
20 20
21#ifndef UNLOCKED_IO_H 21#ifndef UNLOCKED_IO_H
22# define UNLOCKED_IO_H 1 22# define UNLOCKED_IO_H 1
23 23
24# ifndef USE_UNLOCKED_IO
25# define USE_UNLOCKED_IO 1
26# endif
27
28# if USE_UNLOCKED_IO
29
30/* These are wrappers for functions/macros from the GNU C library, and 24/* These are wrappers for functions/macros from the GNU C library, and
31 from other C libraries supporting POSIX's optional thread-safe functions. 25 from other C libraries supporting POSIX's optional thread-safe functions.
32 26
@@ -38,95 +32,106 @@
38 the *_unlocked functions directly. On hosts that lack those 32 the *_unlocked functions directly. On hosts that lack those
39 functions, invoke the non-thread-safe versions instead. */ 33 functions, invoke the non-thread-safe versions instead. */
40 34
41# include <stdio.h> 35# include <stdio.h>
42 36
43# if HAVE_DECL_CLEARERR_UNLOCKED 37# if HAVE_DECL_CLEARERR_UNLOCKED
44# undef clearerr 38# undef clearerr
45# define clearerr(x) clearerr_unlocked (x) 39# define clearerr(x) clearerr_unlocked (x)
46# else 40# else
47# define clearerr_unlocked(x) clearerr (x) 41# define clearerr_unlocked(x) clearerr (x)
48# endif 42# endif
49# if HAVE_DECL_FEOF_UNLOCKED 43
50# undef feof 44# if HAVE_DECL_FEOF_UNLOCKED
51# define feof(x) feof_unlocked (x) 45# undef feof
52# else 46# define feof(x) feof_unlocked (x)
53# define feof_unlocked(x) feof (x) 47# else
54# endif 48# define feof_unlocked(x) feof (x)
55# if HAVE_DECL_FERROR_UNLOCKED 49# endif
56# undef ferror 50
57# define ferror(x) ferror_unlocked (x) 51# if HAVE_DECL_FERROR_UNLOCKED
58# else 52# undef ferror
59# define ferror_unlocked(x) ferror (x) 53# define ferror(x) ferror_unlocked (x)
60# endif 54# else
61# if HAVE_DECL_FFLUSH_UNLOCKED 55# define ferror_unlocked(x) ferror (x)
62# undef fflush 56# endif
63# define fflush(x) fflush_unlocked (x) 57
64# else 58# if HAVE_DECL_FFLUSH_UNLOCKED
65# define fflush_unlocked(x) fflush (x) 59# undef fflush
66# endif 60# define fflush(x) fflush_unlocked (x)
67# if HAVE_DECL_FGETS_UNLOCKED 61# else
68# undef fgets 62# define fflush_unlocked(x) fflush (x)
69# define fgets(x,y,z) fgets_unlocked (x,y,z) 63# endif
70# else 64
71# define fgets_unlocked(x,y,z) fgets (x,y,z) 65# if HAVE_DECL_FGETS_UNLOCKED
72# endif 66# undef fgets
73# if HAVE_DECL_FPUTC_UNLOCKED 67# define fgets(x,y,z) fgets_unlocked (x,y,z)
74# undef fputc 68# else
75# define fputc(x,y) fputc_unlocked (x,y) 69# define fgets_unlocked(x,y,z) fgets (x,y,z)
76# else 70# endif
77# define fputc_unlocked(x,y) fputc (x,y) 71
78# endif 72# if HAVE_DECL_FPUTC_UNLOCKED
79# if HAVE_DECL_FPUTS_UNLOCKED 73# undef fputc
80# undef fputs 74# define fputc(x,y) fputc_unlocked (x,y)
81# define fputs(x,y) fputs_unlocked (x,y) 75# else
82# else 76# define fputc_unlocked(x,y) fputc (x,y)
83# define fputs_unlocked(x,y) fputs (x,y) 77# endif
84# endif 78
85# if HAVE_DECL_FREAD_UNLOCKED 79# if HAVE_DECL_FPUTS_UNLOCKED
86# undef fread 80# undef fputs
87# define fread(w,x,y,z) fread_unlocked (w,x,y,z) 81# define fputs(x,y) fputs_unlocked (x,y)
88# else 82# else
89# define fread_unlocked(w,x,y,z) fread (w,x,y,z) 83# define fputs_unlocked(x,y) fputs (x,y)
90# endif 84# endif
91# if HAVE_DECL_FWRITE_UNLOCKED 85
92# undef fwrite 86# if HAVE_DECL_FREAD_UNLOCKED
93# define fwrite(w,x,y,z) fwrite_unlocked (w,x,y,z) 87# undef fread
94# else 88# define fread(w,x,y,z) fread_unlocked (w,x,y,z)
95# define fwrite_unlocked(w,x,y,z) fwrite (w,x,y,z) 89# else
96# endif 90# define fread_unlocked(w,x,y,z) fread (w,x,y,z)
97# if HAVE_DECL_GETC_UNLOCKED 91# endif
98# undef getc 92
99# define getc(x) getc_unlocked (x) 93# if HAVE_DECL_FWRITE_UNLOCKED
100# else 94# undef fwrite
101# define getc_unlocked(x) getc (x) 95# define fwrite(w,x,y,z) fwrite_unlocked (w,x,y,z)
102# endif 96# else
103# if HAVE_DECL_GETCHAR_UNLOCKED 97# define fwrite_unlocked(w,x,y,z) fwrite (w,x,y,z)
104# undef getchar 98# endif
105# define getchar() getchar_unlocked () 99
106# else 100# if HAVE_DECL_GETC_UNLOCKED
107# define getchar_unlocked() getchar () 101# undef getc
108# endif 102# define getc(x) getc_unlocked (x)
109# if HAVE_DECL_PUTC_UNLOCKED 103# else
110# undef putc 104# define getc_unlocked(x) getc (x)
111# define putc(x,y) putc_unlocked (x,y) 105# endif
112# else 106
113# define putc_unlocked(x,y) putc (x,y) 107# if HAVE_DECL_GETCHAR_UNLOCKED
114# endif 108# undef getchar
115# if HAVE_DECL_PUTCHAR_UNLOCKED 109# define getchar() getchar_unlocked ()
116# undef putchar 110# else
117# define putchar(x) putchar_unlocked (x) 111# define getchar_unlocked() getchar ()
118# else 112# endif
119# define putchar_unlocked(x) putchar (x) 113
120# endif 114# if HAVE_DECL_PUTC_UNLOCKED
121 115# undef putc
122# undef flockfile 116# define putc(x,y) putc_unlocked (x,y)
123# define flockfile(x) ((void) 0) 117# else
124 118# define putc_unlocked(x,y) putc (x,y)
125# undef ftrylockfile 119# endif
126# define ftrylockfile(x) 0 120
127 121# if HAVE_DECL_PUTCHAR_UNLOCKED
128# undef funlockfile 122# undef putchar
129# define funlockfile(x) ((void) 0) 123# define putchar(x) putchar_unlocked (x)
130 124# else
131# endif /* USE_UNLOCKED_IO */ 125# define putchar_unlocked(x) putchar (x)
126# endif
127
128# undef flockfile
129# define flockfile(x) ((void) 0)
130
131# undef ftrylockfile
132# define ftrylockfile(x) 0
133
134# undef funlockfile
135# define funlockfile(x) ((void) 0)
136
132#endif /* UNLOCKED_IO_H */ 137#endif /* UNLOCKED_IO_H */
diff --git a/lib/xalloc-die.c b/lib/xalloc-die.c
new file mode 100644
index 0000000..ff5ac9e
--- /dev/null
+++ b/lib/xalloc-die.c
@@ -0,0 +1,45 @@
1/* Report a memory allocation failure and exit.
2
3 Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004 Free
4 Software Foundation, Inc.
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,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include "xalloc.h"
25
26#include <stdlib.h>
27
28#include "error.h"
29#include "exitfail.h"
30
31#include "gettext.h"
32#define _(msgid) gettext (msgid)
33#define N_(msgid) msgid
34
35void
36xalloc_die (void)
37{
38 error (exit_failure, 0, "%s", _("memory exhausted"));
39
40 /* The `noreturn' cannot be given to error, since it may return if
41 its first argument is 0. To help compilers understand the
42 xalloc_die does not return, call abort. Also, the abort is a
43 safety feature if exit_failure is 0 (which shouldn't happen). */
44 abort ();
45}
diff --git a/lib/xalloc.h b/lib/xalloc.h
index 4b65858..f80977e 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -1,7 +1,7 @@
1/* xalloc.h -- malloc with out-of-memory checking 1/* xalloc.h -- malloc with out-of-memory checking
2 2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2003 Free Software Foundation, Inc. 4 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
5 5
6 This program is free software; you can redistribute it and/or modify 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 7 it under the terms of the GNU General Public License as published by
@@ -15,13 +15,19 @@
15 15
16 You should have received a copy of the GNU General Public License 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, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20#ifndef XALLOC_H_ 20#ifndef XALLOC_H_
21# define XALLOC_H_ 21# define XALLOC_H_
22 22
23# include <stddef.h> 23# include <stddef.h>
24 24
25
26# ifdef __cplusplus
27extern "C" {
28# endif
29
30
25# ifndef __attribute__ 31# ifndef __attribute__
26# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ 32# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
27# define __attribute__(x) 33# define __attribute__(x)
@@ -32,18 +38,9 @@
32# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) 38# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
33# endif 39# endif
34 40
35/* If this pointer is non-zero, run the specified function upon each 41/* This function is always triggered when memory is exhausted.
36 allocation failure. It is initialized to zero. */ 42 It must be defined by the application, either explicitly
37extern void (*xalloc_fail_func) (void); 43 or by using gnulib's xalloc-die module. This is the
38
39/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
40 message is output. It is translated via gettext.
41 Its value is "memory exhausted". */
42extern char const xalloc_msg_memory_exhausted[];
43
44/* This function is always triggered when memory is exhausted. It is
45 in charge of honoring the two previous items. It exits with status
46 exit_failure (defined in exitfail.h). This is the
47 function to call when one wants the program to die because of a 44 function to call when one wants the program to die because of a
48 memory allocation failure. */ 45 memory allocation failure. */
49extern void xalloc_die (void) ATTRIBUTE_NORETURN; 46extern void xalloc_die (void) ATTRIBUTE_NORETURN;
@@ -56,8 +53,8 @@ void *xrealloc (void *p, size_t s);
56void *xnrealloc (void *p, size_t n, size_t s); 53void *xnrealloc (void *p, size_t n, size_t s);
57void *x2realloc (void *p, size_t *pn); 54void *x2realloc (void *p, size_t *pn);
58void *x2nrealloc (void *p, size_t *pn, size_t s); 55void *x2nrealloc (void *p, size_t *pn, size_t s);
59void *xclone (void const *p, size_t s); 56void *xmemdup (void const *p, size_t s);
60char *xstrdup (const char *str); 57char *xstrdup (char const *str);
61 58
62/* Return 1 if an array of N objects, each of size S, cannot exist due 59/* Return 1 if an array of N objects, each of size S, cannot exist due
63 to size arithmetic overflow. S must be positive and N must be 60 to size arithmetic overflow. S must be positive and N must be
@@ -74,14 +71,9 @@ char *xstrdup (const char *str);
74# define xalloc_oversized(n, s) \ 71# define xalloc_oversized(n, s) \
75 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) 72 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
76 73
77/* These macros are deprecated; they will go away soon, and are retained 74# ifdef __cplusplus
78 temporarily only to ease conversion to the functions described above. */ 75}
79# define CCLONE(p, n) xclone (p, (n) * sizeof *(p)) 76# endif
80# define CLONE(p) xclone (p, sizeof *(p)) 77
81# define NEW(type, var) type *var = xmalloc (sizeof (type))
82# define XCALLOC(type, n) xcalloc (n, sizeof (type))
83# define XMALLOC(type, n) xnmalloc (n, sizeof (type))
84# define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
85# define XFREE(p) free (p)
86 78
87#endif /* !XALLOC_H_ */ 79#endif /* !XALLOC_H_ */
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index 181006b..687633c 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -1,7 +1,7 @@
1/* xmalloc.c -- malloc with out of memory checking 1/* xmalloc.c -- malloc with out of memory checking
2 2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003, 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002, 2003 Free Software Foundation, Inc. 4 1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 5
6 This program is free software; you can redistribute it and/or modify 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 7 it under the terms of the GNU General Public License as published by
@@ -15,9 +15,9 @@
15 15
16 You should have received a copy of the GNU General Public License 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, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23 23
@@ -26,44 +26,19 @@
26#include <stdlib.h> 26#include <stdlib.h>
27#include <string.h> 27#include <string.h>
28 28
29#include "gettext.h"
30#define _(msgid) gettext (msgid)
31#define N_(msgid) msgid
32
33#include "error.h"
34#include "exitfail.h"
35
36#ifndef SIZE_MAX 29#ifndef SIZE_MAX
37# define SIZE_MAX ((size_t) -1) 30# define SIZE_MAX ((size_t) -1)
38#endif 31#endif
39 32
40#ifndef HAVE_MALLOC 33/* 1 if calloc is known to be compatible with GNU calloc. This
41"you must run the autoconf test for a GNU libc compatible malloc" 34 matters if we are not also using the calloc module, which defines
42#endif 35 HAVE_CALLOC and supports the GNU API even on non-GNU platforms. */
43 36#if defined HAVE_CALLOC || defined __GLIBC__
44#ifndef HAVE_REALLOC 37enum { HAVE_GNU_CALLOC = 1 };
45"you must run the autoconf test for a GNU libc compatible realloc" 38#else
39enum { HAVE_GNU_CALLOC = 0 };
46#endif 40#endif
47 41
48/* If non NULL, call this function when memory is exhausted. */
49void (*xalloc_fail_func) (void) = 0;
50
51/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
52 before exiting when memory is exhausted. Goes through gettext. */
53char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
54
55void
56xalloc_die (void)
57{
58 if (xalloc_fail_func)
59 (*xalloc_fail_func) ();
60 error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
61 /* The `noreturn' cannot be given to error, since it may return if
62 its first argument is 0. To help compilers understand the
63 xalloc_die does terminate, call abort. */
64 abort ();
65}
66
67/* Allocate an array of N objects, each with S bytes of memory, 42/* Allocate an array of N objects, each with S bytes of memory,
68 dynamically, with error checking. S must be nonzero. */ 43 dynamically, with error checking. S must be nonzero. */
69 44
@@ -71,7 +46,7 @@ static inline void *
71xnmalloc_inline (size_t n, size_t s) 46xnmalloc_inline (size_t n, size_t s)
72{ 47{
73 void *p; 48 void *p;
74 if (xalloc_oversized (n, s) || ! (p = malloc (n * s))) 49 if (xalloc_oversized (n, s) || (! (p = malloc (n * s)) && n != 0))
75 xalloc_die (); 50 xalloc_die ();
76 return p; 51 return p;
77} 52}
@@ -96,7 +71,7 @@ xmalloc (size_t n)
96static inline void * 71static inline void *
97xnrealloc_inline (void *p, size_t n, size_t s) 72xnrealloc_inline (void *p, size_t n, size_t s)
98{ 73{
99 if (xalloc_oversized (n, s) || ! (p = realloc (p, n * s))) 74 if (xalloc_oversized (n, s) || (! (p = realloc (p, n * s)) && n != 0))
100 xalloc_die (); 75 xalloc_die ();
101 return p; 76 return p;
102} 77}
@@ -238,18 +213,29 @@ xcalloc (size_t n, size_t s)
238{ 213{
239 void *p; 214 void *p;
240 /* Test for overflow, since some calloc implementations don't have 215 /* Test for overflow, since some calloc implementations don't have
241 proper overflow checks. */ 216 proper overflow checks. But omit overflow and size-zero tests if
242 if (xalloc_oversized (n, s) || ! (p = calloc (n, s))) 217 HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
218 returns NULL if successful. */
219 if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
220 || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
243 xalloc_die (); 221 xalloc_die ();
244 return p; 222 return p;
245} 223}
246 224
247/* Clone an object P of size S, with error checking. There's no need 225/* Clone an object P of size S, with error checking. There's no need
248 for xnclone (P, N, S), since xclone (P, N * S) works without any 226 for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
249 need for an arithmetic overflow check. */ 227 need for an arithmetic overflow check. */
250 228
251void * 229void *
252xclone (void const *p, size_t s) 230xmemdup (void const *p, size_t s)
253{ 231{
254 return memcpy (xmalloc (s), p, s); 232 return memcpy (xmalloc (s), p, s);
255} 233}
234
235/* Clone STRING. */
236
237char *
238xstrdup (char const *string)
239{
240 return xmemdup (string, strlen (string) + 1);
241}