1*22028508SToomas Soome /*-
2*22028508SToomas Soome * Copyright (c) 1998 Robert Nordier
3*22028508SToomas Soome * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4*22028508SToomas Soome * All rights reserved.
5*22028508SToomas Soome *
6*22028508SToomas Soome * Redistribution and use in source and binary forms are freely
7*22028508SToomas Soome * permitted provided that the above copyright notice and this
8*22028508SToomas Soome * paragraph and the following disclaimer are duplicated in all
9*22028508SToomas Soome * such forms.
10*22028508SToomas Soome *
11*22028508SToomas Soome * This software is provided "AS IS" and without any express or
12*22028508SToomas Soome * implied warranties, including, without limitation, the implied
13*22028508SToomas Soome * warranties of merchantability and fitness for a particular
14*22028508SToomas Soome * purpose.
15*22028508SToomas Soome */
16*22028508SToomas Soome
17*22028508SToomas Soome #include <sys/cdefs.h>
18*22028508SToomas Soome __FBSDID("$FreeBSD$");
19*22028508SToomas Soome
20*22028508SToomas Soome #include <sys/param.h>
21*22028508SToomas Soome
22*22028508SToomas Soome #include <stdarg.h>
23*22028508SToomas Soome
24*22028508SToomas Soome #include "cons.h"
25*22028508SToomas Soome #include "util.h"
26*22028508SToomas Soome
27*22028508SToomas Soome void
memcpy(void * dst,const void * src,int len)28*22028508SToomas Soome memcpy(void *dst, const void *src, int len)
29*22028508SToomas Soome {
30*22028508SToomas Soome const char *s = src;
31*22028508SToomas Soome char *d = dst;
32*22028508SToomas Soome
33*22028508SToomas Soome while (len--)
34*22028508SToomas Soome *d++ = *s++;
35*22028508SToomas Soome }
36*22028508SToomas Soome
37*22028508SToomas Soome void
memset(void * b,int c,size_t len)38*22028508SToomas Soome memset(void *b, int c, size_t len)
39*22028508SToomas Soome {
40*22028508SToomas Soome char *bp = b;
41*22028508SToomas Soome
42*22028508SToomas Soome while (len--)
43*22028508SToomas Soome *bp++ = (unsigned char)c;
44*22028508SToomas Soome }
45*22028508SToomas Soome
46*22028508SToomas Soome int
memcmp(const void * b1,const void * b2,size_t len)47*22028508SToomas Soome memcmp(const void *b1, const void *b2, size_t len)
48*22028508SToomas Soome {
49*22028508SToomas Soome const unsigned char *p1, *p2;
50*22028508SToomas Soome
51*22028508SToomas Soome for (p1 = b1, p2 = b2; len > 0; len--, p1++, p2++) {
52*22028508SToomas Soome if (*p1 != *p2)
53*22028508SToomas Soome return ((*p1) - (*p2));
54*22028508SToomas Soome }
55*22028508SToomas Soome return (0);
56*22028508SToomas Soome }
57*22028508SToomas Soome
58*22028508SToomas Soome int
strcmp(const char * s1,const char * s2)59*22028508SToomas Soome strcmp(const char *s1, const char *s2)
60*22028508SToomas Soome {
61*22028508SToomas Soome
62*22028508SToomas Soome for (; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
63*22028508SToomas Soome ;
64*22028508SToomas Soome return ((unsigned char)*s1 - (unsigned char)*s2);
65*22028508SToomas Soome }
66*22028508SToomas Soome
67*22028508SToomas Soome int
strncmp(const char * s1,const char * s2,size_t len)68*22028508SToomas Soome strncmp(const char *s1, const char *s2, size_t len)
69*22028508SToomas Soome {
70*22028508SToomas Soome
71*22028508SToomas Soome for (; len > 0 && *s1 == *s2 && *s1 != '\0'; len--, s1++, s2++)
72*22028508SToomas Soome ;
73*22028508SToomas Soome return (len == 0 ? 0 : (unsigned char)*s1 - (unsigned char)*s2);
74*22028508SToomas Soome }
75*22028508SToomas Soome
76*22028508SToomas Soome void
strcpy(char * dst,const char * src)77*22028508SToomas Soome strcpy(char *dst, const char *src)
78*22028508SToomas Soome {
79*22028508SToomas Soome
80*22028508SToomas Soome while (*src != '\0')
81*22028508SToomas Soome *dst++ = *src++;
82*22028508SToomas Soome *dst = '\0';
83*22028508SToomas Soome }
84*22028508SToomas Soome
85*22028508SToomas Soome void
strcat(char * dst,const char * src)86*22028508SToomas Soome strcat(char *dst, const char *src)
87*22028508SToomas Soome {
88*22028508SToomas Soome
89*22028508SToomas Soome while (*dst != '\0')
90*22028508SToomas Soome dst++;
91*22028508SToomas Soome while (*src != '\0')
92*22028508SToomas Soome *dst++ = *src++;
93*22028508SToomas Soome *dst = '\0';
94*22028508SToomas Soome }
95*22028508SToomas Soome
96*22028508SToomas Soome char *
strchr(const char * s,char ch)97*22028508SToomas Soome strchr(const char *s, char ch)
98*22028508SToomas Soome {
99*22028508SToomas Soome
100*22028508SToomas Soome for (; *s != '\0'; s++) {
101*22028508SToomas Soome if (*s == ch)
102*22028508SToomas Soome return ((char *)(uintptr_t)(const void *)s);
103*22028508SToomas Soome }
104*22028508SToomas Soome return (NULL);
105*22028508SToomas Soome }
106*22028508SToomas Soome
107*22028508SToomas Soome size_t
strlen(const char * s)108*22028508SToomas Soome strlen(const char *s)
109*22028508SToomas Soome {
110*22028508SToomas Soome size_t len = 0;
111*22028508SToomas Soome
112*22028508SToomas Soome while (*s++ != '\0')
113*22028508SToomas Soome len++;
114*22028508SToomas Soome return (len);
115*22028508SToomas Soome }
116*22028508SToomas Soome
117*22028508SToomas Soome int
printf(const char * fmt,...)118*22028508SToomas Soome printf(const char *fmt, ...)
119*22028508SToomas Soome {
120*22028508SToomas Soome va_list ap;
121*22028508SToomas Soome const char *hex = "0123456789abcdef";
122*22028508SToomas Soome char buf[32], *s;
123*22028508SToomas Soome uint16_t *S;
124*22028508SToomas Soome unsigned long long u;
125*22028508SToomas Soome int c, l;
126*22028508SToomas Soome
127*22028508SToomas Soome va_start(ap, fmt);
128*22028508SToomas Soome while ((c = *fmt++) != '\0') {
129*22028508SToomas Soome if (c != '%') {
130*22028508SToomas Soome putchar(c);
131*22028508SToomas Soome continue;
132*22028508SToomas Soome }
133*22028508SToomas Soome l = 0;
134*22028508SToomas Soome nextfmt:
135*22028508SToomas Soome c = *fmt++;
136*22028508SToomas Soome switch (c) {
137*22028508SToomas Soome case 'l':
138*22028508SToomas Soome l++;
139*22028508SToomas Soome goto nextfmt;
140*22028508SToomas Soome case 'c':
141*22028508SToomas Soome putchar(va_arg(ap, int));
142*22028508SToomas Soome break;
143*22028508SToomas Soome case 's':
144*22028508SToomas Soome for (s = va_arg(ap, char *); *s != '\0'; s++)
145*22028508SToomas Soome putchar(*s);
146*22028508SToomas Soome break;
147*22028508SToomas Soome case 'S': /* Assume console can cope with wide chars */
148*22028508SToomas Soome for (S = va_arg(ap, uint16_t *); *S != 0; S++)
149*22028508SToomas Soome putchar(*S);
150*22028508SToomas Soome break;
151*22028508SToomas Soome case 'd': /* A lie, always prints unsigned */
152*22028508SToomas Soome case 'u':
153*22028508SToomas Soome case 'x':
154*22028508SToomas Soome switch (l) {
155*22028508SToomas Soome case 2:
156*22028508SToomas Soome u = va_arg(ap, unsigned long long);
157*22028508SToomas Soome break;
158*22028508SToomas Soome case 1:
159*22028508SToomas Soome u = va_arg(ap, unsigned long);
160*22028508SToomas Soome break;
161*22028508SToomas Soome default:
162*22028508SToomas Soome u = va_arg(ap, unsigned int);
163*22028508SToomas Soome break;
164*22028508SToomas Soome }
165*22028508SToomas Soome s = buf;
166*22028508SToomas Soome if (c == 'd' || c == 'u') {
167*22028508SToomas Soome do
168*22028508SToomas Soome *s++ = '0' + (u % 10U);
169*22028508SToomas Soome while (u /= 10);
170*22028508SToomas Soome } else {
171*22028508SToomas Soome do
172*22028508SToomas Soome *s++ = hex[u & 0xfu];
173*22028508SToomas Soome while (u >>= 4);
174*22028508SToomas Soome }
175*22028508SToomas Soome while (--s >= buf)
176*22028508SToomas Soome putchar(*s);
177*22028508SToomas Soome break;
178*22028508SToomas Soome }
179*22028508SToomas Soome }
180*22028508SToomas Soome va_end(ap);
181*22028508SToomas Soome return (0);
182*22028508SToomas Soome }
183