Remove unnecessary files

This commit is contained in:
pjht 2020-07-24 07:14:52 -05:00
parent cd8b2eddcf
commit 611e4e1ac2
17 changed files with 0 additions and 1101 deletions

View File

@ -1,104 +0,0 @@
#include "../../cpu/i386/ports.h"
#include "../vga.h"
#include "pci.h"
#include <klog.h>
#include <stdint.h>
#include <stdlib.h>
pci_dev_common_info** pci_devs;
static uint32_t max_devs;
uint32_t pci_num_devs;
static uint32_t read_config(uint8_t bus,uint8_t device,uint8_t func,uint8_t offset) {
uint32_t address;
uint32_t lbus=(uint32_t)bus;
uint32_t ldev=(uint32_t)device;
uint32_t lfunc=(uint32_t)func;
address=(uint32_t)((lbus << 16)|(ldev << 11)|(lfunc<<8)|(offset&0xfc)|((uint32_t)0x80000000));
port_long_out(PCI_CONFIG_ADDRESS,address);
uint32_t data=port_long_in(PCI_CONFIG_DATA);
return data;
}
static void write_config(uint8_t bus,uint8_t device,uint8_t func,uint8_t offset,uint32_t data) {
uint32_t address;
uint32_t lbus=(uint32_t)bus;
uint32_t ldev=(uint32_t)device;
uint32_t lfunc=(uint32_t)func;
address=(uint32_t)((lbus << 16)|(ldev << 11)|(lfunc<<8)|(offset&0xfc)|((uint32_t)0x80000000));
port_long_out(PCI_CONFIG_ADDRESS,address);
port_long_out(PCI_CONFIG_DATA,data);
}
pci_dev_common_info* pci_get_dev_info(uint8_t bus,uint8_t device,uint8_t func) {
uint32_t* info=malloc(sizeof(uint32_t)*5);
info[0]=read_config(bus,device,func,0);
info[1]=read_config(bus,device,func,4);
info[2]=read_config(bus,device,func,8);
info[3]=read_config(bus,device,func,0xC);
pci_dev_common_info* pci_info=(pci_dev_common_info*)info;
pci_info->bus=bus;
pci_info->device=device;
pci_info->func=func;
return pci_info;
}
void pci_set_dev_info(pci_dev_common_info* inf) {
uint32_t* info=(uint32_t*)inf;
write_config(inf->bus,inf->device,inf->func,0,info[0]);
write_config(inf->bus,inf->device,inf->func,4,info[1]);
write_config(inf->bus,inf->device,inf->func,8,info[2]);
write_config(inf->bus,inf->device,inf->func,0xC,info[3]);
}
static void checkFunction(pci_dev_common_info* info);
static void checkDevice(uint8_t bus, uint8_t device) {
pci_dev_common_info* info=pci_get_dev_info(bus,device,0);
if(info->vend_id==0xFFFF||info->class_code==0xFF) {
return;
}
checkFunction(info);
if((info->header_type&0x80)!=0) {
for(uint8_t function=1;function<8;function++) {
pci_dev_common_info* info=pci_get_dev_info(bus,device,function);
if(info->vend_id!=0xFFFF&&info->class_code!=0xFF) {
checkFunction(info);
}
}
}
}
static void checkFunction(pci_dev_common_info* info) {
if (pci_num_devs==max_devs) {
max_devs+=32;
pci_devs=malloc(sizeof(pci_dev_common_info)*max_devs);
}
pci_devs[pci_num_devs]=info;
pci_num_devs++;
klog("INFO","Found PCI device. Class code:%x, Subclass:%x Prog IF:%x",info->class_code,info->subclass,info->prog_if);
klog("INFO","Vendor ID:%x, Device ID:%x",info->vend_id,info->dev_id);
if ((info->header_type&0x7f)==0) {
for (uint8_t offset=0x10;offset<0x10+4*6;offset+=4) {
uint32_t bar=read_config(info->bus,info->device,info->func,offset);
if (bar!=0) {
if (bar&0x1) {
klog("INFO","IO BAR%d:%x",(offset-0x10)/4,bar&0xFFFFFFFC);
} else {
klog("INFO","MEM BAR%d:%x",(offset-0x10)/4,bar&0xFFFFFFF0);
}
}
}
}
}
void pci_init() {
pci_devs=malloc(sizeof(pci_dev_common_info)*32);
max_devs=32;
pci_num_devs=0;
for(uint16_t bus=0;bus<1; bus++) {
for(uint8_t device=0;device<32; device++) {
checkDevice(bus, device);
}
}
}

