Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more string.h functions #139

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,41 @@
#include <stddef.h> // size_t

size_t strlen(const char *s);
size_t strnlen(const char *s, size_t n);

char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
char *strstr(const char *s1, const char *s2);

int strcmp(const char *p, const char *q);
int strncmp(const char *p, const char *q, size_t n);
int strcoll(const char *p, const char *q);

char *strcpy(char *dst, const char *src);
char *strncpy(char *dst, const char *src, size_t n);

char *stpcpy(char *dst, const char *src);
char *stpncpy(char *dst, const char *src, size_t n);

char *strcat(char *dst, const char *src);
char *strncat(char *dst, const char *src, size_t n);

char *strdup(const char *str);
char *strndup(const char *str, size_t size);

void *memcpy(void *dst, const void *src, size_t n);
void *memccpy(void *dst, const void *src, int c, size_t n);
void *memmove(void *dst, const void *src, size_t);
void *memset(void *buf, int val, size_t size);
void *memchr(const void *buf, int c, size_t n);
int memcmp(const void *buf1, const void *buf2, size_t n);

size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject);
char *strpbrk(const char *s, const char *accept);

char *strerror(int no);
int strerror_r(int no, char *dst, size_t n);

char *strtok(char *src, const char *delim);
char *strtok_r(char *src, const char *delim, char **ptr);
7 changes: 6 additions & 1 deletion libsrc/stdio/perror.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "stdio.h"
#include "string.h"
#include "errno.h"

void perror(const char *msg) {
fprintf(stderr, "perror: %s\n", msg);
int no = errno;
if (msg != NULL && *msg != '\0')
fprintf(stderr, "%s: ", msg);
fprintf(stderr, "%s\n", strerror(no));
}
8 changes: 8 additions & 0 deletions libsrc/string/memccpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "string.h"

void *memccpy(void *dst, const void *src, int c, size_t n) {
char *b = memchr(src, c, n);
if (b != NULL)
n = b - (char *)src;
return memcpy(dst, src, n);
}
7 changes: 7 additions & 0 deletions libsrc/string/stpcpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "string.h"

char *stpcpy(char *dst, const char *src) {
while ((*dst++ = *src++) != '\0')
;
return dst;
}
7 changes: 7 additions & 0 deletions libsrc/string/stpncpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "string.h"

char *stpncpy(char *dst, const char *src, size_t n) {
for (; n > 0 && (*dst++ = *src++) != '\0'; --n)
;
return dst;
}
5 changes: 5 additions & 0 deletions libsrc/string/strcoll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "string.h"

int strcoll(const char *p, const char *q) {
return strcmp(p, q);
}
8 changes: 8 additions & 0 deletions libsrc/string/strcspn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "string.h"

size_t strcspn(const char *s, const char *reject) {
size_t n;
for (n = 0; s[n] != '\0' && strchr(reject, s[n]) == NULL; ++n)
;
return n;
}
45 changes: 45 additions & 0 deletions libsrc/string/strerror.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "string.h"

static char *strerrors[] = {
"Success",
"Operation not permitted",
"No such file or directory",
"No such process",
"Interrupted system call",
"I/O error",
"No such device or address",
"Argument list too long",
"Exec format error",
"Bad file number",
"No child processes",
"Try again",
"Out of memory",
"Permission denied",
"Bad address",
"Block device required",
"Device or resource busy",
"File exists",
"Cross-device link",
"No such device",
"Not a directory",
"Is a directory",
"Invalid argument",
"File table overflow",
"Too many open files",
"Not a typewriter",
"Text file busy",
"File too large",
"No space left on device",
"Illegal seek",
"Read-only file system",
"Too many links",
"Broken pipe",
"Math argument out of domain of func",
"Math result not representable",
};

char *strerror(int no) {
if (no < 0 || no >= sizeof(strerrors) / sizeof(*strerrors))
return "Unknown error";
return strerrors[no];
}
6 changes: 6 additions & 0 deletions libsrc/string/strerror_r.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "string.h"

int strerror_r(int no, char *dst, size_t n) {
strncpy(dst, strerror(no), n);
return 0;
}
8 changes: 8 additions & 0 deletions libsrc/string/strnlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "string.h"

size_t strnlen(const char *s, size_t n) {
size_t len;
for (len = 0; len < n && s[len] != '\0'; ++len)
;
return len;
}
8 changes: 8 additions & 0 deletions libsrc/string/strpbrk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "string.h"

char *strpbrk(const char *s, const char *accept) {
const char *r = s + strcspn(s, accept);
if (*r == '\0')
return NULL;
return (char *)r;
}
8 changes: 8 additions & 0 deletions libsrc/string/strspn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "string.h"

size_t strspn(const char *s, const char *accept) {
size_t n;
for (n = 0; s[n] != '\0' && strchr(accept, s[n]) != NULL; ++n)
;
return n;
}
6 changes: 6 additions & 0 deletions libsrc/string/strtok.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "string.h"

char *strtok(char *src, const char *delim) {
static char *ptr;
return strtok_r(src, delim, &ptr);
}
14 changes: 14 additions & 0 deletions libsrc/string/strtok_r.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "string.h"

char *strtok_r(char *src, const char *delim, char **ptr) {
if (src != NULL)
*ptr = src;
*ptr += strspn(*ptr, delim);
if (**ptr == '\0')
return NULL;
char *r = *ptr;
size_t n = strcspn(r, delim);
r[n] = '\0';
*ptr += n + 1;
return r;
}
Loading