summaryrefslogtreecommitdiff
path: root/src/stdio
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2024-02-02 13:56:33 -0500
committerJakob Kaivo <jkk@ung.org>2024-02-02 13:56:33 -0500
commit8b3f2fa09e9241e20926c8171753a271c21ec93d (patch)
treea8bdff34505bfcf999467053dbfbf276bf7233f1 /src/stdio
parent3a9026cf8557c8efec34e652b6bf37ce49f12f86 (diff)
first cut of tracking previous return values with ftell()/fseek() as POC
Diffstat (limited to 'src/stdio')
-rw-r--r--src/stdio/_stdio.h7
-rw-r--r--src/stdio/freopen.c6
-rw-r--r--src/stdio/fseek.c3
-rw-r--r--src/stdio/ftell.c12
4 files changed, 22 insertions, 6 deletions
diff --git a/src/stdio/_stdio.h b/src/stdio/_stdio.h
index d47ac5dd..c69b3787 100644
--- a/src/stdio/_stdio.h
+++ b/src/stdio/_stdio.h
@@ -59,7 +59,7 @@ struct __FILE {
int thread; /* the owning thread if locked */
int orientation:2; /* 0 = undetermind, < 0 = byte, > 0 = wide */
- int operation; /* TODO: previous operation, NONE, INPUT, OUTPUT (are there others?) */
+ int operation:2; /* TODO: previous operation, NONE, INPUT, OUTPUT (are there others?) */
int eof:1; /* eof indicator */
int err:1; /* error indicator */
int text:1; /* is this a text file? */
@@ -67,6 +67,11 @@ struct __FILE {
#ifdef _POSIX_C_SOURCE
pid_t pipe_pid; /* if stream is a pipe, the child pid */
#endif
+
+ fpos_t *valid_fpos;
+ size_t nvalid_fpos;
+ long int *valid_ftell;
+ size_t nvalid_ftell;
};
struct io_options {
diff --git a/src/stdio/freopen.c b/src/stdio/freopen.c
index 3a1ef656..d431b0e3 100644
--- a/src/stdio/freopen.c
+++ b/src/stdio/freopen.c
@@ -96,6 +96,12 @@ FILE * freopen(const char * restrict filename, const char * restrict mode, FILE
stream->buf = stream->ibuf;
}
+ free(stream->valid_fpos);
+ stream->nvalid_fpos = 0;
+
+ free(stream->valid_ftell);
+ stream->nvalid_ftell = 0;
+
stream->text = !(strchr(mode, 'b'));
/*
diff --git a/src/stdio/fseek.c b/src/stdio/fseek.c
index 47388c9a..9f4bd733 100644
--- a/src/stdio/fseek.c
+++ b/src/stdio/fseek.c
@@ -12,8 +12,7 @@ int fseek(FILE *stream, long int offset, int whence)
if (whence != SEEK_SET) {
UNDEFINED("In call to fseek(): Only SEEK_SET is supported for text files");
}
- /* if offset is not previous */
- /* UNDEFINED("fseek() on text files requires an offset previously returned by ftell()"); */
+ ASSERT_PREV(offset, stream->valid_ftell, stream->nvalid_ftell, "ftell");
}
if (whence == SEEK_CUR) {
diff --git a/src/stdio/ftell.c b/src/stdio/ftell.c
index f68c18b2..ad23bf31 100644
--- a/src/stdio/ftell.c
+++ b/src/stdio/ftell.c
@@ -5,15 +5,21 @@
long int ftell(FILE *stream)
{
+ long int ret = -1L;
+
SIGNAL_SAFE(0);
+ ASSERT_STREAM(stream, 0, 0);
- (void)stream;
- /* TODO */
/*
RETURN_SUCCESS(the current file position);
RETURN_FAILURE(LITERAL(-1L));
*/
- return -1L;
+
+ if (stream->text) {
+ ADD_PREV(ret, stream->valid_ftell, stream->nvalid_ftell);
+ }
+
+ return ret;
}
/***