View File

@ -1,13 +0,0 @@
#ifndef PCI_INTERN_H
#define PCI_INTERN_H
#include "../pci.h"
#include <stdint.h>
#define PCI_CONFIG_ADDRESS 0xCF8
#define PCI_CONFIG_DATA 0xCFC
pci_dev_common_info* pci_get_dev_info(uint8_t bus,uint8_t device,uint8_t func);
void pci_set_dev_info(pci_dev_common_info* inf);
#endif

View File

@ -1,159 +0,0 @@
#include "../../cpu/i386/isr.h"
#include "../../cpu/i386/ports.h"
#include "../../fs/devfs.h"
#include "../serial.h"
#include "../vga.h"
#include <devbuf.h>
#include <klog.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define SERIAL_LINE_ENABLE_DLAB 0x80
static devbuf* bufs[4];
static int data_port(int com) {
switch (com) {
case 0: return 0x3f8;
case 1: return 0x2f8;
case 2: return 0x3e8;
case 3: return 0x2e8;
}
return 0;
}
static int int_port(int com) {
switch (com) {
case 0: return 0x3f9;
case 1: return 0x2f9;
case 2: return 0x3e9;
case 3: return 0x2e9;
}
return 0;
}
static int fifo_port(int com) {
switch (com) {
case 0: return 0x3fa;
case 1: return 0x2fa;
case 2: return 0x3ea;
case 3: return 0x2ea;
}
return 0;
}
static int line_cmd_port(int com) {
switch (com) {
case 0: return 0x3fb;
case 1: return 0x2fb;
case 2: return 0x3eb;
case 3: return 0x2eb;
}
return 0;
}
static int modem_port(int com) {
switch (com) {
case 0: return 0x3fc;
case 1: return 0x2fc;
case 2: return 0x3ec;
case 3: return 0x2ec;
}
return 0;
}
static int line_stat_port(int com) {
switch (com) {
case 0: return 0x3fd;
case 1: return 0x2fd;
case 2: return 0x3ed;
case 3: return 0x2ed;
}
return 0;
}
static int scratch_port(int com) {
switch (com) {
case 0: return 0x3ff;
case 1: return 0x2ff;
case 2: return 0x3ef;
case 3: return 0x2ef;
}
return 0;
}
static void configure_baud_rate(uint32_t divisor,int com) {
port_byte_out(line_cmd_port(com),SERIAL_LINE_ENABLE_DLAB);
port_byte_out(data_port(com),(divisor>>8)&0xFF);
port_byte_out(data_port(com),divisor&0xFF);
}
static int is_transmit_fifo_empty(int com) {
return port_byte_in(line_stat_port(com))&0x20;
}
static void configure(uint32_t com, uint32_t rate) {
configure_baud_rate(115200/rate,com);
port_byte_out(line_cmd_port(com),0x03);
port_byte_out(fifo_port(com),0xC7);
port_byte_out(modem_port(com),0x03);
port_byte_out(int_port(com),0x01);
}
static int drv(char* filename,int c,long pos,char wr) {
int com;
switch (filename[4]) {
case '0':
com=0;
break;
case '1':
com=1;
break;
case '2':
com=2;
break;
case '3':
com=3;
break;
}
if (wr) {
while (!is_transmit_fifo_empty(com)) continue;
port_byte_out(data_port(com),c);
return 0;
} else {
return devbuf_get(bufs[com]);
}
}
void serial_int_handler_1(registers_t regs) {
char data=port_byte_in(data_port(0));
if (data=='\r') {
data='\n';
}
devbuf_add(data,bufs[0]);
}
void serial_int_handler_2(registers_t regs) {
devbuf_add(port_byte_in(data_port(1)),bufs[1]);
}
void serial_init() {
klog("INFO","Scanning for serial ports");
for (int i=0;i<2;i++) {
port_byte_out(scratch_port(i),0xaa);
if (port_byte_in(scratch_port(i))==0xaa) {
klog("INFO","Found COM%d",i+1);
bufs[i]=devbuf_init();
switch (i) {
case 0:
isr_register_handler(IRQ4,serial_int_handler_1);
configure(0,9600);
devfs_add(drv,"ttyS0");
break;
case 1:
isr_register_handler(IRQ3,serial_int_handler_2);
configure(1,9600);
devfs_add(drv,"ttyS1");
}
}
}
}

