Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add strerror_l.



details:   https://anonhg.NetBSD.org/src/rev/c5558388a4e1
branches:  trunk
changeset: 789459:c5558388a4e1
user:      joerg <joerg%NetBSD.org@localhost>
date:      Mon Aug 19 13:03:12 2013 +0000

description:
Add strerror_l.

diffstat:

 include/string.h             |   3 +-
 lib/libc/include/extern.h    |   8 ++++-
 lib/libc/include/namespace.h |   3 +-
 lib/libc/string/strerror.c   |  66 +++++++++++++++++++++++++++++++++----------
 lib/libc/string/strerror_r.c |  47 +++++++++++++++----------------
 5 files changed, 84 insertions(+), 43 deletions(-)

diffs (241 lines):

diff -r b9d80ea975dd -r c5558388a4e1 include/string.h
--- a/include/string.h  Mon Aug 19 10:59:39 2013 +0000
+++ b/include/string.h  Mon Aug 19 13:03:12 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: string.h,v 1.44 2013/06/24 04:21:20 riastradh Exp $    */
+/*     $NetBSD: string.h,v 1.45 2013/08/19 13:03:12 joerg Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -122,6 +122,7 @@
 __BEGIN_DECLS
 int     strcoll_l(const char *, const char *, locale_t);
 size_t  strxfrm_l(char * __restrict, const char * __restrict, size_t, locale_t);
+__aconst char *strerror_l(int, locale_t);
 __END_DECLS
 #endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
 
diff -r b9d80ea975dd -r c5558388a4e1 lib/libc/include/extern.h
--- a/lib/libc/include/extern.h Mon Aug 19 10:59:39 2013 +0000
+++ b/lib/libc/include/extern.h Mon Aug 19 13:03:12 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.22 2013/05/04 18:31:47 christos Exp $     */
+/*     $NetBSD: extern.h,v 1.23 2013/08/19 13:03:12 joerg Exp $        */
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -27,12 +27,18 @@
 #include <stdarg.h>
 #include <ucontext.h>
 
+#ifndef __LOCALE_T_DECLARED
+typedef struct _locale         *locale_t;
+#define __LOCALE_T_DECLARED
+#endif /* __LOCALE_T_DECLARED */
+
 __BEGIN_DECLS
 extern char *__minbrk;
 int __getcwd(char *, size_t);
 int __getlogin(char *, size_t);
 int __setlogin(const char *);
 void _resumecontext(void) __dead;
+__dso_hidden int       _strerror_lr(int, char *, size_t, locale_t);
 const char *__strerror(int , char *, size_t);
 const char *__strsignal(int , char *, size_t);
 char *__dtoa(double, int, int, int *, int *, char **);
diff -r b9d80ea975dd -r c5558388a4e1 lib/libc/include/namespace.h
--- a/lib/libc/include/namespace.h      Mon Aug 19 10:59:39 2013 +0000
+++ b/lib/libc/include/namespace.h      Mon Aug 19 13:03:12 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: namespace.h,v 1.167 2013/08/19 08:03:33 joerg Exp $    */
+/*     $NetBSD: namespace.h,v 1.168 2013/08/19 13:03:12 joerg Exp $    */
 
 /*-
  * Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
@@ -57,6 +57,7 @@
 #define inet_pton      _inet_pton
 #define pipe           _pipe
 #define sbrk           _sbrk
+#define strerror_l     _strerror_l
 #define strerror_r     _strerror_r
 #define strlcat                _strlcat
 #define strlcpy                _strlcpy
diff -r b9d80ea975dd -r c5558388a4e1 lib/libc/string/strerror.c
--- a/lib/libc/string/strerror.c        Mon Aug 19 10:59:39 2013 +0000
+++ b/lib/libc/string/strerror.c        Mon Aug 19 13:03:12 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: strerror.c,v 1.14 2006/01/26 11:13:42 kleink Exp $     */
+/*     $NetBSD: strerror.c,v 1.15 2013/08/19 13:03:12 joerg Exp $      */
 
 /*
  * Copyright (c) 1988 Regents of the University of California.
@@ -30,30 +30,64 @@
  */
 
 #include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char *sccsid = "@(#)strerror.c  5.6 (Berkeley) 5/4/91";
