-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathulib.c
75 lines (66 loc) · 1022 Bytes
/
ulib.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
#include "ulib.h"
#include "sys_call_entry.h"
#include "string.h"
int printf(const char *fmt, ...)
{
int rc;
char buf[256];
va_list arg;
arg = (va_list)((char *)(&fmt) + sizeof(fmt));
rc = vsprintf(buf, fmt, arg);
write(buf, rc);
return rc;
}
int vsprintf(char *buf, const char *fmt, va_list args)
{
char *p;
char tmp[256];
va_list p_next_arg = args;
for (p=buf; *fmt; fmt++) {
if (*fmt != '%') {
*p++ = *fmt;
continue;
}
fmt++;
switch (*fmt) {
case 'x':
itoa(tmp, *((int *)p_next_arg));
strcmp(p, tmp);
p_next_arg += 4;
p += strlen(tmp);
break;
case 's':
break;
default:
break;
}
}
return (p - buf);
}
char * itoa(char * str, int num)
{
char * p = str;
char ch;
int i;
int flag = 0;
*p++ = '0';
*p++ = 'x';
if(num == 0){
*p++ = '0';
}
else{
for(i=28;i>=0;i-=4){
ch = (num >> i) & 0xF;
if(flag || (ch > 0)){
flag = 1;
ch += '0';
if(ch > '9'){
ch += 7;
}
*p++ = ch;
}
}
}
*p = 0;
return str;
}