Read in text mode in a needed place only

Converting CR+LF to LF in blocking_read() which is a underlying read
function, may affect to the functions which do not perform line-based
operation.

    modified:   funcs.c
    modified:   main.c
    modified:   misc.c
    modified:   os2.c
    modified:   shf.c
This commit is contained in:
KO Myung-Hun 2016-12-17 17:04:37 +09:00
parent 18ff277047
commit 20dbf6b45b
5 changed files with 31 additions and 11 deletions

12
funcs.c
View File

@ -1975,6 +1975,9 @@ c_read(const char **wp)
#define c_read_opts "Aad:N:n:prst:u,"
#else
#define c_read_opts "Aad:N:n:prsu,"
#endif
#ifdef __OS2__
int saved_mode;
#endif
while ((c = ksh_getopt(wp, &builtin_opt, c_read_opts)) != -1)
@ -2104,7 +2107,13 @@ c_read(const char **wp)
}
#endif
#ifdef __OS2__
saved_mode = setmode(fd, O_TEXT);
#endif
if ((bytesread = blocking_read(fd, xp, bytesleft)) == (size_t)-1) {
#ifdef __OS2__
setmode(fd, saved_mode);
#endif
if (errno == EINTR) {
/* check whether the signal would normally kill */
if (!fatal_trap_check()) {
@ -2119,6 +2128,9 @@ c_read(const char **wp)
rv = 2;
goto c_read_out;
}
#ifdef __OS2__
setmode(fd, saved_mode);
#endif
switch (readmode) {
case READALL:

2
main.c
View File

@ -1467,7 +1467,7 @@ openpipe(int *pv)
if (pv[1] != lpv[1])
close(lpv[1]);
#ifdef __OS2__
setmode(pv[0], O_TEXT);
setmode(pv[0], O_BINARY);
setmode(pv[1], O_BINARY);
#endif
}

9
misc.c
View File

@ -1344,10 +1344,6 @@ blocking_read(int fd, char *buf, size_t nbytes)
ssize_t ret;
bool tried_reset = false;
#ifdef __OS2__
int saved_mode = setmode(fd, O_TEXT);
int saved_errno;
#endif
while ((ret = read(fd, buf, nbytes)) < 0) {
if (!tried_reset && errno == EAGAIN) {
if (reset_nonblock(fd) > 0) {
@ -1358,11 +1354,6 @@ blocking_read(int fd, char *buf, size_t nbytes)
}
break;
}
#ifdef __OS2__
saved_errno = errno;
setmode(fd, saved_mode);
errno = saved_errno;
#endif
return (ret);
}

3
os2.c
View File

@ -191,7 +191,8 @@ void os2_init(int *argcp, const char ***argvp)
init_extlibpath();
env_slashify();
setmode(STDIN_FILENO, O_TEXT);
if (!isatty(STDIN_FILENO))
setmode(STDIN_FILENO, O_BINARY);
if (!isatty(STDOUT_FILENO))
setmode(STDOUT_FILENO, O_BINARY);
if (!isatty(STDERR_FILENO))

16
shf.c
View File

@ -518,7 +518,23 @@ shf_getse(char *buf, ssize_t bsize, struct shf *shf)
shf->rnleft -= ncopy;
buf += ncopy;
bsize -= ncopy;
#ifdef __OS2__
if (end && buf > orig_buf + 1 && buf[-2] == '\r') {
buf--;
bsize++;
buf[-1] = '\n';
}
#endif
} while (!end && bsize);
#ifdef __OS2__
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);
}