-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_printf.c
176 lines (159 loc) · 2.87 KB
/
my_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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* COMAN ROBERT - 323CB*/
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
static int write_stdout(const char *token, int length)
{
int rc;
int bytes_written = 0;
do {
rc = write(1, token + bytes_written, length - bytes_written);
if (rc < 0)
return rc;
bytes_written += rc;
} while (bytes_written < length);
return bytes_written;
}
static void swap(char *a, char *b)
{
char c = *a;
*a = *b;
*b = c;
}
static void mirror(char *s)
{
char* start = s, *end = s;
while(*end != '\0')
end++;
end--;
int l = strlen(s);
for(int i = 0; i < l / 2; i++){
swap(start, end);
start++;
end--;
}
}
static int write_char(char c);
static int write_int(int x)
{
if(x == 0){
write_stdout("0", 1);
return 1;
}
char* string = malloc(100);
int i = 0, negative = 0;
if(x < 0){
write_char('-');
negative++;
}
while(x){
string[i] = abs((x % 10)) + '0';
i++;
x /= 10;
}
string[i] = '\0';
mirror(string);
int len = strlen(string);
write_stdout(string, strlen(string));
free(string);
return len + negative;
}
static int write_unsigned_int(unsigned int x){
if(x == 0){
write_stdout("0", 1);
return 1;
}
char* string = malloc(100);
if(!string)
exit(-1);
int i = 0;
while(x){
string[i] = (x % 10) + '0';
i++;
x /= 10;
}
string[i] = '\0';
mirror(string);
write_stdout(string, strlen(string));
int len = strlen(string);
free(string);
return len;
}
static int write_hex(unsigned int x){
if(x == 0){
write_stdout("0", 1);
return 1;
}
char* string = malloc(100);
int i = 0;
while(x){
int digit = x % 16;
if(digit > 9)
string[i] = digit - 10 + 'a';
else
string[i] = digit + '0';
i++;
x /= 16;
}
string[i] = '\0';
mirror(string);
write_stdout(string, strlen(string));
int len = strlen(string);
free(string);
return len;
}
static int write_char(char c){
char* string = malloc(1);
if(!string)
return 0;
string[0] = c;
write_stdout(string, 1);
free(string);
return 1;
}
static int write_string(char* string){
for(int i = 0; i < strlen(string); i++)
write_char(string[i]);
return strlen(string);
}
int iocla_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
int nr_args = 0;
char* s = (char*) format;
// iterating through "format"
while(*s != '\0'){
if(s[0] == '%'){
if(s[1] == 'd'){
nr_args += write_int(va_arg(args, int));
}
else if(s[1] == 'u'){
nr_args += write_unsigned_int(va_arg(args, unsigned int));
}
else if(s[1] == 'x'){
nr_args += write_hex(va_arg(args, unsigned int));
}
else if(s[1] =='c'){
nr_args += write_char(va_arg(args, int));
}
else if(s[1] == 's'){
char* s = va_arg(args, char*);
nr_args += write_string(s);
}
else if(s[1] == '%'){
nr_args += write_char('%');
}
s += 2;
}
else{
write_char(s[0]);
s++;
nr_args++;
}
}
return nr_args;
}