Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
file.c File Reference
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <syscall.h>
Include dependency graph for file.c:

Go to the source code of this file.

Functions

FILE_alloc_file ()
 
void _free_file (FILE *fh)
 
FILEfopen (const char *path, const char *mode)
 
int fclose (FILE *fh)
 
int feof (FILE *fh)
 
int fflush (FILE *fh)
 
size_t fread (void *buf, size_t size, size_t nmemb, FILE *fh)
 
size_t fwrite (const void *buf, size_t size, size_t nmemb, FILE *fh)
 
int fputc (int ch, FILE *fh)
 
int fputs (const char *str, FILE *fh)
 
int puts (const char *str)
 
int fgetc (FILE *fh)
 
char * fgets (char *str, int size, FILE *fh)
 

Variables

static FILE __stdin = { 1, STDIN_FILENO, 0 }
 
static FILE __stdout = { 1, STDOUT_FILENO, 0 }
 
static FILE __stderr = { 1, STDERR_FILENO, 0 }
 
FILEstdin = &__stdin
 
FILEstdout = &__stdout
 
FILEstderr = &__stderr
 
static FILE fds [FOPEN_MAX]
 

Function Documentation

◆ _alloc_file()

FILE * _alloc_file ( )

Definition at line 39 of file file.c.

40{
41 int i;
42
43 for (i = 0; i < FOPEN_MAX; i++) {
44 if (fds[i].in_use == 0) {
45 fds[i].in_use = 1;
46 return &fds[i];
47 }
48 }
49
50 // XXX: malloc
51
52 return NULL;
53}
static FILE fds[FOPEN_MAX]
Definition: file.c:19
#define NULL
Definition: stddef.h:6
#define FOPEN_MAX
Definition: stdio.h:24
int in_use
Definition: stdio.h:9
Here is the caller graph for this function:

◆ _free_file()

void _free_file ( FILE fh)

Definition at line 56 of file file.c.

57{
58 fh->fd = 0;
59 fh->offset = 0;
60 fh->in_use = 0;
61
62 // XXX: free
63}
fpos_t offset
Definition: stdio.h:11
uint64_t fd
Definition: stdio.h:10
Here is the caller graph for this function:

◆ fclose()

int fclose ( FILE fh)

Definition at line 86 of file file.c.

87{
88 int status = OSClose(fh->fd);
89
90 _free_file(fh);
91
92 return status;
93}
void _free_file(FILE *fh)
Definition: file.c:56
int OSClose(uint64_t fd)
Definition: syscall.c:83
Here is the call graph for this function:

◆ feof()

int feof ( FILE fh)

Definition at line 96 of file file.c.

97{
98 errno = ENOSYS;
99 return -1;
100}
#define ENOSYS
Definition: errno.h:18
#define errno
Definition: errno.h:7

◆ fflush()

int fflush ( FILE fh)

Definition at line 103 of file file.c.

104{
105 errno = ENOSYS;
106 return -1;
107}

◆ fgetc()

int fgetc ( FILE fh)

Definition at line 157 of file file.c.

158{
159 char ch;
160 if (fread(&ch, 1, 1, fh) == 1)
161 return ch;
162 return EOF;
163}
size_t fread(void *buf, size_t size, size_t nmemb, FILE *fh)
Definition: file.c:110
#define EOF
Definition: stdio.h:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fgets()

char * fgets ( char *  str,
int  size,
FILE fh 
)

Definition at line 166 of file file.c.

167{
168 int i;
169
170 for (i = 0; i < (size - 1); i++) {
171 int ch = fgetc(fh);
172 if (ch == EOF)
173 return NULL;
174 if (ch == '\b') {
175 if (i > 0)
176 i -= 1;
177 i -= 1;
178 continue;
179 }
180 str[i] = (char)ch;
181 if (ch == '\n') {
182 str[i + 1] = '\0';
183 return str;
184 }
185 }
186
187 str[size - 1] = '\0';
188 return str;
189}
int fgetc(FILE *fh)
Definition: file.c:157
uint32_t size
Definition: multiboot.h:0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fopen()

FILE * fopen ( const char *  path,
const char *  mode 
)

Definition at line 66 of file file.c.

67{
68 uint64_t fd;
69 FILE *fh;
70 // XXX: handle mode
71
72 fd = OSOpen(path, 0);
73 if (fd == 0)
74 return NULL;
75
76 fh = _alloc_file();
77 fh->fd = fd;
78 fh->offset = 0;
79
80 // XXX: handle append
81
82 return fh;
83}
FILE * _alloc_file()
Definition: file.c:39
uint64_t OSOpen(const char *path, uint64_t flags)
Definition: syscall.c:77
Definition: stdio.h:8
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:

◆ fputc()

int fputc ( int  ch,
FILE fh 
)

Definition at line 124 of file file.c.

125{
126 if (fwrite(&ch, 1, 1, fh) == 1)
127 return ch;
128 return EOF;
129}
size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *fh)
Definition: file.c:117
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fputs()

int fputs ( const char *  str,
FILE fh 
)

Definition at line 132 of file file.c.

133{
134 int status = fwrite(str, strlen(str), 1, fh);
135 if (status > 0)
136 return status;
137 // XXX: error handling
138 return EOF;
139}
size_t strlen(const char *str)
Definition: string.c:112
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fread()

size_t fread ( void *  buf,
size_t  size,
size_t  nmemb,
FILE fh 
)

Definition at line 110 of file file.c.

111{
112 return OSRead(fh->fd, buf, 0, size * nmemb);
113 // set errno
114}
static char buf[4096]
Definition: ethdump.c:10
int OSRead(uint64_t fd, void *addr, uint64_t off, uint64_t length)
Definition: syscall.c:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fwrite()

size_t fwrite ( const void *  buf,
size_t  size,
size_t  nmemb,
FILE fh 
)

Definition at line 117 of file file.c.

118{
119 return OSWrite(fh->fd, buf, 0, size * nmemb);
120 // set errno
121}
int OSWrite(uint64_t fd, const void *addr, uint64_t off, uint64_t length)
Definition: syscall.c:65
Here is the call graph for this function:
Here is the caller graph for this function:

◆ puts()

int puts ( const char *  str)

Definition at line 142 of file file.c.

143{
144 int status, status2;
145 status = fputs(str, stdout);
146 if (status < 0)
147 return EOF;
148
149 status2 = fputc('\n', stdout);
150 if (status2 < 0)
151 return EOF;
152
153 return status;
154}
int fputs(const char *str, FILE *fh)
Definition: file.c:132
FILE * stdout
Definition: file.c:16
int fputc(int ch, FILE *fh)
Definition: file.c:124
Here is the call graph for this function:

Variable Documentation

◆ __stderr

FILE __stderr = { 1, STDERR_FILENO, 0 }
static

Definition at line 13 of file file.c.

◆ __stdin

FILE __stdin = { 1, STDIN_FILENO, 0 }
static

Definition at line 11 of file file.c.

◆ __stdout

FILE __stdout = { 1, STDOUT_FILENO, 0 }
static

Definition at line 12 of file file.c.

◆ fds

FILE fds[FOPEN_MAX]
static
Initial value:
= {
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
}

Definition at line 19 of file file.c.

◆ stderr

FILE* stderr = &__stderr

Definition at line 17 of file file.c.

◆ stdin

FILE* stdin = &__stdin

Definition at line 15 of file file.c.

◆ stdout

FILE* stdout = &__stdout

Definition at line 16 of file file.c.