Cosmetics, fix typos (trivial).
[coreboot.git] / payloads / libpayload / libc / string.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2008 Advanced Micro Devices, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. 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  * 3. 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 AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <libpayload.h>
32
33 /**
34  * Calculate the length of a fixed-size string.
35  *
36  * @param str The input string.
37  * @param maxlen Return at most maxlen characters as length of the string.
38  * @return The length of the string, not including the final NUL character.
39  *         The maximum length returned is maxlen.
40  */
41 size_t strnlen(const char *str, size_t maxlen)
42 {
43         size_t len = 0;
44
45         /* NULL and empty strings have length 0. */
46         if (!str)
47                 return 0;
48
49         /* Loop until we find a NUL character, or maxlen is reached. */
50         while ((*str++ != '\0') && (len < maxlen))
51                 len++;
52
53         return len;
54 }
55
56 /**
57  * Calculate the length of a string.
58  *
59  * @param str The input string.
60  * @return The length of the string, not including the final NUL character.
61  */
62 size_t strlen(const char *str)
63 {
64         size_t len = 0;
65
66         /* NULL and empty strings have length 0. */
67         if (!str)
68                 return 0;
69
70         /* Loop until we find a NUL character. */
71         while (*str++ != '\0')
72                 len++;
73
74         return len;
75 }
76
77 /**
78  * Compare two strings.
79  *
80  * @param s1 The first string.
81  * @param s2 The second string.
82  * @return Returns a value less than zero, if s1 is shorter than s2. Returns
83  *         zero, if s1 equals s2. Returns a value greater than zero, if
84  *         s1 is longer than s2.
85  */
86 int strcmp(const char *s1, const char *s2)
87 {
88         char c1, c2;
89
90         /* Set c1 == c2, so that we can enter the while loop. */
91         c1 = 0;
92         c2 = 0;
93
94         /* Compare characters until they differ, or one of the strings ends. */
95         while (c1 == c2) {
96                 /* Read the next character from each string. */
97                 c1 = *s1++;
98                 c2 = *s2++;
99
100                 /* Return something negative (if s1 is shorter than s2), or
101                    zero (if s1 equals s2). */
102                 if (c1 == '\0')
103                         return c1 - c2;
104         }
105
106         /* Return something positive (if s1 is longer than s2), or zero (if s1
107            and s2 are equal). */
108         return c1 - c2;
109 }
110
111 /**
112  * Compare two strings with fixed length.
113  *
114  * @param s1 The first string.
115  * @param s2 The second string.
116  * @param maxlen Return at most maxlen characters as length of the string.
117  * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
118  */
119 int strncmp(const char *s1, const char *s2, int maxlen)
120 {
121         int i;
122
123         for (i = 0; i < maxlen; i++) {
124                 if (s1[i] != s2[i])
125                         return s1[i] - s2[i];
126         }
127
128         return 0;
129 }
130
131 char *strncpy(char *d, const char *s, int n)
132 {
133         /* Use +1 to get the NUL terminator. */
134         int max = n > strlen(s) + 1 ? strlen(s) + 1 : n;
135         int i;
136
137         for (i = 0; i < max; i++)
138                 d[i] = (char)s[i];
139
140         return d;
141 }
142
143 char *strcpy(char *d, const char *s)
144 {
145         return strncpy(d, s, strlen(s) + 1);
146 }
147
148 char *strncat(char *d, const char *s, int n)
149 {
150         char *p = d + strlen(d);
151         int max = n > strlen(s) ? strlen(s) : n;
152         int i;
153
154         for (i = 0; i < max; i++)
155                 p[i] = s[i];
156
157         p[i] = '\0';
158         return d;
159 }
160
161 char *strchr(const char *s, int c)
162 {
163         char *p = (char *)s;
164
165         for (; *p != 0; p++) {
166                 if (*p == c)
167                         return p;
168         }
169
170         return NULL;
171 }
172
173 char *strdup(const char *s)
174 {
175         int n = strlen(s);
176         char *p = malloc(n);
177
178         if (p != NULL)
179                 strncpy(p, s, n);
180
181         return p;
182 }
183
184 char *strstr(const char *h, const char *n)
185 {
186         int hn = strlen(h);
187         int nn = strlen(n);
188         int i;
189
190         for (i = 0; i <= hn - nn; i++)
191                 if (!strcmp(&h[i], n))
192                         return (char *)&h[i];
193
194         return NULL;
195 }