summaryrefslogtreecommitdiff
path: root/src/stdio/fgetpos.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2024-06-07 14:50:18 -0400
committerJakob Kaivo <jkk@ung.org>2024-06-07 14:50:18 -0400
commitfd466889b49dfe528567587183c2aedd27836f27 (patch)
tree02802cd3785b46a627530427d0e8774742754a7a /src/stdio/fgetpos.c
parentc195cb021ead7de765a0b37f5ab793802be46731 (diff)
split formatted I/O header, unify undefined behavior reporting with UNDEFINED_FMT() macro
Diffstat (limited to 'src/stdio/fgetpos.c')
-rw-r--r--src/stdio/fgetpos.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/stdio/fgetpos.c b/src/stdio/fgetpos.c
index 8e6d201e..2f105f6a 100644
--- a/src/stdio/fgetpos.c
+++ b/src/stdio/fgetpos.c
@@ -6,10 +6,21 @@
int fgetpos(FILE * restrict stream, fpos_t * restrict pos)
{
SIGNAL_SAFE(0);
+ ASSERT_STREAM(stream, 0, 0);
+ ASSERT_NONNULL(pos);
ASSERT_NOOVERLAP(stream, sizeof(*stream), pos, sizeof(*pos));
flockfile(stream);
- *pos = stream->pos;
+ if (!(pos->__impl >= stream->fpos && pos->__impl <= stream->fpos + stream->nfpos)) {
+ struct __fpos_t *tmp = realloc(stream->fpos, sizeof(*stream->fpos) * (stream->nfpos + 1));
+ if (tmp == NULL) {
+ abort();
+ }
+ stream->fpos = tmp;
+ pos->__impl = &(stream->fpos[stream->nfpos++]);
+ }
+
+ pos->__impl->pos = ftell(stream);
funlockfile(stream);
return 0;
}