From 336611559ba8c67be677e861773a4ccb44390d4f Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Sat, 20 Aug 2011 10:40:07 +1000 Subject: [PATCH] Add the error-checking function xwcsdup() --- lib/.gitignore | 4 ++++ m4/.gitignore | 2 ++ m4/gnulib-cache.m4 | 3 ++- src/intf.c | 2 -- src/utils.c | 20 ++++++++++++++++++++ src/utils.h | 15 +++++++++++++++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/.gitignore b/lib/.gitignore index a34ed72..c2b6dae 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -109,11 +109,15 @@ vfprintf.c vsnprintf.c wchar.in.h wcrtomb.c +wcsdup-impl.h +wcsdup.c wcsrtombs-impl.h wcsrtombs-state.c wcsrtombs.c wctype.in.h wcwidth.c +wmemcpy-impl.h +wmemcpy.c xsize.h alloca.h diff --git a/m4/.gitignore b/m4/.gitignore index 86d99a2..1ad61a1 100644 --- a/m4/.gitignore +++ b/m4/.gitignore @@ -110,8 +110,10 @@ warn-on-use.m4 wchar_h.m4 wchar_t.m4 wcrtomb.m4 +wcsdup.m4 wcsrtombs.m4 wctype_h.m4 wcwidth.m4 wint_t.m4 +wmemcpy.m4 xsize.m4 diff --git a/m4/gnulib-cache.m4 b/m4/gnulib-cache.m4 index 66f7648..b6e250d 100644 --- a/m4/gnulib-cache.m4 +++ b/m4/gnulib-cache.m4 @@ -15,7 +15,7 @@ # Specification in the form of a command-line invocation: -# gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --no-conditional-dependencies --no-libtool --macro-prefix=gl assert btowc config-h ctype fprintf-posix getopt-gnu gettext gettext-h gettimeofday langinfo locale mbrtowc mbsrtowcs printf-posix sigaction signal snprintf-posix stat stdarg stdbool stdio strdup-posix striconv string strncat strstr sys_stat sys_time unistd vfprintf-posix vsnprintf-posix wchar wcrtomb wcsrtombs wctype-h wcwidth +# gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --no-conditional-dependencies --no-libtool --macro-prefix=gl assert btowc config-h ctype fprintf-posix getopt-gnu gettext gettext-h gettimeofday langinfo locale mbrtowc mbsrtowcs printf-posix sigaction signal snprintf-posix stat stdarg stdbool stdio strdup-posix striconv string strncat strstr sys_stat sys_time unistd vfprintf-posix vsnprintf-posix wchar wcrtomb wcsdup wcsrtombs wctype-h wcwidth # Specification in the form of a few gnulib-tool.m4 macro invocations: gl_LOCAL_DIR([]) @@ -53,6 +53,7 @@ gl_MODULES([ vsnprintf-posix wchar wcrtomb + wcsdup wcsrtombs wctype-h wcwidth diff --git a/src/intf.c b/src/intf.c index 03a66ad..09b6501 100644 --- a/src/intf.c +++ b/src/intf.c @@ -44,8 +44,6 @@ typedef struct txwin { // Declarations for argument processing in mkchstr() -#define EILSEQ_REPL '?' // Illegal byte sequence replacement character - #define MAXFMTARGS 8 // Maximum number of positional arguments enum argument_type { diff --git a/src/utils.c b/src/utils.c index e698ad3..7b0adac 100644 --- a/src/utils.c +++ b/src/utils.c @@ -487,5 +487,25 @@ char *xstrdup (const char *str) } +/***********************************************************************/ +// xwcsdup: Duplicate a wide-character string, with checking + +wchar_t *xwcsdup (const wchar_t *str) +{ + wchar_t *s; + + + if (str == NULL) + str = L""; + + s = wcsdup(str); + if (s == NULL) { + err_exit_nomem(); + } + + return s; +} + + /***********************************************************************/ // End of file diff --git a/src/utils.h b/src/utils.h index 4c9aabe..88699ba 100644 --- a/src/utils.h +++ b/src/utils.h @@ -42,6 +42,9 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define EILSEQ_REPL '?' // Illegal character sequence replacement + + /************************************************************************ * Global variable declarations * ************************************************************************/ @@ -328,4 +331,16 @@ extern void *xmalloc (size_t size); extern char *xstrdup (const char *str); +/* + Function: xwcsdup - Duplicate a wide-character string, with checking + Parameters: str - String to duplicate + Returns: wchar_t * - Pointer to new string, allocated with malloc() + + This wrapper function duplicates a string by calling wcsdup(), then + checks if a NULL pointer has been returned. If so, the program + terminates with an "Out of memory" error. +*/ +extern wchar_t *xwcsdup (const wchar_t *str); + + #endif /* included_UTILS_H */