1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include <ctype.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <wctype.h>
36 
37 // Contains an implementation of all locale-specific functions (those
38 // ending in _l, like strcoll_l()), as simple wrapper to the non-locale
39 // specific ones for now.
40 //
41 // That's because Android's C library doesn't support locales. Or more
42 // specifically, only supports the "C" one.
43 //
44 // TODO(digit): Write a more complete implementation that uses JNI to
45 //              invoke the platform APIs to implement proper handling.
46 //
47 
48 ///////////////////////////////////////////////////////////////////////
49 // stdio.h declarations
50 
51 #if !defined(__LP64__)
vasprintf_l(char ** strp,locale_t l,const char * fmt,va_list args)52 int vasprintf_l(char** strp, locale_t l, const char* fmt, va_list args) {
53     // Ignore locale.
54     return vasprintf(strp, fmt, args);
55 }
56 
asprintf_l(char ** strp,locale_t locale,const char * fmt,...)57 int asprintf_l(char** strp, locale_t locale, const char* fmt, ...) {
58     va_list args;
59     va_start(args, fmt);
60     int result = vasprintf_l(strp, locale, fmt, args);
61     va_end(args);
62     return result;
63 }
64 
vsprintf_l(char * str,locale_t l,const char * fmt,va_list args)65 int vsprintf_l(char* str, locale_t l, const char* fmt, va_list args) {
66     // Ignore locale.
67     return vsprintf(str, fmt, args);
68 }
69 
sprintf_l(char * str,locale_t l,const char * fmt,...)70 int sprintf_l(char* str, locale_t l, const char* fmt, ...) {
71     va_list args;
72     va_start(args, fmt);
73     int result = vsprintf_l(str, l, fmt, args);
74     va_end(args);
75     return result;
76 }
77 
vsnprintf_l(char * str,size_t size,locale_t l,const char * fmt,va_list args)78 int vsnprintf_l(char* str, size_t size, locale_t l, const char* fmt, va_list args) {
79     return vsnprintf(str, size, fmt, args);
80 }
81 
snprintf_l(char * str,size_t size,locale_t l,const char * fmt,...)82 int snprintf_l(char* str, size_t size, locale_t l, const char* fmt, ...) {
83   va_list args;
84   va_start(args, fmt);
85   int result = vsnprintf_l(str, size, l, fmt, args);
86   va_end(args);
87   return result;
88 }
89 
vsscanf_l(const char * str,locale_t l,const char * fmt,va_list args)90 int vsscanf_l(const char* str, locale_t l, const char* fmt, va_list args) {
91     return vsscanf(str, fmt, args);
92 }
93 
sscanf_l(const char * str,locale_t l,const char * fmt,...)94 int sscanf_l(const char* str, locale_t l, const char* fmt, ...) {
95     va_list args;
96     va_start(args, fmt);
97     int result = vsscanf_l(str, l, fmt, args);
98     va_end(args);
99     return result;
100 }
101 
102 ///////////////////////////////////////////////////////////////////////
103 // stdlib.h declarations
104 
strtol_l(const char * nptr,char ** endptr,int base,locale_t loc)105 long strtol_l(const char *nptr, char **endptr, int base, locale_t loc) {
106     return strtol(nptr, endptr, base);
107 }
108 
strtoul_l(const char * nptr,char ** endptr,int base,locale_t loc)109 unsigned long strtoul_l(const char *nptr, char **endptr, int base, locale_t loc) {
110     return strtoul(nptr, endptr, base);
111 }
112 
113 #endif // !__LP64__
114 
strtoll_l(const char * nptr,char ** endptr,int base,locale_t loc)115 long long strtoll_l(const char *nptr, char **endptr, int base, locale_t loc) {
116     return strtoll(nptr, endptr, base);
117 }
118 
strtoull_l(const char * nptr,char ** endptr,int base,locale_t loc)119 unsigned long long strtoull_l(const char *nptr, char **endptr, int base, locale_t loc) {
120     return strtoull(nptr, endptr, base);
121 }
122 
strtold_l(const char * nptr,char ** endptr,locale_t loc)123 long double strtold_l (const char *nptr, char **endptr, locale_t loc) {
124     return strtold(nptr, endptr);
125 }
126