|
26 | 26 | #include <ctype.h> |
27 | 27 | #include <time.h> |
28 | 28 | #include <unistd.h> // mkstemp(), close(), unlink() |
| 29 | +#include <fcntl.h> |
| 30 | +#include <sys/stat.h> |
29 | 31 |
|
30 | 32 | #define AIGSIM_LIBRARY_ONLY |
31 | 33 |
|
@@ -284,17 +286,29 @@ static int ends_with(const char *s, const char *suf) { |
284 | 286 |
|
285 | 287 | static int make_tmp_file(char *path, size_t cap, const char *prefix) { |
286 | 288 | // Creates an existing temp file (for input) |
| 289 | +#if defined(__wasm) |
| 290 | + static int seq = 0; // no risk of collision since we're in a sandbox |
| 291 | + snprintf(path, cap, "%s%08d", prefix, seq++); |
| 292 | + int fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE); |
| 293 | +#else |
287 | 294 | snprintf(path, cap, "/tmp/%sXXXXXX", prefix); |
288 | 295 | int fd = mkstemp(path); |
| 296 | +#endif |
289 | 297 | if (fd < 0) return 0; |
290 | 298 | close(fd); |
291 | 299 | return 1; |
292 | 300 | } |
293 | 301 |
|
294 | 302 | static int make_tmp_path_noexist(char *path, size_t cap, const char *prefix) { |
295 | 303 | // Creates a unique temp path that does not exist (for output) |
| 304 | +#if defined(__wasm) |
| 305 | + static int seq = 0; // no risk of collision since we're in a sandbox |
| 306 | + snprintf(path, cap, "%s%08d", prefix, seq++); |
| 307 | + int fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE); |
| 308 | +#else |
296 | 309 | snprintf(path, cap, "/tmp/%sXXXXXX", prefix); |
297 | 310 | int fd = mkstemp(path); |
| 311 | +#endif |
298 | 312 | if (fd < 0) return 0; |
299 | 313 | close(fd); |
300 | 314 | unlink(path); |
@@ -527,7 +541,11 @@ static int SimulateCompareAigBin(const AigMan *p1, const char *bin, |
527 | 541 | // Run external binary: "<bin> <inFile> <outFile>" |
528 | 542 | remove(outFile); |
529 | 543 | snprintf(cmd, sizeof(cmd), "%s %s %s", bin, inFile, outFile); |
| 544 | +#if defined(__wasm) |
| 545 | + int rc = -1; |
| 546 | +#else |
530 | 547 | int rc = system(cmd); |
| 548 | +#endif |
531 | 549 | if (rc != 0) { |
532 | 550 | fprintf(stderr, "Error: system() failed (rc=%d): %s\n", rc, cmd); |
533 | 551 | goto fail; |
|
0 commit comments