All pastes #139794 Raw Edit

Stuff

public c v1 · immutable
#139794 ·published 2006-08-20 03:45 UTC
rendered paste body
void *memcpy(void *dest, const void *src, size_t count){	const char *sp = (const char *)src;	char *dp = (char *)dest;	for(; count != 0; count--) *dp++ = *sp++;	return dest;}void *memset(void *dest, char val, size_t count){	char *temp = (char *)dest;	for( ; count != 0; count--) *temp++ = val;	return dest;}unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count){	unsigned short *temp = (unsigned short *)dest;	for( ; count != 0; count--) *temp++ = val;	return dest;}size_t strlen(const char *str){	size_t retval;	for(retval = 0; *str != '\0'; str++) retval++;	return retval;}boolean strcmp(const char *str1, const char *str2){	int strlength = strlen(str1);	// if they're different lengths they're obviously not the same string	if (strlength != strlen(str2))		return false;	// otherwise, compare each character	int i;	for (i = 0; i < strlength; i++)		if (str1[i] != str2[i])			return false;	// if we got this far, they're equal	return true;}boolean strncmp(const char *str1, const char *str2, size_t n){	int i;	for (i = 0; i < n; i++)		if (str1[i] != str2[i])			return false;	// if we got this far, they're equal	return true;}// POSIX says these should be like strcpy(dest, src)// But one of my favorite osdev quotes: "In a way, it's easier.// You don't have to work around some brain-dead decision some// other developer made."// (on top of that, Popcorn's not UNIX.)char *strcpy(const char *src, char *dest){	return strncpy(src, dest, strlen(src));}char *strncpy(const char *src, char *dest, size_t n){	int i;	for (i = 0; i < n; i++)		dest[i] = src[i];	return dest;}int strchr(char *str, char c){	int i;	for (i = 0; i < strlen(str); i++)		if (str[i] == c) return i;}int strrchr(char *str, char c){	int i;	for (i = strlen(str); i > 0; i--)		if (str[i] == c) return i;}int strnumtokens(const char *str, char splitchar){	int i;	int tokens = 1; // all strings have at least one token	// Actually, what if you split 'a' by 'a'?	// Actually, why *would* you split 'a' by 'a'?	for (i = 0; i < strlen(str); i++)		if (str[i] == splitchar)			tokens++;	return tokens;}char *strsplit(const char *str, char splitchar, int token_num){	//if (token_num >= strnumtokens(str, splitchar))	//	printf("1");	//	return NULL;	// copy the string so we can change it if we want	// lib/string.c:128: warning: initialization discards qualifiers from pointer target type - no idea how to fix that.	char *string = str;	int i;	// find the start of the token	for (i = 0; i < token_num; i++)	{		// using string += ... doesn't seem to work here.		int j;		// don't put this in the for loop...		int k = strchr(string, splitchar);		for (j = 0; j <= k; j++)			string++;	}	// put a \0 on the end of that token	string[strchr(string, splitchar)] = '\0';		return string;}