Last active
August 10, 2018 04:50
-
-
Save dunnousername/fd0024ae8e18336dda25ff4dfb63392b to your computer and use it in GitHub Desktop.
Totally untested stdlib files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define isupper(c) ((c > 0x40) && (c < 0x5B)) | |
#define islower(c) ((c > 0x60) && (c < 0x7B)) | |
#define iscntrl(c) ((c < 0x20) || (c == 0x7F)) | |
#define isdigit(c) ((c >= 0x30) && (c <= 0x39)) | |
#define isspace(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) | |
#define isprint(c) ((c >= 0x20) && (c <= 0x7E)) | |
#define isgraph(c) (isprint(c) && !isspace(c)) | |
#define isalpha(c) (isupper(c) && islower(c)) | |
#define isalnum(c) (isalpha(c) || isdigit(c)) | |
#define isxdigit(c) (isnum(c) || ((c > 0x40) && (c <= 0x47)) || ((c > 0x60) && (c <= 0x66))) | |
#define tolower(c) ((islower(c) || !isalpha(c)) ? c : c + 0x20) | |
#define toupper(c) ((isupper(c) || !isalpha(c)) ? c : c - 0x20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern int errno; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CHAR_BIT 8 | |
#define CHAR_MAX ((1 << CHAR_BIT) - 1) | |
#define CHAR_MIN 0 | |
#define INT_MAX ((1 << (sizeof(int) * CHAR_BIT - 1)) - 1) | |
#define INT_MIN (-(1 << (sizeof(int) * CHAR_BIT - 1)) | |
#define LONG_MAX ((1 << (sizeof(long) * CHAR_BIT - 1)) - 1) | |
#define LONG_MIN (-(1 << (sizeof(long) * CHAR_BIT - 1)) | |
#define SCHAR_MAX ((1 << (CHAR_BIT - 1)) - 1) | |
#define SCHAR_MIN (-(1 << (CHAR_BIT - 1))) | |
#define SHRT_MAX ((1 << (sizeof(short) * CHAR_BIT - 1)) - 1) | |
#define SHRT_MIN (-(1 << (sizeof(short) * CHAR_BIT - 1))) | |
#define UCHAR_MAX CHAR_MAX | |
#define UCHAR_MIN CHAR_MIN | |
#define UINT_MAX ((1 << (sizeof(int) * CHAR_BIT)) - 1) | |
#define ULONG_MAX ((1 << (sizeof(long) * CHAR_BIT)) - 1) | |
#define USHRT_MAX ((1 << (sizeof(short) * CHAR_BIT)) - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "limits.h" | |
#include "string.h" | |
#include "stdlib.h" | |
#include <stdbool.h> | |
size_t strlen(const char *cs) { | |
int i = 0; | |
while (cs[i]) | |
i++; | |
return i; | |
} | |
char *strcpy(char *s, const char *ct) { | |
return strncpy(s, ct, INT_MAX); | |
} | |
char *strncpy(char *s, const char *ct, int n) { | |
int i = 0; | |
int j = 0; | |
while (i < n) | |
if (ct[j]) | |
s[i++] = ct[j++]; | |
else | |
s[i++] = 0; | |
s[i] = 0; | |
return s; | |
} | |
char *strcat(char *s, const char *ct) { | |
return strncat(s, ct, INT_MAX); | |
} | |
char *strncat(char *s, const char *ct, int n) { | |
return strncpy(s[strlen(s)], ct, INT_MAX); | |
} | |
int strcmp(const char *cs, const char *ct) { | |
return strncmp(cs, ct, INT_MAX); | |
} | |
int strncmp(const char *cs, const char *ct, int n) { | |
int i = 0; | |
while (cs[i] && ct[i] && (i < n)) | |
if (cs[i] == ct[i]) | |
i++; | |
else | |
return cs[i] - ct[i]; | |
return 0; | |
} | |
char *strchr(const char *cs, int c) { | |
int i = 0; | |
while (cs[i] != 0) | |
if (cs[i] == c) | |
return i; | |
else | |
i++; | |
return NULL; | |
} | |
char *strrchr(const char *cs, int c) { | |
int i = strlen(cs) - 1; | |
while (i >= 0) | |
if (cs[i] == c) | |
return i; | |
else | |
i--; | |
return NULL; | |
} | |
size_t strspn(const char *cs, const char *ct) { | |
int i = 0; | |
while (cs[i]) { | |
int j = 0; | |
while ((cs[i] != ct[j]) && ct[j]) | |
j++; | |
if (ct[j]) | |
i++; | |
else | |
break; | |
} | |
return i; | |
} | |
size_t strcspn(const char *cs, const char *ct) { | |
int i = 0; | |
while (cs[i]) { | |
int j = 0; | |
while ((cs[i] != ct[j]) && ct[j]) | |
j++; | |
if (ct[j]) | |
break; | |
else | |
i++; | |
} | |
return i; | |
} | |
char *strpbrk(const char *cs, const char *ct) { | |
int l = strlen(cs); | |
int i = strcspn(cs, ct); | |
return i >= l ? NULL : &cs[i]; | |
} | |
char *strstr(const char *cs, const char *ct) { | |
int i = 0; | |
while (cs[i]) { | |
int j = 0; | |
while (cs[i + j] && ct[j]) | |
if (cs[i + j] != ct[j]) | |
break; | |
else | |
j++; | |
if (!cs[i + j]) | |
return NULL; | |
if (ct[j]) | |
i += j + 1; | |
else | |
return &cs[i]; | |
} | |
return NULL; | |
} | |
char *strerror(int n) { | |
// TODO | |
return ""; | |
} | |
static char *_tokptr; | |
static char *_oldptr; | |
static bool done; | |
char *strtok(char *s, const char *ct) { | |
if (s) { | |
_tokptr = s; | |
done = false; | |
} else if (done || !_tokptr) | |
return _tokptr = NULL; | |
else | |
free(_oldptr); | |
size_t p = strspn(_tokptr, ct); | |
_tokptr = &_tokptr[p]; | |
size_t i = strcspn(_tokptr, ct); | |
_oldptr = _tokptr; | |
done = i == strlen(_tokptr); | |
if (!done) { | |
_oldptr = malloc(i + 1); | |
if (!_oldptr) { | |
// TODO : implement out of memory handling | |
} | |
strncpy(_oldptr, _tokptr, i); | |
_tokptr = &_tokptr[i]; | |
} | |
return _oldptr; | |
} | |
void *memcpy(void *s, const void *ct, int n) { | |
int i = 0; | |
while (i < n) { | |
((char *) s)[i] = ((const char *) ct)[i]; | |
i++; | |
} | |
return s; | |
} | |
void *memmove(void *s, const void *ct, int n) { | |
char *temp = malloc(n); // TODO : implement out of memory handling | |
memcpy(temp, ct, n); | |
memcpy(s, temp, n); | |
free(temp); | |
return s; | |
} | |
int memcmp(const void *cs, const void *ct, int n) { | |
int i = 0; | |
while (i < n) | |
if ((const char *)cs[i] == (const char *)ct[i]) | |
i++; | |
else | |
return cs[i] - ct[i]; | |
return 0; | |
} | |
void *memchr(const void *s, int c, size_t n) { | |
int i = 0; | |
while (s[i] && (i < n)) | |
if (s[i] == c) | |
return &s[i]; | |
else | |
i++; | |
return NULL; | |
} | |
void *memset(void *s, int c, size_t n) { | |
int i = 0; | |
while (i < n) | |
s[i++] = c; | |
return s; | |
} | |
int strcoll(const char *cs, const char *ct) { | |
// TODO : when locales are implemented, implement this | |
return strcmp(cs, ct); | |
} | |
size_t strxfrm(char *cs, const char *ct, size_t n) { | |
// TODO : when locales are implemented, implement this | |
return strncpy(cs, ct, n); | |
} | |
char *strdup(const char *cs) { | |
char *ret = malloc(strlen(cs)); // TODO : implement out of memory handling | |
strcpy(ret, cs); | |
return ret; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
size_t strlen(const char *cs); | |
char *strcpy(char *s, const char *ct); | |
char *strncpy(char *s, const char *ct, int n); | |
char *strcat(char *s, const char *ct); | |
char *strncat(char *s, const char *ct, int n); | |
int strcmp(const char *cs, const char *ct); | |
int strncmp(const char *cs, const char *ct, int n); | |
char *strchr(const char *cs, int c); | |
char *strrchr(const char *cs, int c); | |
size_t strspn(const char *cs, const char *ct); | |
size_t strcspn(const char *cs, const char *ct); | |
char *strpbrk(const char *cs, const char *ct); | |
char *strstr(const char *cs, const char *ct); | |
char *strerror(int n); | |
char *strtok(char *s, const char *ct); | |
void *memcpy(void *s, const void *ct, int n); | |
void *memmove(void *s, const void *ct, int n); | |
int memcmp(const void *cs, const void *ct, int n); | |
void *memchr(const void *s, int c, size_t n); | |
void *memset(void *s, int c, size_t n); | |
int strcoll(const char *cs, const char *ct); | |
size_t strxfrm(char *cs, const char *ct, size_t n); | |
char *strdup(const char *cs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment