diff --git a/3/file/fileappend.c b/3/file/fileappend.c new file mode 100644 index 0000000..7b27020 --- /dev/null +++ b/3/file/fileappend.c @@ -0,0 +1,46 @@ +#include +#include +#include + +int readFile(int fd){ + + char buff[1024]; + + ssize_t rbit = pread(fd, buff, 1024, 0); + + if(rbit < 0){ + printf("Error in reading file\n"); + return -1; + } + + buff[rbit] = '\0'; + + printf("rbit: %ld %s\n", rbit, buff); + + return 0; + +} + +int main(void){ + int fd = open("./test/fileat", O_RDWR | O_SYNC); + + if(fd < 0){ + printf("Error in opening file fileat.\n"); + return -1; + } + + readFile(fd); + + ssize_t wbit = pwrite(fd, "How", 3, 0); + + if(wbit < 0) { + printf("Error in writing file.\n"); + return -1; + } + + readFile(fd); + + close(fd); + + return 0; +} diff --git a/3/file/filecntl.c b/3/file/filecntl.c new file mode 100644 index 0000000..c4bd681 --- /dev/null +++ b/3/file/filecntl.c @@ -0,0 +1,30 @@ +#include +#include +#include + +int getFL(int fd) { return fcntl(fd, F_GETFL, 0); } + +int main(void) { + int fd = open("./test/fileat", O_RDWR); + + printf("FL: %d\n", getFL(fd)); + + if (fcntl(fd, F_SETFL, getFL(fd) | O_APPEND) < 0) { + printf("fcntl F_SETFL error\n"); + return -1; + } + + printf("FL: %d\n", getFL(fd)); + + char buff[] = "Nope"; + int wbit = write(fd, buff, sizeof(buff)); + + if (wbit < 0) { + printf("write buff error\n"); + return -1; + } + + close(fd); + + return 0; +} diff --git a/3/file/filedup.c b/3/file/filedup.c new file mode 100644 index 0000000..aab20ce --- /dev/null +++ b/3/file/filedup.c @@ -0,0 +1,31 @@ +#include +#include +#include + +int main(void) { + int fd = open("./test/fileat", O_RDWR | O_SYNC); + + int fd2 = dup(fd); + + off_t off = lseek(fd, 3, SEEK_SET); + + if (off == -1) { + printf("Fail to call lseek\n"); + return -1; + } + + char buff[1024]; + + int rbit = read(fd2, buff, 1024); + + if (rbit < 0) { + printf("Error in reading file fileat"); + return -1; + } + + buff[rbit] = 0; + + printf("rbit: %ld %s\n", rbit, buff); + + return 0; +} diff --git a/3/file/filerw.c b/3/file/filerw.c new file mode 100644 index 0000000..8b06d01 --- /dev/null +++ b/3/file/filerw.c @@ -0,0 +1,33 @@ +#include +#include +#include + +int main(void){ + int fd = open("./test/fileat", O_RDWR | O_SYNC); + + if(fd == -1){ + printf("Error in opening file fileat.\n"); + } + + char buff[1024]; + + ssize_t rbit = read(fd, buff, 1024); + + if(rbit > 0) + buff[rbit] = '\0'; + + printf("%ld %s\n", rbit, buff); + + off_t off = lseek(fd, 0, SEEK_END); + + printf("offset: %ld\n", off); + + char buff2[32] = "Hello World!"; + + ssize_t wbit = write(fd, buff2, 13); + + printf("wbit: %ld\n", wbit); + + + return 0; +} diff --git a/3/file/filesync.c b/3/file/filesync.c new file mode 100644 index 0000000..f4a8dc8 --- /dev/null +++ b/3/file/filesync.c @@ -0,0 +1,51 @@ +#include +#include +#include + +int readFile(){ + + int fd2 = open("./test/fileat", O_RDONLY); + + if(fd2 < 0){ + printf("Error in opening file fileat\n"); + return -1; + } + + char buff2[1024]; + + ssize_t rbit = read(fd2, buff2, 1024); + + if(rbit < 0){ + printf("Error in reading file fileat\n"); + return -1; + } + buff2[rbit] = 0; + printf("rbit: %ld %s", rbit, buff2); + + close(fd2); + return 0; + +} + +int main(void){ + int fd = open("./test/fileat", O_RDWR | O_APPEND); + + char buff[] = "Helloworld"; + + ssize_t wbit = write(fd, buff, sizeof(buff)); + + if(wbit != sizeof(buff)){ + printf("Error occur when wrote file fileat\n"); + return -1; + } + + readFile(); + + fsync(fd); + + readFile(); + + close(fd); + + return 0; +} diff --git a/3/file/openfile.c b/3/file/openfile.c new file mode 100644 index 0000000..9e1f78d --- /dev/null +++ b/3/file/openfile.c @@ -0,0 +1,35 @@ +#include +#include +#include + +#define RWRR S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH + +int main(void) { + int fd = open("./test/file", O_RDONLY | O_CREAT, RWRR); + int fd2 = open("./test/", O_DIRECTORY | O_EXCL); + + printf("%d\n", fd); + printf("%d\n", fd2); + + int fd3 = openat(fd2, "./fileat", O_TRUNC | O_RDWR | O_CREAT | O_SYNC, RWRR); + + int fd4 = creat("./test/filecreat", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + + off_t off = lseek(fd4, 8, SEEK_SET); + + off = lseek(fd4, -2, SEEK_CUR); + + off = lseek(fd4, 1024, SEEK_CUR); + + printf("%ld", off); + + close(fd4); + + close(fd3); + + close(fd2); + + close(fd); + + return 0; +} diff --git a/3/file/test/fileat b/3/file/test/fileat new file mode 100644 index 0000000..78086f7 Binary files /dev/null and b/3/file/test/fileat differ diff --git a/3/file/test/filecreat b/3/file/test/filecreat new file mode 100644 index 0000000..e69de29