Simple Operating System
file.h
Go to the documentation of this file.
1 
6 #ifndef FILE_H
7 #define FILE_H
8 
9 #include "types.h"
10 
11 #pragma GCC diagnostic push
12 #pragma GCC diagnostic ignored "-Wreturn-type"
13 #pragma GCC diagnostic ignored "-Wunused-parameter"
14 
15 //https://cplusplus.com/reference/cstdio/FILE/
16 
20 typedef struct {
21  Byte id;
22  Word size;
23 } FILE;
24 
33 FILE *open(int filename, int mode) {
34  //https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html
35  static FILE file;
36  short val;
37  asm ("int 0x21":"=a"(val):"a"(0x100), "b"(filename));
38  if(val < 0)
39  return NULL;
40  file.id = (Byte)val;
41  file.size = (val >> 8) * 512;//sys_open returns size in Sectors
42  return &file;
43 }
44 
53 int read ( int buf, size_t size, FILE * stream ) {
54  asm("int 0x21"::"a"(0x0200), "b"(stream->id), "c"(buf), "d"(size));
55 }
56 
57 
66 int write ( int buf, size_t size, FILE * stream ) {
67  asm("int 0x21"::"a"(0x0300), "b"(stream->id), "c"(buf), "d"(size));
68 }
69 
77 int create(const int filename, size_t size) {
78  asm("int 0x21"::"a"(0x0400), "b"(filename), "c"(size));
79 }
80 
87 int remove(const int filename) {
88  asm("int 0x21"::"a"(0x0500), "b"(filename));
89 }
90 
91 #pragma GCC diagnostic pop
92 #endif
int create(const int filename, size_t size)
Create file.
Definition: file.h:77
int remove(const int filename)
Remove file.
Definition: file.h:87
int write(int buf, size_t size, FILE *stream)
Write to file.
Definition: file.h:66
FILE * open(int filename, int mode)
Open file.
Definition: file.h:33
int read(int buf, size_t size, FILE *stream)
Read from file.
Definition: file.h:53
Information about file.
Definition: file.h:20
Byte id
Definition: file.h:21
Word size
Definition: file.h:22
useful macros, definitions, enums etc.
#define NULL
pointer to NULL
Definition: types.h:22