diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-01-02 11:31:53 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-01-02 11:31:53 -0800 |
commit | faf6b1d3504a8e999d94469abf92938492bedb7b (patch) | |
tree | c986c1218d4206c18b71758a1ef09c47b47bdf63 | |
parent | fe80f74127650c4e51ea72975fdc5e3b631843db (diff) |
mkstemp
-rw-r--r-- | src/library.js | 9 | ||||
-rw-r--r-- | tests/files.cpp | 18 | ||||
-rwxr-xr-x | tests/runner.py | 2 |
3 files changed, 28 insertions, 1 deletions
diff --git a/src/library.js b/src/library.js index 159c7087..42eb951f 100644 --- a/src/library.js +++ b/src/library.js @@ -1227,6 +1227,15 @@ LibraryManager.library = { // http://pubs.opengroup.org/onlinepubs/009695399/functions/creat.html return _open(path, {{{ cDefine('O_WRONLY') }}} | {{{ cDefine('O_CREAT') }}} | {{{ cDefine('O_TRUNC') }}}, allocate([mode, 0, 0, 0], 'i32', ALLOC_STACK)); }, + mkstemp__deps: ['creat'], + mkstemp: function(template) { + if (!_mkstemp.counter) _mkstemp.counter = 0; + var c = (_mkstemp.counter++).toString(); + var rep = 'XXXXXX'; + while (c.length < rep.length) c = '0' + c; + writeArrayToMemory(intArrayFromString(c), template + Pointer_stringify(template).indexOf(rep)); + return _creat(template, 0600); + }, fcntl__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__flock_struct_layout'], fcntl: function(fildes, cmd, varargs, dup2) { // int fcntl(int fildes, int cmd, ...); diff --git a/tests/files.cpp b/tests/files.cpp index e1a38421..04baa151 100644 --- a/tests/files.cpp +++ b/tests/files.cpp @@ -1,6 +1,7 @@ #include <assert.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> int main() { @@ -103,6 +104,23 @@ int main() fclose(inf); printf("fscanfed: %d - %s\n", number, text); + // temp files + const char *tname = "file_XXXXXX.txt"; + char tname1[100]; + char tname2[100]; + strcpy(tname1, tname); + strcpy(tname2, tname); + assert(!strcmp(tname1, tname2)); // equal + int f1 = mkstemp(tname1); + int f2 = mkstemp(tname2); + assert(f1 != f2); + //printf("%d,%d,%s,%s\n", f1, f2, tname1, tname2); + assert(strcmp(tname1, tname2)); // not equal + assert(fopen(tname1, "r")); + assert(fopen(tname2, "r")); + assert(!fopen(tname2+1, "r")); // sanity check that we can't open just anything + printf("ok.\n"); + return 0; } diff --git a/tests/runner.py b/tests/runner.py index 97ec3b94..1ab5c7af 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -5021,7 +5021,7 @@ def process(filename): other.close() src = open(path_from_root('tests', 'files.cpp'), 'r').read() - self.do_run(src, 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\ntexte\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\n', + self.do_run(src, 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\ntexte\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\n', post_build=post, extra_emscripten_args=['-H', 'libc/fcntl.h']) def test_files_m(self): |