View File

@ -1,84 +0,0 @@
#include "../../cpu/i386/ports.h"
#include "../vga.h"
#include <grub/text_fb_info.h>
#include <stddef.h>
#include <string.h>
#define xy_to_indx(x,y) ((x+(y*width))*2)
static char* screen;
static int width;
static int height;
static int x;
static int y;
static vga_colors fg_color;
static vga_colors bg_color;
static void set_char(int x,int y,char c) {
screen[xy_to_indx(x,y)]=c;
screen[xy_to_indx(x,y)+1]=(bg_color<<4)|fg_color;
}
void vga_clear() {
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
set_char(x,y,' ');
}
}
}
static void set_cursor(int x,int y) {
int pos=(x+(y*width));
port_byte_out(0x3D4,0xF);
port_byte_out(0x3D5,pos&0xFF);
port_byte_out(0x3D4,0xE);
port_byte_out(0x3D5,(pos&0xFF00)>>8);
}
void vga_init(text_fb_info framebuffer_info) {
x=0;
y=0;
fg_color=VGA_WHITE;
bg_color=VGA_BLACK;
screen=framebuffer_info.address;
width=framebuffer_info.width;
height=framebuffer_info.height;
port_byte_out(0x3D4,0xA);
port_byte_out(0x3D5,(port_byte_in(0x3D5)&0xC0)|14);
port_byte_out(0x3D4,0xB);
port_byte_out(0x3D5,(port_byte_in(0x3D5)&0xE0)|15);
set_cursor(0,0);
vga_clear();
}
void vga_write_string(const char* string) {
for (size_t i=0;i<strlen(string);i++) {
char c=string[i];
if (c=='\n') {
x=0;
y++;
} else {
set_char(x,y,c);
x++;
}
if (x==width) {
x=0;
y++;
}
if (y==height) {
x=0;
y=0;
// char* pg1=(char*)((uint32_t)screen+0xfa0);
// memcpy(pg1,&screen[xy_to_indx(0,1)],xy_to_indx(0,24));
// vga_clear();
// memcpy(&screen,pg1,xy_to_indx(0,25));
}
}
set_cursor(x,y);
}
void vga_backspace() {
if (x!=0) {
x--;
set_char(x,y,' ');
set_cursor(x,y);
}
}

View File

@ -1,6 +0,0 @@
#ifndef PARALLEL_H
#define PARALLEL_H
void parallel_init();
#endif

View File

@ -1,49 +0,0 @@
#ifndef PCI_H
#define PCI_H
#include <stdint.h>
typedef struct {
uint16_t vend_id;
uint16_t dev_id;
uint16_t command;
uint16_t status;
uint8_t rev_id;
uint8_t prog_if;
uint8_t subclass;
uint8_t class_code;
uint8_t cache_line_size;
uint8_t lat_timer;
uint8_t header_type;
uint8_t bist;
uint16_t bus;
uint8_t device;
uint8_t func;
} __attribute__((packed)) pci_dev_common_info;
typedef enum {
PCI_CLASS_UNCLASSIFIED=0x0,
PCI_CLASS_STORAGE=0x1,
PCI_CLASS_NETWORK=0x2,
PCI_CLASS_DISPLAY=0x3,
PCI_CLASS_MULTIMEDIA=0x4,
PCI_CLASS_MEMORY=0x5,
PCI_CLASS_BRIDGE=0x6,
PCI_CLASS_SIMPCOM=0x7,
PCI_CLASS_BASEPERIPH=0x8,
PCI_CLASS_INPDEV=0x9,
PCI_CLASS_DOCK=0xa,
PCI_CLASS_CPU=0xb,
PCI_CLASS_SERBUS=0xc,
PCI_CLASS_WIRELESS=0xd,
PCI_CLASS_INTELLIGENT=0xe,
PCI_CLASS_SATELLITE=0xf,
PCI_CLASS_ENCRYPTION=0x10,
PCI_CLASS_SIGPROCESS=0x11,
} pci_class;
extern pci_dev_common_info** pci_devs;
extern uint32_t pci_num_devs;
void pci_init();
#endif

