summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdio')
-rw-r--r--lib/libc/stdio/fdopen.c4
-rw-r--r--lib/libc/stdio/findfp.c1
-rw-r--r--lib/libc/stdio/fmemopen.c3
-rw-r--r--lib/libc/stdio/fopen.c4
-rw-r--r--lib/libc/stdio/freopen.c5
-rw-r--r--lib/libc/stdio/ftell.c30
-rw-r--r--lib/libc/stdio/stdio.c2
7 files changed, 24 insertions, 25 deletions
diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c
index b936998..fbe37e8 100644
--- a/lib/libc/stdio/fdopen.c
+++ b/lib/libc/stdio/fdopen.c
@@ -91,7 +91,9 @@ fdopen(int fd, const char *mode)
* O_APPEND bit set, assert __SAPP so that __swrite() caller
* will _sseek() to the end before write.
*/
- if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
+ if (fdflags & O_APPEND)
+ fp->_flags2 |= __S2OAP;
+ else if (oflags & O_APPEND)
fp->_flags |= __SAPP;
fp->_file = fd;
fp->_cookie = fp;
diff --git a/lib/libc/stdio/findfp.c b/lib/libc/stdio/findfp.c
index 0eabe82..b8bb5af 100644
--- a/lib/libc/stdio/findfp.c
+++ b/lib/libc/stdio/findfp.c
@@ -155,6 +155,7 @@ found:
/* fp->_fl_mutex = NULL; */ /* once set always set (reused) */
fp->_orientation = 0;
memset(&fp->_mbstate, 0, sizeof(mbstate_t));
+ fp->_flags2 = 0;
return (fp);
}
diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c
index ebd3596..bcf187d 100644
--- a/lib/libc/stdio/fmemopen.c
+++ b/lib/libc/stdio/fmemopen.c
@@ -149,6 +149,9 @@ fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
return (NULL);
}
+ if (mode[0] == 'a')
+ f->_flags |= __SAPP;
+
/*
* Turn off buffering, so a write past the end of the buffer
* correctly returns a short object count.
diff --git a/lib/libc/stdio/fopen.c b/lib/libc/stdio/fopen.c
index b08e336..9ab61ba 100644
--- a/lib/libc/stdio/fopen.c
+++ b/lib/libc/stdio/fopen.c
@@ -91,7 +91,9 @@ fopen(const char * __restrict file, const char * __restrict mode)
* we can do about this. (We could set __SAPP and check in
* fseek and ftell.)
*/
- if (oflags & O_APPEND)
+ if (oflags & O_APPEND) {
+ fp->_flags2 |= __S2OAP;
(void)_sseek(fp, (fpos_t)0, SEEK_END);
+ }
return (fp);
}
diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c
index 4dcd50f..e0104c8 100644
--- a/lib/libc/stdio/freopen.c
+++ b/lib/libc/stdio/freopen.c
@@ -187,6 +187,7 @@ finish:
fp->_lb._size = 0;
fp->_orientation = 0;
memset(&fp->_mbstate, 0, sizeof(mbstate_t));
+ fp->_flags2 = 0;
if (f < 0) { /* did not get it after all */
if (isopen)
@@ -240,8 +241,10 @@ finish:
* we can do about this. (We could set __SAPP and check in
* fseek and ftell.)
*/
- if (oflags & O_APPEND)
+ if (oflags & O_APPEND) {
+ fp->_flags2 |= __S2OAP;
(void) _sseek(fp, (fpos_t)0, SEEK_END);
+ }
FUNLOCKFILE(fp);
return (fp);
}
diff --git a/lib/libc/stdio/ftell.c b/lib/libc/stdio/ftell.c
index 745d500..fc792af 100644
--- a/lib/libc/stdio/ftell.c
+++ b/lib/libc/stdio/ftell.c
@@ -39,7 +39,6 @@ __FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <errno.h>
-#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include "un-namespace.h"
@@ -88,7 +87,6 @@ _ftello(FILE *fp, fpos_t *offset)
{
fpos_t pos;
size_t n;
- int dflags;
if (fp->_seek == NULL) {
errno = ESPIPE; /* historic practice */
@@ -99,7 +97,13 @@ _ftello(FILE *fp, fpos_t *offset)
* Find offset of underlying I/O object, then
* adjust for buffered bytes.
*/
- if (fp->_flags & __SOFF)
+ if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
+ fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
+ ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP))) {
+ pos = _sseek(fp, (fpos_t)0, SEEK_END);
+ if (pos == -1)
+ return (1);
+ } else if (fp->_flags & __SOFF)
pos = fp->_offset;
else {
pos = _sseek(fp, (fpos_t)0, SEEK_CUR);
@@ -119,29 +123,13 @@ _ftello(FILE *fp, fpos_t *offset)
}
if (HASUB(fp))
pos -= fp->_r; /* Can be negative at this point. */
- } else if ((fp->_flags & __SWR) && fp->_p != NULL) {
- dflags = 0;
- if (fp->_flags & __SAPP)
- dflags = O_APPEND;
- else if (fp->_file != -1 &&
- (dflags = _fcntl(fp->_file, F_GETFL)) < 0)
- return (1);
- if ((dflags & O_APPEND) &&
- (pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) {
- if ((fp->_flags & __SOPT) || __sflush(fp) ||
- (pos = _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1)
- return (1);
- else {
- *offset = pos;
- return (0);
- }
- }
+ } else if ((fp->_flags & __SWR) && fp->_p != NULL &&
+ (n = fp->_p - fp->_bf._base) > 0) {
/*
* Writing. Any buffered characters cause the
* position to be greater than that in the
* underlying object.
*/
- n = fp->_p - fp->_bf._base;
if (pos > OFF_MAX - n) {
errno = EOVERFLOW;
return (1);
diff --git a/lib/libc/stdio/stdio.c b/lib/libc/stdio/stdio.c
index 44ee0ab..5d6fb9a 100644
--- a/lib/libc/stdio/stdio.c
+++ b/lib/libc/stdio/stdio.c
@@ -117,7 +117,7 @@ _swrite(FILE *fp, char const *buf, int n)
ret = (*fp->_write)(fp->_cookie, buf, n);
/* __SOFF removed even on success in case O_APPEND mode is set. */
if (ret >= 0) {
- if ((fp->_flags & (__SAPP|__SOFF)) == (__SAPP|__SOFF) &&
+ if ((fp->_flags & __SOFF) && !(fp->_flags2 & __S2OAP) &&
fp->_offset <= OFF_MAX - ret)
fp->_offset += ret;
else
OpenPOWER on IntegriCloud