diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 6 | ||||
-rw-r--r-- | lib/error.c | 207 | ||||
-rw-r--r-- | lib/error.h | 36 | ||||
-rw-r--r-- | lib/error.m4 | 19 | ||||
-rw-r--r-- | lib/exit.h | 32 | ||||
-rw-r--r-- | lib/exitfail.c | 27 | ||||
-rw-r--r-- | lib/exitfail.h | 20 | ||||
-rw-r--r-- | lib/fsusage.c | 57 | ||||
-rw-r--r-- | lib/fsusage.h | 14 | ||||
-rw-r--r-- | lib/fsusage.m4 | 53 | ||||
-rw-r--r-- | lib/full-read.h | 24 | ||||
-rw-r--r-- | lib/getloadavg.c | 166 | ||||
-rw-r--r-- | lib/getopt.c | 144 | ||||
-rw-r--r-- | lib/getopt.h | 52 | ||||
-rw-r--r-- | lib/getopt1.c | 61 | ||||
-rw-r--r-- | lib/gettext.h | 17 | ||||
-rw-r--r-- | lib/ls-mntd-fs.m4 | 44 | ||||
-rw-r--r-- | lib/malloc.c | 6 | ||||
-rw-r--r-- | lib/mountlist.c | 213 | ||||
-rw-r--r-- | lib/mountlist.h | 33 | ||||
-rw-r--r-- | lib/onceonly.m4 | 63 | ||||
-rw-r--r-- | lib/realloc.c | 13 | ||||
-rw-r--r-- | lib/snprintf.c | 133 | ||||
-rw-r--r-- | lib/strtod.c | 21 | ||||
-rw-r--r-- | lib/unlocked-io.h | 66 | ||||
-rw-r--r-- | lib/unlocked-io.m4 | 22 | ||||
-rw-r--r-- | lib/xalloc.h | 88 | ||||
-rw-r--r-- | lib/xalloc.m4 | 32 | ||||
-rw-r--r-- | lib/xmalloc.c | 210 | ||||
-rw-r--r-- | lib/xstrdup.c | 33 |
30 files changed, 1121 insertions, 791 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index 7b07f555..2e9aeee9 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -2,7 +2,8 @@ noinst_LIBRARIES = libnagiosplug.a -libnagiosplug_a_SOURCES = getopt.c getopt1.c snprintf.c fsusage.c mountlist.c xmalloc.c +libnagiosplug_a_SOURCES = getopt.c getopt1.c fsusage.c snprintf.c mountlist.c \ + xmalloc.c xstrdup.c exitfail.c libnagiosplug_a_LIBADD = @LIBOBJS@ libnagiosplug_a_DEPENDENCIES = $(libnagiosplug_a_LIBADD) @@ -12,6 +13,7 @@ EXTRA_DIST = ulonglong.m4 codeset.m4 getloadavg.m4 gettext.m4 glibc21.m4 iconv.m inttypes.m4 inttypes_h.m4 inttypes-pri.m4 isc-posix.m4 lcmessage.m4 lib-ld.m4 \ lib-link.m4 lib-prefix.m4 progtest.m4 stdint_h.m4 uintmax_t.m4 afs.m4 \ fstypename.m4 fsusage.m4 ls-mntd-fs.m4 getopt.h gettext.h fsusage.h mountlist.h\ - error.m4 error.h error.c getloadavg.c xalloc.h unlocked-io.h malloc.c realloc.c strtod.c + error.m4 error.h error.c getloadavg.c xalloc.h unlocked-io.h unlocked-io.m4 malloc.c \ + onceonly.m4 realloc.c strtod.c exitfail.h full-read.h xstrdup.c xalloc.m4 exit.h INCLUDES = -I$(srcdir) -I$(top_srcdir)/intl diff --git a/lib/error.c b/lib/error.c index 2296124a..1149235a 100644 --- a/lib/error.c +++ b/lib/error.c @@ -1,5 +1,7 @@ /* Error handler for noninteractive utilities - Copyright (C) 1990-1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 1990-1998, 2000-2002, 2003 Free Software Foundation, Inc. + This file is part of the GNU C Library. + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) @@ -20,7 +22,12 @@ # include <config.h> #endif +#include "error.h" + +#include <stdarg.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> #ifdef _LIBC # include <libintl.h> @@ -33,28 +40,6 @@ # define mbsrtowcs __mbsrtowcs #endif -#if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC -# if __STDC__ -# include <stdarg.h> -# define VA_START(args, lastarg) va_start(args, lastarg) -# else -# include <varargs.h> -# define VA_START(args, lastarg) va_start(args) -# endif -#else -# define va_alist a1, a2, a3, a4, a5, a6, a7, a8 -# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; -#endif - -#if STDC_HEADERS || _LIBC -# include <stdlib.h> -# include <string.h> -#else -void exit (); -#endif - -#include "error.h" - #if !_LIBC # include "unlocked-io.h" #endif @@ -66,11 +51,7 @@ void exit (); /* If NULL, error will flush stdout, then print on stderr the program name, a colon and a space. Otherwise, error will call this function without parameters instead. */ -void (*error_print_progname) ( -#if __STDC__ - 0 - void -#endif - ); +void (*error_print_progname) (void); /* This variable is incremented each time `error' is called. */ unsigned int error_message_count; @@ -98,6 +79,8 @@ extern void __error_at_line (int status, int errnum, const char *file_name, # undef putc # define putc(c, fp) INTUSE(_IO_putc) (c, fp) +# include <bits/libc-lock.h> + #else /* not _LIBC */ # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P @@ -107,34 +90,17 @@ extern void __error_at_line (int status, int errnum, const char *file_name, char *strerror_r (); # endif +# ifndef SIZE_MAX +# define SIZE_MAX ((size_t) -1) +# endif + /* The calling program should define program_name and set it to the name of the executing program. */ -extern char *progname; +extern char *program_name; # if HAVE_STRERROR_R || defined strerror_r # define __strerror_r strerror_r -# else -# if HAVE_STRERROR -# ifndef HAVE_DECL_STRERROR -"this configure-time declaration test was not run" -# endif -# if !HAVE_DECL_STRERROR -char *strerror (); -# endif -# else -static char * -private_strerror (int errnum) -{ - extern char *sys_errlist[]; - extern int sys_nerr; - - if (errnum > 0 && errnum <= sys_nerr) - return _(sys_errlist[errnum]); - return _("Unknown system error"); -} -# define strerror private_strerror -# endif /* HAVE_STRERROR */ -# endif /* HAVE_STRERROR_R || defined strerror_r */ +# endif #endif /* not _LIBC */ static void @@ -172,93 +138,70 @@ print_errno_message (int errnum) fprintf (stderr, ": %s", s); } -#ifdef VA_START static void error_tail (int status, int errnum, const char *message, va_list args) { -# if HAVE_VPRINTF || _LIBC -# if _LIBC +#if _LIBC if (_IO_fwide (stderr, 0) > 0) { -# define ALLOCA_LIMIT 2000 +# define ALLOCA_LIMIT 2000 size_t len = strlen (message) + 1; - wchar_t *wmessage = NULL; - mbstate_t st; - size_t res; - const char *tmp; - - do + const wchar_t *wmessage = L"out of memory"; + wchar_t *wbuf = (len < ALLOCA_LIMIT + ? alloca (len * sizeof *wbuf) + : len <= SIZE_MAX / sizeof *wbuf + ? malloc (len * sizeof *wbuf) + : NULL); + + if (wbuf) { - if (len < ALLOCA_LIMIT) - wmessage = (wchar_t *) alloca (len * sizeof (wchar_t)); - else - { - if (wmessage != NULL && len / 2 < ALLOCA_LIMIT) - wmessage = NULL; - - wmessage = (wchar_t *) realloc (wmessage, - len * sizeof (wchar_t)); - - if (wmessage == NULL) - { - fputws_unlocked (L"out of memory\n", stderr); - return; - } - } - + size_t res; + mbstate_t st; + const char *tmp = message; memset (&st, '\0', sizeof (st)); - tmp =message; + res = mbsrtowcs (wbuf, &tmp, len, &st); + wmessage = res == (size_t) -1 ? L"???" : wbuf; } - while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len); - - if (res == (size_t) -1) - /* The string cannot be converted. */ - wmessage = (wchar_t *) L"???"; __vfwprintf (stderr, wmessage, args); + if (! (len < ALLOCA_LIMIT)) + free (wbuf); } else -# endif +#endif vfprintf (stderr, message, args); -# else - _doprnt (message, args, stderr); -# endif va_end (args); ++error_message_count; if (errnum) print_errno_message (errnum); -# if _LIBC +#if _LIBC if (_IO_fwide (stderr, 0) > 0) putwc (L'\n', stderr); else -# endif +#endif putc ('\n', stderr); fflush (stderr); if (status) exit (status); } -#endif /* Print the program name and error message MESSAGE, which is a printf-style format string with optional args. If ERRNUM is nonzero, print its corresponding system error message. Exit with status STATUS if it is nonzero. */ -/* VARARGS */ void -#if defined VA_START && __STDC__ error (int status, int errnum, const char *message, ...) -#else -error (status, errnum, message, va_alist) - int status; - int errnum; - char *message; - va_dcl -#endif { -#ifdef VA_START va_list args; + +#if defined _LIBC && defined __libc_ptf_call + /* We do not want this call to be cut short by a thread + cancellation. Therefore disable cancellation for now. */ + int state = PTHREAD_CANCEL_ENABLE; + __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state), + 0); #endif fflush (stdout); @@ -271,29 +214,20 @@ error (status, errnum, message, va_alist) { #if _LIBC if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s: ", progname); + __fwprintf (stderr, L"%s: ", program_name); else #endif - fprintf (stderr, "%s: ", progname); + fprintf (stderr, "%s: ", program_name); } -#ifdef VA_START - VA_START (args, message); + va_start (args, message); error_tail (status, errnum, message, args); -#else - fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); - - ++error_message_count; - if (errnum) - print_errno_message (errnum); - putc ('\n', stderr); - fflush (stderr); - if (status) - exit (status); -#endif #ifdef _LIBC _IO_funlockfile (stderr); +# ifdef __libc_ptf_call + __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0); +# endif #endif } @@ -302,22 +236,10 @@ error (status, errnum, message, va_alist) int error_one_per_line; void -#if defined VA_START && __STDC__ error_at_line (int status, int errnum, const char *file_name, unsigned int line_number, const char *message, ...) -#else -error_at_line (status, errnum, file_name, line_number, message, va_alist) - int status; - int errnum; - const char *file_name; - unsigned int line_number; - char *message; - va_dcl -#endif { -#ifdef VA_START va_list args; -#endif if (error_one_per_line) { @@ -334,6 +256,14 @@ error_at_line (status, errnum, file_name, line_number, message, va_alist) old_line_number = line_number; } +#if defined _LIBC && defined __libc_ptf_call + /* We do not want this call to be cut short by a thread + cancellation. Therefore disable cancellation for now. */ + int state = PTHREAD_CANCEL_ENABLE; + __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state), + 0); +#endif + fflush (stdout); #ifdef _LIBC _IO_flockfile (stderr); @@ -344,10 +274,10 @@ error_at_line (status, errnum, file_name, line_number, message, va_alist) { #if _LIBC if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s: ", progname); + __fwprintf (stderr, L"%s: ", program_name); else #endif - fprintf (stderr, "%s:", progname); + fprintf (stderr, "%s:", program_name); } if (file_name != NULL) @@ -360,23 +290,14 @@ error_at_line (status, errnum, file_name, line_number, message, va_alist) fprintf (stderr, "%s:%d: ", file_name, line_number); } -#ifdef VA_START - VA_START (args, message); + va_start (args, message); error_tail (status, errnum, message, args); -#else - fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); - - ++error_message_count; - if (errnum) - print_errno_message (errnum); - putc ('\n', stderr); - fflush (stderr); - if (status) - exit (status); -#endif #ifdef _LIBC _IO_funlockfile (stderr); +# ifdef __libc_ptf_call + __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0); +# endif #endif } diff --git a/lib/error.h b/lib/error.h index 177b2dcb..8ed63595 100644 --- a/lib/error.h +++ b/lib/error.h @@ -1,24 +1,20 @@ /* Declaration for error-reporting function - Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + Copyright (C) 1995, 1996, 1997, 2003 Free Software Foundation, Inc. + This file is part of the GNU C Library. - - NOTE: The canonical source of this file is maintained with the GNU C Library. - Bugs can be reported to bug-glibc@prep.ai.mit.edu. - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2, or (at your option) any - later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, - USA. */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _ERROR_H #define _ERROR_H 1 @@ -40,17 +36,15 @@ extern "C" { #endif -#if defined (__STDC__) && __STDC__ - /* Print a message with `fprintf (stderr, FORMAT, ...)'; if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM). If STATUS is nonzero, terminate the program with `exit (STATUS)'. */ -extern void error (int status, int errnum, const char *format, ...) +extern void error (int __status, int __errnum, const char *__format, ...) __attribute__ ((__format__ (__printf__, 3, 4))); -extern void error_at_line (int status, int errnum, const char *fname, - unsigned int lineno, const char *format, ...) +extern void error_at_line (int __status, int __errnum, const char *__fname, + unsigned int __lineno, const char *__format, ...) __attribute__ ((__format__ (__printf__, 5, 6))); /* If NULL, error will flush stdout, then print on stderr the program @@ -58,12 +52,6 @@ extern void error_at_line (int status, int errnum, const char *fname, function without parameters instead. */ extern void (*error_print_progname) (void); -#else -void error (); -void error_at_line (); -extern void (*error_print_progname) (); -#endif - /* This variable is incremented each time `error' is called. */ extern unsigned int error_message_count; diff --git a/lib/error.m4 b/lib/error.m4 index 717725d6..0bc2e689 100644 --- a/lib/error.m4 +++ b/lib/error.m4 @@ -1,14 +1,15 @@ -#serial 5 +#serial 9 -dnl FIXME: put these prerequisite-only *.m4 files in a separate -dnl directory -- otherwise, they'll conflict with existing files. +AC_DEFUN([gl_ERROR], +[ + AC_FUNC_ERROR_AT_LINE + dnl Note: AC_FUNC_ERROR_AT_LINE does AC_LIBSOURCES([error.h, error.c]). + jm_PREREQ_ERROR +]) -dnl These are the prerequisite macros for GNU's error.c file. +# Prerequisites of lib/error.c. AC_DEFUN([jm_PREREQ_ERROR], [ - AC_CHECK_FUNCS(strerror vprintf doprnt) - AC_CHECK_DECLS([strerror]) - AC_CHECK_HEADERS([libintl.h]) - AC_FUNC_STRERROR_R - AC_HEADER_STDC + AC_REQUIRE([AC_FUNC_STRERROR_R]) + : ]) diff --git a/lib/exit.h b/lib/exit.h new file mode 100644 index 00000000..4e8d4651 --- /dev/null +++ b/lib/exit.h @@ -0,0 +1,32 @@ +/* exit() function. + Copyright (C) 1995, 2001 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifndef _EXIT_H +#define _EXIT_H + +/* Get exit() declaration. */ +#include <stdlib.h> + +/* Some systems do not define EXIT_*, even with STDC_HEADERS. */ +#ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +#endif +#ifndef EXIT_FAILURE +# define EXIT_FAILURE 1 +#endif + +#endif /* _EXIT_H */ diff --git a/lib/exitfail.c b/lib/exitfail.c new file mode 100644 index 00000000..2ae5f695 --- /dev/null +++ b/lib/exitfail.c @@ -0,0 +1,27 @@ +/* Failure exit status + + Copyright (C) 2002, 2003 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#if HAVE_CONFIG_H +# include <config.h> +#endif + +#include "exitfail.h" +#include "exit.h" + +int volatile exit_failure = EXIT_FAILURE; diff --git a/lib/exitfail.h b/lib/exitfail.h new file mode 100644 index 00000000..cf5ab719 --- /dev/null +++ b/lib/exitfail.h @@ -0,0 +1,20 @@ +/* Failure exit status + + Copyright (C) 2002 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +extern int volatile exit_failure; diff --git a/lib/fsusage.c b/lib/fsusage.c index 7339c805..d9260293 100644 --- a/lib/fsusage.c +++ b/lib/fsusage.c @@ -1,5 +1,7 @@ /* fsusage.c -- return space usage of mounted filesystems - Copyright (C) 1991, 1992, 1996, 1998, 1999 Free Software Foundation, Inc. + + Copyright (C) 1991, 1992, 1996, 1998, 1999, 2002, 2003 Free + Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,23 +17,26 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "config.h" +#if HAVE_CONFIG_H +# include <config.h> +#endif #if HAVE_INTTYPES_H # include <inttypes.h> +#else +# if HAVE_STDINT_H +# include <stdint.h> +# endif #endif +#ifndef UINTMAX_MAX +# define UINTMAX_MAX ((uintmax_t) -1) +#endif + #include <sys/types.h> #include <sys/stat.h> #include "fsusage.h" -#if HAVE_LIMITS_H -# include <limits.h> -#endif -#ifndef CHAR_BIT -# define CHAR_BIT 8 -#endif - -int statfs (); +#include <limits.h> #if HAVE_SYS_PARAM_H # include <sys/param.h> @@ -49,7 +54,7 @@ int statfs (); # include <sys/fs/s5param.h> #endif -#if defined (HAVE_SYS_FILSYS_H) && !defined (_CRAY) +#if defined HAVE_SYS_FILSYS_H && !defined _CRAY # include <sys/filsys.h> /* SVR2 */ #endif @@ -70,11 +75,18 @@ int statfs (); int statvfs (); #endif +#include "full-read.h" + /* Many space usage primitives use all 1 bits to denote a value that is not applicable or unknown. Propagate this information by returning - a uintmax_t value that is all 1 bits if the argument is all 1 bits, - even if the argument is unsigned and smaller than uintmax_t. */ -#define PROPAGATE_ALL_ONES(x) ((x) == -1 ? (uintmax_t) -1 : (uintmax_t) (x)) + a uintmax_t value that is all 1 bits if X is all 1 bits, even if X + is unsigned and narrower than uintmax_t. */ +#define PROPAGATE_ALL_ONES(x) \ + ((sizeof (x) < sizeof (uintmax_t) \ + && (~ (x) == (sizeof (x) < sizeof (int) \ + ? - (1 << (sizeof (x) * CHAR_BIT)) \ + : 0))) \ + ? UINTMAX_MAX : (x)) /* Extract the top bit of X as an uintmax_t value. */ #define EXTRACT_TOP_BIT(x) ((x) \ @@ -89,8 +101,6 @@ int statvfs (); otherwise, use PROPAGATE_ALL_ONES. */ #define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1)) -int safe_read (); - /* Fill in the fields of FSP with information about space usage for the filesystem on which PATH resides. DISK is the device on which PATH is mounted, for space-getting @@ -147,7 +157,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp) if (fd < 0) return -1; lseek (fd, (off_t) SUPERBOFF, 0); - if (safe_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd) + if (full_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd) { close (fd); return -1; @@ -160,7 +170,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp) fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.s_tfree); fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.s_tfree) != 0; fsp->fsu_files = (fsd.s_isize == -1 - ? (uintmax_t) -1 + ? UINTMAX_MAX : (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1)); fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.s_tinode); @@ -217,7 +227,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp) /* Empirically, the block counts on most SVR3 and SVR3-derived systems seem to always be in terms of 512-byte blocks, no matter what value f_bsize has. */ -# if _AIX || defined(_CRAY) +# if _AIX || defined _CRAY fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize); # else fsp->fsu_blocksize = 512; @@ -233,12 +243,13 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp) return -1; /* f_frsize isn't guaranteed to be supported. */ - fsp->fsu_blocksize = - PROPAGATE_ALL_ONES (fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize); + fsp->fsu_blocksize = (fsd.f_frsize + ? PROPAGATE_ALL_ONES (fsd.f_frsize) + : PROPAGATE_ALL_ONES (fsd.f_bsize)); #endif /* STAT_STATVFS */ -#if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) +#if !defined STAT_STATFS2_FS_DATA && !defined STAT_READ_FILSYS /* !Ultrix && !SVR2 */ fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks); @@ -253,7 +264,7 @@ get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp) return 0; } -#if defined(_AIX) && defined(_I386) +#if defined _AIX && defined _I386 /* AIX PS/2 does not supply statfs. */ int diff --git a/lib/fsusage.h b/lib/fsusage.h index e0c0db58..e2cbbf12 100644 --- a/lib/fsusage.h +++ b/lib/fsusage.h @@ -1,5 +1,6 @@ /* fsusage.h -- declarations for filesystem space usage info - Copyright (C) 1991, 1992, 1997 Free Software Foundation, Inc. + + Copyright (C) 1991, 1992, 1997, 2003 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,15 +32,6 @@ struct fs_usage uintmax_t fsu_ffree; /* Free file nodes. */ }; -# ifndef PARAMS -# if defined PROTOTYPES || (defined __STDC__ && __STDC__) -# define PARAMS(Args) Args -# else -# define PARAMS(Args) () -# endif -# endif - -int get_fs_usage PARAMS ((const char *path, const char *disk, - struct fs_usage *fsp)); +int get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp); #endif diff --git a/lib/fsusage.m4 b/lib/fsusage.m4 index 85d0fc8f..a0ab1e15 100644 --- a/lib/fsusage.m4 +++ b/lib/fsusage.m4 @@ -1,7 +1,18 @@ -#serial 9 +#serial 11 # From fileutils/configure.in +AC_DEFUN([gl_FSUSAGE], +[ + AC_CHECK_HEADERS_ONCE(sys/param.h) + AC_CHECK_HEADERS(sys/mount.h sys/vfs.h sys/fs_types.h) + jm_FILE_SYSTEM_USAGE([gl_cv_fs_space=yes], [gl_cv_fs_space=no]) + if test $gl_cv_fs_space = yes; then + AC_LIBOBJ(fsusage) + gl_PREREQ_FSUSAGE_EXTRA + fi +]) + # Try to determine how a program can obtain filesystem usage information. # If successful, define the appropriate symbol (see fsusage.c) and # execute ACTION-IF-FOUND. Otherwise, execute ACTION-IF-NOT-FOUND. @@ -193,7 +204,43 @@ if test $ac_fsusage_space = no; then ac_fsusage_space=yes) fi -dnl AS_IF([test $ac_fsusage_space = yes], [$1], [$2]) -if test $ac_fsusage_space = yes ; then [$1] ; else [$2] ; fi +AS_IF([test $ac_fsusage_space = yes], [$1], [$2]) ]) + + +# Check for SunOS statfs brokenness wrt partitions 2GB and larger. +# If <sys/vfs.h> exists and struct statfs has a member named f_spare, +# enable the work-around code in fsusage.c. +AC_DEFUN([jm_STATFS_TRUNCATES], +[ + AC_MSG_CHECKING([for statfs that truncates block counts]) + AC_CACHE_VAL(fu_cv_sys_truncating_statfs, + [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ +#if !defined(sun) && !defined(__sun) +choke -- this is a workaround for a Sun-specific problem +#endif +#include <sys/types.h> +#include <sys/vfs.h>]], + [[struct statfs t; long c = *(t.f_spare);]])], + [fu_cv_sys_truncating_statfs=yes], + [fu_cv_sys_truncating_statfs=no])]) + if test $fu_cv_sys_truncating_statfs = yes; then + AC_DEFINE(STATFS_TRUNCATES_BLOCK_COUNTS, 1, + [Define if the block counts reported by statfs may be truncated to 2GB + and the correct values may be stored in the f_spare array. + (SunOS 4.1.2, 4.1.3, and 4.1.3_U1 are reported to have this problem. + SunOS 4.1.1 seems not to be affected.)]) + fi + AC_MSG_RESULT($fu_cv_sys_truncating_statfs) +]) + + +# Prerequisites of lib/fsusage.c not done by jm_FILE_SYSTEM_USAGE. +AC_DEFUN([gl_PREREQ_FSUSAGE_EXTRA], +[ + AC_REQUIRE([jm_AC_TYPE_UINTMAX_T]) + AC_CHECK_HEADERS_ONCE(fcntl.h) + AC_CHECK_HEADERS(dustat.h sys/fs/s5param.h sys/filsys.h sys/statfs.h sys/statvfs.h) + jm_STATFS_TRUNCATES +]) diff --git a/lib/full-read.h b/lib/full-read.h new file mode 100644 index 00000000..71f19a3b --- /dev/null +++ b/lib/full-read.h @@ -0,0 +1,24 @@ +/* An interface to read() that reads all it is asked to read. + + Copyright (C) 2002 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, read to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#include <stddef.h> + +/* Read COUNT bytes at BUF to descriptor FD, retrying if interrupted + or if partial reads occur. Return the number of bytes successfully + read, setting errno if that is less than COUNT. errno = 0 means EOF. */ +extern size_t full_read (int fd, void *buf, size_t count); diff --git a/lib/getloadavg.c b/lib/getloadavg.c index e9c4c088..c6b6fee1 100644 --- a/lib/getloadavg.c +++ b/lib/getloadavg.c @@ -1,6 +1,10 @@ /* Get the system load averages. - Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995, 1997 - Free Software Foundation, Inc. + + Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, + 1995, 1997, 1999, 2000, 2003, 2004 Free Software Foundation, Inc. + + NOTE: The canonical source of this file is maintained with gnulib. + Bugs can be reported to bug-gnulib@gnu.org. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -35,12 +39,17 @@ LOAD_AVE_TYPE Type of the load average array in the kernel. Must be defined unless one of apollo, DGUX, NeXT, or UMAX is defined; + or we have libkstat; otherwise, no load average is available. + HAVE_NLIST_H nlist.h is available. NLIST_STRUCT defaults + to this. NLIST_STRUCT Include nlist.h, not a.out.h, and the nlist n_name element is a pointer, not an array. - NLIST_NAME_UNION struct nlist has an n_un member, not n_name. + HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'. LINUX_LDAV_FILE [__linux__]: File containing load averages. + HAVE_LOCALE_H locale.h is available. + HAVE_SETLOCALE The `setlocale' function is available. Specific system predefines this file uses, aside from setting default values if not emacs: @@ -62,7 +71,7 @@ VMS WINDOWS32 No-op for Windows95/NT. __linux__ Linux: assumes /proc filesystem mounted. - Support from Michael K. Johnson. + Support from Michael K. Johnson. __NetBSD__ NetBSD: assumes /kern filesystem mounted. In addition, to avoid nesting many #ifdefs, we internally set @@ -81,7 +90,7 @@ /* Both the Emacs and non-Emacs sections want this. Some configuration files' definitions for the LOAD_AVE_CVT macro (like sparc.h's) use macros like FSCALE, defined here. */ -#ifdef unix +#if defined (unix) || defined (__unix) # include <sys/param.h> #endif @@ -98,13 +107,15 @@ extern int errno; #endif -#if HAVE_LOCALE_H +#ifdef HAVE_LOCALE_H # include <locale.h> #endif -#if !HAVE_SETLOCALE +#ifndef HAVE_SETLOCALE # define setlocale(Category, Locale) /* empty */ #endif +#include "cloexec.h" + #ifndef HAVE_GETLOADAVG /* The existing Emacs configuration files define a macro called @@ -117,7 +128,7 @@ extern int errno; LOAD_AVE_CVT, but future machine config files should just define LDAV_CVT directly. */ -# if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT) +# if !defined (LDAV_CVT) && defined (LOAD_AVE_CVT) # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0) # endif @@ -137,6 +148,12 @@ extern int errno; # undef FSCALE # endif +/* Same issues as for NeXT apply to the HURD-based GNU system. */ +# ifdef __GNU__ +# undef BSD +# undef FSCALE +# endif /* __GNU__ */ + /* Set values that are different from the defaults, which are set a little farther down with #ifndef. */ @@ -155,11 +172,11 @@ extern int errno; # define sun # endif -# if defined(hp300) && !defined(hpux) +# if defined (hp300) && !defined (hpux) # define MORE_BSD # endif -# if defined(ultrix) && defined(mips) +# if defined (ultrix) && defined (mips) # define decstation # endif @@ -167,7 +184,7 @@ extern int errno; # define SVR4 # endif -# if (defined(sun) && defined(SVR4)) || defined (SOLARIS2) +# if (defined (sun) && defined (SVR4)) || defined (SOLARIS2) # define SUNOS_5 # endif @@ -239,7 +256,7 @@ extern int errno; # define LOAD_AVE_TYPE long # endif -# if defined(alliant) && defined(i860) /* Alliant FX/2800 */ +# if defined (alliant) && defined (i860) /* Alliant FX/2800 */ # define LOAD_AVE_TYPE long # endif @@ -263,7 +280,7 @@ extern int errno; # define FSCALE 1024.0 # endif -# if defined(alliant) && defined(i860) /* Alliant FX/2800 */ +# if defined (alliant) && defined (i860) /* Alliant FX/2800 */ /* <sys/param.h> defines an incorrect value for FSCALE on an Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */ # undef FSCALE @@ -279,7 +296,7 @@ extern int errno; # define FSCALE 2048.0 # endif -# if defined(MIPS) || defined(SVR4) || defined(decstation) +# if defined (MIPS) || defined (SVR4) || defined (decstation) # define FSCALE 256 # endif @@ -314,69 +331,7 @@ extern int errno; # endif # endif -/* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */ -# ifndef NLIST_STRUCT - -# ifdef MORE_BSD -# define NLIST_STRUCT -# endif - -# ifdef sun -# define NLIST_STRUCT -# endif - -# ifdef decstation -# define NLIST_STRUCT -# endif - -# ifdef hpux -# define NLIST_STRUCT -# endif - -# if defined (_SEQUENT_) || defined (sequent) -# define NLIST_STRUCT -# endif - -# ifdef sgi -# define NLIST_STRUCT -# endif - -# ifdef SVR4 -# define NLIST_STRUCT -# endif - -# ifdef sony_news -# define NLIST_STRUCT -# endif - -# ifdef OSF_ALPHA -# define NLIST_STRUCT -# endif - -# if defined (ardent) && defined (titan) -# define NLIST_STRUCT -# endif - -# ifdef tek4300 -# define NLIST_STRUCT -# endif - -# ifdef butterfly -# define NLIST_STRUCT -# endif - -# if defined(alliant) && defined(i860) /* Alliant FX/2800 */ -# define NLIST_STRUCT -# endif - -# ifdef _AIX -# define NLIST_STRUCT -# endif - -# endif /* defined (NLIST_STRUCT) */ - - -# if defined(sgi) || (defined(mips) && !defined(BSD)) +# if defined (sgi) || (defined (mips) && !defined (BSD)) # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31)) # endif @@ -389,7 +344,7 @@ extern int errno; # define KERNEL_FILE "/hp-ux" # endif -# if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || (defined (ardent) && defined (titan))) +# if !defined (KERNEL_FILE) && (defined (_SEQUENT_) || defined (MIPS) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan))) # define KERNEL_FILE "/unix" # endif @@ -398,7 +353,7 @@ extern int errno; # define LDAV_SYMBOL "_Loadavg" # endif -# if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX)) +# if !defined (LDAV_SYMBOL) && ((defined (hpux) && !defined (hp9000s300)) || defined (_SEQUENT_) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)) || defined (_AIX)) # define LDAV_SYMBOL "avenrun" # endif @@ -410,7 +365,7 @@ extern int errno; /* LOAD_AVE_TYPE should only get defined if we're going to use the nlist method. */ -# if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL)) +# if !defined (LOAD_AVE_TYPE) && (defined (BSD) || defined (LDAV_CVT) || defined (KERNEL_FILE) || defined (LDAV_SYMBOL)) # define LOAD_AVE_TYPE double # endif @@ -459,7 +414,7 @@ extern int errno; # endif /* LOAD_AVE_TYPE */ -# if defined(__GNU__) && !defined (NeXT) +# if defined (__GNU__) && !defined (NeXT) /* Note that NeXT Openstep defines __GNU__ even though it should not. */ /* GNU system acts much like NeXT, for load average purposes, but not exactly. */ @@ -506,7 +461,7 @@ extern int errno; # include <sys/dg_sys_info.h> # endif -# if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION) +# if defined (HAVE_FCNTL_H) || defined (_POSIX_VERSION) # include <fcntl.h> # else # include <sys/file.h> @@ -528,7 +483,7 @@ static unsigned int samples; static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */ # endif /* DGUX */ -# ifdef LOAD_AVE_TYPE +# if !defined (HAVE_LIBKSTAT) && defined (LOAD_AVE_TYPE) /* File descriptor open to /dev/kmem or VMS load ave driver. */ static int channel; /* Nonzero iff channel is valid. */ @@ -536,7 +491,7 @@ static int getloadavg_initialized; /* Offset in kmem to seek to read load average, or 0 means invalid. */ static long offset; -# if !defined(VMS) && !defined(sgi) && !defined(__linux__) +# if !defined (VMS) && !defined (sgi) && !defined (__linux__) static struct nlist nl[2]; # endif /* Not VMS or sgi */ @@ -544,7 +499,7 @@ static struct nlist nl[2]; static kvm_t *kd; # endif /* SUNOS_5 */ -# endif /* LOAD_AVE_TYPE */ +# endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */ /* Put the 1 minute, 5 minute and 15 minute load averages into the first NELEM elements of LOADAVG. @@ -552,9 +507,7 @@ static kvm_t *kd; or -1 if an error occurred. */ int -getloadavg (loadavg, nelem) - double loadavg[]; - int nelem; +getloadavg (double loadavg[], int nelem) { int elem = 0; /* Return value. */ @@ -577,7 +530,7 @@ getloadavg (loadavg, nelem) if (kc == 0) return -1; ksp = kstat_lookup (kc, "unix", 0, "system_misc"); - if (ksp == 0 ) + if (ksp == 0) return -1; if (kstat_read (kc, ksp, 0) == -1) return -1; @@ -592,20 +545,20 @@ getloadavg (loadavg, nelem) } if (nelem >= 1) - loadavg[elem++] = (double) kn->value.ul/FSCALE; + loadavg[elem++] = (double) kn->value.ul / FSCALE; if (nelem >= 2) { kn = kstat_data_lookup (ksp, "avenrun_5min"); if (kn != 0) { - loadavg[elem++] = (double) kn->value.ul/FSCALE; + loadavg[elem++] = (double) kn->value.ul / FSCALE; if (nelem >= 3) { kn = kstat_data_lookup (ksp, "avenrun_15min"); if (kn != 0) - loadavg[elem++] = (double) kn->value.ul/FSCALE; + loadavg[elem++] = (double) kn->value.ul / FSCALE; } } } @@ -870,8 +823,8 @@ getloadavg (loadavg, nelem) for (elem = 0; elem < nelem; elem++) loadavg[elem] = (load_ave.tl_lscale == 0 - ? load_ave.tl_avenrun.d[elem] - : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale)); + ? load_ave.tl_avenrun.d[elem] + : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale)); # endif /* OSF_ALPHA */ # if !defined (LDAV_DONE) && defined (VMS) @@ -914,7 +867,7 @@ getloadavg (loadavg, nelem) return -1; # endif /* VMS */ -# if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS) +# if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) && !defined (VMS) /* UNIX-specific code -- read the average from /dev/kmem. */ @@ -930,13 +883,13 @@ getloadavg (loadavg, nelem) strcpy (nl[0].n_name, LDAV_SYMBOL); strcpy (nl[1].n_name, ""); # else /* NLIST_STRUCT */ -# ifdef NLIST_NAME_UNION +# ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME nl[0].n_un.n_name = LDAV_SYMBOL; nl[1].n_un.n_name = 0; -# else /* not NLIST_NAME_UNION */ +# else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */ nl[0].n_name = LDAV_SYMBOL; nl[1].n_name = 0; -# endif /* not NLIST_NAME_UNION */ +# endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */ # endif /* NLIST_STRUCT */ # ifndef SUNOS_5 @@ -973,12 +926,7 @@ getloadavg (loadavg, nelem) { /* Set the channel to close on exec, so it does not litter any child's descriptor table. */ -# ifdef FD_SETFD -# ifndef FD_CLOEXEC -# define FD_CLOEXEC 1 -# endif - (void) fcntl (channel, F_SETFD, FD_CLOEXEC); -# endif + set_cloexec_flag (channel, true); getloadavg_initialized = 1; } # else /* SUNOS_5 */ @@ -1010,9 +958,9 @@ getloadavg (loadavg, nelem) # else /* SUNOS_5 */ if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave)) != sizeof (load_ave)) - { - kvm_close (kd); - getloadavg_initialized = 0; + { + kvm_close (kd); + getloadavg_initialized = 0; } # endif /* SUNOS_5 */ } @@ -1046,9 +994,7 @@ getloadavg (loadavg, nelem) #ifdef TEST void -main (argc, argv) - int argc; - char **argv; +main (int argc, char **argv) { int naptime = 0; diff --git a/lib/getopt.c b/lib/getopt.c index 289d137e..6dcdbebf 100644 --- a/lib/getopt.c +++ b/lib/getopt.c @@ -2,24 +2,26 @@ NOTE: getopt is now part of the C library, so if you don't know what "Keep this file name-space clean" means, talk to drepper@gnu.org before changing it! - Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002 - Free Software Foundation, Inc. + + Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, + 1996, 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, + Inc. + This file is part of the GNU C Library. - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. - The GNU C Library is distributed in the hope that it will be useful, + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. Ditto for AIX 3.2 and <stdlib.h>. */ @@ -31,14 +33,6 @@ # include <config.h> #endif -#if !defined __STDC__ || !__STDC__ -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -# ifndef const -# define const -# endif -#endif - #include <stdio.h> /* Comment out all this code if we are using the GNU C Library, and are not @@ -69,26 +63,22 @@ # include <unistd.h> #endif /* GNU C library. */ +#include <string.h> + #ifdef VMS # include <unixlib.h> -# if HAVE_STRING_H - 0 -# include <string.h> -# endif #endif -#ifndef _ +#ifdef _LIBC +# include <libintl.h> +#else /* This is for other GNU distributions with internationalized messages. */ -# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC -# include <libintl.h> -# ifndef _ -# define _(msgid) gettext (msgid) -# endif -# else -# define _(msgid) (msgid) -# endif -# if defined _LIBC && defined USE_IN_LIBIO -# include <wchar.h> -# endif +# include "gettext.h" +#endif +#define _(msgid) gettext (msgid) + +#if defined _LIBC && defined USE_IN_LIBIO +# include <wchar.h> #endif #ifndef attribute_hidden @@ -197,20 +187,7 @@ static enum /* Value of POSIXLY_CORRECT environment variable. */ static char *posixly_correct; -#ifdef __GNU_LIBRARY__ -/* We want to avoid inclusion of string.h with non-GNU libraries - because there are many ways it can cause trouble. - On some systems, it contains special magic macros that don't work - in GCC. */ -# include <string.h> -# define my_index strchr -#else - -# if HAVE_STRING_H -# include <string.h> -# else -# include <strings.h> -# endif +#ifndef __GNU_LIBRARY__ /* Avoid depending on library functions or files whose names are inconsistent. */ @@ -219,32 +196,6 @@ static char *posixly_correct; extern char *getenv (); #endif -static char * -my_index (str, chr) - const char *str; - int chr; -{ - while (*str) - { - if (*str == chr) - return (char *) str; - str++; - } - return 0; -} - -/* If using GCC, we can safely declare strlen this way. - If not using GCC, it is ok not to declare it. */ -#ifdef __GNUC__ -/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. - That was relevant to code that was here before. */ -# if (!defined __STDC__ || !__STDC__) && !defined strlen -/* gcc with -traditional declares the built-in strlen to return int, - and has done so at least since version 2.4.5. -- rms. */ -extern int strlen (const char *); -# endif /* not __STDC__ */ -#endif /* __GNUC__ */ - #endif /* not __GNU_LIBRARY__ */ /* Handle permutation of arguments. */ @@ -298,13 +249,8 @@ static int nonoption_flags_len; `first_nonopt' and `last_nonopt' are relocated so that they describe the new indices of the non-options in ARGV after they are moved. */ -#if defined __STDC__ && __STDC__ -static void exchange (char **); -#endif - static void -exchange (argv) - char **argv; +exchange (char **argv) { int bottom = first_nonopt; int middle = last_nonopt; @@ -384,14 +330,8 @@ exchange (argv) /* Initialize the internal data when the first call is made. */ -#if defined __STDC__ && __STDC__ -static const char *_getopt_initialize (int, char *const *, const char *); -#endif static const char * -_getopt_initialize (argc, argv, optstring) - int argc; - char *const *argv; - const char *optstring; +_getopt_initialize (int argc, char *const *argv, const char *optstring) { /* Start processing options with ARGV-element 1 (since ARGV-element 0 is the program name); the sequence of previously skipped @@ -510,13 +450,9 @@ _getopt_initialize (argc, argv, optstring) long-named options. */ int -_getopt_internal (argc, argv, optstring, longopts, longind, long_only) - int argc; - char *const *argv; - const char *optstring; - const struct option *longopts; - int *longind; - int long_only; +_getopt_internal (int argc, char *const *argv, + const char *optstring, const struct option *longopts, + int *longind, int long_only) { int print_errors = opterr; if (optstring[0] == ':') @@ -641,7 +577,8 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) if (longopts != NULL && (argv[optind][1] == '-' - || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) + || (long_only + && (argv[optind][2] || !strchr (optstring, argv[optind][1]))))) { char *nameend; const struct option *p; @@ -826,7 +763,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) option, then it's an error. Otherwise interpret it as a short option. */ if (!long_only || argv[optind][1] == '-' - || my_index (optstring, *nextchar) == NULL) + || strchr (optstring, *nextchar) == NULL) { if (print_errors) { @@ -881,7 +818,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) { char c = *nextchar++; - char *temp = my_index (optstring, c); + char *temp = strchr (optstring, c); /* Increment `optind' when we start to process its last character. */ if (*nextchar == '\0') @@ -1191,10 +1128,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) } int -getopt (argc, argv, optstring) - int argc; - char *const *argv; - const char *optstring; +getopt (int argc, char *const *argv, const char *optstring) { return _getopt_internal (argc, argv, optstring, (const struct option *) 0, @@ -1210,9 +1144,7 @@ getopt (argc, argv, optstring) the above definition of `getopt'. */ int -main (argc, argv) - int argc; - char **argv; +main (int argc, char **argv) { int c; int digit_optind = 0; diff --git a/lib/getopt.h b/lib/getopt.h index 4283c35b..5e15191e 100644 --- a/lib/getopt.h +++ b/lib/getopt.h @@ -1,21 +1,23 @@ /* Declarations for getopt. - Copyright (C) 1989-1994, 1996-1999, 2001 Free Software Foundation, Inc. + + Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, + 1999, 2001, 2003 Free Software Foundation, Inc. + This file is part of the GNU C Library. - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. - The GNU C Library is distributed in the hope that it will be useful, + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _GETOPT_H @@ -78,7 +80,7 @@ extern int optopt; The field `has_arg' is: no_argument (or 0) if the option does not take an argument, required_argument (or 1) if the option requires an argument, - optional_argument (or 2) if the option takes an optional argument. + optional_argument (or 2) if the option takes an optional argument. If the field `flag' is not NULL, it points to a variable that is set to the value given in the field `val' when the option is found, but @@ -93,11 +95,7 @@ extern int optopt; struct option { -# if (defined __STDC__ && __STDC__) || defined __cplusplus const char *name; -# else - char *name; -# endif /* has_arg can't be an enum because some compilers complain about type mismatches in all the code that assumes it is an int. */ int has_arg; @@ -137,17 +135,16 @@ struct option arguments to the option '\0'. This behavior is specific to the GNU `getopt'. */ -#if (defined __STDC__ && __STDC__) || defined __cplusplus -# ifdef __GNU_LIBRARY__ +#ifdef __GNU_LIBRARY__ /* Many other libraries have conflicting prototypes for getopt, with differences in the consts, in stdlib.h. To avoid compilation errors, only prototype getopt for the GNU C library. */ extern int getopt (int ___argc, char *const *___argv, const char *__shortopts); -# else /* not __GNU_LIBRARY__ */ +#else /* not __GNU_LIBRARY__ */ extern int getopt (); -# endif /* __GNU_LIBRARY__ */ +#endif /* __GNU_LIBRARY__ */ -# ifndef __need_getopt +#ifndef __need_getopt extern int getopt_long (int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind); @@ -160,16 +157,7 @@ extern int _getopt_internal (int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind, int __long_only); -# endif -#else /* not __STDC__ */ -extern int getopt (); -# ifndef __need_getopt -extern int getopt_long (); -extern int getopt_long_only (); - -extern int _getopt_internal (); -# endif -#endif /* __STDC__ */ +#endif #ifdef __cplusplus } diff --git a/lib/getopt1.c b/lib/getopt1.c index ad06cc7f..3288c726 100644 --- a/lib/getopt1.c +++ b/lib/getopt1.c @@ -1,22 +1,23 @@ /* getopt_long and getopt_long_only entry points for GNU getopt. - Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 - Free Software Foundation, Inc. + + Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, + 1997, 1998, 2003 Free Software Foundation, Inc. + This file is part of the GNU C Library. - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. - The GNU C Library is distributed in the hope that it will be useful, + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H #include <config.h> @@ -28,14 +29,6 @@ # include "getopt.h" #endif -#if !defined __STDC__ || !__STDC__ -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -#ifndef const -#define const -#endif -#endif - #include <stdio.h> /* Comment out all this code if we are using the GNU C Library, and are not @@ -68,12 +61,11 @@ #endif int -getopt_long (argc, argv, options, long_options, opt_index) - int argc; - char *const *argv; - const char *options; - const struct option *long_options; - int *opt_index; +getopt_long (int argc, + char *const *argv, + const char *options, + const struct option *long_options, + int *opt_index) { return _getopt_internal (argc, argv, options, long_options, opt_index, 0); } @@ -84,12 +76,11 @@ getopt_long (argc, argv, options, long_options, opt_index) instead. */ int -getopt_long_only (argc, argv, options, long_options, opt_index) - int argc; - char *const *argv; - const char *options; - const struct option *long_options; - int *opt_index; +getopt_long_only (int argc, + char *const *argv, + const char *options, + const struct option *long_options, + int *opt_index) { return _getopt_internal (argc, argv, options, long_options, opt_index, 1); } @@ -106,9 +97,7 @@ libc_hidden_def (getopt_long_only) #include <stdio.h> int -main (argc, argv) - int argc; - char **argv; +main (int argc, char **argv) { int c; int digit_optind = 0; diff --git a/lib/gettext.h b/lib/gettext.h index 8b262f4c..835732e0 100644 --- a/lib/gettext.h +++ b/lib/gettext.h @@ -1,20 +1,19 @@ /* Convenience header for conditional use of GNU <libintl.h>. Copyright (C) 1995-1998, 2000-2002 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU Library General Public License as published - by the Free Software Foundation; either version 2, or (at your option) + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU Library General Public - License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, - USA. */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _LIBGETTEXT_H #define _LIBGETTEXT_H 1 diff --git a/lib/ls-mntd-fs.m4 b/lib/ls-mntd-fs.m4 index 3ba42a31..c28466bb 100644 --- a/lib/ls-mntd-fs.m4 +++ b/lib/ls-mntd-fs.m4 @@ -1,4 +1,4 @@ -#serial 12 +#serial 14 dnl From Jim Meyering. dnl @@ -10,13 +10,20 @@ dnl AC_DEFUN([jm_LIST_MOUNTED_FILESYSTEMS], [ AC_CHECK_FUNCS(listmntent getmntinfo) -AC_CHECK_HEADERS(mntent.h sys/param.h sys/ucred.h sys/mount.h sys/fs_types.h) +AC_CHECK_HEADERS_ONCE(sys/param.h) + +# We must include grp.h before ucred.h on OSF V4.0, since ucred.h uses +# NGROUPS (as the array dimension for a struct member) without a definition. +AC_CHECK_HEADERS(sys/ucred.h, [], [], [#include <grp.h>]) + +AC_CHECK_HEADERS(mntent.h sys/mount.h sys/fs_types.h) getfsstat_includes="\ $ac_includes_default #if HAVE_SYS_PARAM_H # include <sys/param.h> /* needed by powerpc-apple-darwin1.3.7 */ #endif #if HAVE_SYS_UCRED_H +# include <grp.h> /* needed for definition of NGROUPS */ # include <sys/ucred.h> /* needed by powerpc-apple-darwin1.3.7 */ #endif #if HAVE_SYS_MOUNT_H @@ -63,7 +70,7 @@ yes ac_list_mounted_fs=found AC_DEFINE(MOUNTED_LISTMNTENT, 1, [Define if there is a function named listmntent that can be used to - list all mounted filesystems. (UNICOS)]) + list all mounted filesystems. (UNICOS)]) fi fi @@ -79,8 +86,8 @@ if test -z "$ac_list_mounted_fs"; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_VMOUNT, 1, [Define if there is a function named mntctl that can be used to read - the list of mounted filesystems, and there is a system header file - that declares `struct vmount.' (AIX)]) + the list of mounted filesystems, and there is a system header file + that declares `struct vmount.' (AIX)]) fi fi @@ -117,9 +124,9 @@ if test $ac_cv_func_getmntent = yes; then if test $fu_cv_sys_mounted_getmntent1 = yes; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_GETMNTENT1, 1, - [Define if there is a function named getmntent for reading the list - of mounted filesystems, and that function takes a single argument. - (4.3BSD, SunOS, HP-UX, Dynix, Irix)]) + [Define if there is a function named getmntent for reading the list + of mounted filesystems, and that function takes a single argument. + (4.3BSD, SunOS, HP-UX, Dynix, Irix)]) fi fi @@ -134,8 +141,8 @@ if test $ac_cv_func_getmntent = yes; then if test $fu_cv_sys_mounted_getmntent2 = yes; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_GETMNTENT2, 1, - [Define if there is a function named getmntent for reading the list of - mounted filesystems, and that function takes two arguments. (SVR4)]) + [Define if there is a function named getmntent for reading the list of + mounted filesystems, and that function takes two arguments. (SVR4)]) fi fi @@ -166,7 +173,7 @@ if test -z "$ac_list_mounted_fs"; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_GETFSSTAT, 1, [Define if there is a function named getfsstat for reading the - list of mounted filesystems. (DEC Alpha running OSF/1)]) + list of mounted filesystems. (DEC Alpha running OSF/1)]) fi fi @@ -184,9 +191,9 @@ if test -z "$ac_list_mounted_fs"; then if test $fu_cv_sys_mounted_fread_fstyp = yes; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_FREAD_FSTYP, 1, -[Define if (like SVR2) there is no specific function for reading the - list of mounted filesystems, and your system has these header files: - <sys/fstyp.h> and <sys/statfs.h>. (SVR3)]) + [Define if (like SVR2) there is no specific function for reading the + list of mounted filesystems, and your system has these header files: + <sys/fstyp.h> and <sys/statfs.h>. (SVR3)]) fi fi @@ -204,7 +211,7 @@ if test -z "$ac_list_mounted_fs"; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_GETMNTINFO, 1, [Define if there is a function named getmntinfo for reading the - list of mounted filesystems. (4.4BSD, Darwin)]) + list of mounted filesystems. (4.4BSD, Darwin)]) fi fi @@ -222,7 +229,7 @@ if test -z "$ac_list_mounted_fs"; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_GETMNT, 1, [Define if there is a function named getmnt for reading the list of - mounted filesystems. (Ultrix)]) + mounted filesystems. (Ultrix)]) fi fi @@ -243,7 +250,7 @@ if test -z "$ac_list_mounted_fs"; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_FS_STAT_DEV, 1, [Define if there are functions named next_dev and fs_stat_dev for - reading the list of mounted filesystems. (BeOS)]) + reading the list of mounted filesystems. (BeOS)]) fi fi @@ -259,7 +266,8 @@ if test -z "$ac_list_mounted_fs"; then ac_list_mounted_fs=found AC_DEFINE(MOUNTED_FREAD, 1, [Define if there is no specific function for reading the list of - mounted filesystems. fread will be used to read /etc/mnttab. (SVR2) ]) + mounted filesystems. fread will be used to read /etc/mnttab. + (SVR2) ]) fi fi diff --git a/lib/malloc.c b/lib/malloc.c index 5e7674bd..a43d1692 100644 --- a/lib/malloc.c +++ b/lib/malloc.c @@ -22,14 +22,12 @@ #endif #undef malloc -#include <sys/types.h> - -char *malloc (); +#include <stdlib.h> /* Allocate an N-byte block of memory from the heap. If N is zero, allocate a 1-byte block. */ -char * +void * rpl_malloc (size_t n) { if (n == 0) diff --git a/lib/mountlist.c b/lib/mountlist.c index 1af3cbbd..631be312 100644 --- a/lib/mountlist.c +++ b/lib/mountlist.c @@ -1,5 +1,5 @@ /* mountlist.c -- return a list of mounted filesystems - Copyright (C) 1991, 1992, 1997-2000 Free Software Foundation, Inc. + Copyright (C) 1991, 1992, 1997-2004 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,29 +15,24 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "config.h" +#if HAVE_CONFIG_H +# include <config.h> +#endif #include <stdio.h> #include <sys/types.h> -#include "mountlist.h" +#include <stdlib.h> +#include <string.h> -#ifdef STDC_HEADERS -# include <stdlib.h> -#else -void free (); -#endif -#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) -# include <string.h> -#else -# include <strings.h> +#include "xalloc.h" + +#ifndef SIZE_MAX +# define SIZE_MAX ((size_t) -1) #endif #ifndef strstr char *strstr (); #endif -/* char *xmalloc (); */ -/* char *realloc (); */ -/* char *xstrdup (); */ #include <errno.h> #ifndef errno @@ -56,18 +51,35 @@ extern int errno; # include <sys/param.h> #endif -#if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */ -# include <sys/mount.h> -# include <sys/fs_types.h> +#if defined MOUNTED_GETFSSTAT /* OSF_1 and Darwin1.3.x */ +# if HAVE_SYS_UCRED_H +# include <grp.h> /* needed on OSF V4.0 for definition of NGROUPS, + NGROUPS is used as an array dimension in ucred.h */ +# include <sys/ucred.h> /* needed by powerpc-apple-darwin1.3.7 */ +# endif +# if HAVE_SYS_MOUNT_H +# include <sys/mount.h> +# endif +# if HAVE_SYS_FS_TYPES_H +# include <sys/fs_types.h> /* needed by powerpc-apple-darwin1.3.7 */ +# endif +# if HAVE_STRUCT_FSSTAT_F_FSTYPENAME +# define FS_TYPE(Ent) ((Ent).f_fstypename) +# else +# define FS_TYPE(Ent) mnt_names[(Ent).f_type] +# endif #endif /* MOUNTED_GETFSSTAT */ #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ # include <mntent.h> -# if !defined(MOUNTED) -# if defined(MNT_MNTTAB) /* HP-UX. */ +# if !defined MOUNTED +# if defined _PATH_MOUNTED /* GNU libc */ +# define MOUNTED _PATH_MOUNTED +# endif +# if defined MNT_MNTTAB /* HP-UX. */ # define MOUNTED MNT_MNTTAB # endif -# if defined(MNTTABNAME) /* Dynix. */ +# if defined MNTTABNAME /* Dynix. */ # define MOUNTED MNTTABNAME # endif # endif @@ -121,12 +133,16 @@ extern int errno; # include <sys/mntent.h> #endif -#if defined (MNTOPT_IGNORE) && defined (HAVE_HASMNTOPT) +#undef MNT_IGNORE +#if defined MNTOPT_IGNORE && defined HAVE_HASMNTOPT # define MNT_IGNORE(M) hasmntopt ((M), MNTOPT_IGNORE) #else # define MNT_IGNORE(M) 0 #endif +#include "mountlist.h" +#include "unlocked-io.h" + #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ /* Return the value of the hexadecimal number represented by CP. No prefix (like '0x') or suffix (like 'h') is expected to be @@ -303,21 +319,22 @@ read_filesystem_list (int need_fs_type) remove. Specifically, automount create normal NFS mounts. */ - if(listmntent(&mntlist, KMTAB, NULL, NULL) < 0) + if (listmntent (&mntlist, KMTAB, NULL, NULL) < 0) return NULL; for (p = mntlist; p; p = p->next) { mnt = p->ment; - me = (struct mount_entry*) malloc(sizeof (struct mount_entry)); - me->me_devname = strdup(mnt->mnt_fsname); - me->me_mountdir = strdup(mnt->mnt_dir); - me->me_type = strdup(mnt->mnt_type); + me = xmalloc (sizeof *me); + me->me_devname = xstrdup (mnt->mnt_fsname); + me->me_mountdir = xstrdup (mnt->mnt_dir); + me->me_type = xstrdup (mnt->mnt_type); + me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = -1; *mtail = me; mtail = &me->me_next; } - freemntlist(mntlist); + freemntlist (mntlist); } #endif @@ -334,10 +351,11 @@ read_filesystem_list (int need_fs_type) while ((mnt = getmntent (fp))) { - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); - me->me_devname = strdup (mnt->mnt_fsname); - me->me_mountdir = strdup (mnt->mnt_dir); - me->me_type = strdup (mnt->mnt_type); + me = xmalloc (sizeof *me); + me->me_devname = xstrdup (mnt->mnt_fsname); + me->me_mountdir = xstrdup (mnt->mnt_dir); + me->me_type = xstrdup (mnt->mnt_type); + me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); devopt = strstr (mnt->mnt_opts, "dev="); @@ -373,10 +391,11 @@ read_filesystem_list (int need_fs_type) { char *fs_type = fsp_to_string (fsp); - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); - me->me_devname = strdup (fsp->f_mntfromname); - me->me_mountdir = strdup (fsp->f_mntonname); + me = xmalloc (sizeof *me); + me->me_devname = xstrdup (fsp->f_mntfromname); + me->me_mountdir = xstrdup (fsp->f_mntonname); me->me_type = fs_type; + me->me_type_malloced = 0; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ @@ -398,10 +417,11 @@ read_filesystem_list (int need_fs_type) 0 < (val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY, (char *) 0))) { - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); - me->me_devname = strdup (fsd.fd_req.devname); - me->me_mountdir = strdup (fsd.fd_req.path); + me = xmalloc (sizeof *me); + me->me_devname = xstrdup (fsd.fd_req.devname); + me->me_mountdir = xstrdup (fsd.fd_req.path); me->me_type = gt_names[fsd.fd_req.fstype]; + me->me_type_malloced = 0; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = fsd.fd_req.dev; @@ -415,7 +435,7 @@ read_filesystem_list (int need_fs_type) } #endif /* MOUNTED_GETMNT. */ -#if defined (MOUNTED_FS_STAT_DEV) /* BeOS */ +#if defined MOUNTED_FS_STAT_DEV /* BeOS */ { /* The next_dev() and fs_stat_dev() system calls give the list of all filesystems, including the information returned by statvfs() @@ -458,19 +478,17 @@ read_filesystem_list (int need_fs_type) continue; if (strcmp (d->d_name, ".") == 0) - name = strdup ("/"); + name = xstrdup ("/"); else { - name = malloc (1 + strlen (d->d_name) + 1); + name = xmalloc (1 + strlen (d->d_name) + 1); name[0] = '/'; strcpy (name + 1, d->d_name); } if (lstat (name, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode)) { - struct rootdir_entry *re; - - re = (struct rootdir_entry *) malloc (sizeof (struct rootdir_entry)); + struct rootdir_entry *re = xmalloc (sizeof *re); re->name = name; re->dev = statbuf.st_dev; re->ino = statbuf.st_ino; @@ -496,10 +514,11 @@ read_filesystem_list (int need_fs_type) if (re->dev == fi.dev && re->ino == fi.root) break; - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); - me->me_devname = strdup (fi.device_name[0] != '\0' ? fi.device_name : fi.fsh_name); - me->me_mountdir = strdup (re != NULL ? re->name : fi.fsh_name); - me->me_type = strdup (fi.fsh_name); + me = xmalloc (sizeof *me); + me->me_devname = xstrdup (fi.device_name[0] != '\0' ? fi.device_name : fi.fsh_name); + me->me_mountdir = xstrdup (re != NULL ? re->name : fi.fsh_name); + me->me_type = xstrdup (fi.fsh_name); + me->me_type_malloced = 1; me->me_dev = fi.dev; me->me_dummy = 0; me->me_remote = (fi.flags & B_FS_IS_SHARED) != 0; @@ -520,18 +539,21 @@ read_filesystem_list (int need_fs_type) } #endif /* MOUNTED_FS_STAT_DEV */ -#if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */ +#if defined MOUNTED_GETFSSTAT /* __alpha running OSF_1 */ { - int numsys, counter, bufsize; + int numsys, counter; + size_t bufsize; struct statfs *stats; - numsys = getfsstat ((struct statfs *)0, 0L, MNT_WAIT); + numsys = getfsstat ((struct statfs *)0, 0L, MNT_NOWAIT); if (numsys < 0) return (NULL); + if (SIZE_MAX / sizeof *stats <= numsys) + xalloc_die (); - bufsize = (1 + numsys) * sizeof (struct statfs); - stats = (struct statfs *)malloc (bufsize); - numsys = getfsstat (stats, bufsize, MNT_WAIT); + bufsize = (1 + numsys) * sizeof *stats; + stats = xmalloc (bufsize); + numsys = getfsstat (stats, bufsize, MNT_NOWAIT); if (numsys < 0) { @@ -541,10 +563,11 @@ read_filesystem_list (int need_fs_type) for (counter = 0; counter < numsys; counter++) { - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); - me->me_devname = strdup (stats[counter].f_mntfromname); - me->me_mountdir = strdup (stats[counter].f_mntonname); - me->me_type = mnt_names[stats[counter].f_type]; + me = xmalloc (sizeof *me); + me->me_devname = xstrdup (stats[counter].f_mntfromname); + me->me_mountdir = xstrdup (stats[counter].f_mntonname); + me->me_type = xstrdup (FS_TYPE (stats[counter])); + me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ @@ -558,7 +581,7 @@ read_filesystem_list (int need_fs_type) } #endif /* MOUNTED_GETFSSTAT */ -#if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */ +#if defined MOUNTED_FREAD || defined MOUNTED_FREAD_FSTYP /* SVR[23]. */ { struct mnttab mnt; char *table = "/etc/mnttab"; @@ -570,17 +593,18 @@ read_filesystem_list (int need_fs_type) while (fread (&mnt, sizeof mnt, 1, fp) > 0) { - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); + me = xmalloc (sizeof *me); # ifdef GETFSTYP /* SVR3. */ - me->me_devname = strdup (mnt.mt_dev); + me->me_devname = xstrdup (mnt.mt_dev); # else - me->me_devname = malloc (strlen (mnt.mt_dev) + 6); + me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6); strcpy (me->me_devname, "/dev/"); strcpy (me->me_devname + 5, mnt.mt_dev); # endif - me->me_mountdir = strdup (mnt.mt_filsys); + me->me_mountdir = xstrdup (mnt.mt_filsys); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ me->me_type = ""; + me->me_type_malloced = 0; # ifdef GETFSTYP /* SVR3. */ if (need_fs_type) { @@ -589,7 +613,10 @@ read_filesystem_list (int need_fs_type) if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1) - me->me_type = strdup (typebuf); + { + me->me_type = xstrdup (typebuf); + me->me_type_malloced = 1; + } } # endif me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); @@ -602,6 +629,7 @@ read_filesystem_list (int need_fs_type) if (ferror (fp)) { + /* The last fread() call must have failed. */ int saved_errno = errno; fclose (fp); errno = saved_errno; @@ -615,13 +643,14 @@ read_filesystem_list (int need_fs_type) #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */ { - struct mntent **mnttbl=getmnttbl(),**ent; + struct mntent **mnttbl = getmnttbl (), **ent; for (ent=mnttbl;*ent;ent++) { - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); - me->me_devname = strdup ( (*ent)->mt_resource); - me->me_mountdir = strdup( (*ent)->mt_directory); - me->me_type = strdup ((*ent)->mt_fstype); + me = xmalloc (sizeof *me); + me->me_devname = xstrdup ( (*ent)->mt_resource); + me->me_mountdir = xstrdup ( (*ent)->mt_directory); + me->me_type = xstrdup ((*ent)->mt_fstype); + me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ @@ -630,7 +659,7 @@ read_filesystem_list (int need_fs_type) *mtail = me; mtail = &me->me_next; } - endmnttbl(); + endmnttbl (); } #endif @@ -679,10 +708,11 @@ read_filesystem_list (int need_fs_type) { while ((ret = getmntent (fp, &mnt)) == 0) { - me = (struct mount_entry *) malloc (sizeof (struct mount_entry)); - me->me_devname = strdup (mnt.mnt_special); - me->me_mountdir = strdup (mnt.mnt_mountp); - me->me_type = strdup (mnt.mnt_fstype); + me = xmalloc (sizeof *me); + me->me_devname = xstrdup (mnt.mnt_special); + me->me_mountdir = xstrdup (mnt.mnt_mountp); + me->me_type = xstrdup (mnt.mnt_fstype); + me->me_type_malloced = 1; me->me_dummy = MNT_IGNORE (&mnt) != 0; me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ @@ -711,21 +741,32 @@ read_filesystem_list (int need_fs_type) int bufsize; char *entries, *thisent; struct vmount *vmp; + int n_entries; + int i; /* Ask how many bytes to allocate for the mounted filesystem info. */ - mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize); - entries = malloc (bufsize); + if (mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize) != 0) + return NULL; + entries = xmalloc (bufsize); /* Get the list of mounted filesystems. */ - mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries); + n_entries = mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries); + if (n_entries < 0) + { + int saved_errno = errno; + free (entries); + errno = saved_errno; + return NULL; + } - for (thisent = entries; thisent < entries + bufsize; - thisent += vmp->vmt_length) + for (i = 0, thisent = entries; + i < n_entries; + i++, thisent += vmp->vmt_length) { char *options, *ignore; vmp = (struct vmount *) thisent; - me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry)); + me = xmalloc (sizeof *me); if (vmp->vmt_flags & MNT_REMOTE) { char *host, *path; @@ -734,7 +775,7 @@ read_filesystem_list (int need_fs_type) /* Prepend the remote pathname. */ host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off; path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off; - me->me_devname = malloc (strlen (host) + strlen (path) + 2); + me->me_devname = xmalloc (strlen (host) + strlen (path) + 2); strcpy (me->me_devname, host); strcat (me->me_devname, ":"); strcat (me->me_devname, path); @@ -742,11 +783,12 @@ read_filesystem_list (int need_fs_type) else { me->me_remote = 0; - me->me_devname = strdup (thisent + + me->me_devname = xstrdup (thisent + vmp->vmt_data[VMT_OBJECT].vmt_off); } - me->me_mountdir = strdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off); - me->me_type = strdup (fstype_to_string (vmp->vmt_gfstype)); + me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off); + me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype)); + me->me_type_malloced = 1; options = thisent + vmp->vmt_data[VMT_ARGS].vmt_off; ignore = strstr (options, "ignore"); me->me_dummy = (ignore @@ -777,7 +819,8 @@ read_filesystem_list (int need_fs_type) me = mount_list->me_next; free (mount_list->me_devname); free (mount_list->me_mountdir); - /* FIXME: me_type is not always malloced. */ + if (mount_list->me_type_malloced) + free (mount_list->me_type); free (mount_list); mount_list = me; } diff --git a/lib/mountlist.h b/lib/mountlist.h index c41490c9..9e550a08 100644 --- a/lib/mountlist.h +++ b/lib/mountlist.h @@ -1,5 +1,7 @@ /* mountlist.h -- declarations for list of mounted filesystems - Copyright (C) 1991, 1992, 1998, 2000 Free Software Foundation, Inc. + + Copyright (C) 1991, 1992, 1998, 2000, 2001, 2002, 2003 Free + Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,27 +26,28 @@ struct mount_entry dev_t me_dev; /* Device number of me_mountdir. */ unsigned int me_dummy : 1; /* Nonzero for dummy filesystems. */ unsigned int me_remote : 1; /* Nonzero for remote fileystems. */ + unsigned int me_type_malloced : 1; /* Nonzero if me_type was malloced. */ struct mount_entry *me_next; }; -#ifndef PARAMS -# if defined PROTOTYPES || (defined __STDC__ && __STDC__) -# define PARAMS(Args) Args -# else -# define PARAMS(Args) () -# endif -#endif - -struct mount_entry *read_filesystem_list PARAMS ((int need_fs_type)); +struct mount_entry *read_filesystem_list (int need_fs_type); #ifndef ME_DUMMY -# define ME_DUMMY(fs_name, fs_type) \ - (!strcmp (fs_type, "auto") \ - || !strcmp (fs_type, "autofs") \ +# define ME_DUMMY(Fs_name, Fs_type) \ + (!strcmp (Fs_type, "autofs") \ /* for Irix 6.5 */ \ - || !strcmp (fs_type, "ignore")) + || !strcmp (Fs_type, "ignore")) #endif +#undef STREQ +#define STREQ(a, b) (strcmp ((a), (b)) == 0) + #ifndef ME_REMOTE -# define ME_REMOTE(fs_name, fs_type) (strchr (fs_name, ':') != 0) +/* A file system is `remote' if its Fs_name contains a `:' + or if (it is of type smbfs and its Fs_name starts with `//'). */ +# define ME_REMOTE(Fs_name, Fs_type) \ + (strchr ((Fs_name), ':') != 0 \ + || ((Fs_name)[0] == '/' \ + && (Fs_name)[1] == '/' \ + && STREQ (Fs_type, "smbfs"))) #endif diff --git a/lib/onceonly.m4 b/lib/onceonly.m4 new file mode 100644 index 00000000..50b33f48 --- /dev/null +++ b/lib/onceonly.m4 @@ -0,0 +1,63 @@ +# onceonly.m4 serial 3 (gettext-0.12) +dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc. +dnl This file is free software, distributed under the terms of the GNU +dnl General Public License. As a special exception to the GNU General +dnl Public License, this file may be distributed as part of a program +dnl that contains a configuration script generated by Autoconf, under +dnl the same distribution terms as the rest of that program. + +dnl This file defines some "once only" variants of standard autoconf macros. +dnl AC_CHECK_HEADERS_ONCE like AC_CHECK_HEADERS +dnl AC_CHECK_FUNCS_ONCE like AC_CHECK_FUNCS +dnl AC_CHECK_DECLS_ONCE like AC_CHECK_DECLS +dnl AC_REQUIRE([AC_HEADER_STDC]) like AC_HEADER_STDC +dnl The advantage is that the check for each of the headers/functions/decls +dnl will be put only once into the 'configure' file. It keeps the size of +dnl the 'configure' file down, and avoids redundant output when 'configure' +dnl is run. +dnl The drawback is that the checks cannot be conditionalized. If you write +dnl if some_condition; then gl_CHECK_HEADERS(stdlib.h); fi +dnl inside an AC_DEFUNed function, the gl_CHECK_HEADERS macro call expands to +dnl empty, and the check will be inserted before the body of the AC_DEFUNed +dnl function. + +dnl Autoconf version 2.57 or newer is recommended. +AC_PREREQ(2.54) + +# AC_CHECK_HEADERS_ONCE(HEADER1 HEADER2 ...) is a once-only variant of +# AC_CHECK_HEADERS(HEADER1 HEADER2 ...). +AC_DEFUN([AC_CHECK_HEADERS_ONCE], [ + : + AC_FOREACH([gl_HEADER_NAME], [$1], [ + AC_DEFUN([gl_CHECK_HEADER_]m4_quote(translit(m4_defn([gl_HEADER_NAME]), + [-./], [___])), [ + AC_CHECK_HEADERS(gl_HEADER_NAME) + ]) + AC_REQUIRE([gl_CHECK_HEADER_]m4_quote(translit(gl_HEADER_NAME, + [-./], [___]))) + ]) +]) + +# AC_CHECK_FUNCS_ONCE(FUNC1 FUNC2 ...) is a once-only variant of +# AC_CHECK_FUNCS(FUNC1 FUNC2 ...). +AC_DEFUN([AC_CHECK_FUNCS_ONCE], [ + : + AC_FOREACH([gl_FUNC_NAME], [$1], [ + AC_DEFUN([gl_CHECK_FUNC_]m4_defn([gl_FUNC_NAME]), [ + AC_CHECK_FUNCS(m4_defn([gl_FUNC_NAME])) + ]) + AC_REQUIRE([gl_CHECK_FUNC_]m4_defn([gl_FUNC_NAME])) + ]) +]) + +# AC_CHECK_DECLS_ONCE(DECL1 DECL2 ...) is a once-only variant of +# AC_CHECK_DECLS(DECL1, DECL2, ...). +AC_DEFUN([AC_CHECK_DECLS_ONCE], [ + : + AC_FOREACH([gl_DECL_NAME], [$1], [ + AC_DEFUN([gl_CHECK_DECL_]m4_defn([gl_DECL_NAME]), [ + AC_CHECK_DECLS(m4_defn([gl_DECL_NAME])) + ]) + AC_REQUIRE([gl_CHECK_DECL_]m4_defn([gl_DECL_NAME])) + ]) +]) diff --git a/lib/realloc.c b/lib/realloc.c index d0d3e4ab..ccbf9913 100644 --- a/lib/realloc.c +++ b/lib/realloc.c @@ -1,5 +1,5 @@ /* Work around bug on some systems where realloc (NULL, 0) fails. - Copyright (C) 1997 Free Software Foundation, Inc. + Copyright (C) 1997, 2003 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,19 +22,14 @@ #endif #undef realloc -#include <sys/types.h> - -char *malloc (); -char *realloc (); +#include <stdlib.h> /* Change the size of an allocated block of memory P to N bytes, with error checking. If N is zero, change it to 1. If P is NULL, use malloc. */ -char * -rpl_realloc (p, n) - char *p; - size_t n; +void * +rpl_realloc (void *p, size_t n) { if (n == 0) n = 1; diff --git a/lib/snprintf.c b/lib/snprintf.c index 3de2a96f..633517de 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -1,4 +1,8 @@ /* + * NOTE: If you change this file, please merge it into rsync, samba, etc. + */ + +/* * Copyright Patrick Powell 1995 * This code is based on code written by Patrick Powell (papowell@astart.com) * It may be used for any purpose as long as this notice remains intact @@ -53,17 +57,57 @@ * got rid of fcvt code (twas buggy and made testing harder) * added C99 semantics * + * date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0 + * actually print args for %g and %e + * + * date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0 + * Since includes.h isn't included here, VA_COPY has to be defined here. I don't + * see any include file that is guaranteed to be here, so I'm defining it + * locally. Fixes AIX and Solaris builds. + * + * date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13 + * put the ifdef for HAVE_VA_COPY in one place rather than in lots of + * functions + * + * date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4 + * Fix usage of va_list passed as an arg. Use __va_copy before using it + * when it exists. + * + * date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14 + * Fix incorrect zpadlen handling in fmtfp. + * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it. + * few mods to make it easier to compile the tests. + * addedd the "Ollie" test to the floating point ones. + * + * Martin Pool (mbp@samba.org) April 2003 + * Remove NO_CONFIG_H so that the test case can be built within a source + * tree with less trouble. + * Remove unnecessary SAFE_FREE() definition. + * + * Martin Pool (mbp@samba.org) May 2003 + * Put in a prototype for dummy_snprintf() to quiet compiler warnings. + * + * Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even + * if the C library has some snprintf functions already. **************************************************************/ -#ifndef NO_CONFIG_H /* for some tests */ +#ifndef NO_CONFIG_H #include "config.h" #else #define NULL 0 -#endif +#endif #ifdef TEST_SNPRINTF /* need math library headers for testing */ -#include <math.h> -#endif + +/* In test mode, we pretend that this system doesn't have any snprintf + * functions, regardless of what config.h says. */ +# undef HAVE_SNPRINTF +# undef HAVE_VSNPRINTF +# undef HAVE_C99_VSNPRINTF +# undef HAVE_ASPRINTF +# undef HAVE_VASPRINTF +# include <math.h> +#endif /* TEST_SNPRINTF */ #ifdef HAVE_STRING_H #include <string.h> @@ -75,12 +119,8 @@ #ifdef HAVE_CTYPE_H #include <ctype.h> #endif -#ifdef HAVE_SYS_TYPES_H #include <sys/types.h> -#endif -#ifdef HAVE_STDARG_H #include <stdarg.h> -#endif #ifdef HAVE_STDLIB_H #include <stdlib.h> #endif @@ -89,8 +129,9 @@ /* only include stdio.h if we are not re-defining snprintf or vsnprintf */ #include <stdio.h> /* make the compiler happy with an empty file */ + void dummy_snprintf(void); void dummy_snprintf(void) {} -#else +#endif /* HAVE_SNPRINTF, etc */ #ifdef HAVE_LONG_DOUBLE #define LDOUBLE long double @@ -104,29 +145,17 @@ #define LLONG long #endif -/* free memory if the pointer is valid and zero the pointer */ -#ifndef SAFE_FREE -#define SAFE_FREE(x) do { if ((x) != NULL) {free((x)); (x)=NULL;} } while(0) -#endif - #ifndef VA_COPY #ifdef HAVE_VA_COPY +#define VA_COPY(dest, src) va_copy(dest, src) +#else +#ifdef HAVE___VA_COPY #define VA_COPY(dest, src) __va_copy(dest, src) #else #define VA_COPY(dest, src) (dest) = (src) #endif #endif -static size_t dopr(char *buffer, size_t maxlen, const char *format, - va_list args_in); -static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, - char *value, int flags, int min, int max); -static void fmtint(char *buffer, size_t *currlen, size_t maxlen, - long value, int base, int min, int max, int flags); -static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, - LDOUBLE fvalue, int min, int max, int flags); -static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); - /* * dopr(): poor man's version of doprintf */ @@ -161,6 +190,19 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); #define MAX(p,q) (((p) >= (q)) ? (p) : (q)) #endif +/* yes this really must be a ||. Don't muck with this (tridge) */ +#if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF) + +static size_t dopr(char *buffer, size_t maxlen, const char *format, + va_list args_in); +static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, + char *value, int flags, int min, int max); +static void fmtint(char *buffer, size_t *currlen, size_t maxlen, + long value, int base, int min, int max, int flags); +static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, + LDOUBLE fvalue, int min, int max, int flags); +static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); + static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in) { char ch; @@ -345,6 +387,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args fvalue = va_arg (args, LDOUBLE); else fvalue = va_arg (args, double); + fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); break; case 'G': flags |= DP_F_UP; @@ -353,6 +396,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args fvalue = va_arg (args, LDOUBLE); else fvalue = va_arg (args, double); + fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); break; case 'c': dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); @@ -625,7 +669,7 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, int padlen = 0; /* amount to pad */ int zpadlen = 0; int caps = 0; - int index; + int idx; double intpart; double fracpart; double temp; @@ -684,11 +728,11 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, do { temp = intpart*0.1; my_modf(temp, &intpart); - index = (int) ((temp -intpart +0.05)* 10.0); - /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ - /* printf ("%llf, %f, %x\n", temp, intpart, index); */ + idx = (int) ((temp -intpart +0.05)* 10.0); + /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ + /* printf ("%llf, %f, %x\n", temp, intpart, idx); */ iconvert[iplace++] = - (caps? "0123456789ABCDEF":"0123456789abcdef")[index]; + (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; } while (intpart && (iplace < 311)); if (iplace == 311) iplace--; iconvert[iplace] = 0; @@ -699,11 +743,11 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, do { temp = fracpart*0.1; my_modf(temp, &fracpart); - index = (int) ((temp -fracpart +0.05)* 10.0); - /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */ - /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */ + idx = (int) ((temp -fracpart +0.05)* 10.0); + /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */ + /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */ fconvert[fplace++] = - (caps? "0123456789ABCDEF":"0123456789abcdef")[index]; + (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; } while(fracpart && (fplace < 311)); if (fplace == 311) fplace--; } @@ -773,24 +817,21 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) (*currlen)++; } -/* yes this really must be a ||. Don't muck with this (tridge) */ -#if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF) - int vsnprintf (char *str, size_t count, const char *fmt, va_list args) + int smb_vsnprintf (char *str, size_t count, const char *fmt, va_list args) { return dopr(str, count, fmt, args); } +#define vsnprintf smb_vsnprintf #endif -/* yes this really must be a ||. Don't muck wiith this (tridge) +/* yes this really must be a ||. Don't muck with this (tridge) * * The logic for these two is that we need our own definition if the * OS *either* has no definition of *sprintf, or if it does have one - * that doesn't work properly according to the autoconf test. Perhaps - * these should really be smb_snprintf to avoid conflicts with buggy - * linkers? -- mbp + * that doesn't work properly according to the autoconf test. */ -#if !defined(HAVE_SNPRINTF) /* || !defined(HAVE_C99_SNPRINTF) */ - int snprintf(char *str,size_t count,const char *fmt,...) +#if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF) +int smb_snprintf(char *str,size_t count,const char *fmt,...) { size_t ret; va_list ap; @@ -800,6 +841,7 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) va_end(ap); return ret; } +#define snprintf smb_snprintf #endif #endif @@ -868,8 +910,9 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) "-16.16f", NULL }; - double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, - 0.9996, 1.996, 4.136, 5.030201, 0}; + double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996, + 0.9996, 1.996, 4.136, 5.030201, 0.00205, + /* END LIST */ 0}; char *int_fmt[] = { "%-1.5d", "%1.5d", @@ -977,4 +1020,4 @@ static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) return 0; } -#endif /* SNPRINTF_TEST */ +#endif /* TEST_SNPRINTF */ diff --git a/lib/strtod.c b/lib/strtod.c index 10edb5cf..161f97a7 100644 --- a/lib/strtod.c +++ b/lib/strtod.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991, 1992, 1997, 1999 Free Software Foundation, Inc. +/* Copyright (C) 1991, 1992, 1997, 1999, 2003 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -37,22 +37,9 @@ extern int errno; #include <math.h> -#if HAVE_FLOAT_H -# include <float.h> -#else -# define DBL_MAX 1.7976931348623159e+308 -# define DBL_MIN 2.2250738585072010e-308 -#endif - -#if STDC_HEADERS -# include <stdlib.h> -# include <string.h> -#else -# define NULL 0 -# ifndef HUGE_VAL -# define HUGE_VAL HUGE -# endif -#endif +#include <float.h> +#include <stdlib.h> +#include <string.h> /* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the character after the last one used in the number is put in *ENDPTR. */ diff --git a/lib/unlocked-io.h b/lib/unlocked-io.h index a7240fb9..36a7a488 100644 --- a/lib/unlocked-io.h +++ b/lib/unlocked-io.h @@ -1,21 +1,20 @@ /* Prefer faster, non-thread-safe stdio functions if available. - Copyright (C) 2001, 2002 Free Software Foundation, Inc. + Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU Library General Public License as published - by the Free Software Foundation; either version 2, or (at your option) + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU Library General Public - License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, - USA. */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Written by Jim Meyering. */ @@ -28,63 +27,106 @@ # if USE_UNLOCKED_IO -/* These are wrappers for functions/macros from GNU libc. +/* These are wrappers for functions/macros from the GNU C library, and + from other C libraries supporting POSIX's optional thread-safe functions. + The standard I/O functions are thread-safe. These *_unlocked ones are more efficient but not thread-safe. That they're not thread-safe is - fine since all of the applications in this package are single threaded. */ + fine since all of the applications in this package are single threaded. + + Also, some code that is shared with the GNU C library may invoke + the *_unlocked functions directly. On hosts that lack those + functions, invoke the non-thread-safe versions instead. */ + +# include <stdio.h> # if HAVE_DECL_CLEARERR_UNLOCKED # undef clearerr # define clearerr(x) clearerr_unlocked (x) +# else +# define clearerr_unlocked(x) clearerr (x) # endif # if HAVE_DECL_FEOF_UNLOCKED # undef feof # define feof(x) feof_unlocked (x) +# else +# define feof_unlocked(x) feof (x) # endif # if HAVE_DECL_FERROR_UNLOCKED # undef ferror # define ferror(x) ferror_unlocked (x) +# else +# define ferror_unlocked(x) ferror (x) # endif # if HAVE_DECL_FFLUSH_UNLOCKED # undef fflush # define fflush(x) fflush_unlocked (x) +# else +# define fflush_unlocked(x) fflush (x) # endif # if HAVE_DECL_FGETS_UNLOCKED # undef fgets # define fgets(x,y,z) fgets_unlocked (x,y,z) +# else +# define fgets_unlocked(x,y,z) fgets (x,y,z) # endif # if HAVE_DECL_FPUTC_UNLOCKED # undef fputc # define fputc(x,y) fputc_unlocked (x,y) +# else +# define fputc_unlocked(x,y) fputc (x,y) # endif # if HAVE_DECL_FPUTS_UNLOCKED # undef fputs # define fputs(x,y) fputs_unlocked (x,y) +# else +# define fputs_unlocked(x,y) fputs (x,y) # endif # if HAVE_DECL_FREAD_UNLOCKED # undef fread # define fread(w,x,y,z) fread_unlocked (w,x,y,z) +# else +# define fread_unlocked(w,x,y,z) fread (w,x,y,z) # endif # if HAVE_DECL_FWRITE_UNLOCKED # undef fwrite # define fwrite(w,x,y,z) fwrite_unlocked (w,x,y,z) +# else +# define fwrite_unlocked(w,x,y,z) fwrite (w,x,y,z) # endif # if HAVE_DECL_GETC_UNLOCKED # undef getc # define getc(x) getc_unlocked (x) +# else +# define getc_unlocked(x) getc (x) # endif # if HAVE_DECL_GETCHAR_UNLOCKED # undef getchar # define getchar() getchar_unlocked () +# else +# define getchar_unlocked() getchar () # endif # if HAVE_DECL_PUTC_UNLOCKED # undef putc # define putc(x,y) putc_unlocked (x,y) +# else +# define putc_unlocked(x,y) putc (x,y) # endif # if HAVE_DECL_PUTCHAR_UNLOCKED # undef putchar # define putchar(x) putchar_unlocked (x) +# else +# define putchar_unlocked(x) putchar (x) # endif +# undef flockfile +# define flockfile(x) ((void) 0) + +# undef ftrylockfile +# define ftrylockfile(x) 0 + +# undef funlockfile +# define funlockfile(x) ((void) 0) + # endif /* USE_UNLOCKED_IO */ #endif /* UNLOCKED_IO_H */ diff --git a/lib/unlocked-io.m4 b/lib/unlocked-io.m4 new file mode 100644 index 00000000..f8e98f1d --- /dev/null +++ b/lib/unlocked-io.m4 @@ -0,0 +1,22 @@ +#serial 8 -*- autoconf -*- + +dnl From Jim Meyering. +dnl +dnl See if the glibc *_unlocked I/O macros or functions are available. +dnl Use only those *_unlocked macros or functions that are declared +dnl (because some of them were declared in Solaris 2.5.1 but were removed +dnl in Solaris 2.6, whereas we want binaries built on Solaris 2.5.1 to run +dnl on Solaris 2.6). + +AC_DEFUN([jm_FUNC_GLIBC_UNLOCKED_IO], +[ + dnl Persuade glibc and Solaris <stdio.h> to declare + dnl fgets_unlocked(), fputs_unlocked() etc. + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + + AC_CHECK_DECLS_ONCE( + [clearerr_unlocked feof_unlocked ferror_unlocked + fflush_unlocked fgets_unlocked fputc_unlocked fputs_unlocked + fread_unlocked fwrite_unlocked getc_unlocked + getchar_unlocked putc_unlocked putchar_unlocked]) +]) diff --git a/lib/xalloc.h b/lib/xalloc.h index 098a6c2e..4b658581 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -1,5 +1,7 @@ /* xalloc.h -- malloc with out-of-memory checking - Copyright (C) 1990-1998, 1999, 2000 Free Software Foundation, Inc. + + Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, + 1999, 2000, 2003 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,13 +20,7 @@ #ifndef XALLOC_H_ # define XALLOC_H_ -# ifndef PARAMS -# if defined PROTOTYPES || (defined __STDC__ && __STDC__) -# define PARAMS(Args) Args -# else -# define PARAMS(Args) () -# endif -# endif +# include <stddef.h> # ifndef __attribute__ # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ @@ -36,14 +32,9 @@ # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) # endif -/* Exit value when the requested amount of memory is not available. - It is initialized to EXIT_FAILURE, but the caller may set it to - some other value. */ -extern int xalloc_exit_failure; - /* If this pointer is non-zero, run the specified function upon each allocation failure. It is initialized to zero. */ -extern void (*xalloc_fail_func) PARAMS ((void)); +extern void (*xalloc_fail_func) (void); /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this message is output. It is translated via gettext. @@ -51,37 +42,46 @@ extern void (*xalloc_fail_func) PARAMS ((void)); extern char const xalloc_msg_memory_exhausted[]; /* This function is always triggered when memory is exhausted. It is - in charge of honoring the three previous items. This is the + in charge of honoring the two previous items. It exits with status + exit_failure (defined in exitfail.h). This is the function to call when one wants the program to die because of a memory allocation failure. */ -extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN; - -void *xmalloc PARAMS ((size_t n)); -void *xcalloc PARAMS ((size_t n, size_t s)); -void *xrealloc PARAMS ((void *p, size_t n)); -char *xstrdup PARAMS ((const char *str)); - -# define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items))) -# define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items))) -# define XREALLOC(Ptr, Type, N_items) \ - ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items))) - -/* Declare and alloc memory for VAR of type TYPE. */ -# define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) - -/* Free VAR only if non NULL. */ -# define XFREE(Var) \ - do { \ - if (Var) \ - free (Var); \ - } while (0) - -/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ -# define CCLONE(Src, Num) \ - (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num))) - -/* Return a malloc'ed copy of SRC. */ -# define CLONE(Src) CCLONE (Src, 1) - +extern void xalloc_die (void) ATTRIBUTE_NORETURN; + +void *xmalloc (size_t s); +void *xnmalloc (size_t n, size_t s); +void *xzalloc (size_t s); +void *xcalloc (size_t n, size_t s); +void *xrealloc (void *p, size_t s); +void *xnrealloc (void *p, size_t n, size_t s); +void *x2realloc (void *p, size_t *pn); +void *x2nrealloc (void *p, size_t *pn, size_t s); +void *xclone (void const *p, size_t s); +char *xstrdup (const char *str); + +/* Return 1 if an array of N objects, each of size S, cannot exist due + to size arithmetic overflow. S must be positive and N must be + nonnegative. This is a macro, not an inline function, so that it + works correctly even when SIZE_MAX < N. + + By gnulib convention, SIZE_MAX represents overflow in size + calculations, so the conservative dividend to use here is + SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. + However, malloc (SIZE_MAX) fails on all known hosts where + sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for + exactly-SIZE_MAX allocations on such hosts; this avoids a test and + branch when S is known to be 1. */ +# define xalloc_oversized(n, s) \ + ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) + +/* These macros are deprecated; they will go away soon, and are retained + temporarily only to ease conversion to the functions described above. */ +# define CCLONE(p, n) xclone (p, (n) * sizeof *(p)) +# define CLONE(p) xclone (p, sizeof *(p)) +# define NEW(type, var) type *var = xmalloc (sizeof (type)) +# define XCALLOC(type, n) xcalloc (n, sizeof (type)) +# define XMALLOC(type, n) xnmalloc (n, sizeof (type)) +# define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type)) +# define XFREE(p) free (p) #endif /* !XALLOC_H_ */ diff --git a/lib/xalloc.m4 b/lib/xalloc.m4 new file mode 100644 index 00000000..dec84177 --- /dev/null +++ b/lib/xalloc.m4 @@ -0,0 +1,32 @@ +# xalloc.m4 serial 8 +dnl Copyright (C) 2002-2003 Free Software Foundation, Inc. +dnl This file is free software, distributed under the terms of the GNU +dnl General Public License. As a special exception to the GNU General +dnl Public License, this file may be distributed as part of a program +dnl that contains a configuration script generated by Autoconf, under +dnl the same distribution terms as the rest of that program. + +AC_DEFUN([gl_XALLOC], +[ + gl_PREREQ_XALLOC + gl_PREREQ_XMALLOC + gl_PREREQ_XSTRDUP +]) + +# Prerequisites of lib/xalloc.h. +AC_DEFUN([gl_PREREQ_XALLOC], [ + : +]) + +# Prerequisites of lib/xmalloc.c. +AC_DEFUN([gl_PREREQ_XMALLOC], [ + AC_REQUIRE([AC_C_INLINE]) + AC_REQUIRE([jm_FUNC_MALLOC]) + AC_REQUIRE([jm_FUNC_REALLOC]) + : +]) + +# Prerequisites of lib/xstrdup.c. +AC_DEFUN([gl_PREREQ_XSTRDUP], [ + : +]) diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 3affee7a..181006b4 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -1,5 +1,7 @@ /* xmalloc.c -- malloc with out of memory checking - Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc. + + Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003, + 1999, 2000, 2002, 2003 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,30 +21,22 @@ # include <config.h> #endif -#include <sys/types.h> +#include "xalloc.h" -#if STDC_HEADERS -# include <stdlib.h> -#else -void *calloc (); -void *malloc (); -void *realloc (); -void free (); -#endif +#include <stdlib.h> +#include <string.h> #include "gettext.h" #define _(msgid) gettext (msgid) #define N_(msgid) msgid #include "error.h" -#include "xalloc.h" +#include "exitfail.h" -#ifndef EXIT_FAILURE -# define EXIT_FAILURE 1 +#ifndef SIZE_MAX +# define SIZE_MAX ((size_t) -1) #endif -/* The following tests require AC_PREREQ(2.54). */ - #ifndef HAVE_MALLOC "you must run the autoconf test for a GNU libc compatible malloc" #endif @@ -51,12 +45,8 @@ void free (); "you must run the autoconf test for a GNU libc compatible realloc" #endif -/* Exit value when the requested amount of memory is not available. - The caller may set it to some other value. */ -int xalloc_exit_failure = EXIT_FAILURE; - /* If non NULL, call this function when memory is exhausted. */ -void (*xalloc_fail_func) PARAMS ((void)) = 0; +void (*xalloc_fail_func) (void) = 0; /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message before exiting when memory is exhausted. Goes through gettext. */ @@ -67,11 +57,29 @@ xalloc_die (void) { if (xalloc_fail_func) (*xalloc_fail_func) (); - error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); + error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); /* The `noreturn' cannot be given to error, since it may return if its first argument is 0. To help compilers understand the - xalloc_die does terminate, call exit. */ - exit (EXIT_FAILURE); + xalloc_die does terminate, call abort. */ + abort (); +} + +/* Allocate an array of N objects, each with S bytes of memory, + dynamically, with error checking. S must be nonzero. */ + +static inline void * +xnmalloc_inline (size_t n, size_t s) +{ + void *p; + if (xalloc_oversized (n, s) || ! (p = malloc (n * s))) + xalloc_die (); + return p; +} + +void * +xnmalloc (size_t n, size_t s) +{ + return xnmalloc_inline (n, s); } /* Allocate N bytes of memory dynamically, with error checking. */ @@ -79,35 +87,169 @@ xalloc_die (void) void * xmalloc (size_t n) { - void *p; + return xnmalloc_inline (n, 1); +} + +/* Change the size of an allocated block of memory P to an array of N + objects each of S bytes, with error checking. S must be nonzero. */ - p = malloc (n); - if (p == 0) +static inline void * +xnrealloc_inline (void *p, size_t n, size_t s) +{ + if (xalloc_oversized (n, s) || ! (p = realloc (p, n * s))) xalloc_die (); return p; } +void * +xnrealloc (void *p, size_t n, size_t s) +{ + return xnrealloc_inline (p, n, s); +} + /* Change the size of an allocated block of memory P to N bytes, with error checking. */ void * xrealloc (void *p, size_t n) { - p = realloc (p, n); - if (p == 0) - xalloc_die (); - return p; + return xnrealloc_inline (p, n, 1); } -/* Allocate memory for N elements of S bytes, with error checking. */ + +/* If P is null, allocate a block of at least *PN such objects; + otherwise, reallocate P so that it contains more than *PN objects + each of S bytes. *PN must be nonzero unless P is null, and S must + be nonzero. Set *PN to the new number of objects, and return the + pointer to the new block. *PN is never set to zero, and the + returned pointer is never null. + + Repeated reallocations are guaranteed to make progress, either by + allocating an initial block with a nonzero size, or by allocating a + larger block. + + In the following implementation, nonzero sizes are doubled so that + repeated reallocations have O(N log N) overall cost rather than + O(N**2) cost, but the specification for this function does not + guarantee that sizes are doubled. + + Here is an example of use: + + int *p = NULL; + size_t used = 0; + size_t allocated = 0; + + void + append_int (int value) + { + if (used == allocated) + p = x2nrealloc (p, &allocated, sizeof *p); + p[used++] = value; + } + + This causes x2nrealloc to allocate a block of some nonzero size the + first time it is called. + + To have finer-grained control over the initial size, set *PN to a + nonzero value before calling this function with P == NULL. For + example: + + int *p = NULL; + size_t used = 0; + size_t allocated = 0; + size_t allocated1 = 1000; + + void + append_int (int value) + { + if (used == allocated) + { + p = x2nrealloc (p, &allocated1, sizeof *p); + allocated = allocated1; + } + p[used++] = value; + } + + */ + +static inline void * +x2nrealloc_inline (void *p, size_t *pn, size_t s) +{ + size_t n = *pn; + + if (! p) + { + if (! n) + { + /* The approximate size to use for initial small allocation + requests, when the invoking code specifies an old size of + zero. 64 bytes is the largest "small" request for the + GNU C library malloc. */ + enum { DEFAULT_MXFAST = 64 }; + + n = DEFAULT_MXFAST / s; + n += !n; + } + } + else + { + if (SIZE_MAX / 2 / s < n) + xalloc_die (); + n *= 2; + } + + *pn = n; + return xrealloc (p, n * s); +} + +void * +x2nrealloc (void *p, size_t *pn, size_t s) +{ + return x2nrealloc_inline (p, pn, s); +} + +/* If P is null, allocate a block of at least *PN bytes; otherwise, + reallocate P so that it contains more than *PN bytes. *PN must be + nonzero unless P is null. Set *PN to the new block's size, and + return the pointer to the new block. *PN is never set to zero, and + the returned pointer is never null. */ + +void * +x2realloc (void *p, size_t *pn) +{ + return x2nrealloc_inline (p, pn, 1); +} + +/* Allocate S bytes of zeroed memory dynamically, with error checking. + There's no need for xnzalloc (N, S), since it would be equivalent + to xcalloc (N, S). */ + +void * +xzalloc (size_t s) +{ + return memset (xmalloc (s), 0, s); +} + +/* Allocate zeroed memory for N elements of S bytes, with error + checking. S must be nonzero. */ void * xcalloc (size_t n, size_t s) { void *p; - - p = calloc (n, s); - if (p == 0) + /* Test for overflow, since some calloc implementations don't have + proper overflow checks. */ + if (xalloc_oversized (n, s) || ! (p = calloc (n, s))) xalloc_die (); return p; } + +/* Clone an object P of size S, with error checking. There's no need + for xnclone (P, N, S), since xclone (P, N * S) works without any + need for an arithmetic overflow check. */ + +void * +xclone (void const *p, size_t s) +{ + return memcpy (xmalloc (s), p, s); +} diff --git a/lib/xstrdup.c b/lib/xstrdup.c new file mode 100644 index 00000000..58f18beb --- /dev/null +++ b/lib/xstrdup.c @@ -0,0 +1,33 @@ +/* xstrdup.c -- copy a string with out of memory checking + Copyright (C) 1990, 1996, 1998, 2001, 2003 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#if HAVE_CONFIG_H +# include <config.h> +#endif + +/* Specification. */ +#include "xalloc.h" + +#include <string.h> + +/* Return a newly allocated copy of STRING. */ + +char * +xstrdup (const char *string) +{ + return xclone (string, strlen (string) + 1); +} |