I need to build a OS, a very small and basic one, with actually least functionality, coded in C.
Probably a CUI OS which does some memory management and has at least a text editor and a calculator, its just going to be a experimentation about how to make a code that has full and direct control over your hardware.
Still I'll be requiring an interface, that will need input/output functions like printf(&args), scanf(&args). Now my basic question is should I use existing headers or go for coding actually from scratch, and why so ?
I'd be more than very thankful to you guys for and help.
First, you can't link against anything from
libc
... you're going to have to code everything from scratch.Now having worked on a micro-kernel myself, I would not use the actual
stdio
headers that come withlibc
since they are going to be cluttered with a lot of extra information that will be either irrelevant for your OS, or will create compiler errors due to missing definitions, etc. What I would do though is keep the function signatures for these standard functions the same ... so in the end you would have a file calledstdio.h
for your OS, but it would be a very stripped down header file with the basic minimum requirements for your needs, and only having the standard I/O functions you need, with the correct standard signatures.Keep in mind on the back-end, i.e., in your
stdio.c
file, you're going to have to point these functions to a custom console-driver or some other type of character drive for your display. Either that, or you could just use them as wrappers for some other kernel-level display printing routine. You are also going to want to make sure that even though you may use a#include <stdio.h>
directive in your other OS code modules to access these printing functions, you do not link againstlibc
. This can be done usinggcc -ffreestanding
.Kernel itself would not require the
printf
andscanf
functions, if you do not want to keep the kernel in kernel mode and work the apps you have planned for. But for basic printf and scanf features, you can write your own printf and scanf functions, which would provide basic support for printing ans taking input. I do not have much experience on this, but you can try make a console buffer, where the keyboard driver puts the read in ASCII characters (after conversion from scan codes), and then make theprintf
andscanf
work on it. I have one basic implementation were i have wrote agets
instead ofscanf
and kept things simple. To get integer output you can write anatoi
function to convert the string to a number.To port in other libraries, you need to make the components which the libraries depend on. You need to make the decision if you can code in those support in the kernel so that the libraries could be ported in. If it is more difficult then coding some basic input output functions i think won't be bad at this stage,
Just retarget newlib.
printf, scanf,
etc relies on implementation specific funcions to get a single char or print a single char. You can then make yourstdin
andstdout
the UART 1 for example.