View File

@ -1,6 +0,0 @@
#ifndef PS2_EXT_H
#define PS2_EXT_H
void ps2_init();
#endif

View File

@ -1,8 +0,0 @@
#ifndef SERIAL_H
#define SERIAL_H
#include <stdint.h>
void serial_init();
#endif

View File

@ -1,7 +0,0 @@
#ifndef TIMER_H
#define TIMER_H
void wait(int milli);
void timer_init();
#endif

View File

@ -1,29 +0,0 @@
#ifndef vga_H
#define vga_H
typedef enum {
VGA_BLACK=0,
VGA_BLUE=1,
VGA_GREEN=2,
VGA_CYAN=3,
VGA_RED=4,
VGA_PURPLE=5,
VGA_BROWN=6,
VGA_GRAY=7,
VGA_DARK_GRAY=8,
VGA_LIGHT_BLUE=9,
VGA_LIGHT_GREEN=10,
VGA_LIGHT_CYAN=11,
VGA_LIGHT_RED=12,
VGA_LIGHT_PURPLE=13,
VGA_YELLOW=14,
VGA_WHITE=15
} vga_colors;
void vga_init();
void vga_write_string(const char *string);
void vga_clear();
void vga_backspace();
#endif

View File

@ -1,85 +0,0 @@
#include "../../cpu/x86_64/ports.h"
#include "../vga.h"
#include <grub/text_fb_info.h>
#include <stddef.h>
#include <string.h>
#define xy_to_indx(x,y) ((x+(y*width))*2)
static char* screen;
static int width;
static int height;
static int x;
static int y;
static vga_colors fg_color;
static vga_colors bg_color;
static void set_char(int x,int y,char c) {
screen[xy_to_indx(x,y)]=c;
screen[xy_to_indx(x,y)+1]=(bg_color<<4)|fg_color;
}
void vga_clear() {
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
set_char(x,y,' ');
}
}
}
static void set_cursor(int x,int y) {
int pos=(x+(y*width));
port_byte_out(0x3D4,0xF);
port_byte_out(0x3D5,pos&0xFF);
port_byte_out(0x3D4,0xE);
port_byte_out(0x3D5,(pos&0xFF00)>>8);
}
void vga_init(text_fb_info framebuffer_info) {
x=0;
y=0;
fg_color=VGA_WHITE;
bg_color=VGA_BLACK;
screen=framebuffer_info.address;
width=framebuffer_info.width;
height=framebuffer_info.height;
port_byte_out(0x3D4,0xA);
port_byte_out(0x3D5,(port_byte_in(0x3D5)&0xC0)|14);
port_byte_out(0x3D4,0xB);
port_byte_out(0x3D5,(port_byte_in(0x3D5)&0xE0)|15);
set_cursor(0,0);
vga_clear();
}
void vga_write_string(const char* string) {
for (size_t i=0;i<strlen(string);i++) {
char c=string[i];
if (c=='\n') {
x=0;
y++;
} else {
set_char(x,y,c);
x++;
}
if (x==width) {
x=0;
y++;
}
if (y==height) {
x=0;
y=0;
// char* pg1=(char*)((uint32_t)screen+0xfa0);
// memcpy(pg1,&screen[xy_to_indx(0,1)],xy_to_indx(0,24));
// vga_clear();
// memcpy(&screen,pg1,xy_to_indx(0,25));
}
}
set_cursor(x,y);
}
void vga_backspace() {
if (x!=0) {
x--;
set_char(x,y,' ');
set_cursor(x,y);
}
}

