summaryrefslogtreecommitdiffstats
path: root/lib/libc/locale
diff options
context:
space:
mode:
authorphantom <phantom@FreeBSD.org>2003-06-25 22:42:33 +0000
committerphantom <phantom@FreeBSD.org>2003-06-25 22:42:33 +0000
commit551d2ed557c103f13e630b91adebf5f4423348fb (patch)
tree74ee561a7f7202eb14362249dc2a3ac92f0de6d4 /lib/libc/locale
parenta30cd00d3ac6058650b0c7f8e2b195022b9321be (diff)
downloadFreeBSD-src-551d2ed557c103f13e630b91adebf5f4423348fb.zip
FreeBSD-src-551d2ed557c103f13e630b91adebf5f4423348fb.tar.gz
Reduce code duplication by separating _PathLocle detection code into
internal helper function.
Diffstat (limited to 'lib/libc/locale')
-rw-r--r--lib/libc/locale/setlocale.c53
-rw-r--r--lib/libc/locale/setlocale.h2
-rw-r--r--lib/libc/locale/setrunelocale.c19
3 files changed, 37 insertions, 37 deletions
diff --git a/lib/libc/locale/setlocale.c b/lib/libc/locale/setlocale.c
index 48c40bc..cea9326 100644
--- a/lib/libc/locale/setlocale.c
+++ b/lib/libc/locale/setlocale.c
@@ -262,6 +262,7 @@ loadlocale(category)
char *new = new_categories[category];
char *old = current_categories[category];
int (*func)(const char *);
+ int saved_errno;
if ((new[0] == '.' &&
(new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
@@ -270,27 +271,11 @@ loadlocale(category)
return (NULL);
}
- if (_PathLocale == NULL) {
- char *p = getenv("PATH_LOCALE");
-
- if (p != NULL
-#ifndef __NETBSD_SYSCALLS
- && !issetugid()
-#endif
- ) {
- if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
- 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) {
- errno = ENAMETOOLONG;
- return (NULL);
- }
- _PathLocale = strdup(p);
- if (_PathLocale == NULL) {
- errno = ENOMEM;
- return (NULL);
- }
- } else
- _PathLocale = _PATH_LOCALE;
- }
+ saved_errno = errno;
+ errno = __detect_path_locale();
+ if (errno != 0)
+ return (NULL);
+ errno = saved_errno;
switch (category) {
case LC_CTYPE:
@@ -327,3 +312,29 @@ loadlocale(category)
return (NULL);
}
+/*
+ * Detect locale storage location and store its value to _PathLocale variable
+ */
+int
+__detect_path_locale(void)
+{
+ if (_PathLocale == NULL) {
+ char *p = getenv("PATH_LOCALE");
+
+ if (p != NULL
+#ifndef __NETBSD_SYSCALLS
+ && !issetugid()
+#endif
+ ) {
+ if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
+ 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
+ return (ENAMETOOLONG);
+ _PathLocale = strdup(p);
+ if (_PathLocale == NULL)
+ return (errno == 0 ? ENOMEM : errno);
+ } else
+ _PathLocale = _PATH_LOCALE;
+ }
+ return (0);
+}
+
diff --git a/lib/libc/locale/setlocale.h b/lib/libc/locale/setlocale.h
index 9804f05..54f6328 100644
--- a/lib/libc/locale/setlocale.h
+++ b/lib/libc/locale/setlocale.h
@@ -34,4 +34,6 @@
extern char *_PathLocale;
+int __detect_path_locale(void);
+
#endif /* !_SETLOCALE_H_ */
diff --git a/lib/libc/locale/setrunelocale.c b/lib/libc/locale/setrunelocale.c
index cd5384b..4b1ace4 100644
--- a/lib/libc/locale/setrunelocale.c
+++ b/lib/libc/locale/setrunelocale.c
@@ -95,23 +95,10 @@ setrunelocale(char *encoding)
/*
* Slurp the locale file into the cache.
*/
- if (_PathLocale == NULL) {
- char *p = getenv("PATH_LOCALE");
+ ret = __detect_path_locale();
+ if (ret != 0)
+ return (ret);
- if (p != NULL
-#ifndef __NETBSD_SYSCALLS
- && !issetugid()
-#endif
- ) {
- if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
- 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
- return (ENAMETOOLONG);
- _PathLocale = strdup(p);
- if (_PathLocale == NULL)
- return (errno == 0 ? ENOMEM : errno);
- } else
- _PathLocale = _PATH_LOCALE;
- }
/* Range checking not needed, encoding length already checked above */
(void) strcpy(name, _PathLocale);
(void) strcat(name, "/");
OpenPOWER on IntegriCloud