aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Pesch <inolen@gmail.com>2013-07-14 15:12:49 -0700
committerAnthony Pesch <inolen@gmail.com>2013-07-14 15:12:49 -0700
commit739c8dade7eaed3e2fe26cbeb26f051185735500 (patch)
tree643f1574d4dd2c7e36c54f47542eae99292595be
parent65e8abdfabce0c3e6f5f4bef1479bd67bb3d28ad (diff)
- moved std stream isTerminal property to object so all streams can see
- made ttyname use isatty to check if fd is tty - hardcoded /dev/tty as tty name is ttyname_r - fixed and changed isatty and ttyname tests to run natively
-rw-r--r--src/library.js42
-rwxr-xr-xtests/runner.py24
-rw-r--r--tests/unistd/isatty.c38
-rw-r--r--tests/unistd/isatty.js5
-rw-r--r--tests/unistd/isatty.out10
-rw-r--r--tests/unistd/ttyname.c76
-rw-r--r--tests/unistd/ttyname.js1
-rw-r--r--tests/unistd/ttyname.out5
8 files changed, 73 insertions, 128 deletions
diff --git a/src/library.js b/src/library.js
index bf969fd7..eddeb89b 100644
--- a/src/library.js
+++ b/src/library.js
@@ -588,8 +588,11 @@ LibraryManager.library = {
// Create the I/O devices.
var devFolder = FS.createFolder('/', 'dev', true, true);
var stdin = FS.createDevice(devFolder, 'stdin', input);
+ stdin.isTerminal = !stdinOverridden;
var stdout = FS.createDevice(devFolder, 'stdout', null, output);
+ stdout.isTerminal = !stdoutOverridden;
var stderr = FS.createDevice(devFolder, 'stderr', null, error);
+ stderr.isTerminal = !stderrOverridden;
FS.createDevice(devFolder, 'tty', input, output);
FS.createDevice(devFolder, 'null', function(){}, function(){});
@@ -601,7 +604,6 @@ LibraryManager.library = {
isRead: true,
isWrite: false,
isAppend: false,
- isTerminal: !stdinOverridden,
error: false,
eof: false,
ungotten: []
@@ -613,7 +615,6 @@ LibraryManager.library = {
isRead: false,
isWrite: true,
isAppend: false,
- isTerminal: !stdoutOverridden,
error: false,
eof: false,
ungotten: []
@@ -625,7 +626,6 @@ LibraryManager.library = {
isRead: false,
isWrite: true,
isAppend: false,
- isTerminal: !stderrOverridden,
error: false,
eof: false,
ungotten: []
@@ -1688,13 +1688,16 @@ LibraryManager.library = {
isatty: function(fildes) {
// int isatty(int fildes);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/isatty.html
- if (!FS.streams[fildes]) {
+ var stream = FS.streams[fildes];
+ if (!stream) {
___setErrNo(ERRNO_CODES.EBADF);
return 0;
}
- if (FS.streams[fildes].isTerminal) return 1;
- ___setErrNo(ERRNO_CODES.ENOTTY);
- return 0;
+ if (!stream.object.isTerminal) {
+ ___setErrNo(ERRNO_CODES.ENOTTY);
+ return 0;
+ }
+ return 1;
},
lchown__deps: ['chown'],
lchown: function(path, owner, group) {
@@ -1916,30 +1919,21 @@ LibraryManager.library = {
if (!_ttyname.ret) _ttyname.ret = _malloc(256);
return _ttyname_r(fildes, _ttyname.ret, 256) ? 0 : _ttyname.ret;
},
- ttyname_r__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'],
+ ttyname_r__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', 'isatty'],
ttyname_r: function(fildes, name, namesize) {
// int ttyname_r(int fildes, char *name, size_t namesize);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/ttyname.html
var stream = FS.streams[fildes];
+ var ttyname = '/dev/tty';
if (!stream) {
return ___setErrNo(ERRNO_CODES.EBADF);
- } else {
- var object = stream.object;
- if (!object.isDevice || !object.input || !object.output) {
- return ___setErrNo(ERRNO_CODES.ENOTTY);
- } else {
- var ret = stream.path;
- if (namesize < ret.length + 1) {
- return ___setErrNo(ERRNO_CODES.ERANGE);
- } else {
- for (var i = 0; i < ret.length; i++) {
- {{{ makeSetValue('name', 'i', 'ret.charCodeAt(i)', 'i8') }}}
- }
- {{{ makeSetValue('name', 'i', '0', 'i8') }}}
- return 0;
- }
- }
+ } else if (!_isatty(fildes)) {
+ return ___setErrNo(ERRNO_CODES.ENOTTY);
+ } else if (namesize < ttyname.length + 1) {
+ return ___setErrNo(ERRNO_CODES.ERANGE);
}
+ writeStringToMemory(ttyname, name);
+ return 0;
},
symlink__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'],
symlink: function(path1, path2) {
diff --git a/tests/runner.py b/tests/runner.py
index b21eee08..69006184 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -7309,18 +7309,8 @@ def process(filename):
self.do_run(src, expected, extra_emscripten_args=['-H', 'libc/unistd.h'])
def test_unistd_ttyname(self):
- add_pre_run = '''
-def process(filename):
- import tools.shared as shared
- src = open(filename, 'r').read().replace(
- '// {{PRE_RUN_ADDITIONS}}',
- open(shared.path_from_root('tests', 'unistd', 'ttyname.js'), 'r').read()
- )
- open(filename, 'w').write(src)
-'''
src = open(path_from_root('tests', 'unistd', 'ttyname.c'), 'r').read()
- expected = open(path_from_root('tests', 'unistd', 'ttyname.out'), 'r').read()
- self.do_run(src, expected, post_build=add_pre_run)
+ self.do_run(src, 'success', force_c=True)
def test_unistd_dup(self):
src = open(path_from_root('tests', 'unistd', 'dup.c'), 'r').read()
@@ -7352,18 +7342,8 @@ def process(filename):
self.do_run(src, expected)
def test_unistd_isatty(self):
- add_pre_run = '''
-def process(filename):
- import tools.shared as shared
- src = open(filename, 'r').read().replace(
- '// {{PRE_RUN_ADDITIONS}}',
- open(shared.path_from_root('tests', 'unistd', 'isatty.js'), 'r').read()
- )
- open(filename, 'w').write(src)
-'''
src = open(path_from_root('tests', 'unistd', 'isatty.c'), 'r').read()
- expected = open(path_from_root('tests', 'unistd', 'isatty.out'), 'r').read()
- self.do_run(src, expected, post_build=add_pre_run)
+ self.do_run(src, 'success', force_c=True)
def test_unistd_sysconf(self):
src = open(path_from_root('tests', 'unistd', 'sysconf.c'), 'r').read()
diff --git a/tests/unistd/isatty.c b/tests/unistd/isatty.c
index cc1ff641..191036e9 100644
--- a/tests/unistd/isatty.c
+++ b/tests/unistd/isatty.c
@@ -1,28 +1,28 @@
-#include <stdio.h>
+#include <assert.h>
#include <errno.h>
-#include <unistd.h>
#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
int main() {
- printf("read: %d\n", isatty(open("/read", O_RDONLY)));
- printf("errno: %d\n", errno);
- errno = 0;
+ int err;
+
+ err = isatty(-1);
+ assert(!err);
+ assert(errno == EBADF);
- printf("write: %d\n", isatty(open("/write", O_WRONLY)));
- printf("errno: %d\n", errno);
- errno = 0;
+ err = isatty(open("/dev/stdin", O_RDONLY));
+ assert(err == 1);
- printf("all: %d\n", isatty(open("/all", O_RDONLY)));
- printf("errno: %d\n", errno);
- errno = 0;
+ err = isatty(open("/dev/null", O_RDONLY));
+ assert(!err);
- printf("folder: %d\n", isatty(open("/folder", O_RDONLY)));
- printf("errno: %d\n", errno);
- errno = 0;
+ err = isatty(open("/dev", O_RDONLY));
+ assert(!err);
- printf("file: %d\n", isatty(open("/file", O_RDONLY)));
- printf("errno: %d\n", errno);
- errno = 0;
+ puts("success");
- return 0;
-}
+ return EXIT_SUCCESS;
+} \ No newline at end of file
diff --git a/tests/unistd/isatty.js b/tests/unistd/isatty.js
deleted file mode 100644
index d88bd2be..00000000
--- a/tests/unistd/isatty.js
+++ /dev/null
@@ -1,5 +0,0 @@
-FS.createDevice('/', 'read', function() {}, null);
-FS.createDevice('/', 'write', null, function() {});
-FS.createDevice('/', 'all', function() {}, function() {});
-FS.createFolder('/', 'folder', true, true);
-FS.createDataFile('/', 'file', 'test', true, true);
diff --git a/tests/unistd/isatty.out b/tests/unistd/isatty.out
deleted file mode 100644
index 6823c1f0..00000000
--- a/tests/unistd/isatty.out
+++ /dev/null
@@ -1,10 +0,0 @@
-read: 0
-errno: 25
-write: 0
-errno: 25
-all: 0
-errno: 25
-folder: 0
-errno: 25
-file: 0
-errno: 25
diff --git a/tests/unistd/ttyname.c b/tests/unistd/ttyname.c
index 7080be5d..b7cc27bd 100644
--- a/tests/unistd/ttyname.c
+++ b/tests/unistd/ttyname.c
@@ -1,51 +1,43 @@
-#include <stdio.h>
+#include <assert.h>
#include <errno.h>
-#include <unistd.h>
#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
int main() {
+ int err;
+ int stdin, null, dev;
char buffer[256];
- int d = open("/device", O_RDWR);
- int f = open("/", O_RDONLY);
char* result;
+ stdin = open("/dev/stdin", O_RDONLY);
+ null = open("/dev/null", O_RDONLY);
+ dev = open("/dev", O_RDONLY);
+
result = ctermid(buffer);
- if (result) {
- printf("ctermid: %s\n", result);
- } else {
- printf("ctermid errno: %d\n", errno);
- errno = 0;
- }
-
- if (ttyname_r(d, buffer, 256) == 0) {
- printf("ttyname_r(d, ..., 256): %s\n", buffer);
- } else {
- printf("ttyname_r(d, ..., 256) errno: %d\n", errno);
- errno = 0;
- }
-
- if (ttyname_r(d, buffer, 2) == 0) {
- printf("ttyname_r(d, ..., 2): %s\n", buffer);
- } else {
- printf("ttyname_r(d, ..., 2) errno: %d\n", errno);
- errno = 0;
- }
-
- result = ttyname(d);
- if (result) {
- printf("ttyname(d): %s\n", result);
- } else {
- printf("ttyname(d) errno: %d\n", errno);
- errno = 0;
- }
-
- result = ttyname(f);
- if (result) {
- printf("ttyname(f): %s\n", result);
- } else {
- printf("ttyname(f) errno: %d\n", errno);
- errno = 0;
- }
-
- return 0;
+ assert(!strcmp(result, "/dev/tty"));
+
+ // strstr instead of strcmp as native code may
+ // be using a virtual console (e.g. /dev/tty02)
+ err = ttyname_r(stdin, buffer, 256);
+ assert(!err);
+ assert(strstr(buffer, "/dev/tty"));
+
+ err = ttyname_r(stdin, buffer, 2);
+ assert(err == ERANGE);
+
+ result = ttyname(stdin);
+ assert(strstr(result, "/dev/tty"));
+
+ result = ttyname(null);
+ assert(!result);
+
+ result = ttyname(dev);
+ assert(!result);
+
+ puts("success");
+
+ return EXIT_SUCCESS;
}
diff --git a/tests/unistd/ttyname.js b/tests/unistd/ttyname.js
deleted file mode 100644
index a21f417f..00000000
--- a/tests/unistd/ttyname.js
+++ /dev/null
@@ -1 +0,0 @@
-FS.createDevice('/', 'device', function() {}, function() {});
diff --git a/tests/unistd/ttyname.out b/tests/unistd/ttyname.out
deleted file mode 100644
index b47cbf75..00000000
--- a/tests/unistd/ttyname.out
+++ /dev/null
@@ -1,5 +0,0 @@
-ctermid: /dev/tty
-ttyname_r(d, ..., 256): /device
-ttyname_r(d, ..., 2) errno: 34
-ttyname(d): /device
-ttyname(f) errno: 25