Skip to content

Commit 7b0c201

Browse files
committed
* Implemented vsprintf, sprintf, and printf
* Incremented kernel version * Updated README.md * Updated system.h * Updated string.h * Updated Makefile
1 parent 4a420e2 commit 7b0c201

File tree

9 files changed

+191
-8
lines changed

9 files changed

+191
-8
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ OBJECTS= $(SRCDIR)/boot/boot.o \
3333
$(SRCDIR)/drivers/io/ports.o \
3434
$(SRCDIR)/drivers/power/reboot.o \
3535
$(SRCDIR)/common/libs/string.o \
36+
$(SRCDIR)/common/libs/printf.o \
3637
$(SRCDIR)/drivers/video/vga_text.o \
3738
$(SRCDIR)/drivers/io/serial.o \
3839
$(SRCDIR)/kernel/kernel.o

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A new rewrite of SectorOS_RE2.
44

5-
The kernel is currently v0.23.03.3ALPR
5+
The kernel is currently v0.23.03.4ALPR
66

77
## Build
88

docs/changelog.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@
3232
* Implemented most of string operations
3333
* Updated system.h
3434
* Incremented kernel version
35-
* Updated README.md
35+
* Updated README.md
36+
37+
27-03-2023: 07:23 PM IST v0.23.03.4ALPR
38+
* Implemented vsprintf, sprintf, and printf
39+
* Incremented kernel version
40+
* Updated README.md
41+
* Updated system.h
42+
* Updated string.h
43+
* Updated Makefile

src/common/libs/printf.c

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include "printf.h"
2+
3+
int is_fmt_letter(char c)
4+
{
5+
return c == 'c' || c == 'd' || c == 'i' || c == 'e' || c == 'E' || c == 'f' || c == 'g' || c == 'G' || c == 'o' || c == 's' || c == 'u' || c == 'x' || c == 'X' || c == 'p' || c == 'n';
6+
}
7+
8+
void vsprintf(char *str, void (*putchar)(char), const char *fmt, va_list arg)
9+
{
10+
static uint32_t* pos;
11+
char c;
12+
int sign, ival, sys;
13+
char buf[512];
14+
char width_str[10];
15+
uint32_t uval;
16+
uint32_t size = 8;
17+
uint32_t i;
18+
int size_override = 0;
19+
memset(buf, 0, 512);
20+
21+
while ((c = *fmt++) != 0)
22+
{
23+
sign = 0;
24+
25+
if (c == '%')
26+
{
27+
c = *fmt++;
28+
switch (c)
29+
{
30+
case '0':
31+
size_override = 1;
32+
i = 0;
33+
c = *fmt;
34+
while (!is_fmt_letter(c))
35+
{
36+
width_str[i++] = c;
37+
fmt++;
38+
c = *fmt;
39+
}
40+
width_str[i] = 0;
41+
fmt++;
42+
size = atoi(width_str);
43+
case 'd':
44+
case 'u':
45+
case 'x':
46+
case 'p':
47+
{
48+
if (c == 'd' || c == 'u')
49+
sys = 10;
50+
else
51+
sys = 16;
52+
uval = ival = va_arg(arg, int);
53+
if (c == 'd' && ival < 0)
54+
{
55+
sign = 1;
56+
uval = -ival;
57+
}
58+
itoa(buf, uval, sys);
59+
uint32_t len = strlen(buf);
60+
if (!size_override)
61+
size = len;
62+
if ((c == 'x' || c == 'p' || c == 'd') && len < size)
63+
{
64+
for (i = 0; i < len; i++)
65+
{
66+
buf[size - 1 - i] = buf[len - 1 - i];
67+
}
68+
for (i = 0; i < size - len; i++)
69+
{
70+
buf[i] = '0';
71+
}
72+
}
73+
if (c == 'd' && sign)
74+
{
75+
if (str)
76+
{
77+
*(str + *pos) = '-';
78+
*pos = *pos + 1;
79+
}
80+
else
81+
(*putchar)('-');
82+
}
83+
if (str)
84+
{
85+
strcpy(str + *pos, buf);
86+
*pos = *pos + strlen(buf);
87+
}
88+
else
89+
{
90+
char *t = buf;
91+
while (*t)
92+
{
93+
putchar(*t);
94+
t++;
95+
}
96+
}
97+
}
98+
break;
99+
case 'c':
100+
{
101+
if (str)
102+
{
103+
*(str + *pos) = (char)va_arg(arg, int);
104+
*pos = *pos + 1;
105+
}
106+
else
107+
{
108+
(*putchar)((char)va_arg(arg, int));
109+
}
110+
}break;
111+
case 's':
112+
{
113+
if (str)
114+
{
115+
char *t = (char *)va_arg(arg, int);
116+
strcpy(str + (*pos), t);
117+
*pos = *pos + strlen(t);
118+
}
119+
else
120+
{
121+
char *t = (char *)va_arg(arg, int);
122+
while (*t)
123+
{
124+
putchar(*t);
125+
t++;
126+
}
127+
}
128+
}
129+
break;
130+
default:
131+
break;
132+
}
133+
continue;
134+
}
135+
if (str)
136+
{
137+
*(str + *pos) = c;
138+
*pos = *pos + 1;
139+
}
140+
else
141+
{
142+
(*putchar)(c);
143+
}
144+
}
145+
}
146+
147+
void printf(const char* fmt, ...)
148+
{
149+
va_list args;
150+
va_start(args, fmt);
151+
vsprintf(NULL, text_putc, fmt, args);
152+
va_end(args);
153+
}

