source: mainline/uspace/lib/posix/source/strings.c@ be2bb4f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since be2bb4f 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
RevLine 
[ae1c11b]1/*
2 * Copyright (c) 2011 Jiri Zarevucky
[4f4b4e7]3 * Copyright (c) 2011 Petr Koupy
[ae1c11b]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
[fc3680e]33/** @file Additional string manipulation.
[ae1c11b]34 */
35
[491e1ee]36#define LIBPOSIX_INTERNAL
[fdf97f6]37#define __POSIX_DEF__(x) posix_##x
[ae1c11b]38
[9b1503e]39#include "internal/common.h"
[a3da2b2]40#include "posix/strings.h"
[a6d908c1]41
[a3da2b2]42#include "posix/string.h"
43#include "posix/ctype.h"
[ae1c11b]44
[8ecef91]45#include "libc/mem.h"
46
[4f4b4e7]47/**
[fc3680e]48 * Find first set bit (beginning with the least significant bit).
[4f4b4e7]49 *
[fc3680e]50 * @param i Integer in which to look for the first set bit.
51 * @return Index of first set bit. Bits are numbered starting at one.
[4f4b4e7]52 */
[ae1c11b]53int posix_ffs(int i)
54{
[8ecef91]55 if (i == 0) {
56 return 0;
57 }
58
59 int result = 0;
60
61 // XXX: assumes at most 32-bit int
62 if (!(i & 0xFFFF)) {
63 result |= 16;
64 i >>= 16;
65 }
66 if (!(i & 0xFF)) {
67 result |= 8;
68 i >>= 8;
69 }
70 if (!(i & 0xF)) {
71 result |= 4;
72 i >>= 4;
73 }
74 if (!(i & 0x3)) {
75 result |= 2;
76 i >>= 2;
77 }
78 if (!(i & 0x1)) {
79 result |= 1;
80 }
81
82 return result + 1;
[ae1c11b]83}
84
[4f4b4e7]85/**
[fc3680e]86 * Compare two strings (case-insensitive).
[4f4b4e7]87 *
[fc3680e]88 * @param s1 First string to be compared.
89 * @param s2 Second string to be compared.
90 * @return Difference of the first pair of inequal characters,
91 * or 0 if strings have the same content.
[4f4b4e7]92 */
[ae1c11b]93int posix_strcasecmp(const char *s1, const char *s2)
94{
[6128390]95 return posix_strncasecmp(s1, s2, STR_NO_LIMIT);
[ae1c11b]96}
97
[4f4b4e7]98/**
[fc3680e]99 * Compare part of two strings (case-insensitive).
[4f4b4e7]100 *
[fc3680e]101 * @param s1 First string to be compared.
102 * @param s2 Second string to be compared.
103 * @param n Maximum number of characters to be compared.
104 * @return Difference of the first pair of inequal characters,
105 * or 0 if strings have the same content.
[4f4b4e7]106 */
[ae1c11b]107int posix_strncasecmp(const char *s1, const char *s2, size_t n)
108{
[6128390]109 for (size_t i = 0; i < n; ++i) {
110 int cmp = tolower(s1[i]) - tolower(s2[i]);
111 if (cmp != 0) {
112 return cmp;
113 }
114
115 if (s1[i] == 0) {
116 return 0;
117 }
118 }
119
120 return 0;
[ae1c11b]121}
122
[4f4b4e7]123/**
[fc3680e]124 * Compare two memory areas.
[4f4b4e7]125 *
[fc3680e]126 * @param mem1 Pointer to the first area to compare.
127 * @param mem2 Pointer to the second area to compare.
128 * @param n Common size of both areas.
129 * @return If n is 0, return zero. If the areas match, return
130 * zero. Otherwise return non-zero.
[4f4b4e7]131 */
[ae1c11b]132int posix_bcmp(const void *mem1, const void *mem2, size_t n)
133{
[8ecef91]134 return bcmp(mem1, mem2, n);
[ae1c11b]135}
136
[4f4b4e7]137/**
[fc3680e]138 * Copy bytes in memory with overlapping areas.
[4f4b4e7]139 *
[fc3680e]140 * @param src Source area.
141 * @param dest Destination area.
142 * @param n Number of bytes to copy.
[4f4b4e7]143 */
[fc3680e]144void posix_bcopy(const void *src, void *dest, size_t n)
[ae1c11b]145{
[8ecef91]146 /* Note that memmove has different order of arguments. */
[fc3680e]147 memmove(dest, src, n);
[ae1c11b]148}
149
[4f4b4e7]150/**
[fc3680e]151 * Reset bytes in memory area to zero.
[4f4b4e7]152 *
[fc3680e]153 * @param mem Memory area to be zeroed.
154 * @param n Number of bytes to reset.
[4f4b4e7]155 */
[ae1c11b]156void posix_bzero(void *mem, size_t n)
157{
[8ecef91]158 bzero(mem, n);
[ae1c11b]159}
160
[4f4b4e7]161/**
[fc3680e]162 * Scan string for a first occurence of a character.
[4f4b4e7]163 *
[fc3680e]164 * @param s String in which to look for the character.
165 * @param c Character to look for.
166 * @return Pointer to the specified character on success,
167 * NULL pointer otherwise.
[4f4b4e7]168 */
[ae1c11b]169char *posix_index(const char *s, int c)
170{
171 return posix_strchr(s, c);
172}
173
[4f4b4e7]174/**
[fc3680e]175 * Scan string for a last occurence of a character.
176 *
177 * @param s String in which to look for the character.
178 * @param c Character to look for.
179 * @return Pointer to the specified character on success,
180 * NULL pointer otherwise.
[4f4b4e7]181 */
[ae1c11b]182char *posix_rindex(const char *s, int c)
183{
184 return posix_strrchr(s, c);
185}
186
187/** @}
188 */
Note: See TracBrowser for help on using the repository browser.