Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
libc.c File Reference
#include <stddef.h>
#include <stdint.h>
#include <string.h>
Include dependency graph for libc.c:

Go to the source code of this file.

Functions

char * strchr (const char *s, int c)
 
char * strtok_r (char *str, const char *delim, char **last)
 
char * strcpy (char *to, const char *from)
 
char * strncpy (char *to, const char *from, size_t length)
 
int strcmp (const char *s1, const char *s2)
 
int strncmp (const char *s1, const char *s2, size_t len)
 
size_t strlen (const char *str)
 
void * memset (void *dst, int c, size_t length)
 
void * memcpy (void *dst, const void *src, size_t length)
 
int memcmp (const void *b1, const void *b2, size_t length)
 

Function Documentation

◆ memcmp()

int memcmp ( const void *  b1,
const void *  b2,
size_t  length 
)

Definition at line 149 of file libc.c.

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}
Here is the caller graph for this function:

◆ memcpy()

void * memcpy ( void *  dst,
const void *  src,
size_t  length 
)

Definition at line 134 of file libc.c.

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}
unsigned char uint8_t
Definition: types.h:10
Here is the caller graph for this function:

◆ memset()

void * memset ( void *  dst,
int  c,
size_t  length 
)

Definition at line 121 of file libc.c.

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}
Here is the caller graph for this function:

◆ strchr()

char * strchr ( const char *  s,
int  c 
)

Definition at line 11 of file libc.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}
#define NULL
Definition: stddef.h:6
Here is the caller graph for this function:

◆ strcmp()

int strcmp ( const char *  s1,
const char *  s2 
)

Definition at line 80 of file libc.c.

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}
Here is the caller graph for this function:

◆ strcpy()

char * strcpy ( char *  to,
const char *  from 
)

Definition at line 58 of file libc.c.

59{
60 char *save = to;
61
62 for (; (*to = *from); ++from, ++to);
63
64 return save;
65}
Here is the caller graph for this function:

◆ strlen()

size_t strlen ( const char *  str)

Definition at line 111 of file libc.c.

112{
113 const char *s;
114
115 for (s = str; *s; ++s);
116
117 return (s - str);
118}
Here is the caller graph for this function:

◆ strncmp()

int strncmp ( const char *  s1,
const char *  s2,
size_t  len 
)

Definition at line 90 of file libc.c.

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}
uint64_t len
Definition: multiboot.h:2
Here is the caller graph for this function:

◆ strncpy()

char * strncpy ( char *  to,
const char *  from,
size_t  length 
)

Definition at line 68 of file libc.c.

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}
Here is the caller graph for this function:

◆ strtok_r()

char * strtok_r ( char *  str,
const char *  delim,
char **  last 
)

Definition at line 24 of file libc.c.

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}
char * strchr(const char *s, int c)
Definition: libc.c:11
Here is the call graph for this function:
Here is the caller graph for this function: