Skip to content

Commit

Permalink
Function pointer for fflush
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Nov 15, 2023
1 parent 851b8f6 commit 7ad6b37
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
17 changes: 17 additions & 0 deletions libsrc/stdio/_fflush.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "stdio.h"

#include "unistd.h"
#include "./_file.h"

int _fflush(FILE *fp) {
if (fp->flag & FF_WRITE) {
unsigned int wp = fp->wp;
if (wp > 0) {
size_t write_size = write(fp->fd, fp->wbuf, wp);
if (write_size != (size_t)wp)
return EOF;
fp->wp = 0;
}
}
return 0;
}
2 changes: 2 additions & 0 deletions libsrc/stdio/_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern const cookie_io_functions_t _kFileCookieIoFunctions;

struct FILE {
const cookie_io_functions_t *iof; // not struct but const pointer.
int (*flush)(struct FILE *fp);
unsigned char *wbuf;

int fd;
Expand All @@ -51,6 +52,7 @@ extern ssize_t _fread(void *cookie, char *buf, size_t size);
extern ssize_t _fwrite(void *cookie, const char *buf, size_t size);
extern int _fseek(void *cookie, off_t *offset, int origin);
extern int _fclose(void *cookie);
extern int _fflush(FILE *fp);
extern void _finit(FILE *fp);

extern void _remove_opened_file(FILE *fp);
Expand Down
19 changes: 0 additions & 19 deletions libsrc/stdio/_fseek.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,8 @@
#include "unistd.h"
#include "./_file.h"

static int _fflush(FILE *fp) {
unsigned int wp = fp->wp;
if (wp > 0) {
size_t write_size = write(fp->fd, fp->wbuf, wp);
if (write_size != (size_t)wp)
return EOF;
fp->wp = 0;
}
return 0;
}

int _fseek(void *cookie, off_t *offset, int origin) {
FILE *fp = cookie;
if (fp->flag & FF_WRITE) {
_fflush(fp);
if (origin == SEEK_CUR && *offset == 0) {
fp->flag &= ~FF_EOF;
*offset = 0; // TODO:
return 0;
}
}
off_t result = lseek(fp->fd, *offset, origin);
if (result == -1)
return -1;
Expand Down
1 change: 1 addition & 0 deletions libsrc/stdio/fdopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ FILE *fdopen(int fd, const char *mode) {
FILE *fp = calloc(1, sizeof(*fp));
if (fp != NULL) {
fp->iof = &_kFileCookieIoFunctions;
fp->flush = _fflush;
fp->fd = fd;
fp->rp = fp->rs = 0;
fp->wp = 0;
Expand Down
4 changes: 3 additions & 1 deletion libsrc/stdio/fflush.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "stdio.h"

#include "./_file.h"

int fflush(FILE *fp) {
return fseek(fp, 0, SEEK_CUR);
return (fp->flush)(fp);
}
1 change: 1 addition & 0 deletions libsrc/stdio/fmemopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) {
};

fp->iof = &kMemVTable;
fp->flush = _memflush;
fp->wbuf = buf;
fp->ws = size;
fp->pmem = NULL;
Expand Down
5 changes: 4 additions & 1 deletion libsrc/stdio/fseek.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "./_file.h"

int fseek(FILE *fp, long offset, int origin) {
fflush(fp);
off_t o = offset;
return (fp->iof->seek)(fp, &o, origin);
if ((fp->iof->seek)(fp, &o, origin) == -1)
return -1;
return 0;
}
6 changes: 3 additions & 3 deletions libsrc/stdio/stdin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#define IOF &_kFileCookieIoFunctions

static FILE _stdin = {.iof = IOF, .fd = STDIN_FILENO, .flag = FF_READ};
static FILE _stdout = {.iof = IOF, .fd = STDOUT_FILENO, .flag = FF_WRITE, .wbuf = _stdout.wwork, .ws = sizeof(_stdout.wwork)};
static FILE _stderr = {.iof = IOF, .fd = STDERR_FILENO, .flag = FF_WRITE, .wbuf = _stderr.wwork, .ws = sizeof(_stderr.wwork)};
static FILE _stdin = {.iof = IOF, .flush = _fflush, .fd = STDIN_FILENO, .flag = FF_READ};
static FILE _stdout = {.iof = IOF, .flush = _fflush, .fd = STDOUT_FILENO, .flag = FF_WRITE, .wbuf = _stdout.wwork, .ws = sizeof(_stdout.wwork)};
static FILE _stderr = {.iof = IOF, .flush = _fflush, .fd = STDERR_FILENO, .flag = FF_WRITE, .wbuf = _stderr.wwork, .ws = sizeof(_stderr.wwork)};
FILE *stdin = &_stdin;
FILE *stdout = &_stdout;
FILE *stderr = &_stderr;
Expand Down
1 change: 1 addition & 0 deletions src/wcc/www/lib_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"./libsrc/misc/tolower.c",
"./libsrc/misc/toupper.c",
"./libsrc/stdio/_fclose.c",
"./libsrc/stdio/_fflush.c",
"./libsrc/stdio/_finit.c",
"./libsrc/stdio/_fread.c",
"./libsrc/stdio/_fseek.c",
Expand Down

0 comments on commit 7ad6b37

Please sign in to comment.