View File

@ -1,71 +0,0 @@
#include "../..//vfs.h"
#include "devfs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char** devices;
static dev_drv* dev_drivers;
static size_t num_devices;
static size_t max_devices;
char devfs_drv(fs_op op,FILE* stream,void* data1,void* data2) {
if (op==FSOP_MOUNT) {
return 1;
}
if (op==FSOP_OPEN) {
for (size_t i=0;i<num_devices;i++) {
if (strcmp(devices[i],stream->path)==0) {
return 1;
}
}
return 0;
}
if (op==FSOP_GETC) {
size_t i;
for (i=0;i<num_devices;i++) {
if (strcmp(devices[i],stream->path)==0) {
break;
}
}
*((int*)data1)=dev_drivers[i]((char*)stream->path,0,stream->pos,0);
stream->pos+=1;
return 1;
}
if (op==FSOP_PUTC) {
size_t i;
for (i=0;i<num_devices;i++) {
if (strcmp(devices[i],stream->path)==0) {
break;
}
}
dev_drivers[i]((char*)stream->path,*((int*)data1),stream->pos,1);
stream->pos+=1;
return 1;
}
if (op==FSOP_CLOSE) {
return 1;
}
return 0;
}
void init_devfs() {
devices=malloc(sizeof(char*)*32);
dev_drivers=malloc(sizeof(dev_drv)*32);
num_devices=0;
max_devices=32;
register_fs(devfs_drv,"devfs");
mount("/dev/","","devfs");
}
void devfs_add(dev_drv drv,char* name) {
if (num_devices==max_devices) {
devices=realloc(devices,sizeof(char*)*(max_devices+32));
dev_drivers=realloc(dev_drivers,sizeof(dev_drv)*(max_devices+32));
max_devices+=32;
}
dev_drivers[num_devices]=drv;
devices[num_devices]=malloc(sizeof(char)*(strlen(name)+1));
strcpy(devices[num_devices],name);
num_devices++;
}

View File

@ -1,9 +0,0 @@
#define DEVFS_H
#ifndef DEVFS_H
typedef int (*dev_drv)(char* filename,int c,long pos,char wr);
void init_devfs();
void devfs_add(dev_drv drv,char* name);
#endif

View File

@ -1,85 +0,0 @@
#include "../..//vfs.h"
#include <klog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char** names;
static long* offsets;
static unsigned long* sizes;
static size_t num_files;
static FILE* initrd_fd;
static char drv(fs_op op,FILE* stream,void* data1,void* data2) {
if (op==FSOP_MOUNT) {
return 1;
}
if (op==FSOP_OPEN) {
char file_exists=0;
for (size_t i=0;i<num_files;i++) {
if (strcmp(names[i],stream->path)==0) {
file_exists=1;
}
}
return file_exists;
}
if (op==FSOP_GETC) {
size_t i;
for (i=0;i<num_files;i++) {
if (strcmp(names[i],stream->path)==0) {
break;
}
}
if (stream->pos>=sizes[i]) {
*((int*)data1)=EOF;
stream->eof=1;
return 1;
}
fseek(initrd_fd,offsets[i]+stream->pos,SEEK_SET);
*((int*)data1)=fgetc(initrd_fd);
stream->pos+=1;
return 1;
}
if (op==FSOP_CLOSE) {
return 1;
}
return 0;
}
void initrd_init() {
initrd_fd=fopen("/dev/initrd","r");
if (!initrd_fd) {
klog("PANIC","Cannot open initrd!");
for(;;) {}
}
size_t max_files=32;
num_files=0;
names=malloc(sizeof(char*)*32);
offsets=malloc(sizeof(long)*32);
sizes=malloc(sizeof(long)*32);
for (size_t i=0;i<1;i++) {
if (i==max_files) {
names=realloc(names,sizeof(char*)*(max_files+32));
offsets=realloc(offsets,sizeof(long)*(max_files+32));
sizes=realloc(sizes,sizeof(long)*(max_files+32));
max_files+=32;
}
uint32_t name_size;
fread(&name_size,4,1,initrd_fd);
if (name_size==0) {
break;
}
char* name=malloc(sizeof(char)*(name_size+1));
fread(name,1,name_size+1,initrd_fd);
long contents_size;
fread(&contents_size,4,1,initrd_fd);
long datapos=ftell(initrd_fd);
fseek(initrd_fd,contents_size,SEEK_CUR);
names[i]=name;
offsets[i]=datapos;
sizes[i]=contents_size;
num_files++;
}
fseek(initrd_fd,0,SEEK_SET);
register_fs(drv,"initrd");
}

