- fp->_p = p;
- fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size;
-
- for (; n > 0; n -= t, p += t) {
- t = _swrite(fp, (char *)p, n);
- if (t <= 0) {
- fp->_flags |= __SERR;
- return (EOF);
+ if ((t & __SRD) != 0) {
+ if (fp->_seek == NULL) {
+ /*
+ * No way to seek this file -- just return "success."
+ */
+ return (0);
+ }
+
+ n = fp->_r;
+
+ if (n > 0) {
+ /*
+ * See _fseeko's dumb path.
+ */
+ if (_sseek(fp, (fpos_t)-n, SEEK_CUR) == -1) {
+ if (errno == ESPIPE) {
+ /*
+ * Ignore ESPIPE errors, since there's no way to put the bytes
+ * back into the pipe.
+ */
+ return (0);
+ }
+ return (EOF);
+ }
+
+ if (HASUB(fp)) {
+ FREEUB(fp);
+ }
+ fp->_p = fp->_bf._base;
+ fp->_r = 0;
+ fp->_flags &= ~__SEOF;
+ memset(&fp->_mbstate, 0, sizeof(mbstate_t));
+ }
+ return (0);
+ }
+
+ if ((t & __SWR) != 0) {
+ n = fp->_p - p; /* write this much */
+
+ /*
+ * Set these immediately to avoid problems with longjmp and to allow
+ * exchange buffering (via setvbuf) in user write function.
+ */
+ fp->_p = p;
+ fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size;
+
+ for (; n > 0; n -= t, p += t) {
+ t = _swrite(fp, (char *)p, n);
+ if (t <= 0) {
+ /* 5340694: reset _p and _w on EAGAIN */
+ if (t < 0 && errno == EAGAIN) {
+ if (p > fp->_p) {
+ /* some was written */
+ memmove(fp->_p, p, n);
+ fp->_p += n;
+ if (!(fp->_flags & (__SLBF|__SNBF)))
+ fp->_w -= n;
+ }
+ }
+ fp->_flags |= __SERR;
+ return (EOF);
+ }