1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
* Copyright 2014 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <emscripten.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
EM_ASM(
var O_RDONLY = 0;
var O_WRONLY = 1;
var O_RDWR = 2;
var O_CREAT = 64;
var O_EXCL = 128;
var O_TRUNC = 512;
var O_APPEND = 1024;
var O_ACCMODE = 2097155;
FS.trackingDelegate['willMovePath'] = function(oldpath, newpath) {
out('About to move "' + oldpath + '" to "' + newpath + '"');
};
FS.trackingDelegate['onMovePath'] = function(oldpath, newpath) {
out('Moved "' + oldpath + '" to "' + newpath + '"');
};
FS.trackingDelegate['willDeletePath'] = function(path) {
out('About to delete "' + path + '"');
};
FS.trackingDelegate['onDeletePath'] = function(path) {
out('Deleted "' + path + '"');
};
FS.trackingDelegate['onOpenFile'] = function(path, flags) {
var stat = FS.stat(path);
var fileSize = stat.size;
var trackingFlags = [];
if (flags & O_CREAT) {
trackingFlags.push('O_CREAT');
}
if (flags & O_TRUNC) {
trackingFlags.push('O_TRUNC');
}
if (flags & O_APPEND) {
trackingFlags.push('O_APPEND');
}
if (flags & O_EXCL) {
trackingFlags.push('O_EXCL');
}
// The argument flags must include one of the following access
// modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the
// file read-only, write-only, or read/write, respectively.
// https://man7.org/linux/man-pages/man2/open.2.html
if ((flags & O_ACCMODE) == O_WRONLY) {
trackingFlags.push('O_WRONLY');
}
if ((flags & O_ACCMODE) == O_RDWR) {
trackingFlags.push('O_RDWR');
}
if ((flags & O_ACCMODE) == O_RDONLY) {
trackingFlags.push('O_RDONLY');
}
var output = 'Opened "' + path + '" with flags ';
for (var i = 0; i < trackingFlags.length; i++) {
output += trackingFlags[i] + ' ';
}
output += 'and file size ' + fileSize;
out(output)
};
FS.trackingDelegate['onReadFile'] = function(path, bytesRead) {
out('Read ' + bytesRead + ' bytes from "' + path + '"');
};
FS.trackingDelegate['onWriteToFile'] = function(path, bytesWritten) {
out('Wrote to file "' + path + '" with ' + bytesWritten + ' bytes written');
};
FS.trackingDelegate['onSeekFile'] = function(path, position, whence) {
out('Seek on "' + path + '" with position ' + position + ' and whence ' + whence);
};
FS.trackingDelegate['onCloseFile'] = function(path) {
out('Closed ' + path);
};
FS.trackingDelegate['onMakeDirectory'] = function(path, mode) {
out('Created directory "' + path + '" with mode ' + mode);
};
FS.trackingDelegate['onMakeSymlink'] = function(oldpath, newpath) {
out('Created symlink from "' + oldpath + '" to "' + newpath + '"');
};
);
FILE *file;
char buffer[100];
file = fopen("/file.txt", "w+");
fputs("hello", file);
fwrite(" world", 7, 1, file);
fseek(file, 0, SEEK_SET);
fread(buffer, 13, 1, file);
printf("%s\n", buffer);
fclose(file);
rename("/file.txt", "/renamed.txt");
file = fopen("/renamed.txt", "r");
char str[256] = {};
fgets(str, 255, file);
printf("File read returned '%s'\n", str);
fclose(file);
// Test unistd.h functions
int fd = open("/renamed.txt", O_RDONLY);
close(fd);
fd = open("/renamed.txt", O_WRONLY);
close(fd);
fd = open("/renamed.txt", O_RDWR);
char test[] = "test";
write(fd, test, 10);
lseek(fd, 0, SEEK_SET);
char output[100];
read(fd, output, 100);
printf("read returned %s\n", output);
close(fd);
fd = open("/renamed.txt", O_CREAT, 0666);
close(fd);
fd = open("/renamed.txt", O_EXCL | O_WRONLY);
close(fd);
fd = open("/renamed.txt", O_TRUNC);
close(fd);
fd = open("/renamed.txt", O_TRUNC | O_WRONLY);
close(fd);
fd = open("/renamed.txt", O_TRUNC | O_RDWR);
close(fd);
fd = open("/renamed.txt", O_APPEND | O_RDONLY);
close(fd);
remove("/renamed.txt");
mkdir("/home/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
symlink("/renamed.txt", "/file.txt");
}
|