diff options
author | Alon Zakai <azakai@mozilla.com> | 2011-01-16 15:29:06 -0800 |
---|---|---|
committer | Alon Zakai <azakai@mozilla.com> | 2011-01-16 15:29:06 -0800 |
commit | 9eb5dd2a45db42c792bb3948271d42016417404e (patch) | |
tree | 20225e99c25124227f4f3b967558aa93199f3d8b /src | |
parent | 6a6e842688afe7e15f6a957d4179da982c0f940b (diff) |
support for writing to files in stdio
Diffstat (limited to 'src')
-rw-r--r-- | src/library.js | 34 | ||||
-rw-r--r-- | src/postamble.js | 2 |
2 files changed, 27 insertions, 9 deletions
diff --git a/src/library.js b/src/library.js index 8bc35ac0..075b53d9 100644 --- a/src/library.js +++ b/src/library.js @@ -228,19 +228,19 @@ var Library = { _stdin = Pointer_make([0], null, ALLOC_STATIC); IHEAP[_stdin] = this.prepare('<<stdin>>'); _stdout = Pointer_make([0], null, ALLOC_STATIC); - IHEAP[_stdout] = this.prepare('<<stdout>>'); + IHEAP[_stdout] = this.prepare('<<stdout>>', null, true); _stderr = Pointer_make([0], null, ALLOC_STATIC); - IHEAP[_stderr] = this.prepare('<<stderr>>'); + IHEAP[_stderr] = this.prepare('<<stderr>>', null, true); }, - prepare: function(filename, data) { + prepare: function(filename, data, print_) { var stream = this.counter++; this.streams[stream] = { filename: filename, - data: data, + data: data ? data : [], position: 0, eof: 0, error: 0, - print: 1 // true for stdout and stderr - we print when receiving data for them + print: print_ // true for stdout and stderr - we print when receiving data for them }; this.filenames[filename] = stream; return stream; @@ -249,9 +249,19 @@ var Library = { fopen__deps: ['STDIO'], fopen: function(filename, mode) { - var str = Pointer_stringify(filename); - //assert(str in this._STDIO.filenames, 'No information for file: ' + str); - return this._STDIO.filenames[str]; + filename = Pointer_stringify(filename); + mode = Pointer_stringify(mode); + if (mode.indexOf('r') >= 0) { + //assert(filename in this._STDIO.filenames, 'No information for file: ' + filename); + var stream = this._STDIO.filenames[filename]; + var info = this._STDIO.streams[stream]; + info.position = info.error = info.eof = 0; + return stream; + } else if (mode.indexOf('w') >= 0) { + return this._STDIO.prepare(filename); + } else { + assert(false, 'fopen with odd params: ' + mode); + } }, __01fopen64_: 'fopen', @@ -304,7 +314,13 @@ var Library = { var info = this._STDIO.streams[stream]; if (info.print) { __print__(intArrayToString(Array_copy(ptr, count*size))); - } // XXX + } else { + for (var i = 0; i < size*count; i++) { + info.data[info.position] = HEAP[ptr]; + info.position++; + ptr++; + } + } return count; }, diff --git a/src/postamble.js b/src/postamble.js index 87850d96..70c45b98 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -38,3 +38,5 @@ Module['run'] = run; run(args); #endif +// {{POST_RUN_ADDITIONS}} + |