14a5d661aSToomas Soome /*-
24a5d661aSToomas Soome * Copyright (c) 1998 Robert Nordier
34a5d661aSToomas Soome * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
44a5d661aSToomas Soome * All rights reserved.
54a5d661aSToomas Soome *
64a5d661aSToomas Soome * Redistribution and use in source and binary forms are freely
74a5d661aSToomas Soome * permitted provided that the above copyright notice and this
84a5d661aSToomas Soome * paragraph and the following disclaimer are duplicated in all
94a5d661aSToomas Soome * such forms.
104a5d661aSToomas Soome *
114a5d661aSToomas Soome * This software is provided "AS IS" and without any express or
124a5d661aSToomas Soome * implied warranties, including, without limitation, the implied
134a5d661aSToomas Soome * warranties of merchantability and fitness for a particular
144a5d661aSToomas Soome * purpose.
154a5d661aSToomas Soome */
164a5d661aSToomas Soome
174a5d661aSToomas Soome #include <sys/cdefs.h>
184a5d661aSToomas Soome __FBSDID("$FreeBSD$");
194a5d661aSToomas Soome
204a5d661aSToomas Soome #include <sys/param.h>
214a5d661aSToomas Soome
224a5d661aSToomas Soome #include <stdarg.h>
234a5d661aSToomas Soome
244a5d661aSToomas Soome #include "cons.h"
254a5d661aSToomas Soome #include "util.h"
264a5d661aSToomas Soome
274a5d661aSToomas Soome void
memcpy(void * dst,const void * src,int len)284a5d661aSToomas Soome memcpy(void *dst, const void *src, int len)
294a5d661aSToomas Soome {
304a5d661aSToomas Soome const char *s = src;
314a5d661aSToomas Soome char *d = dst;
324a5d661aSToomas Soome
334a5d661aSToomas Soome while (len--)
344a5d661aSToomas Soome *d++ = *s++;
354a5d661aSToomas Soome }
364a5d661aSToomas Soome
374a5d661aSToomas Soome void
memset(void * b,int c,size_t len)384a5d661aSToomas Soome memset(void *b, int c, size_t len)
394a5d661aSToomas Soome {
404a5d661aSToomas Soome char *bp = b;
414a5d661aSToomas Soome
424a5d661aSToomas Soome while (len--)
434a5d661aSToomas Soome *bp++ = (unsigned char)c;
444a5d661aSToomas Soome }
454a5d661aSToomas Soome
464a5d661aSToomas Soome int
memcmp(const void * b1,const void * b2,size_t len)474a5d661aSToomas Soome memcmp(const void *b1, const void *b2, size_t len)
484a5d661aSToomas Soome {
494a5d661aSToomas Soome const unsigned char *p1, *p2;
504a5d661aSToomas Soome
514a5d661aSToomas Soome for (p1 = b1, p2 = b2; len > 0; len--, p1++, p2++) {
524a5d661aSToomas Soome if (*p1 != *p2)
534a5d661aSToomas Soome return ((*p1) - (*p2));
544a5d661aSToomas Soome }
554a5d661aSToomas Soome return (0);
564a5d661aSToomas Soome }
574a5d661aSToomas Soome
584a5d661aSToomas Soome int
strcmp(const char * s1,const char * s2)594a5d661aSToomas Soome strcmp(const char *s1, const char *s2)
604a5d661aSToomas Soome {
614a5d661aSToomas Soome
624a5d661aSToomas Soome for (; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
634a5d661aSToomas Soome ;
644a5d661aSToomas Soome return ((unsigned char)*s1 - (unsigned char)*s2);
654a5d661aSToomas Soome }
664a5d661aSToomas Soome
674a5d661aSToomas Soome int
strncmp(const char * s1,const char * s2,size_t len)684a5d661aSToomas Soome strncmp(const char *s1, const char *s2, size_t len)
694a5d661aSToomas Soome {
704a5d661aSToomas Soome
714a5d661aSToomas Soome for (; len > 0 && *s1 == *s2 && *s1 != '\0'; len--, s1++, s2++)
724a5d661aSToomas Soome ;
734a5d661aSToomas Soome return (len == 0 ? 0 : (unsigned char)*s1 - (unsigned char)*s2);
744a5d661aSToomas Soome }
754a5d661aSToomas Soome
764a5d661aSToomas Soome void
strcpy(char * dst,const char * src)774a5d661aSToomas Soome strcpy(char *dst, const char *src)
784a5d661aSToomas Soome {
794a5d661aSToomas Soome
804a5d661aSToomas Soome while (*src != '\0')
814a5d661aSToomas Soome *dst++ = *src++;
824a5d661aSToomas Soome *dst = '\0';
834a5d661aSToomas Soome }
844a5d661aSToomas Soome
854a5d661aSToomas Soome void
strcat(char * dst,const char * src)864a5d661aSToomas Soome strcat(char *dst, const char *src)
874a5d661aSToomas Soome {
884a5d661aSToomas Soome
894a5d661aSToomas Soome while (*dst != '\0')
904a5d661aSToomas Soome dst++;
914a5d661aSToomas Soome while (*src != '\0')
924a5d661aSToomas Soome *dst++ = *src++;
934a5d661aSToomas Soome *dst = '\0';
944a5d661aSToomas Soome }
954a5d661aSToomas Soome
964a5d661aSToomas Soome char *
strchr(const char * s,char ch)974a5d661aSToomas Soome strchr(const char *s, char ch)
984a5d661aSToomas Soome {
994a5d661aSToomas Soome
1004a5d661aSToomas Soome for (; *s != '\0'; s++) {
1014a5d661aSToomas Soome if (*s == ch)
1024a5d661aSToomas Soome return ((char *)(uintptr_t)(const void *)s);
1034a5d661aSToomas Soome }
1044a5d661aSToomas Soome return (NULL);
1054a5d661aSToomas Soome }
1064a5d661aSToomas Soome
1074a5d661aSToomas Soome size_t
strlen(const char * s)1084a5d661aSToomas Soome strlen(const char *s)
1094a5d661aSToomas Soome {
1104a5d661aSToomas Soome size_t len = 0;
1114a5d661aSToomas Soome
1124a5d661aSToomas Soome while (*s++ != '\0')
1134a5d661aSToomas Soome len++;
1144a5d661aSToomas Soome return (len);
1154a5d661aSToomas Soome }
1164a5d661aSToomas Soome
1174a5d661aSToomas Soome int
printf(const char * fmt,...)1184a5d661aSToomas Soome printf(const char *fmt, ...)
1194a5d661aSToomas Soome {
1204a5d661aSToomas Soome va_list ap;
1214a5d661aSToomas Soome const char *hex = "0123456789abcdef";
1224a5d661aSToomas Soome char buf[32], *s;
123*c5121490SToomas Soome uint16_t *S;
1244a5d661aSToomas Soome unsigned long long u;
1254a5d661aSToomas Soome int c, l;
1264a5d661aSToomas Soome
1274a5d661aSToomas Soome va_start(ap, fmt);
1284a5d661aSToomas Soome while ((c = *fmt++) != '\0') {
1294a5d661aSToomas Soome if (c != '%') {
1304a5d661aSToomas Soome putchar(c);
1314a5d661aSToomas Soome continue;
1324a5d661aSToomas Soome }
1334a5d661aSToomas Soome l = 0;
1344a5d661aSToomas Soome nextfmt:
1354a5d661aSToomas Soome c = *fmt++;
1364a5d661aSToomas Soome switch (c) {
1374a5d661aSToomas Soome case 'l':
1384a5d661aSToomas Soome l++;
1394a5d661aSToomas Soome goto nextfmt;
1404a5d661aSToomas Soome case 'c':
1414a5d661aSToomas Soome putchar(va_arg(ap, int));
1424a5d661aSToomas Soome break;
1434a5d661aSToomas Soome case 's':
1444a5d661aSToomas Soome for (s = va_arg(ap, char *); *s != '\0'; s++)
1454a5d661aSToomas Soome putchar(*s);
1464a5d661aSToomas Soome break;
147*c5121490SToomas Soome case 'S': /* Assume console can cope with wide chars */
148*c5121490SToomas Soome for (S = va_arg(ap, uint16_t *); *S != 0; S++)
149*c5121490SToomas Soome putchar(*S);
150*c5121490SToomas Soome break;
1514a5d661aSToomas Soome case 'd': /* A lie, always prints unsigned */
1524a5d661aSToomas Soome case 'u':
1534a5d661aSToomas Soome case 'x':
1544a5d661aSToomas Soome switch (l) {
1554a5d661aSToomas Soome case 2:
1564a5d661aSToomas Soome u = va_arg(ap, unsigned long long);
1574a5d661aSToomas Soome break;
1584a5d661aSToomas Soome case 1:
1594a5d661aSToomas Soome u = va_arg(ap, unsigned long);
1604a5d661aSToomas Soome break;
1614a5d661aSToomas Soome default:
1624a5d661aSToomas Soome u = va_arg(ap, unsigned int);
1634a5d661aSToomas Soome break;
1644a5d661aSToomas Soome }
1654a5d661aSToomas Soome s = buf;
1664a5d661aSToomas Soome if (c == 'd' || c == 'u') {
1674a5d661aSToomas Soome do
1684a5d661aSToomas Soome *s++ = '0' + (u % 10U);
1694a5d661aSToomas Soome while (u /= 10);
1704a5d661aSToomas Soome } else {
1714a5d661aSToomas Soome do
1724a5d661aSToomas Soome *s++ = hex[u & 0xfu];
1734a5d661aSToomas Soome while (u >>= 4);
1744a5d661aSToomas Soome }
1754a5d661aSToomas Soome while (--s >= buf)
1764a5d661aSToomas Soome putchar(*s);
1774a5d661aSToomas Soome break;
1784a5d661aSToomas Soome }
1794a5d661aSToomas Soome }
1804a5d661aSToomas Soome va_end(ap);
1814a5d661aSToomas Soome return (0);
1824a5d661aSToomas Soome }
183