summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/sparc64/shared/helenos/boot/generic/string.c
blob: 9fd537cf6bd311c747d94e20410eb257a65e16da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (c) 2001-2004 Jakub Jermar
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup generic	
 * @{
 */

/*
 * $Id$
 *
 * Modifications are made to compile for RTEMS. Remove strncpy() and atoi() 
 *
 */


#include <string.h>

/**
 * @file
 * @brief	String manipulation functions.
 */

/** Return number of characters in a string.
 *
 * @param str		NULL terminated string.
 *
 * @return		Number of characters in str.
 */
size_t strlen(const char *str)
{
	int i;
	
	for (i = 0; str[i]; i++)
		;
	
	return i;
}

/** Compare two NULL terminated strings.
 *
 * Do a char-by-char comparison of two NULL terminated strings.
 * The strings are considered equal iff they consist of the same
 * characters on the minimum of their lengths.
 *
 * @param src		First string to compare.
 * @param dst		Second string to compare.
 *
 * @return		0 if the strings are equal, -1 if first is smaller,
 * 			1 if second smaller.
 *
 */
int strcmp(const char *src, const char *dst)
{
	for (; *src && *dst; src++, dst++) {
		if (*src < *dst)
			return -1;
		if (*src > *dst)
			return 1;
	}
	if (*src == *dst)
		return 0;
	if (!*src)
		return -1;
	return 1;
}


/** Compare two NULL terminated strings.
 *
 * Do a char-by-char comparison of two NULL terminated strings.
 * The strings are considered equal iff they consist of the same
 * characters on the minimum of their lengths and specified maximal
 * length.
 *
 * @param src		First string to compare.
 * @param dst		Second string to compare.
 * @param len		Maximal length for comparison.
 *
 * @return		0 if the strings are equal, -1 if first is smaller,
 * 			1 if second smaller.
 *
 */
int strncmp(const char *src, const char *dst, size_t len)
{
	int i;
	
	for (i = 0; *src && *dst && i < len; src++, dst++, i++) {
		if (*src < *dst)
			return -1;
		if (*src > *dst)
			return 1;
	}
	if (i == len || *src == *dst)
		return 0;
	if (!*src)
		return -1;
	return 1;
}
#if 0
/** Copy NULL terminated string.
 *
 * Copy at most 'len' characters from string 'src' to 'dest'.
 * If 'src' is shorter than 'len', '\0' is inserted behind the
 * last copied character.
 *
 * @param src		Source string.
 * @param dest		Destination buffer.
 * @param len		Size of destination buffer.
 */
void strncpy(char *dest, const char *src, size_t len)
{
	int i;
	for (i = 0; i < len; i++) {
		if (!(dest[i] = src[i]))
			return;
	}
	dest[i-1] = '\0';
}

/** Convert ascii representation to unative_t.
 *
 * Supports 0x for hexa & 0 for octal notation.
 * Does not check for overflows, does not support negative numbers
 *
 * @param text		Textual representation of number.
 * @return		Converted number or 0 if no valid number found.
 */
unative_t atoi(const char *text)
{
	int base = 10;
	unative_t result = 0;

	if (text[0] == '0' && text[1] == 'x') {
		base = 16;
		text += 2;
	} else if (text[0] == '0')
		base = 8;

	while (*text) {
		if (base != 16 &&
		    ((*text >= 'A' && *text <= 'F') ||
		    (*text >='a' && *text <='f')))
			break;
		if (base == 8 && *text >='8')
			break;

		if (*text >= '0' && *text <= '9') {
			result *= base;
			result += *text - '0';
		} else if (*text >= 'A' && *text <= 'F') {
			result *= base;
			result += *text - 'A' + 10;
		} else if (*text >= 'a' && *text <= 'f') {
			result *= base;
			result += *text - 'a' + 10;
		} else
			break;
		text++;
	}

	return result;
}
#endif
/** Move piece of memory to another, possibly overlapping, location.
 *
 * @param dst		Destination address.
 * @param src		Source address.
 * @param len		Number of bytes to move.
 *
 * @return		Destination address.
 */
void *memmove(void *dst, const void *src, size_t len)
{
	char *d = dst;
	const char *s = src;
	if (s < d) {
		while (len--)
			*(d + len) = *(s + len);
	} else {
		while (len--)
			*d++ = *s++;
	}
	
	return dst;
}

/** @}
 */