forked from embedded2014/freertos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clib.c
162 lines (145 loc) · 3.74 KB
/
clib.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "fio.h"
#include <stdarg.h>
#include "clib.h"
void send_byte(char );
size_t fio_printf(int fd, const char *format, ...){
int i,count=0;
va_list(v1);
va_start(v1, format);
int tmpint;
char *tmpcharp;
for(i=0; format[i]; ++i){
if(format[i]=='%'){
switch(format[i+1]){
case '%':
send_byte('%'); break;
case 'd':
case 'x':
case 'X':
tmpint = va_arg(v1, int);
tmpcharp = itoa(format[i+1]=='x'?"0123456789abcdef":"0123456789ABCDEF", tmpint, format[i+1]=='d'?10: 16);
fio_write(fd, tmpcharp, strlen(tmpcharp));
break;
case 's':
tmpcharp = va_arg(v1, char *);
fio_write(fd, tmpcharp, strlen(tmpcharp));
break;
}
/* Skip the next character */
++i;
}else
fio_write(fd, format+i, 1);
}
va_end(v1);
return count;
}
int sprintf(char *dest, const char *format, ...){
int i,count=0, p;
va_list(v1);
va_start(v1, format);
int tmpint;
char *tmpcharp;
char tmpchar;
for(i=0, p=0; format[i]; ++i){
if(format[i]=='%'){
switch(format[i+1]){
case '%':
dest[p++]='%'; break;
case 'd':
case 'x':
case 'X':
case 'u':
tmpint = va_arg(v1, int);
if(format[i+1]=='u')
tmpcharp = utoa(format[i+1]=='X'?"0123456789ABCDEF":"0123456789abcdef" ,(unsigned)tmpint, 10);
else
tmpcharp = itoa(format[i+1]=='X'?"0123456789ABCDEF":"0123456789abcdef", tmpint, format[i+1]=='d'?10: 16);
//fio_write(fd, tmpcharp, 3);i
for(;*tmpcharp;++tmpcharp, ++p)
dest[p]=*tmpcharp;
break;
case 's':
tmpcharp = va_arg(v1, char *);
for(;*tmpcharp;++tmpcharp, ++p)
dest[p]=*tmpcharp;
break;
case 'c':
tmpchar = va_arg(v1, int);
dest[p++]=tmpchar;
break;
}
/* Skip the next character */
++i;
}else
dest[p++]=format[i];
}
va_end(v1);
dest[p]='\0';
return count;
}
size_t strlen(const char *str){
size_t count;
for(count=0;*str;++count, ++str);
return count;
}
char *strcat(char * restrict dest, const char * restrict source){
/* locate '\0' in dest */
for(;*dest;++dest);
/* copy character from source */
for(;*source; ++dest, ++source)
*dest=*source;
*dest='\0';
return dest;
}
char *itoa(const char *numbox, int num, unsigned int base){
static char buf[32]={0};
int i;
if(num==0){
buf[30]='0';
return &buf[30];
}
int negative=(num<0);
if(negative) num=-num;
for(i=30; i>=0&# --i, num/=base)
buf[i] = numbox[num % base];
if(negative){
buf[i]='-';
--i;
}
return buf+i+1;
}
char *utoa(const char *numbox, unsigned int num, unsigned int base){
static char buf[32]={0};
int i;
if(num==0){
buf[30]='0';
return &buf[30];
}
for(i=30; i>=0&# --i, num/=base)
buf[i] = numbox [num % base];
return buf+i+1;
}
// atoi - convert string to integer
// return -1 if numstr is not a number
// return 0 if success
// Parameter:
// *numstr: string to convert from
// *num: int variable to store the result
int atoi(char *numstr, signed int *num) {
int result = 0;
int i = 0,neg = 0;
if (numstr[0] == '-') {
neg = 1;
i = 1;
}
for (; numstr[i] != '\0'; i++) {
if (numstr[i] < '0' && numstr[i] > '9')
return -1;
result = result * 10 + numstr[i] - '0';
}
if (neg == 1)
*num = -(result);
else
*num = result;
return 0;
}