-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.c
159 lines (144 loc) · 3.14 KB
/
strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* A collection of useful string functions.
*
* NOTE: all of these functions assume a properly formed C string with a terminating
* NULL byte.
*
*/
#include "mylib.h"
#include "strings.h"
#include "syscall.h"
/**
* strncmp:
* @brief checks each value in a string for equality.
*
* This function compares up to n bytes of two strings and checks if each value is equal.
*
* @param s1 - string one.
* @param s2 - string two.
* @param n - the number of bytes to compare.
* @return 0 if s1 == s2; -1 otherwise.
*/
int strncmp(const char *s1, const char *s2, size_t n)
{
size_t i = 0;
for (i = 0; i < n; i++)
{
if (s1[i] != s2[i])
{
return -1;
}
}
return 0;
}
/**
* strlen:
* @brief returns the number of bytes in a string (excluding the terminating NULL byte).
*
* @param s - the string
* @return the number of bytes in s.
*/
size_t strlen(const char *s)
{
size_t i = 0;
char *pos = (char *)s;
/**
* a NULL string has no length.
*/
if (!pos)
{
return 0;
}
/**
* count bytes until we find a '\0'.
*/
for (; *pos != '\0'; pos++)
{
i++;
}
return i;
}
/**
* strncpy:
* @brief copies up to n bytes from one pointer to another.
*
* @param dest - the destination pointer
* @param src - the source pointer
* @param n - the number of bytes to copy.
* @return a pointer to dest.
*/
char *strncpy(char *dest, const char *src, size_t n)
{
size_t i = 0;
/**
* copy up to n bytes or until null byte.
*/
for (i = 0; i < n && src[i] != '\0'; i++)
{
dest[i] = src[i];
}
/**
* zero out the rest.
*/
for (; i < n; i++)
{
dest[i] = '\0';
}
return dest;
}
/**
* strtok:
* @brief tokenizes a string on a given delimeter.
*
* NOTE: this function varies from the libc strtok in a number of ways:
* (1) it takes a char delimeter,
* (2) it does not track a pointer globally and
* (3) it returns the next occurrence, not the first.
*
* @param str - the string to tokenize.
* @param del - a character to serve as the delimeter.
* @return a pointer to one byte beyond the next delimeter; NULL if none is found.
*/
char *strtok(char *str, char del)
{
int i = 0;
for (i = 0; str[i] != del && str[i] != '\0'; i++) {}
if (str[i] == '\0')
{
return NULL;
}
str[i++] = '\0';
return str + i;
}
/**
* memset:
* @brief sets a range of addresses to a given value.
*
* @param s - a pointer to the start of the range.
* @param c - the value to set the memory to.
* @param n - the number of bytes to set.
* @return a pointer to the start of the range.
*/
void *memset(void *s, int c, size_t n)
{
char *ptr = s;
unsigned int i = 0;
for (i = 0; i < n; i++)
{
ptr[i] = c;
}
return (void *)ptr;
}
/**
* printstr:
* @brief a wrapper function for calling the write syscall on a string.
*
* @param fd - the file descriptor to write to.
* @param str - the string to print.
* @return void.
*/
void printstr(int fd, char *str)
{
size_t len = strlen(str);
syscall3(SYSWRITE, fd, str, len);
}