diff options
author | Kjell Winblad <[email protected]> | 2019-06-20 12:27:15 +0200 |
---|---|---|
committer | Kjell Winblad <[email protected]> | 2019-07-18 14:40:30 +0200 |
commit | c1b9be4a5d45869e8f3f93979782a14eab65fec3 (patch) | |
tree | d4d35496348e15e8020311d0336dbacd2aa67e68 /erts/emulator/sys/unix | |
parent | fa1684afd7a2fca598e6d5d366df3fc18a8953a1 (diff) | |
download | otp-c1b9be4a5d45869e8f3f93979782a14eab65fec3.tar.gz otp-c1b9be4a5d45869e8f3f93979782a14eab65fec3.tar.bz2 otp-c1b9be4a5d45869e8f3f93979782a14eab65fec3.zip |
Fix io:columns() and io:rows() are not working from escripts bug
The functions io:columns() and io:rows() only worked correctly inside
interactive erlang shells before this fix. These functions returned
{error,enotsup} before this fix even if stdout and stdin were
connected to a terminal when they were invoked from an escript or a
program started with e.g., `erl -noshell`.
This commit fixes issue ERL-717.
Diffstat (limited to 'erts/emulator/sys/unix')
-rw-r--r-- | erts/emulator/sys/unix/sys_drivers.c | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/erts/emulator/sys/unix/sys_drivers.c b/erts/emulator/sys/unix/sys_drivers.c index 664d677ebd..92020c6f35 100644 --- a/erts/emulator/sys/unix/sys_drivers.c +++ b/erts/emulator/sys/unix/sys_drivers.c @@ -785,15 +785,15 @@ static ErlDrvSSizeT spawn_control(ErlDrvData e, unsigned int cmd, char *buf, static int fd_get_window_size(int fd, Uint32 *width, Uint32 *height) { -#ifdef TIOCGWINSZ +#ifdef TIOCGWINSZ struct winsize ws; if (ioctl(fd,TIOCGWINSZ,&ws) == 0) { *width = (Uint32) ws.ws_col; *height = (Uint32) ws.ws_row; - return 0; + return 1; } #endif - return -1; + return 0; } static ErlDrvSSizeT fd_control(ErlDrvData drv_data, @@ -801,16 +801,28 @@ static ErlDrvSSizeT fd_control(ErlDrvData drv_data, char *buf, ErlDrvSizeT len, char **rbuf, ErlDrvSizeT rlen) { - int fd = (int)(long)drv_data; char resbuff[2*sizeof(Uint32)]; - + ErtsSysDriverData* dd = (ErtsSysDriverData*)drv_data; command -= ERTS_TTYSL_DRV_CONTROL_MAGIC_NUMBER; switch (command) { case FD_CTRL_OP_GET_WINSIZE: { Uint32 w,h; - if (fd_get_window_size(fd,&w,&h)) - return 0; + int success = 0; + if (dd->ofd != NULL) { + /* Try with output file descriptor */ + int out_fd = dd->ofd->fd; + success = fd_get_window_size(out_fd,&w,&h); + } + if (!success && dd->ifd != NULL) { + /* Try with input file descriptor */ + int in_fd = dd->ifd->fd; + success = fd_get_window_size(in_fd,&w,&h); + } + if (!success) { + return -1; + } + /* Succeeded */ memcpy(resbuff,&w,sizeof(Uint32)); memcpy(resbuff+sizeof(Uint32),&h,sizeof(Uint32)); } |