diff --git a/4/fileDirectory/.dirent.c.swp b/4/fileDirectory/.dirent.c.swp new file mode 100644 index 0000000..65e0b12 Binary files /dev/null and b/4/fileDirectory/.dirent.c.swp differ diff --git a/4/fileDirectory/.filetime.c.swp b/4/fileDirectory/.filetime.c.swp new file mode 100644 index 0000000..d1733e3 Binary files /dev/null and b/4/fileDirectory/.filetime.c.swp differ diff --git a/4/fileDirectory/.mkdir.c.swp b/4/fileDirectory/.mkdir.c.swp new file mode 100644 index 0000000..37480d0 Binary files /dev/null and b/4/fileDirectory/.mkdir.c.swp differ diff --git a/4/fileDirectory/access.c b/4/fileDirectory/access.c new file mode 100644 index 0000000..037375e --- /dev/null +++ b/4/fileDirectory/access.c @@ -0,0 +1,19 @@ +#include +#include + +int main(void){ + if(access("./access.c", R_OK | W_OK) < 0){ + printf("access error for acccess.c\n"); + } + else { + printf("read and write access OK\n"); + } + + if(access("./access.c", R_OK | X_OK) < 0){ + printf("access error for acccess.c\n"); + } + else { + printf("exec access OK\n"); + } + return 0; +} diff --git a/4/fileDirectory/chdir.c b/4/fileDirectory/chdir.c new file mode 100644 index 0000000..0eeaf73 --- /dev/null +++ b/4/fileDirectory/chdir.c @@ -0,0 +1,6 @@ +#include + +int main(void) { + // TODO + return 0; +} diff --git a/4/fileDirectory/chmod.c b/4/fileDirectory/chmod.c new file mode 100644 index 0000000..f37ea6f --- /dev/null +++ b/4/fileDirectory/chmod.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +#define RWRWRW S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH +#define RWRR S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH + +int main(void){ + int fd = open("./test/test2", O_RDWR); + if(fd == -1){ + printf("open file test1 error\n"); + } + + fchmod(fd, RWRWRW); + + close(fd); + + chmod("./test/test1", RWRR); + + return 0; +} diff --git a/4/fileDirectory/chown.c b/4/fileDirectory/chown.c new file mode 100644 index 0000000..f86e3d7 --- /dev/null +++ b/4/fileDirectory/chown.c @@ -0,0 +1,8 @@ +#include +#include +#include + +int main(void){ + printf("%ld\n", pathconf("./chown.c",_POSIX_CHOWN_RESTRICTED)); + return 0; +} diff --git a/4/fileDirectory/dirent.c b/4/fileDirectory/dirent.c new file mode 100644 index 0000000..b2a9c4f --- /dev/null +++ b/4/fileDirectory/dirent.c @@ -0,0 +1,23 @@ +#include +#include + +int main(void) { + DIR *pdir = opendir("."); + + if(pdir == NULL) { + printf("opendir error\n"); + return -1; + } + + struct dirent *pdirent = NULL; + + while((pdirent = readdir(pdir)) != NULL){ + long index = telldir(pdir); + printf("%15s %18ld %lu\n", pdirent->d_name, index, pdirent->d_ino); + + } + + closedir(pdir); + + return 0; +} diff --git a/4/fileDirectory/filetime.c b/4/fileDirectory/filetime.c new file mode 100644 index 0000000..e10d239 --- /dev/null +++ b/4/fileDirectory/filetime.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + + +int main(void) { + int fd = open("./filetime.c", O_RDWR); + + if(fd == -1) { + printf("open file eror filetime.c\n"); + return -1; + } + + + struct stat buf; + + if(fstat(fd, &buf) < 0){ + printf("stat error\n"); + return -1; + } + + struct timespec atim = buf.st_atim, + ctim = buf.st_ctim, + mtim = buf.st_mtim; + + printf("sec time: %12ld %12ld %12ld\n", atim.tv_sec, ctim.tv_sec, mtim.tv_sec); + printf("nsec time: %12ld %12ld %12ld\n", atim.tv_nsec, ctim.tv_nsec, mtim.tv_nsec); + + int fd1 = open("./test/test3", O_RDWR); + + if(fd1 == -1) { + printf("open file error test3\n"); + return -1; + } + + struct timespec times[2]; + + times[0] = atim; + times[1] = ctim; + + if(futimens(fd1, times) < 0) { + printf("futimens error\n"); + return -1; + } + + close(fd1); + + close(fd); + + return 0; +} diff --git a/4/fileDirectory/links.c b/4/fileDirectory/links.c new file mode 100644 index 0000000..e9cd9a2 --- /dev/null +++ b/4/fileDirectory/links.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include + +extern int errno; + +int main(void) { + int fd = open("./links.c", O_RDWR); + + if(fd == -1) { + printf("oprn error test1\n"); + return -1; + } + + struct stat buf; + + if(fstat(fd, &buf) < 0) { + printf("fstat error\n"); + return -1; + } + + printf("%lu\n", buf.st_nlink); + + close(fd); + + fd = open(".", O_RDONLY); + + if(fd == -1){ + printf("open error .\n"); + return -1; + } + + int fd2 = open("/home/eric", O_RDONLY); + + if(fd2 == -1){ + printf("open error ~\n"); + return -1; + } + + if(linkat(fd, "links.c", fd2, "test/linksl.c", 0) < 0) { + printf("linkat error\n"); + if(errno != 0) { + printf("strerr: %s\n", strerror(errno)); + } + } + + if(unlinkat(fd2, "test/linksl.c", 0) < 0) { + printf("unlinkat error\n"); + return -1; + } + + close(fd); + close(fd2); + + + return 0; +} diff --git a/4/fileDirectory/mkdir.c b/4/fileDirectory/mkdir.c new file mode 100644 index 0000000..bf92f77 --- /dev/null +++ b/4/fileDirectory/mkdir.c @@ -0,0 +1,18 @@ +#include +#include +#include + +#define RWXRR S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH + +int main(void) { + if(mkdir("/home/eric/test/testmkdir", RWXRR) < 0) { + printf("mkdir error\n"); + } + + if(rmdir("/home/eric/test/testmkdir") < 0) { + printf("rmdir error\n"); + return -1; + } + + return 0; +} diff --git a/4/fileDirectory/rename.c b/4/fileDirectory/rename.c new file mode 100644 index 0000000..3eaa34b --- /dev/null +++ b/4/fileDirectory/rename.c @@ -0,0 +1,10 @@ +#include +#include + +int main(void) { + if(renameat(AT_FDCWD, "test/test2", AT_FDCWD, "test/test3") < 0) { + printf("rename error\n"); + return -1; + } + return 0; +} diff --git a/4/fileDirectory/stat.c b/4/fileDirectory/stat.c new file mode 100644 index 0000000..21b5d87 --- /dev/null +++ b/4/fileDirectory/stat.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +int printStat(struct stat buf){ + + printf("DEV: %ld UID: %d SIZE: %ld BLOCKS: %ld\n", buf.st_dev, buf.st_uid, buf.st_size, buf.st_blocks); + printf("MODE: %d INO: %ld BLKSIZE: %ld GID: %d\n", buf.st_mode, buf.st_ino, buf.st_blksize, buf.st_gid); + + if(S_ISREG(buf.st_mode)){ + printf("a regular file\n"); + } + else if(S_ISDIR(buf.st_mode)) { + printf("a directory\n"); + } +} + +int main(void){ + struct stat buf; + if(stat("./stat.c", &buf) == -1){ + printf("stat error\n"); + return -1; + } + + printStat(buf); + + int fd = open(".", O_RDONLY); + + if(fd == -1){ + printf("open O_RDONLY failed\n"); + return -1; + } + + if(fstat(fd, &buf) < 0){ + printf("fstat error\n"); + return -1; + } + + printStat(buf); + + close(fd); + + return 0; +} diff --git a/4/fileDirectory/symlinks.c b/4/fileDirectory/symlinks.c new file mode 100644 index 0000000..dda2ebb --- /dev/null +++ b/4/fileDirectory/symlinks.c @@ -0,0 +1,34 @@ +#include +#include +#include + +int main(void) { + + int fd = open("/home/eric", O_RDONLY); + + if(fd == -1) { + printf("open error for directory .\n"); + return -1; + } + + if(symlinkat("symlinks.c", fd, "test/symlinksl.c") < 0) { + printf("symlink error\n"); + return -1; + } + + close(fd); + + char buf[1024]; + ssize_t rbit = readlink("/home/eric/test/symlinksl.c", buf, 1024); + + if(rbit < 0) { + printf("readlink error\n"); + return -1; + } + + buf[rbit] = 0; + + printf("rbit: %d buf: %s\n", rbit, buf); + + return 0; +} diff --git a/4/fileDirectory/test/test1 b/4/fileDirectory/test/test1 new file mode 100644 index 0000000..65f57c2 Binary files /dev/null and b/4/fileDirectory/test/test1 differ diff --git a/4/fileDirectory/test/test3 b/4/fileDirectory/test/test3 new file mode 100644 index 0000000..e69de29 diff --git a/4/fileDirectory/trunc.c b/4/fileDirectory/trunc.c new file mode 100644 index 0000000..33b6a93 --- /dev/null +++ b/4/fileDirectory/trunc.c @@ -0,0 +1,9 @@ +#include +#include + +int main(void){ + if(truncate("./test/test1", 256) < 0) { + printf("truncate error for file test1\n"); + } + return 0; +} diff --git a/4/fileDirectory/umask.c b/4/fileDirectory/umask.c new file mode 100644 index 0000000..d34db96 --- /dev/null +++ b/4/fileDirectory/umask.c @@ -0,0 +1,17 @@ +#include +#include +#include + +#define RWRWRW S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH + +int main(void) { + umask(0); + if(creat("./test/test1", RWRWRW) < 0){ + printf("creat error for test1\n"); + } + umask(S_IWGRP | S_IROTH | S_IWOTH); + + if(creat("./test/test2", RWRWRW) < 0){ + printf("creat error for test1\n"); + } +}