1199767f8SToomas Soome /*-
2199767f8SToomas Soome * Copyright (c) 1998 Robert Nordier
3199767f8SToomas Soome * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4199767f8SToomas Soome * All rights reserved.
5199767f8SToomas Soome *
6199767f8SToomas Soome * Redistribution and use in source and binary forms are freely
7199767f8SToomas Soome * permitted provided that the above copyright notice and this
8199767f8SToomas Soome * paragraph and the following disclaimer are duplicated in all
9199767f8SToomas Soome * such forms.
10199767f8SToomas Soome *
11199767f8SToomas Soome * This software is provided "AS IS" and without any express or
12199767f8SToomas Soome * implied warranties, including, without limitation, the implied
13199767f8SToomas Soome * warranties of merchantability and fitness for a particular
14199767f8SToomas Soome * purpose.
15199767f8SToomas Soome */
16199767f8SToomas Soome
17199767f8SToomas Soome #include <sys/cdefs.h>
18199767f8SToomas Soome __FBSDID("$FreeBSD$");
19199767f8SToomas Soome
20199767f8SToomas Soome #include <sys/param.h>
21199767f8SToomas Soome
22199767f8SToomas Soome #include <stdarg.h>
23199767f8SToomas Soome
24199767f8SToomas Soome #include "cons.h"
25199767f8SToomas Soome #include "util.h"
26199767f8SToomas Soome
27199767f8SToomas Soome void
memcpy(void * dst,const void * src,int len)28199767f8SToomas Soome memcpy(void *dst, const void *src, int len)
29199767f8SToomas Soome {
30199767f8SToomas Soome const char *s = src;
31199767f8SToomas Soome char *d = dst;
32199767f8SToomas Soome
33199767f8SToomas Soome while (len--)
34199767f8SToomas Soome *d++ = *s++;
35199767f8SToomas Soome }
36199767f8SToomas Soome
37199767f8SToomas Soome void
memset(void * b,int c,size_t len)38199767f8SToomas Soome memset(void *b, int c, size_t len)
39199767f8SToomas Soome {
40199767f8SToomas Soome char *bp = b;
41199767f8SToomas Soome
42199767f8SToomas Soome while (len--)
43199767f8SToomas Soome *bp++ = (unsigned char)c;
44199767f8SToomas Soome }
45199767f8SToomas Soome
46199767f8SToomas Soome int
memcmp(const void * b1,const void * b2,size_t len)47199767f8SToomas Soome memcmp(const void *b1, const void *b2, size_t len)
48199767f8SToomas Soome {
49199767f8SToomas Soome const unsigned char *p1, *p2;
50199767f8SToomas Soome
51199767f8SToomas Soome for (p1 = b1, p2 = b2; len > 0; len--, p1++, p2++) {
52199767f8SToomas Soome if (*p1 != *p2)
53199767f8SToomas Soome return ((*p1) - (*p2));
54199767f8SToomas Soome }
55199767f8SToomas Soome return (0);
56199767f8SToomas Soome }
57199767f8SToomas Soome
58199767f8SToomas Soome int
strcmp(const char * s1,const char * s2)59199767f8SToomas Soome strcmp(const char *s1, const char *s2)
60199767f8SToomas Soome {
61199767f8SToomas Soome
62199767f8SToomas Soome for (; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
63199767f8SToomas Soome ;
64199767f8SToomas Soome return ((unsigned char)*s1 - (unsigned char)*s2);
65199767f8SToomas Soome }
66199767f8SToomas Soome
67199767f8SToomas Soome int
strncmp(const char * s1,const char * s2,size_t len)68199767f8SToomas Soome strncmp(const char *s1, const char *s2, size_t len)
69199767f8SToomas Soome {
70199767f8SToomas Soome
71199767f8SToomas Soome for (; len > 0 && *s1 == *s2 && *s1 != '\0'; len--, s1++, s2++)
72199767f8SToomas Soome ;
73199767f8SToomas Soome return (len == 0 ? 0 : (unsigned char)*s1 - (unsigned char)*s2);
74199767f8SToomas Soome }
75199767f8SToomas Soome
76199767f8SToomas Soome void
strcpy(char * dst,const char * src)77199767f8SToomas Soome strcpy(char *dst, const char *src)
78199767f8SToomas Soome {
79199767f8SToomas Soome
80199767f8SToomas Soome while (*src != '\0')
81199767f8SToomas Soome *dst++ = *src++;
82199767f8SToomas Soome *dst = '\0';
83199767f8SToomas Soome }
84199767f8SToomas Soome
85199767f8SToomas Soome void
strcat(char * dst,const char * src)86199767f8SToomas Soome strcat(char *dst, const char *src)
87199767f8SToomas Soome {
88199767f8SToomas Soome
89199767f8SToomas Soome while (*dst != '\0')
90199767f8SToomas Soome dst++;
91199767f8SToomas Soome while (*src != '\0')
92199767f8SToomas Soome *dst++ = *src++;
93199767f8SToomas Soome *dst = '\0';
94199767f8SToomas Soome }
95199767f8SToomas Soome
96199767f8SToomas Soome char *
strchr(const char * s,char ch)97199767f8SToomas Soome strchr(const char *s, char ch)
98199767f8SToomas Soome {
99199767f8SToomas Soome
100199767f8SToomas Soome for (; *s != '\0'; s++) {
101199767f8SToomas Soome if (*s == ch)
102199767f8SToomas Soome return ((char *)(uintptr_t)(const void *)s);
103199767f8SToomas Soome }
104199767f8SToomas Soome return (NULL);
105199767f8SToomas Soome }
106199767f8SToomas Soome
107199767f8SToomas Soome size_t
strlen(const char * s)108199767f8SToomas Soome strlen(const char *s)
109199767f8SToomas Soome {
110199767f8SToomas Soome size_t len = 0;
111199767f8SToomas Soome
112199767f8SToomas Soome while (*s++ != '\0')
113199767f8SToomas Soome len++;
114199767f8SToomas Soome return (len);
115199767f8SToomas Soome }
116199767f8SToomas Soome
117199767f8SToomas Soome int
printf(const char * fmt,...)118199767f8SToomas Soome printf(const char *fmt, ...)
119199767f8SToomas Soome {
120199767f8SToomas Soome va_list ap;
121199767f8SToomas Soome const char *hex = "0123456789abcdef";
122199767f8SToomas Soome char buf[32], *s;
123cfba4bc6SToomas Soome uint16_t *S;
124199767f8SToomas Soome unsigned long long u;
125199767f8SToomas Soome int c, l;
126199767f8SToomas Soome
127199767f8SToomas Soome va_start(ap, fmt);
128199767f8SToomas Soome while ((c = *fmt++) != '\0') {
129199767f8SToomas Soome if (c != '%') {
130199767f8SToomas Soome putchar(c);
131199767f8SToomas Soome continue;
132199767f8SToomas Soome }
133199767f8SToomas Soome l = 0;
134199767f8SToomas Soome nextfmt:
135199767f8SToomas Soome c = *fmt++;
136199767f8SToomas Soome switch (c) {
137199767f8SToomas Soome case 'l':
138199767f8SToomas Soome l++;
139199767f8SToomas Soome goto nextfmt;
140199767f8SToomas Soome case 'c':
141199767f8SToomas Soome putchar(va_arg(ap, int));
142199767f8SToomas Soome break;
143199767f8SToomas Soome case 's':
144199767f8SToomas Soome for (s = va_arg(ap, char *); *s != '\0'; s++)
145199767f8SToomas Soome putchar(*s);
146199767f8SToomas Soome break;
147cfba4bc6SToomas Soome case 'S': /* Assume console can cope with wide chars */
148cfba4bc6SToomas Soome for (S = va_arg(ap, uint16_t *); *S != 0; S++)
149cfba4bc6SToomas Soome putchar(*S);
150cfba4bc6SToomas Soome break;
151199767f8SToomas Soome case 'd': /* A lie, always prints unsigned */
152199767f8SToomas Soome case 'u':
153199767f8SToomas Soome case 'x':
154199767f8SToomas Soome switch (l) {
155199767f8SToomas Soome case 2:
156199767f8SToomas Soome u = va_arg(ap, unsigned long long);
157199767f8SToomas Soome break;
158199767f8SToomas Soome case 1:
159199767f8SToomas Soome u = va_arg(ap, unsigned long);
160199767f8SToomas Soome break;
161199767f8SToomas Soome default:
162199767f8SToomas Soome u = va_arg(ap, unsigned int);
163199767f8SToomas Soome break;
164199767f8SToomas Soome }
165199767f8SToomas Soome s = buf;
166199767f8SToomas Soome if (c == 'd' || c == 'u') {
167199767f8SToomas Soome do
168199767f8SToomas Soome *s++ = '0' + (u % 10U);
169199767f8SToomas Soome while (u /= 10);
170199767f8SToomas Soome } else {
171199767f8SToomas Soome do
172199767f8SToomas Soome *s++ = hex[u & 0xfu];
173199767f8SToomas Soome while (u >>= 4);
174199767f8SToomas Soome }
175199767f8SToomas Soome while (--s >= buf)
176199767f8SToomas Soome putchar(*s);
177199767f8SToomas Soome break;
178199767f8SToomas Soome }
179199767f8SToomas Soome }
180199767f8SToomas Soome va_end(ap);
181199767f8SToomas Soome return (0);
182199767f8SToomas Soome }
183