[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 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[491e1ee] | 36 | #define LIBPOSIX_INTERNAL
|
---|
[ae1c11b] | 37 |
|
---|
[9b1503e] | 38 | #include "internal/common.h"
|
---|
[ae1c11b] | 39 | #include "strings.h"
|
---|
[a6d908c1] | 40 |
|
---|
[ae1c11b] | 41 | #include "string.h"
|
---|
[6128390] | 42 | #include "ctype.h"
|
---|
[ae1c11b] | 43 |
|
---|
[8ecef91] | 44 | #include "libc/mem.h"
|
---|
| 45 |
|
---|
[4f4b4e7] | 46 | /**
|
---|
| 47 | *
|
---|
| 48 | * @param i
|
---|
| 49 | * @return
|
---|
| 50 | */
|
---|
[ae1c11b] | 51 | int posix_ffs(int i)
|
---|
| 52 | {
|
---|
[8ecef91] | 53 | if (i == 0) {
|
---|
| 54 | return 0;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | int result = 0;
|
---|
| 58 |
|
---|
| 59 | // XXX: assumes at most 32-bit int
|
---|
| 60 | if (!(i & 0xFFFF)) {
|
---|
| 61 | result |= 16;
|
---|
| 62 | i >>= 16;
|
---|
| 63 | }
|
---|
| 64 | if (!(i & 0xFF)) {
|
---|
| 65 | result |= 8;
|
---|
| 66 | i >>= 8;
|
---|
| 67 | }
|
---|
| 68 | if (!(i & 0xF)) {
|
---|
| 69 | result |= 4;
|
---|
| 70 | i >>= 4;
|
---|
| 71 | }
|
---|
| 72 | if (!(i & 0x3)) {
|
---|
| 73 | result |= 2;
|
---|
| 74 | i >>= 2;
|
---|
| 75 | }
|
---|
| 76 | if (!(i & 0x1)) {
|
---|
| 77 | result |= 1;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | return result + 1;
|
---|
[ae1c11b] | 81 | }
|
---|
| 82 |
|
---|
[4f4b4e7] | 83 | /**
|
---|
| 84 | *
|
---|
| 85 | * @param s1
|
---|
| 86 | * @param s2
|
---|
| 87 | * @return
|
---|
| 88 | */
|
---|
[ae1c11b] | 89 | int posix_strcasecmp(const char *s1, const char *s2)
|
---|
| 90 | {
|
---|
[6128390] | 91 | return posix_strncasecmp(s1, s2, STR_NO_LIMIT);
|
---|
[ae1c11b] | 92 | }
|
---|
| 93 |
|
---|
[4f4b4e7] | 94 | /**
|
---|
| 95 | *
|
---|
| 96 | * @param s1
|
---|
| 97 | * @param s2
|
---|
| 98 | * @param n
|
---|
| 99 | * @return
|
---|
| 100 | */
|
---|
[ae1c11b] | 101 | int posix_strncasecmp(const char *s1, const char *s2, size_t n)
|
---|
| 102 | {
|
---|
[6128390] | 103 | for (size_t i = 0; i < n; ++i) {
|
---|
| 104 | int cmp = tolower(s1[i]) - tolower(s2[i]);
|
---|
| 105 | if (cmp != 0) {
|
---|
| 106 | return cmp;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | if (s1[i] == 0) {
|
---|
| 110 | return 0;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | return 0;
|
---|
[ae1c11b] | 115 | }
|
---|
| 116 |
|
---|
[4f4b4e7] | 117 | /**
|
---|
| 118 | *
|
---|
| 119 | * @param mem1
|
---|
| 120 | * @param mem2
|
---|
| 121 | * @param n
|
---|
| 122 | * @return
|
---|
| 123 | */
|
---|
[ae1c11b] | 124 | int posix_bcmp(const void *mem1, const void *mem2, size_t n)
|
---|
| 125 | {
|
---|
[8ecef91] | 126 | return bcmp(mem1, mem2, n);
|
---|
[ae1c11b] | 127 | }
|
---|
| 128 |
|
---|
[4f4b4e7] | 129 | /**
|
---|
| 130 | *
|
---|
| 131 | * @param dest
|
---|
| 132 | * @param src
|
---|
| 133 | * @param n
|
---|
| 134 | */
|
---|
[ae1c11b] | 135 | void posix_bcopy(const void *dest, void *src, size_t n)
|
---|
| 136 | {
|
---|
[8ecef91] | 137 | /* Note that memmove has different order of arguments. */
|
---|
| 138 | memmove(src, dest, n);
|
---|
[ae1c11b] | 139 | }
|
---|
| 140 |
|
---|
[4f4b4e7] | 141 | /**
|
---|
| 142 | *
|
---|
| 143 | * @param mem
|
---|
| 144 | * @param n
|
---|
| 145 | */
|
---|
[ae1c11b] | 146 | void posix_bzero(void *mem, size_t n)
|
---|
| 147 | {
|
---|
[8ecef91] | 148 | bzero(mem, n);
|
---|
[ae1c11b] | 149 | }
|
---|
| 150 |
|
---|
[4f4b4e7] | 151 | /**
|
---|
| 152 | *
|
---|
| 153 | * @param s
|
---|
| 154 | * @param c
|
---|
| 155 | * @return
|
---|
| 156 | */
|
---|
[ae1c11b] | 157 | char *posix_index(const char *s, int c)
|
---|
| 158 | {
|
---|
| 159 | return posix_strchr(s, c);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[4f4b4e7] | 162 | /**
|
---|
| 163 | *
|
---|
| 164 | * @param s
|
---|
| 165 | * @param c
|
---|
| 166 | * @return
|
---|
| 167 | */
|
---|
[ae1c11b] | 168 | char *posix_rindex(const char *s, int c)
|
---|
| 169 | {
|
---|
| 170 | return posix_strrchr(s, c);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /** @}
|
---|
| 174 | */
|
---|