libc-functions.c 1.57 KB
Newer Older
1
/*
2
 * All code from uClibc-0.9.32. LGPL V2.1
3 4
 */
#include <ppsi/lib.h>
5 6
#include <stdlib.h>
size_t strnlen(const char *s, size_t max);
7

8 9 10 11 12 13 14
/* Architectures declare pp_puts: map puts (used by pp_printf) to it */
int puts(const char *s)
{
	pp_puts(s);
	return 0;
}

15 16
/* libc/string/strnlen.c */
size_t strnlen(const char *s, size_t max)
17
{
18 19 20 21 22 23 24
	register const char *p = s;

	while (max && *p) {
		++p;
		--max;
	}
	return p - s;
25 26
}

27 28
/* libc/string/memcpy.c */
void *memcpy(void *s1, const void *s2, size_t n)
29
{
30 31
	register char *r1 = s1;
	register const char *r2 = s2;
32

33 34 35 36 37
	while (n) {
		*r1++ = *r2++;
		--n;
	}
	return s1;
38 39
}

40 41
/* libc/string/memcmp.c */
int memcmp(const void *s1, const void *s2, size_t n)
42
{
43 44 45
	register const unsigned char *r1 = s1;
	register const unsigned char *r2 = s2;
	int r = 0;
46

47 48 49
	while (n-- && ((r = ((int)(*r1++)) - *r2++) == 0))
		;
	return r;
50 51
}

52 53
/* libc/string/memset.c */
void *memset(void *s, int c, size_t n)
54
{
55
	register unsigned char *p = s;
56

57 58 59 60
	while (n) {
		*p++ = (unsigned char) c;
		--n;
	}
61 62 63
	return s;
}

64 65 66

/* libc/string/strcpy.c */
char *strcpy(char *s1, const char *s2)
67
{
68
	register char *s = s1;
69

70 71 72
	while ( (*s++ = *s2++) != 0 )
		;
	return s1;
73
}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

/* for strtol even uClibc is too heavy. Let's rewrite some hack */
long int strtol(const char *s, char **end, int base)
{
	long res = 0;

	if (!base)
		base = 10;
	while (*s) {
		if (end)
			*end = (void *)s;
		/* we only use it for cmdline: avoid alpha digits */
		if (base > 10 || *s < '0' || *s >= '0' + base) {
			return res;
		}
		res = res * base + *(s++) - '0';
	}
	return res;
}
int atoi(const char *s)
{
	return strtol(s, NULL, 10);
}