X-Git-Url: http://git.joshuawise.com/netwatch.git/blobdiff_plain/31be35cda26f033fe62db673a8a973f0b15c9de7..923ea2c2cd9df7f02ddcfd8c543f63b76be44660:/lib/minilib.c?ds=sidebyside diff --git a/lib/minilib.c b/lib/minilib.c index f64f3b1..e90f7af 100644 --- a/lib/minilib.c +++ b/lib/minilib.c @@ -1,26 +1,38 @@ #include "console.h" +#include -void memcpy(unsigned char *a2, unsigned char *a1, int bytes) +void memcpy(void *dest, const void *src, int bytes) { + char *cdest = dest; + const char *csrc = src; while (bytes--) - *(a2++) = *(a1++); + *(cdest++) = *(csrc++); } -void memmove(unsigned char *dest, unsigned char *src, int bytes) +void memset(void *dest, int data, int bytes) { - if ((dest > src) && (dest <= (src + bytes))) + unsigned char *cdest = dest; + while (bytes--) + *(cdest++) = (unsigned char)data; +} + +void memmove(void *dest, void *src, int bytes) +{ + char * cdest = dest; + char * csrc = src; + if ((cdest > csrc) && (cdest <= (csrc + bytes))) { /* do it backwards! */ - dest += bytes; - src += bytes; + cdest += bytes; + csrc += bytes; while (bytes--) - *(--dest) = *(--src); + *(--cdest) = *(--csrc); } else while (bytes--) - *(dest++) = *(src++); + *(cdest++) = *(csrc++); } -int memcmp (unsigned char *a2, unsigned char *a1, int bytes) { +int memcmp (const char *a2, const char *a1, int bytes) { while (bytes--) { if (*(a2++) != *(a1++)) @@ -29,7 +41,7 @@ int memcmp (unsigned char *a2, unsigned char *a1, int bytes) { return 0; } -int strcmp (unsigned char *a2, unsigned char *a1) { +int strcmp (const char *a2, const char *a1) { while (1) { if (*a2 != *a1) return 1; if (*a2 == 0) return 0; @@ -38,7 +50,17 @@ int strcmp (unsigned char *a2, unsigned char *a1) { } } -int strlen(char *c) +int strncmp (const char *a2, const char *a1, int n) { + while (n--) { + if (*a2 != *a1) return 1; + if (*a2 == 0) return 0; + a1++; + a2++; + } + return 0; +} + +int strlen(const char *c) { int l = 0; while (*(c++)) @@ -46,20 +68,20 @@ int strlen(char *c) return l; } -void strcpy(unsigned char *a2, unsigned char *a1) +void strcpy(char *a2, const char *a1) { do { *(a2++) = *a1; } while (*(a1++)); } -void puts(char *c) +void puts(const char *c) { putbytes(c, strlen(c)); } static char hexarr[] = "0123456789ABCDEF"; -void tohex(unsigned char *s, unsigned long l) +void tohex(char *s, unsigned long l) { int i; for (i = 0; i < 8; i++) @@ -71,10 +93,21 @@ void tohex(unsigned char *s, unsigned long l) void puthex(unsigned long l) { - unsigned char d[9]; + char d[9]; d[8] = 0; tohex(d, l); puts(d); } +unsigned short htons(unsigned short in) +{ + return (in >> 8) | (in << 8); +} +unsigned int htonl(unsigned int in) +{ + return ((in & 0xff) << 24) | + ((in & 0xff00) << 8) | + ((in & 0xff0000UL) >> 8) | + ((in & 0xff000000UL) >> 24); +}