-#else
-__RCSID("$NetBSD: strerror.c,v 1.14 2006/01/26 11:13:42 kleink Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
+__RCSID("$NetBSD: strerror.c,v 1.15 2013/08/19 13:03:12 joerg Exp $");
+
+#define __SETLOCALE_SOURCE__
 
 #include "namespace.h"
-#include <string.h>
+#include <errno.h>
 #include <limits.h>
-#include <errno.h>
-#include "extern.h"
+#include <locale.h>
+#include <stdlib.h>
+#include <string.h>
 
-/*
- * Since perror() is not allowed to change the contents of strerror()'s
- * static buffer, both functions supply their own buffers to strerror_r()
- */
+#include "extern.h"
+#ifdef _REENTRANT
+#include "reentrant.h"
+#endif
+#include "setlocale_local.h"
 
 __aconst char *
 strerror(int num)
 {
+
+       return strerror_l(num, _current_locale());
+}
+
+#ifdef _REENTRANT
+static thread_key_t strerror_key;
+static once_t strerror_once = ONCE_INITIALIZER;
+
+static void
+strerror_setup(void)
+{
+
+       thr_keycreate(&strerror_key, free);
+}
+#endif
+
+__aconst char *
+strerror_l(int num, locale_t loc)
+{
+#ifdef _REENTRANT
+       int error;
+       char *buf;
+
+       thr_once(&strerror_once, strerror_setup);
+       buf = thr_getspecific(strerror_key);
+       if (buf == NULL) {
+               buf = malloc(NL_TEXTMAX);
+               if (buf == NULL) {
+                       static char fallback_buf[NL_TEXTMAX];
+                       buf = fallback_buf;
+               }
+               thr_setspecific(strerror_key, buf);
+       }
+#else
        static char buf[NL_TEXTMAX];
-       int error = strerror_r(num, buf, sizeof(buf));
+#endif
+
+       error = _strerror_lr(num, buf, NL_TEXTMAX, loc);
        if (error)
                errno = error;
        return buf;
diff -r b9d80ea975dd -r c5558388a4e1 lib/libc/string/strerror_r.c
--- a/lib/libc/string/strerror_r.c      Mon Aug 19 10:59:39 2013 +0000
+++ b/lib/libc/string/strerror_r.c      Mon Aug 19 13:03:12 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: strerror_r.c,v 1.2 2005/07/30 15:21:21 christos Exp $  */
+/*     $NetBSD: strerror_r.c,v 1.3 2013/08/19 13:03:12 joerg Exp $     */
 
 /*
  * Copyright (c) 1988 Regents of the University of California.
@@ -30,38 +30,27 @@
  */
 
 #include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char *sccsid = "@(#)strerror.c  5.6 (Berkeley) 5/4/91";
-#else
-__RCSID("$NetBSD: strerror_r.c,v 1.2 2005/07/30 15:21:21 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
+__RCSID("$NetBSD: strerror_r.c,v 1.3 2013/08/19 13:03:12 joerg Exp $");
 
 #include "namespace.h"
-#ifdef NLS
-#include <limits.h>
-#include <nl_types.h>
-#endif
-
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
+#ifdef NLS
+#include <limits.h>
+#include <nl_types.h>
+#define __SETLOCALE_SOURCE__
+#include <locale.h>
+#include "setlocale_local.h"
+#endif
+
 #include "extern.h"
 
-#ifdef _LIBC
-# ifdef __weak_alias
 __weak_alias(strerror_r, _strerror_r)
-# endif
-#endif
 
 int
-#ifdef _LIBC
-_strerror_r(int num, char *buf, size_t buflen)
-#else
-strerror_r(int num, char *buf, size_t buflen)
-#endif
+_strerror_lr(int num, char *buf, size_t buflen, locale_t loc)
 {
 #define        UPREFIX "Unknown error: %u"
        unsigned int errnum = num;
@@ -70,7 +59,7 @@
 #ifdef NLS
        int saved_errno = errno;
        nl_catd catd;
-       catd = catopen("libc", NL_CAT_LOCALE);
+       catd = catopen_l("libc", NL_CAT_LOCALE, loc);
 #endif
        _DIAGASSERT(buf != NULL);
 
@@ -83,7 +72,7 @@
 #endif
        } else {
 #ifdef NLS
-               slen = snprintf(buf, buflen, 
+               slen = snprintf_l(buf, buflen, loc,
                    catgets(catd, 1, 0xffff, UPREFIX), errnum);
 #else
                slen = snprintf(buf, buflen, UPREFIX, errnum);
@@ -101,3 +90,13 @@
 
        return retval;
 }
+
+int
+strerror_r(int num, char *buf, size_t buflen)
+{
+#ifdef NLS
+       return _strerror_lr(num, buf, buflen, _current_locale());
+#else
+       return _strerror_lr(num, buf, buflen, NULL);
+#endif
+}



Home | Main Index | Thread Index | Old Index