Skip to content

Commit

Permalink
feof
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Sep 2, 2023
1 parent cd54fa2 commit ae4036b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ size_t fread(void *buffer, size_t size, size_t count, FILE *fp);
int fflush(FILE *fp);
int fseek(FILE *fp, long offset, int origin);
long ftell(FILE *fp);
int feof(FILE *fp);
int remove(const char *fn);

int fgetc(FILE *fp);
Expand Down
1 change: 1 addition & 0 deletions libsrc/stdio/_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define FF_BINARY (1 << 0)
#define FF_MEMORY (1 << 1)
#define FF_GROWMEM (1 << 2)
#define FF_EOF (1 << 3)

struct FILE {
int (*fputc)(int c, FILE *fp);
Expand Down
7 changes: 7 additions & 0 deletions libsrc/stdio/feof.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "stdio.h"

#include "_file.h"

int feof(FILE *fp) {
return (fp->flag & FF_EOF) ? 1 : 0;
}
12 changes: 10 additions & 2 deletions libsrc/stdio/fread.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ size_t fread(void *buffer, size_t size, size_t count, FILE *fp) {
memcpy(p, fp->rbuf, n);
p += n;
}
if (len < (ssize_t)sizeof(fp->rbuf) && (ssize_t)n == len) {
fp->flag |= FF_EOF;
}
} else {
// Read to the given buffer directly.
ssize_t len = read(fp->fd, p, total);
p += len;
fp->rp = fp->rs = 0;
if (len >= 0) {
p += len;
fp->rp = fp->rs = 0;
if ((size_t)len < total) {
fp->flag |= FF_EOF;
}
}
}

// TODO: Align by size.
Expand Down
1 change: 1 addition & 0 deletions libsrc/stdio/fseek.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ int fseek(FILE *fp, long offset, int origin) {
off_t result = lseek(fp->fd, offset, origin);
if (result == -1)
return 1; // TODO:
fp->flag &= ~FF_EOF;
return 0;
}
4 changes: 4 additions & 0 deletions libsrc/tests/file_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ TEST(basic_access) {
EXPECT_EQ(datasize, fread(buf, 1, sizeof(buf), fp));
EXPECT_EQ(0, memcmp(buf, data, datasize));

// feof
EXPECT_TRUE(feof(fp));

// fseek: from current position.
EXPECT_EQ(0, fseek(fp, -22, SEEK_CUR));
// ftell
EXPECT_EQ(datasize - 22, ftell(fp));
EXPECT_FALSE(feof(fp));

// fseek: to the end.
EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
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/stdio/assert.c",
"./libsrc/stdio/fclose.c",
"./libsrc/stdio/fdopen.c",
"./libsrc/stdio/feof.c",
"./libsrc/stdio/fflush.c",
"./libsrc/stdio/fgetc.c",
"./libsrc/stdio/fgets.c",
Expand Down

0 comments on commit ae4036b

Please sign in to comment.