source: rtems/bsps/sparc64/shared/helenos/boot/generic/string.c @ 142175ef

5
Last change on this file since 142175ef was 142175ef, checked in by Sebastian Huber <sebastian.huber@…>, on 04/23/18 at 07:57:54

bsps/sparc64: Move helenos to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
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 generic
30 * @{
31 */
32
33/*
34 * Modifications are made to compile for RTEMS. Remove strncpy() and atoi()
35 *
36 */
37
38
39#include <string.h>
40
41/**
42 * @file
43 * @brief       String manipulation functions.
44 */
45
46/** Return number of characters in a string.
47 *
48 * @param str           NULL terminated string.
49 *
50 * @return              Number of characters in str.
51 */
52size_t strlen(const char *str)
53{
54        int i;
55       
56        for (i = 0; str[i]; i++)
57                ;
58       
59        return i;
60}
61
62/** Compare two NULL terminated strings.
63 *
64 * Do a char-by-char comparison of two NULL terminated strings.
65 * The strings are considered equal iff they consist of the same
66 * characters on the minimum of their lengths.
67 *
68 * @param src           First string to compare.
69 * @param dst           Second string to compare.
70 *
71 * @return              0 if the strings are equal, -1 if first is smaller,
72 *                      1 if second smaller.
73 *
74 */
75int strcmp(const char *src, const char *dst)
76{
77        for (; *src && *dst; src++, dst++) {
78                if (*src < *dst)
79                        return -1;
80                if (*src > *dst)
81                        return 1;
82        }
83        if (*src == *dst)
84                return 0;
85        if (!*src)
86                return -1;
87        return 1;
88}
89
90
91/** Compare two NULL terminated strings.
92 *
93 * Do a char-by-char comparison of two NULL terminated strings.
94 * The strings are considered equal iff they consist of the same
95 * characters on the minimum of their lengths and specified maximal
96 * length.
97 *
98 * @param src           First string to compare.
99 * @param dst           Second string to compare.
100 * @param len           Maximal length for comparison.
101 *
102 * @return              0 if the strings are equal, -1 if first is smaller,
103 *                      1 if second smaller.
104 *
105 */
106int strncmp(const char *src, const char *dst, size_t len)
107{
108        int i;
109       
110        for (i = 0; *src && *dst && i < len; src++, dst++, i++) {
111                if (*src < *dst)
112                        return -1;
113                if (*src > *dst)
114                        return 1;
115        }
116        if (i == len || *src == *dst)
117                return 0;
118        if (!*src)
119                return -1;
120        return 1;
121}
122#if 0
123/** Copy NULL terminated string.
124 *
125 * Copy at most 'len' characters from string 'src' to 'dest'.
126 * If 'src' is shorter than 'len', '\0' is inserted behind the
127 * last copied character.
128 *
129 * @param src           Source string.
130 * @param dest          Destination buffer.
131 * @param len           Size of destination buffer.
132 */
133void strncpy(char *dest, const char *src, size_t len)
134{
135        int i;
136        for (i = 0; i < len; i++) {
137                if (!(dest[i] = src[i]))
138                        return;
139        }
140        dest[i-1] = '\0';
141}
142
143/** Convert ascii representation to unative_t.
144 *
145 * Supports 0x for hexa & 0 for octal notation.
146 * Does not check for overflows, does not support negative numbers
147 *
148 * @param text          Textual representation of number.
149 * @return              Converted number or 0 if no valid number found.
150 */
151unative_t atoi(const char *text)
152{
153        int base = 10;
154        unative_t result = 0;
155
156        if (text[0] == '0' && text[1] == 'x') {
157                base = 16;
158                text += 2;
159        } else if (text[0] == '0')
160                base = 8;
161
162        while (*text) {
163                if (base != 16 &&
164                    ((*text >= 'A' && *text <= 'F') ||
165                    (*text >='a' && *text <='f')))
166                        break;
167                if (base == 8 && *text >='8')
168                        break;
169
170                if (*text >= '0' && *text <= '9') {
171                        result *= base;
172                        result += *text - '0';
173                } else if (*text >= 'A' && *text <= 'F') {
174                        result *= base;
175                        result += *text - 'A' + 10;
176                } else if (*text >= 'a' && *text <= 'f') {
177                        result *= base;
178                        result += *text - 'a' + 10;
179                } else
180                        break;
181                text++;
182        }
183
184        return result;
185}
186#endif
187/** Move piece of memory to another, possibly overlapping, location.
188 *
189 * @param dst           Destination address.
190 * @param src           Source address.
191 * @param len           Number of bytes to move.
192 *
193 * @return              Destination address.
194 */
195void *memmove(void *dst, const void *src, size_t len)
196{
197        char *d = dst;
198        const char *s = src;
199        if (s < d) {
200                while (len--)
201                        *(d + len) = *(s + len);
202        } else {
203                while (len--)
204                        *d++ = *s++;
205        }
206       
207        return dst;
208}
209
210/** @}
211 */
Note: See TracBrowser for help on using the repository browser.