View File

@ -1,6 +0,0 @@
#ifndef INITRD_H
#define INITRD_H
uint32_t initrd_init();
#endif

View File

@ -1,358 +0,0 @@
#include "vfs.h"
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _vfs_mapping_struct {
char* mntpnt;
size_t type;
struct _vfs_mapping_struct* next;
} vfs_mapping;
static const char** drv_names;
static fs_drv* drvs;
static size_t max_drvs;
static size_t next_drv_indx;
static vfs_mapping* head_mapping;
static vfs_mapping* tail_mapping;
FILE* stdin=NULL;
FILE* stdout=NULL;
FILE* stderr=NULL;
static int vfsstrcmp(const char* s1,const char* s2) {
int i;
for (i = 0; s1[i] == s2[i]; i++) {
if (s1[i] == '\0') return 0;
}
if (s1[i] == '\0') return 0;
return s1[i] - s2[i];
}
void init_vfs() {
drvs=malloc(sizeof(fs_drv)*32);
drv_names=malloc(sizeof(const char**)*32);
max_drvs=32;
next_drv_indx=0;
head_mapping=NULL;
tail_mapping=NULL;
}
size_t register_fs(fs_drv drv,const char* type) {
if (next_drv_indx==max_drvs) {
drvs=realloc(drvs,sizeof(fs_drv)*(max_drvs+32));
drv_names=realloc(drv_names,sizeof(char*)*(max_drvs+32));
max_drvs+=32;
}
drvs[next_drv_indx]=drv;
drv_names[next_drv_indx]=type;
next_drv_indx++;
return next_drv_indx-1;
}
char mount(char* mntpnt,char* dev,char* type) {
size_t i;
for (i=0;i<next_drv_indx;i++) {
const char* name=drv_names[i];
if (strcmp(name,type)==0) {
break;
}
}
char ok=drvs[i](FSOP_MOUNT,NULL,mntpnt,dev);
if (ok) {
if (head_mapping==NULL) {
vfs_mapping* mapping=malloc(sizeof(vfs_mapping));
mapping->mntpnt=malloc(sizeof(char)*(strlen(mntpnt)+1));
strcpy(mapping->mntpnt,mntpnt);
mapping->type=i;
mapping->next=NULL;
head_mapping=mapping;
tail_mapping=mapping;
} else {
vfs_mapping* mapping=malloc(sizeof(vfs_mapping));
mapping->mntpnt=malloc(sizeof(char)*(strlen(mntpnt)+1));
strcpy(mapping->mntpnt,mntpnt);
mapping->type=i;
mapping->next=NULL;
tail_mapping->next=mapping;
}
return 1;
} else {
return 0;
}
}
FILE* fopen(const char* filename,const char* mode) {
vfs_mapping* mnt=head_mapping;
vfs_mapping* mntpnt=NULL;
const char* path;
size_t mntpnt_len=0;
for (;mnt!=NULL;mnt=mnt->next) {
char* root=mnt->mntpnt;
if (strlen(root)>mntpnt_len) {
if (vfsstrcmp(root,filename)==0) {
mntpnt=mnt;
mntpnt_len=strlen(root);
}
}
}
if (mntpnt) {
path=filename+mntpnt_len;
FILE* stream=malloc(sizeof(FILE));
stream->mntpnt=mntpnt->mntpnt;
stream->path=path;
if (strcmp(mode,"w")==0) {
stream->wr=1;
stream->rd=0;
} else if (strcmp(mode,"r+")==0) {
stream->wr=1;
stream->rd=1;
} else {
stream->wr=0;
stream->rd=1;
}
stream->type=mntpnt->type;
stream->pos=0;
stream->eof=0;
stream->error=0;
char ok=drvs[mntpnt->type](FSOP_OPEN,stream,NULL,NULL);
if (ok) {
return stream;
} else {
free(stream);
return NULL;
}
}
return NULL;
}
int fgetc(FILE* stream) {
if (!stream->rd) {
errno=EBADF;
stream->error=1;
return EOF;
}
int c;
drvs[stream->type](FSOP_GETC,stream,&c,NULL);
return c;
}
int getc() {
return fgetc(stdin);
}
char* fgets(char* str,int count,FILE* stream) {
if (!stream->rd) {
errno=EBADF;
stream->error=1;
return NULL;
}
int i;
for (i=0;i<count-1;i++) {
char c=fgetc(stream);
if (c==EOF) {
return NULL;
} else if (c=='\n') {
str[i]=c;
i++;
break;
}
str[i]=c;
}
str[i]='\0';
return str;
}
size_t fread(void* buffer_ptr,size_t size,size_t count,FILE* stream) {
if (!stream->rd) {
errno=EBADF;
stream->error=1;
return 0;
}
char* buffer=(char*)buffer_ptr;
size_t bytes=size*count;
for (size_t i=0;i<bytes;i++) {
int c=fgetc(stream);
if (c==EOF) {
return (size_t)(i/size);
}
buffer[i]=c;
}
return count;
}
int fputc(int c,FILE* stream) {
if (!stream->wr) {
errno=EBADF;
stream->error=1;
return EOF;
}
char ok=drvs[stream->type](FSOP_PUTC,stream,&c,NULL);
if (ok) {
return c;
} else {
return EOF;
}
}
int putc(int c) {
return fputc(c,stdout);
}
int fputs(const char* s,FILE* stream) {
if (!stream->wr) {
errno=EBADF;
stream->error=1;
return EOF;
}
size_t len=strlen(s);
for (size_t i=0;i<len;i++) {
int c=fputc(s[i],stream);
if (c==EOF) {
return EOF;
}
}
return 1;
}
int puts(const char* s) {
return fputs(s,stdout);
}
size_t fwrite(void* buffer_ptr,size_t size,size_t count,FILE* stream) {
if (!stream->wr) {
errno=EBADF;
stream->error=1;
return 0;
}
char* buffer=(char*)buffer_ptr;
size_t bytes=size*count;
for (size_t i=0;i<bytes;i++) {
int c=fputc((char)buffer[i],stream);
if (c==EOF) {
return (size_t)(i/size);
}
}
return count;
}
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': {
unsigned int i=va_arg(arg,unsigned int);
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;
}
}
int fseek(FILE* stream,long offset,int origin) {
if (origin==SEEK_SET) {
stream->pos=offset;
}
if (origin==SEEK_CUR) {
stream->pos+=offset;
}
return 0;
}
long ftell(FILE* stream) {
return stream->pos;
}
int fclose(FILE* stream) {
drvs[stream->type](FSOP_CLOSE,stream,NULL,NULL);
free(stream);
return 0;
}
int feof(FILE *stream) {
return stream->eof;
}
int ferror(FILE *stream) {
return stream->error;
}

View File

@ -1,22 +0,0 @@
#ifndef VFS_H
#define VFS_H
#include <stdint.h>
#include <stdio.h>
typedef enum {
FSOP_MOUNT,
FSOP_OPEN,
FSOP_GETC,
FSOP_PUTC,
FSOP_CLOSE,
FSOP_UMOUNT
} fs_op;
typedef char (*fs_drv)(fs_op op,FILE* stream,void* data1,void* data2);
void init_vfs();
uint32_t register_fs(fs_drv drv,const char* type);
char mount(char* mntpnt,char* dev,char* type);
#endif