Fdisk needs to unmount partition before trying to delete it
[helenos.git] / uspace / lib / posix / src / strings.c
blob703b9755265a25d1e32cfef080d85b9766a9e2ba
1 /*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * Copyright (c) 2011 Petr Koupy
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
30 /** @addtogroup libposix
31 * @{
33 /** @file Additional string manipulation.
36 #include "internal/common.h"
37 #include <strings.h>
39 #include <string.h>
40 #include <ctype.h>
42 #include <mem.h>
43 #include <str.h>
45 /**
46 * Find first set bit (beginning with the least significant bit).
48 * @param i Integer in which to look for the first set bit.
49 * @return Index of first set bit. Bits are numbered starting at one.
51 int ffs(int i)
53 if (i == 0) {
54 return 0;
57 int result = 0;
59 // XXX: assumes at most 32-bit int
60 if (!(i & 0xFFFF)) {
61 result |= 16;
62 i >>= 16;
64 if (!(i & 0xFF)) {
65 result |= 8;
66 i >>= 8;
68 if (!(i & 0xF)) {
69 result |= 4;
70 i >>= 4;
72 if (!(i & 0x3)) {
73 result |= 2;
74 i >>= 2;
76 if (!(i & 0x1)) {
77 result |= 1;
80 return result + 1;
83 /**
84 * Compare two strings (case-insensitive).
86 * @param s1 First string to be compared.
87 * @param s2 Second string to be compared.
88 * @return Difference of the first pair of inequal characters,
89 * or 0 if strings have the same content.
91 int strcasecmp(const char *s1, const char *s2)
93 return strncasecmp(s1, s2, STR_NO_LIMIT);
96 /**
97 * Compare part of two strings (case-insensitive).
99 * @param s1 First string to be compared.
100 * @param s2 Second string to be compared.
101 * @param n Maximum number of characters to be compared.
102 * @return Difference of the first pair of inequal characters,
103 * or 0 if strings have the same content.
105 int strncasecmp(const char *s1, const char *s2, size_t n)
107 for (size_t i = 0; i < n; ++i) {
108 int cmp = tolower(s1[i]) - tolower(s2[i]);
109 if (cmp != 0) {
110 return cmp;
113 if (s1[i] == 0) {
114 return 0;
118 return 0;
122 * Compare two memory areas.
124 * @param mem1 Pointer to the first area to compare.
125 * @param mem2 Pointer to the second area to compare.
126 * @param n Common size of both areas.
127 * @return If n is 0, return zero. If the areas match, return
128 * zero. Otherwise return non-zero.
130 int bcmp(const void *mem1, const void *mem2, size_t n)
132 return memcmp(mem1, mem2, n);
136 * Copy bytes in memory with overlapping areas.
138 * @param src Source area.
139 * @param dest Destination area.
140 * @param n Number of bytes to copy.
142 void bcopy(const void *src, void *dest, size_t n)
144 /* Note that memmove has different order of arguments. */
145 memmove(dest, src, n);
149 * Reset bytes in memory area to zero.
151 * @param mem Memory area to be zeroed.
152 * @param n Number of bytes to reset.
154 void bzero(void *mem, size_t n)
156 memset(mem, 0, n);
160 * Scan string for a first occurence of a character.
162 * @param s String in which to look for the character.
163 * @param c Character to look for.
164 * @return Pointer to the specified character on success,
165 * NULL pointer otherwise.
167 char *index(const char *s, int c)
169 return strchr(s, c);
173 * Scan string for a last occurence of a character.
175 * @param s String in which to look for the character.
176 * @param c Character to look for.
177 * @return Pointer to the specified character on success,
178 * NULL pointer otherwise.
180 char *rindex(const char *s, int c)
182 return strrchr(s, c);
185 /** @}