source: mainline/uspace/lib/posix/source/locale.c@ bf45993

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bf45993 was fdf97f6, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

Libposix functions are without posix_ prefix

Prior this commit, libposix headers declared all functions as posix_*
and used macros to rename e.g. strncpy to posix_strncpy in all (ported)
sources.

After this change, libposix headers look as normal POSIX compliant headers
(well, almost) and no renaming is done in the source codei (of the ported
applications). Instead, the renaming is done at object files level to
bypass weird problems that are bound to happen if you use macros.

The scheme is following. libposix headers use special macro to declare
the names. When included from outside, the functions have their normal
(standard) names. When included from the libposix sources, posix_ prefix
is added. Thus, when libposix is compiled and linked, it contains the
posix_* naming while compiling of ported software uses the normal
non-prefixed versions. This way the posix_* can use HelenOS libc without
any problem. Before linking, the posix_* prefix is removed from all
symbols and special prefix helenos_libc_ is added to all functions
that exists in our (HelenOS) libc and its name clashes with the POSIX
one.

The following happens, for example, to the open() function that exists in
both libposix and in libc.

  • Headers and sources of libc are left intact.
  • Copy of libc.a is made and to all clashing functions is added the helenos_libc prefix. This library is called libc4posix.a.
  • POSIX_DEF(open)(const char *) is used in libposix headers. This macro expands to plain open when included from the "outside world". But it expands to posix_open when included from libposix sources.
  • Libposix is compiled and linked, containing posix_open() that internally calls open() [the original one from libc].
  • Libposix is transformed - all open() are replaced with prefix variant: helenos_libc_open() and all posix_open() are replaced with open(). The transformed library is stored as libposixaslibc.a

Binutils and PCC are then linked with libc4posix and libposixaslibc
libraries instead of libc and libposix as was done previously.

WARNING: it looks that binutils, PCC and MSIM still works but not all
architectures were tested.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libposix
30 * @{
31 */
32/** @file Locale-specific definitions.
33 */
34
35#define LIBPOSIX_INTERNAL
36#define __POSIX_DEF__(x) posix_##x
37
38#include "internal/common.h"
39#include "posix/locale.h"
40
41#include "posix/errno.h"
42#include "posix/limits.h"
43#include "posix/string.h"
44
45/* Just a very basic dummy implementation.
46 * This should allow code using locales to work properly, but doesn't provide
47 * any localization functionality.
48 * Should be extended/rewritten when or if HelenOS supports locales natively.
49 */
50
51struct __posix_locale {
52 int _dummy;
53};
54
55const struct posix_lconv C_LOCALE = {
56 .currency_symbol = (char *) "",
57 .decimal_point = (char *) ".",
58 .frac_digits = CHAR_MAX,
59 .grouping = (char *) "",
60 .int_curr_symbol = (char *) "",
61 .int_frac_digits = CHAR_MAX,
62 .int_n_cs_precedes = CHAR_MAX,
63 .int_n_sep_by_space = CHAR_MAX,
64 .int_n_sign_posn = CHAR_MAX,
65 .int_p_cs_precedes = CHAR_MAX,
66 .int_p_sep_by_space = CHAR_MAX,
67 .int_p_sign_posn = CHAR_MAX,
68 .mon_decimal_point = (char *) "",
69 .mon_grouping = (char *) "",
70 .mon_thousands_sep = (char *) "",
71 .negative_sign = (char *) "",
72 .n_cs_precedes = CHAR_MAX,
73 .n_sep_by_space = CHAR_MAX,
74 .n_sign_posn = CHAR_MAX,
75 .positive_sign = (char *) "",
76 .p_cs_precedes = CHAR_MAX,
77 .p_sep_by_space = CHAR_MAX,
78 .p_sign_posn = CHAR_MAX,
79 .thousands_sep = (char *) ""
80};
81
82/**
83 * Set program locale.
84 *
85 * @param category What category to set.
86 * @param locale Locale name.
87 * @return Original locale name on success, NULL on failure.
88 */
89char *posix_setlocale(int category, const char *locale)
90{
91 // TODO
92 if (locale == NULL || *locale == '\0' ||
93 posix_strcmp(locale, "C") == 0) {
94 return (char *) "C";
95 }
96 return NULL;
97}
98
99/**
100 * Return locale-specific information.
101 *
102 * @return Information about the current locale.
103 */
104struct posix_lconv *posix_localeconv(void)
105{
106 // TODO
107 return (struct posix_lconv *) &C_LOCALE;
108}
109
110/**
111 * Duplicate locale object.
112 *
113 * @param locobj Object to duplicate.
114 * @return Duplicated object.
115 */
116posix_locale_t posix_duplocale(posix_locale_t locobj)
117{
118 if (locobj == NULL) {
119 errno = EINVAL;
120 return NULL;
121 }
122 posix_locale_t copy = malloc(sizeof(struct __posix_locale));
123 if (copy == NULL) {
124 errno = ENOMEM;
125 return NULL;
126 }
127 memcpy(copy, locobj, sizeof(struct __posix_locale));
128 return copy;
129}
130
131/**
132 * Free locale object.
133 *
134 * @param locobj Object to free.
135 */
136void posix_freelocale(posix_locale_t locobj)
137{
138 if (locobj) {
139 free(locobj);
140 }
141}
142
143/**
144 * Create or modify a locale object.
145 *
146 * @param category_mask Mask of categories to be set or modified.
147 * @param locale Locale to be used.
148 * @param base Object to modify. 0 if new object is to be created.
149 * @return The new/modified locale object.
150 */
151posix_locale_t posix_newlocale(int category_mask, const char *locale,
152 posix_locale_t base)
153{
154 if (locale == NULL ||
155 (category_mask & LC_ALL_MASK) != category_mask) {
156 errno = EINVAL;
157 return NULL;
158 }
159 // TODO
160 posix_locale_t new = malloc(sizeof(struct __posix_locale));
161 if (new == NULL) {
162 errno = ENOMEM;
163 return NULL;
164 }
165 if (base != NULL) {
166 posix_freelocale(base);
167 }
168 return new;
169}
170
171/**
172 * Set locale for the current thread.
173 *
174 * @param newloc Locale to use.
175 * @return The previously set locale or LC_GLOBAL_LOCALE
176 */
177posix_locale_t posix_uselocale(posix_locale_t newloc)
178{
179 // TODO
180 return LC_GLOBAL_LOCALE;
181}
182
183/** @}
184 */
Note: See TracBrowser for help on using the repository browser.