LIBC: Fixing warnings and missing semi-colons and adding stddef.h header.
[tinyx.git] / lib / libc / string.c
blob1a268511b4862597e822be04eed0fea9d7636774
1 /* lib/libc/string.c
3 * Copyright (C) 2007 David Cohen <dacohen@gmail.com>
4 * Copyright (C) 2007 Felipe Balbi <me@felipebalbi.com>
6 * This file is part of Tinyx Nanokernel Project.
8 * Tinyx is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Tinyx is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Tinyx. If not, see <http://www.gnu.org/licenses/>.
22 #include <libc/string.h>
24 /**
25 * memset - Fill a memory region with data
26 * @mem: Pointer to the start of the area.
27 * @data: The byte to fill the area with
28 * @count: The size of the area.
30 void *memset(void *mem, int data, size_t count)
32 char *tmp = mem;
34 while (count--)
35 *tmp++ = data;
36 return mem;
39 /**
40 * memcpy - Copy memory areas
41 * @dest: Destination of copy task
42 * @src: Where the data to copy from is
43 * @count: The size of the area.
45 void *memcpy(void *dest, const void *src, size_t count)
47 char *tmp = dest;
48 const char *s = src;
50 while (count--)
51 *tmp++ = *s++;
52 return dest;
55 /**
56 * strcat - Apend one string to another
57 * @str1: The destination string
58 * @str2: The source string
60 char *strcat(char *str1, const char *str2)
62 char *tmp = str1;
64 while (*str1)
65 str1++;
66 while ((*str1++ = *str2++) != '\0')
68 return tmp;
71 /**
72 * strchr - Find the first ocurrence of a character in a string
73 * @str: The string to search into
74 * @ch: The character to find
76 char *strchr(const char *str, int ch)
78 for (; *str != (char)ch; ++str)
79 if (*str == '\0')
80 return NULL;
81 return (char *)str;
84 /**
85 * strcoll - Compare two strings
86 * @str1: First string
87 * @str2: Second string
89 int strcoll(const char *str1, const char *str2)
91 while ((*str1) == (*str2)) {
92 if (!*str1++)
93 return 0;
94 str2++;
96 while ((*str1++) && (*str2++));
98 return (*str1) ? 1 : -1;
102 * strcmp - Lex comparation of two strings
103 * @str1: First string
104 * @str2: Second string
106 int strcmp(const char *str1, const char *str2)
108 signed char ret;
110 while (1) {
111 if ((ret = *str1 - *str2++) != 0 || !*str1++)
112 break;
114 return ret;
118 * strcpy - Copy the contents of str2 into str1
119 * @str1: The destination string
120 * @str2: The source string
122 char *strcpy(char *str1, const char *str2)
124 char *tmp = str1;
126 while ((*str1++ = *str2++) != '\0');
128 return tmp;
132 * strlen - Find the length of a string
133 * @str: The string to be sized
135 size_t strlen(const char *str)
137 const char *tmp;
139 for (tmp = str; *tmp != '\0'; ++tmp);
141 return tmp - str;
145 * strncat - Append count characters of str2 into str1
146 * @str1: The destination string
147 * @str2: The source string
148 * @count: The number of characters to append
150 char *strncat(char *str1, const char *str2, size_t count)
152 char *tmp = str1;
154 if (count) {
155 while (*str1)
156 str1++;
157 while ((*str1++ = *str2++) != 0) {
158 if (--count == 0) {
159 *str1 = '\0';
160 break;
164 return tmp;
168 * strncmp - Comparison of count characters
169 * @str1: The destination string
170 * @str2: The source string
171 * @count: The number of characters
173 int strncmp(const char *str1, const char *str2, size_t count)
175 signed char ret = 0;
177 while (count) {
178 if ((ret = *str1 - *str2++) != 0 || !*str1++)
179 break;
180 count--;
182 return ret;
186 * strncpy - Copy count characters from str2 into str1
187 * @str1: The destination string
188 * @str2: The source string
189 * @count: The number of characters
191 char *strncpy(char *str1, const char *str2, size_t count)
193 char *tmp = str1;
195 while (count) {
196 if ((*tmp = *str2) != 0)
197 str2++;
198 tmp++;
199 count--;
202 return str1;