4002d5b5e546acb788e26847e4122aa5a7a87467
[lunaix-os.git] / lunaix-os / libs / klibc / string / strcpy.c
1 #include <klibc/string.h>
2 #include <lunaix/compiler.h>
3
4 char* weak
5 strcpy(char* dest, const char* src)
6 {
7     char c;
8     unsigned int i = 0;
9     while ((c = src[i])) {
10         dest[i] = c;
11         i++;
12     }
13     dest[i++] = '\0';
14     return &dest[i];
15 }
16
17 /**
18  * @brief strcpy with constrain on numbers of character.
19  *        this version is smarter than stdc, it will automatically
20  *        handle the null-terminator.
21  * 
22  * @param dest 
23  * @param src 
24  * @param n 
25  * @return char* 
26  */
27 char* weak
28 strncpy(char* dest, const char* src, unsigned long n)
29 {
30     char c;
31     unsigned int i = 0;
32     while (i <= n && (c = src[i]))
33         dest[i++] = c;
34
35     if (!(n < i && src[i - 1])) {
36         while (i <= n)
37             dest[i++] = 0;
38     }
39     else {
40         dest[i - 1] = 0;
41     }
42     
43     return dest;
44 }