This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_printf.c
122 lines (109 loc) · 2.76 KB
/
sys_printf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* A simple vsnprintf implementation */
/* dietlibc complains (rightfully) about the full *printf code being bloated.
Luckily, init does not need all that stuff. Only (v)snprintf is used,
and only with %s %i %c %m placeholders; only limited width support is needed.
So the following is a minimalistic implementation that provides just enough
of dietlibc printf for init to use.
Keeping printf-style format for warn() messages allows seamless substitution
of a full-size printf if necessary, in particular when shared libc is used. */
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdarg.h>
#include <errno.h>
#ifdef exportall
#define static
#endif
int snprintf(char* buf, size_t len, const char* fmt, ...);
int vsnprintf(char* buf, size_t len, const char* fmt, va_list ap);
static int skiptofmt(const char* s)
{
const char* p;
for(p = s; *p && *p != '%'; p++);
return p - s;
}
static int strlncpy(char* dst, size_t dstspace, const char* src, size_t srclen)
{
int cpn = srclen < dstspace ? srclen : dstspace;
memcpy(dst, src, cpn);
return cpn;
}
static int itostr(char* buf, size_t len, int n)
{
short i;
short w = 0;
short s = n < 0 ? 1 : 0; n = n < 0 ? -n : n;
len--;
do {
for(i = w < len ? w : len; i >= 1; i--)
buf[i] = buf[i-1];
buf[0] = '0' + (n % 10);
w++; n /= 10;
} while(n); if(s) {
for(i = w < len ? w : len; i >= 1; i--)
buf[i] = buf[i-1];
buf[0] = '-';
w++;
}
len++;
return w < len ? w : len;
}
int vsnprintf(char* buf, size_t len, const char* fmt, va_list ap)
{
int stf;
int cpn;
int ret = 0;
int pad;
const char* arg;
len--; /* for terminating \0 */
while(*fmt && len) {
if(*fmt == '%') {
fmt++;
pad = 0;
refmt: switch(*(fmt++)) {
case '-':
/* %-*s for compatibility with common libc printfs */
goto refmt;
case '*':
pad = va_arg(ap, int);
goto refmt;
case 's':
arg = va_arg(ap, const char*);
if(!arg) arg = "(null)";
cpn = strlncpy(buf, len, arg, strlen(arg));
break;
case 'i':
cpn = itostr(buf, len, va_arg(ap, int));
break;
case 'c':
if(len > 0) *buf = (char)va_arg(ap, int);
cpn = len > 0 ? 1 : 0;
break;
case 'm':
if((arg = strerror(errno)))
cpn = strlncpy(buf, len, arg, strlen(arg));
else
cpn = itostr(buf, len, errno);
break;
case '\0':
/* the pointer has been moved beyond string boundary,
got to bring it back */
fmt--;
default:
/* can't risk guessing stack contents here, so bail out */
*buf = '\0';
return ret;
} while(cpn < pad && cpn < len)
buf[cpn++] = ' ';
} else {
stf = skiptofmt(fmt);
cpn = strlncpy(buf, len, fmt, stf);
fmt += cpn;
}
len -= cpn;
ret += cpn;
buf += cpn;
}
*buf = '\0';
return ret;
}