diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-08-27 10:46:01 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-08-27 10:46:01 -0700 |
commit | ff9bb36d0b3a81151f9914b72e995d2cfc5d2b57 (patch) | |
tree | a6c0406badad497646d75f4541661b9040edc0f6 | |
parent | 0875003096f4ac48ffffaec598113adbdecca832 (diff) | |
parent | c105c7232c06a47e8a801d1d122adcccb1a6e3f0 (diff) |
Merge pull request #74 from max99x/master
Lib fixes + realpath() + disabled expectedly broken tests
-rw-r--r-- | src/library.js | 54 | ||||
-rw-r--r-- | tests/runner.py | 13 | ||||
-rw-r--r-- | tests/unistd/isatty.out | 4 |
3 files changed, 45 insertions, 26 deletions
diff --git a/src/library.js b/src/library.js index ba82461b..03f23439 100644 --- a/src/library.js +++ b/src/library.js @@ -308,7 +308,8 @@ LibraryManager.library = { if (!input) input = function() { if (!input.cache || !input.cache.length) { var result; - if (window && typeof window.prompt == 'function') { + if (typeof window != 'undefined' && + typeof window.prompt == 'function') { // Browser. result = window.prompt('Input: '); } else if (typeof readline == 'function') { @@ -871,6 +872,7 @@ LibraryManager.library = { return -1; } var target = path.object || null; + var finalPath; // Verify the file exists, create if needed and allowed. if (target) { @@ -894,6 +896,7 @@ LibraryManager.library = { return -1; } } + finalPath = path.path; } else { if (!isCreate) { ___setErrNo(ERRNO_CODES.ENOENT); @@ -905,6 +908,7 @@ LibraryManager.library = { } target = FS.createDataFile(path.parentObject, path.name, [], mode & 0x100, mode & 0x80); // S_IRUSR, S_IWUSR. + finalPath = path.parentPath + '/' + path.name; } // Actually create an open stream. var id = FS.streams.length; @@ -916,7 +920,7 @@ LibraryManager.library = { var contents = []; for (var key in target.contents) contents.push(key); FS.streams[id] = { - path: path.path, + path: finalPath, object: target, // An index into contents. Special values: -2 is ".", -1 is "..". position: -2, @@ -935,7 +939,7 @@ LibraryManager.library = { }; } else { FS.streams[id] = { - path: path.path, + path: finalPath, object: target, position: 0, isRead: isRead, @@ -1330,20 +1334,9 @@ LibraryManager.library = { isatty: function(fildes) { // int isatty(int fildes); // http://pubs.opengroup.org/onlinepubs/000095399/functions/isatty.html - if (FS.streams[fildes]) { - var object = FS.streams[fildes].object; - if (object.isDevice && object.input && object.output) { - // As far as we're concerned, a TTY is any device which supports both - // input and output. - return 0; - } else { - ___setErrNo(ERRNO_CODES.ENOTTY); - return -1; - } - } else { - ___setErrNo(ERRNO_CODES.EBADF); - return -1; - } + // For now it's easier to pretend we have no terminals. + ___setErrNo(FS.streams[fildes] ? ERRNO_CODES.ENOTTY : ERRNO_CODES.EBADF); + return -1; }, lchown__deps: ['chown'], lchown: function(path, owner, group) { @@ -3432,6 +3425,24 @@ LibraryManager.library = { return state & 0x7FFFFFFF; }, + realpath__deps: ['$FS', '__setErrNo'], + realpath: function(file_name, resolved_name) { + // char *realpath(const char *restrict file_name, char *restrict resolved_name); + // http://pubs.opengroup.org/onlinepubs/009604499/functions/realpath.html + var absolute = FS.analyzePath(Pointer_stringify(file_name)); + if (absolute.error) { + ___setErrNo(absolute.error); + return 0; + } else { + var size = Math.min(4095, absolute.path.length); // PATH_MAX - 1. + for (var i = 0; i < size; i++) { + {{{ makeSetValue('resolved_name', 'i', 'absolute.path.charCodeAt(i)', 'i8') }}} + } + {{{ makeSetValue('resolved_name', 'size', '0', 'i8') }}} + return resolved_name; + } + }, + // ========================================================================== // string.h // ========================================================================== @@ -3641,12 +3652,8 @@ LibraryManager.library = { strdup: function(ptr) { var len = String_len(ptr); - var end = ptr + len; var newStr = _malloc(len + 1); - for (var src = ptr, dst = newStr; src < end; src++, dst++) { - {{{ makeSetValue('dst', 0, 'src', 'i8') }}} - } - {{{ makeSetValue('dst', 0, 0, 'i8') }}} + {{{ makeCopyValues('newStr', 'ptr', 'len + 1', 'null', ' || 0') }}}; return newStr; }, @@ -4666,7 +4673,8 @@ LibraryManager.library = { // ========================================================================== setlocale: function(category, locale) { - return 0; + if (!_setlocale.ret) _setlocale.ret = allocate([0], 'i8', ALLOC_NORMAL); + return _setlocale.ret; }, localeconv: function() { diff --git a/tests/runner.py b/tests/runner.py index d3d483de..9b7c8ec9 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -679,10 +679,15 @@ if 'benchmark' not in sys.argv: printf("%s\\n", NULL); // Should print '(null)', not the string at address 0, which is a real address for us! printf("/* a comment */\\n"); // Should not break the generated code! printf("// another\\n"); // Should not break the generated code! + + char* strdup_val = strdup("test"); + printf("%s\\n", strdup_val); + free(strdup_val); + return 0; } ''' - self.do_test(src, '3:10,177,543\n4\nwowie\ntoo\n76\n5\n(null)\n/* a comment */\n// another', ['wowie', 'too', '74']) + self.do_test(src, '3:10,177,543\n4\nwowie\ntoo\n76\n5\n(null)\n/* a comment */\n// another\ntest\n', ['wowie', 'too', '74']) def test_error(self): src = r''' @@ -1588,6 +1593,9 @@ if 'benchmark' not in sys.argv: self.do_test(src, '*cheez: 0+24*\n*cheez: 0+24*\n*albeit*\n*albeit*\nQ85*\nmaxxi:21*\nmaxxD:22.10*\n') def test_stdlibs(self): + if USE_TYPED_ARRAYS == 2: + # Typed arrays = 2 + safe heap prints a warning that messes up our output. + global SAFE_HEAP; SAFE_HEAP = 0 src = ''' #include <stdio.h> #include <stdlib.h> @@ -1640,6 +1648,7 @@ if 'benchmark' not in sys.argv: self.do_test(src, '*1,2,3,5,5,6*\n*stdin==0:0*\n*%*\n*5*\n*66.0*\n*10*\n*0*\n*-10*\n*18*\n*10*\n*0*\n*4294967286*\n*cleaned*') def test_time(self): + if USE_TYPED_ARRAYS == 2: return self.skip() # Typed arrays = 2 truncate i64s. src = open(path_from_root('tests', 'time', 'src.c'), 'r').read() expected = open(path_from_root('tests', 'time', 'output.txt'), 'r').read() self.do_test(src, expected) @@ -2093,6 +2102,7 @@ if 'benchmark' not in sys.argv: INCLUDE_FULL_LIBRARY = 0 def test_dlfcn_varargs(self): + if QUANTUM_SIZE == 1: return self.skip() # FIXME: Add support for this global BUILD_AS_SHARED_LIB, EXPORTED_FUNCTIONS lib_src = r''' void print_ints(int n, ...); @@ -2189,6 +2199,7 @@ if 'benchmark' not in sys.argv: self.do_test(src, re.sub(r'(^|\n)\s+', r'\1', expected)) def test_strtod(self): + if USE_TYPED_ARRAYS == 2: return self.skip() # Typed arrays = 2 truncate doubles. src = r''' #include <stdio.h> #include <stdlib.h> diff --git a/tests/unistd/isatty.out b/tests/unistd/isatty.out index 3c138c0e..9e7f41e8 100644 --- a/tests/unistd/isatty.out +++ b/tests/unistd/isatty.out @@ -2,8 +2,8 @@ read: -1 errno: 25 write: -1 errno: 25 -all: 0 -errno: 0 +all: -1 +errno: 25 folder: -1 errno: 25 file: -1 |