src/common/libs/string.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,12 @@ void stoc(size_t n, char* buf)
305305

306306
char* stoc_r(size_t n)
307307
{
308+
}
309+
310+
void sprintf(char* buf, const char* fmt, ...)
311+
{
312+
va_list args;
313+
va_start(args, fmt);
314+
vsprintf(buf, NULL, fmt, args);
315+
va_end(args);
308316
}

src/include/printf.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef __PRINTF_H__
2+
#define __PRINTF_H__
3+
4+
#include "system.h"
5+
#include "string.h"
6+
#include "vga_text.h"
7+
8+
void vsprintf(char *buf, void (*putchar)(char), const char *fmt, va_list args);
9+
void printf(const char *fmt, ...);
10+
11+
int is_fmt_letter(char c);
12+
13+
#endif /*__PRINTF_H__*/

src/include/string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ void stoc(size_t n, char* buffer); //? Partially implemented
3636
char* stoc_r(size_t n); //! Not implemented
3737
uint32_t chbc(char* str, char c); //* Implemented
3838

39+
void sprintf(char* buf, const char* fmt, ...);
40+
3941
#endif /*__STRING_H__*/

src/include/system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void kernel_panic(char* message);
2626
* RELN: index of current release in the current month
2727
* STATUS: [PR]:Prerelease, [AL]:alpha, [NR]:Normal release
2828
*/
29-
#define KERNEL_VERSION "0.23.03.3ALPR"
29+
#define KERNEL_VERSION "0.23.03.4ALPR"
3030

3131
#define KB 1024
3232
#define MB (1024*KB)

src/kernel/kernel.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "multiboot.h"
33
#include "serial.h"
44
#include "vga_text.h"
5+
#include "printf.h"
56

67
void kernelmain(const multiboot_info_t* info, uint32_t multiboot_magic)
78
{
@@ -17,11 +18,8 @@ void kernelmain(const multiboot_info_t* info, uint32_t multiboot_magic)
1718
return;
1819
}
1920

20-
text_puts("Hello World!\n");
21-
text_puts("multiboot bootloader magic is valid...\n");
22-
text_puts("Kernel version is: ");
23-
text_puts(KERNEL_VERSION);
24-
text_putc('\n');
21+
printf("Hello World!\n");
22+
printf("The kernel verison is %s\n", KERNEL_VERSION);
2523

2624
khalt;
2725
}

0 commit comments

Comments
 (0)