2 * General-purpose C library routines.
3 * NetWatch system management mode administration console
5 * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved.
6 * This program is free software; you can redistribute and/or modify it under
7 * the terms found in the file LICENSE in the root of this source tree.
16 /* We have both _memcpy and memcpy, because gcc might be able to do better in lwip.
17 * For small things, gcc inlines its memcpy, but for large things, we call out
20 void _memcpy(void *dest, const void *src, int bytes)
23 /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */
26 const char *csrc = src;
31 /* Align to src (picked arbitrarily; might as well align to something) */
32 while (bytes && ((unsigned int)csrc & 3))
34 *(cdest++) = *(csrc++);
39 isrc = (const int *)csrc;
44 switch(nwords % 8) /* They see me Duff'sin'. They hatin'. */
46 case 0: nwords--; *(idest++) = *(isrc++);
47 case 7: nwords--; *(idest++) = *(isrc++);
48 case 6: nwords--; *(idest++) = *(isrc++);
49 case 5: nwords--; *(idest++) = *(isrc++);
50 case 4: nwords--; *(idest++) = *(isrc++);
51 case 3: nwords--; *(idest++) = *(isrc++);
52 case 2: nwords--; *(idest++) = *(isrc++);
53 case 1: nwords--; *(idest++) = *(isrc++);
56 cdest = (char *)idest;
57 csrc = (const char *)isrc;
58 while (bytes) /* Clean up the remainder */
60 *(cdest++) = *(csrc++);
65 void memcpy(void *dest, const void *src, int bytes)
67 _memcpy(dest, src, bytes);
70 void memset(void *dest, int data, int bytes)
72 unsigned char *cdest = dest;
74 *(cdest++) = (unsigned char)data;
77 void memmove(void *dest, void *src, int bytes)
81 if ((cdest > csrc) && (cdest <= (csrc + bytes)))
83 /* do it backwards! */
87 *(--cdest) = *(--csrc);
89 memcpy(dest, src, bytes);
92 int memcmp (const char *a2, const char *a1, int bytes) {
95 if (*(a2++) != *(a1++))
101 int strcmp (const char *a2, const char *a1) {
103 if (*a2 != *a1) return 1;
104 if (*a2 == 0) return 0;
110 int strncmp (const char *a2, const char *a1, int n) {
112 if (*a2 != *a1) return 1;
113 if (*a2 == 0) return 0;
120 int strlen(const char *c)
128 void strcpy(char *a2, const char *a1)
135 void strcat(char *dest, char *src)
140 *(dest++) = *(src++);
141 *(dest++) = *(src++);
144 void puts(const char *c)
146 putbytes(c, strlen(c));
149 static char hexarr[] = "0123456789ABCDEF";
150 void tohex(char *s, unsigned long l)
153 for (i = 0; i < 8; i++)
155 s[i] = hexarr[l >> 28];
160 void puthex(unsigned long l)
168 unsigned short htons(unsigned short in)
170 return (in >> 8) | (in << 8);
173 unsigned int htonl(unsigned int in)
175 return ((in & 0xff) << 24) |
176 ((in & 0xff00) << 8) |
177 ((in & 0xff0000UL) >> 8) |
178 ((in & 0xff000000UL) >> 24);