OSDN Git Service

Upgrade to mksh R55.
[android-x86/external-mksh.git] / src / shf.c
index ace0ab6..09cc7c3 100644 (file)
--- a/src/shf.c
+++ b/src/shf.c
@@ -2,7 +2,7 @@
 
 /*-
  * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
- *              2012, 2013, 2015, 2016
+ *              2012, 2013, 2015, 2016, 2017
  *     mirabilos <m@mirbsd.org>
  *
  * Provided that these terms and disclaimer and all copyright notices
@@ -25,7 +25,7 @@
 
 #include "sh.h"
 
-__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.76 2016/07/25 00:04:47 tg Exp $");
+__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.79 2017/04/12 17:08:49 tg Exp $");
 
 /* flags to shf_emptybuf() */
 #define EB_READSW      0x01    /* about to switch to reading */
@@ -289,29 +289,31 @@ shf_sclose(struct shf *shf)
 int
 shf_flush(struct shf *shf)
 {
-       if (shf->flags & SHF_STRING)
-               return ((shf->flags & SHF_WR) ? -1 : 0);
+       int rv = 0;
 
-       if (shf->fd < 0)
+       if (shf->flags & SHF_STRING)
+               rv = (shf->flags & SHF_WR) ? -1 : 0;
+       else if (shf->fd < 0)
                internal_errorf(Tf_sD_s, "shf_flush", "no fd");
-
-       if (shf->flags & SHF_ERROR) {
+       else if (shf->flags & SHF_ERROR) {
                errno = shf->errnosv;
-               return (-1);
-       }
-
-       if (shf->flags & SHF_READING) {
+               rv = -1;
+       } else if (shf->flags & SHF_READING) {
                shf->flags &= ~(SHF_EOF | SHF_READING);
                if (shf->rnleft > 0) {
-                       lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
+                       if (lseek(shf->fd, (off_t)-shf->rnleft,
+                           SEEK_CUR) == -1) {
+                               shf->flags |= SHF_ERROR;
+                               shf->errnosv = errno;
+                               rv = -1;
+                       }
                        shf->rnleft = 0;
                        shf->rp = shf->buf;
                }
-               return (0);
        } else if (shf->flags & SHF_WRITING)
-               return (shf_emptybuf(shf, 0));
+               rv = shf_emptybuf(shf, 0);
 
-       return (0);
+       return (rv);
 }
 
 /*
@@ -518,7 +520,23 @@ shf_getse(char *buf, ssize_t bsize, struct shf *shf)
                shf->rnleft -= ncopy;
                buf += ncopy;
                bsize -= ncopy;
+#ifdef MKSH_WITH_TEXTMODE
+               if (end && buf > orig_buf + 1 && buf[-2] == '\r') {
+                       buf--;
+                       bsize++;
+                       buf[-1] = '\n';
+               }
+#endif
        } while (!end && bsize);
+#ifdef MKSH_WITH_TEXTMODE
+       if (!bsize && buf[-1] == '\r') {
+               int c = shf_getc(shf);
+               if (c == '\n')
+                       buf[-1] = '\n';
+               else if (c != -1)
+                       shf_ungetc(c, shf);
+       }
+#endif
        *buf = '\0';
        return (buf);
 }