CS350 COS
COS
Loading...
Searching...
No Matches
libc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006-2018 Ali Mashtizadeh
3 * All rights reserved.
4 */
5
6#include <stddef.h>
7#include <stdint.h>
8#include <string.h>
9
10char *
11strchr(const char *s, int c)
12{
13 int i;
14 for (i = 0; ; i++)
15 {
16 if (s[i] == c)
17 return (char *)s + i;
18 if (s[i] == '\0')
19 return NULL;
20 }
21}
22
23char *
24strtok_r(char *str, const char *delim, char **last)
25{
26 char *rval;
27
28 if (str == NULL)
29 str = *last;
30
31 // Skip deliminaters in the front
32 while ((str[0] != '\0') && strchr(delim, str[0])) {
33 str++;
34 }
35
36 // We've reached the end of the string
37 if (str[0] == '\0')
38 return NULL;
39
40 // Return first non-delim until the next
41 rval = str;
42
43 // Skip deliminaters in the front
44 while ((str[0] != '\0') && !strchr(delim, str[0])) {
45 str++;
46 }
47
48 if (str[0] != '\0') {
49 str[0] = '\0';
50 str++;
51 }
52 *last = str;
53
54 return rval;
55}
56
57char *
58strcpy(char *to, const char *from)
59{
60 char *save = to;
61
62 for (; (*to = *from); ++from, ++to);
63
64 return save;
65}
66
67char *
68strncpy(char *to, const char *from, size_t length)
69{
70 char *save = to;
71
72 for (; (*to = *from) != '\0' && length > 0; ++from, ++to, length--);
73
74 *to = '\0';
75
76 return save;
77}
78
79int
80strcmp(const char *s1, const char *s2)
81{
82 while (*s1 == *s2++)
83 if (*s1++ == 0)
84 return 0;
85
86 return (*(const uint8_t *)s1 - *(const uint8_t *)(s2 - 1));
87}
88
89int
90strncmp(const char *s1, const char *s2, size_t len)
91{
92 if (len == 0)
93 return 0;
94
95 while (*s1 == *s2) {
96 if (*s1 == 0)
97 return 0;
98
99 s1++;
100 s2++;
101
102 len--;
103 if (len == 0)
104 return 0;
105 }
106
107 return (*(const uint8_t *)s1 - *(const uint8_t *)s2);
108}
109
110size_t
111strlen(const char *str)
112{
113 const char *s;
114
115 for (s = str; *s; ++s);
116
117 return (s - str);
118}
119
120void *
121memset(void *dst, int c, size_t length)
122{
123 uint8_t *p = (uint8_t *)dst;
124
125 while (length-- != 0) {
126 *p = c;
127 p += 1;
128 };
129
130 return dst;
131}
132
133void *
134memcpy(void *dst, const void *src, size_t length)
135{
136 uint8_t *d = (uint8_t *)dst;
137 const uint8_t *s = (const uint8_t *)src;
138
139 while (length-- != 0) {
140 *d = *s;
141 d += 1;
142 s += 1;
143 };
144
145 return dst;
146}
147
148int
149memcmp(const void *b1, const void *b2, size_t length)
150{
151 int i;
152 const char *c1 = (const char *)b1;
153 const char *c2 = (const char *)b2;
154
155 for (i = 0; i < length; i++)
156 {
157 if (*c1 != *c2)
158 return *c2 - *c1;
159 c1++;
160 c2++;
161 }
162
163 return 0;
164}
165
size_t strlen(const char *str)
Definition: libc.c:111
int strcmp(const char *s1, const char *s2)
Definition: libc.c:80
char * strcpy(char *to, const char *from)
Definition: libc.c:58
char * strncpy(char *to, const char *from, size_t length)
Definition: libc.c:68
void * memcpy(void *dst, const void *src, size_t length)
Definition: libc.c:134
int memcmp(const void *b1, const void *b2, size_t length)
Definition: libc.c:149
void * memset(void *dst, int c, size_t length)
Definition: libc.c:121
char * strtok_r(char *str, const char *delim, char **last)
Definition: libc.c:24
char * strchr(const char *s, int c)
Definition: libc.c:11
int strncmp(const char *s1, const char *s2, size_t len)
Definition: libc.c:90
uint64_t len
Definition: multiboot.h:2
#define NULL
Definition: stddef.h:6
unsigned char uint8_t
Definition: types.h:10