Add printf

This commit is contained in:
pjht 2019-08-31 18:47:00 -05:00
parent 7c05554b60
commit 27d0fd6bfd
2 changed files with 96 additions and 1 deletions

View File

@ -191,7 +191,13 @@ int main() {
file=fopen("/dev/vga","w");
vga_write_string("FOPEN RETURNED\n");
} while(file==NULL);
do {
vga_write_string("CALLING FOPEN\n");
file=fopen("/dev/vga","w");
vga_write_string("FOPEN RETURNED\n");
} while(file==NULL);
vga_write_string("CALLING FPUTS\n");
fputs("FPUTS String",file);
fputs("FPUTS String\n",file);
vga_write_string("FPUTS RETURNED\n");
printf("Printf %d\n",size);
}

View File

@ -182,3 +182,92 @@ void mount(char* file,char* type,char* path) {
}
free(msg.msg);
}
int vfprintf(FILE* stream,const char* format,va_list arg) {
int c;
for(;*format!='\0';format++) {
if(*format!='%') {
c=fputc(*format,stream);
if (c==EOF) {
return EOF;
}
continue;
}
format++;
switch(*format) {
case 'c': {
int i=va_arg(arg,int);
c=fputc(i,stream);
if (c==EOF) {
return EOF;
}
break;
}
case 'd': {
int i=va_arg(arg,int); //Fetch Decimal/Integer argument
if(i<0) {
i=-i;
fputc('-',stream);
}
char str[11];
int_to_ascii(i,str);
c=fputs(str,stream);
if (c==EOF) {
return EOF;
}
break;
}
// case 'o': {
// int i=va_arg(arg,unsigned int); //Fetch Octal representation
// puts(convert(i,8));
// break;
// }
case 's': {
char* s=va_arg(arg,char*);
c=fputs(s,stream);
if (c==EOF) {
return EOF;
}
break;
}
case 'x': {
uint32_t i=va_arg(arg,uint32_t);
char str[11];
str[0]='\0';
hex_to_ascii(i,str);
c=fputs(str,stream);
if (c==EOF) {
return EOF;
}
break;
}
}
}
return 1;
}
int fprintf(FILE* stream,const char* format,...) {
va_list arg;
int code;
va_start(arg,format);
code=vfprintf(stream,format,arg);
va_end(arg);
if (code) {
return strlen(format);
} else {
return EOF;
}
}
int printf(const char* format,...) {
va_list arg;
int code;
va_start(arg,format);
code=vfprintf(stdout,format,arg);
va_end(arg);
if (code) {
return strlen(format);
} else {
return EOF;
}
}