From c36f15dc80a39b3cb5492005a906948cbea14c65 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Mon, 7 Dec 2020 12:17:01 +0800 Subject: [PATCH] Add files. --- 4/fileDirectory/.dirent.c.swp | Bin 0 -> 12288 bytes 4/fileDirectory/.filetime.c.swp | Bin 0 -> 12288 bytes 4/fileDirectory/.mkdir.c.swp | Bin 0 -> 12288 bytes 4/fileDirectory/access.c | 19 ++++++++++ 4/fileDirectory/chdir.c | 6 ++++ 4/fileDirectory/chmod.c | 22 ++++++++++++ 4/fileDirectory/chown.c | 8 +++++ 4/fileDirectory/dirent.c | 23 ++++++++++++ 4/fileDirectory/filetime.c | 52 +++++++++++++++++++++++++++ 4/fileDirectory/links.c | 60 ++++++++++++++++++++++++++++++++ 4/fileDirectory/mkdir.c | 18 ++++++++++ 4/fileDirectory/rename.c | 10 ++++++ 4/fileDirectory/stat.c | 45 ++++++++++++++++++++++++ 4/fileDirectory/symlinks.c | 34 ++++++++++++++++++ 4/fileDirectory/test/test1 | Bin 0 -> 256 bytes 4/fileDirectory/test/test3 | 0 4/fileDirectory/trunc.c | 9 +++++ 4/fileDirectory/umask.c | 17 +++++++++ 18 files changed, 323 insertions(+) create mode 100644 4/fileDirectory/.dirent.c.swp create mode 100644 4/fileDirectory/.filetime.c.swp create mode 100644 4/fileDirectory/.mkdir.c.swp create mode 100644 4/fileDirectory/access.c create mode 100644 4/fileDirectory/chdir.c create mode 100644 4/fileDirectory/chmod.c create mode 100644 4/fileDirectory/chown.c create mode 100644 4/fileDirectory/dirent.c create mode 100644 4/fileDirectory/filetime.c create mode 100644 4/fileDirectory/links.c create mode 100644 4/fileDirectory/mkdir.c create mode 100644 4/fileDirectory/rename.c create mode 100644 4/fileDirectory/stat.c create mode 100644 4/fileDirectory/symlinks.c create mode 100644 4/fileDirectory/test/test1 create mode 100644 4/fileDirectory/test/test3 create mode 100644 4/fileDirectory/trunc.c create mode 100644 4/fileDirectory/umask.c diff --git a/4/fileDirectory/.dirent.c.swp b/4/fileDirectory/.dirent.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..65e0b12835e446ec13bd6a98560402306b156854 GIT binary patch literal 12288 zcmeI2O=}ZD7{{l*`!0C&Y1-gMnzvdpU{gVI2~-c36a=AecP8n;b~a3A)0PrFi)YcV zV14--p1kXK@G5xpKbv)FrKO$(@frAKb|=p~&ojSU$ZRz>x(}#TTM`%-g?Re@^X8A) z1+ns22xWETm(LpOp-Zjt(bbL`pC0|pht$_yD)+S+^o-u?8mDYOj8uKO-q*3}XsaTZ z*!{Z1lX0~_s(*S#{)P>(fy_Ya`qkDsacimB@bB}bIl6MW$8y*J8(;%$fDNz#Hoykh z02}y^47lNpc#iYV=4ZZ^&ojTx`72LsfDNz#Hoykh02^QfY=8~00XDz}*uXz%Ae2H} zIxoZv;Q#;s_51(Z1tGqIFW?h+2M)n2Fa`qwpIvU;}J`4X^<=zy{a=8(;%$fDQa@1CwG1qDhWb{+c&6?g~LsoQxFi zXa_@lRQ$?Bh;sAxh|0}YEU6r)PfTf%w2^9Wk$e=cb5~d8rU{GO#~)DT%nC#_wA#3S zP&%R5u_`<2r0Uuv=oV?>#0;k0QJb8K<0FTm-EG`O53;kNYXn#o$|9gbb8ULqTU)Cf zAd{exvuWgLdPKUJcY$3##;eH;7N@H=vj&QTBtvDemz1@Mg|w_c-Os91JFi3sK2N9n tXqq3%^KC(?=6Ape?S$F{<3!7fkDk{iic_g*W#ptzYTK(vmZrV>wV&Vs(B%LC literal 0 HcmV?d00001 diff --git a/4/fileDirectory/.filetime.c.swp b/4/fileDirectory/.filetime.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..d1733e34430a66fcd886d92d362088b6b9d4e6b1 GIT binary patch literal 12288 zcmeI2F>ljA6vwX}X$uH;bWNd3kmJN@kt*6Sb*TU$wJHcf6;k_LETwj^&w&U5f=>Vg zLJSNbX5bruL|4SXz$btWSYhM;&JIZmw5$~`(l72izdOJCy<4hyy?TD>temrJ0-u9I zynpnr@pbg5Shy&JiUXI1cl)d~bsC41U0zh3zh~d3l*)=uy>1Y0tcJmjrBJKbZ@Q{7 zTk(UIS`1?4>L~7-NC%tB{-+B5{!G5Z2G{@$q}ngfO^BJAQzai$g-Ln%&?>8818jf| zumLu}2G{@_U;}L6KQf@(BjOJBJDTr#HouN+yXGNZ*Z><~18jf|umLu}2G{@_U;}J` z4X}YdXrSo{(H+BY{o^PezyJ6C|NnA8h>zeccm-a97vKrd;2O9BPJn4p0F&Sd7z6vj zxBWtV09)V~r^CoP^L8$#=!B6R};4qOYj-&W;Sil@91Hah2fmIz#Qc_Zq3$j|0H|dFH z;F7Z@>(a&FG$|XZ(ooY3VJy*c>gw!PTe*2oZOzoVV_{KSg_7Q3&MK2yC3_x^|Z&g~<>~ptOhZZ$KZ%qVQrE=Sc60pPd)VCAepeXC+a2hqFpGA=+ z|IiC+Qa_C%i#oewR}$USzaJGE#W}I$G%CwZi8&;B_st~Sp;~u+!eHN{*qi`VhWFNo zdf#t7mc)5A%^&DJ9h_|1VDBkyvl)cNP84|N+=!_lbX%#X8$0HecW RNjIs`hK5IdH`FZ}`UyG=RMr3h literal 0 HcmV?d00001 diff --git a/4/fileDirectory/.mkdir.c.swp b/4/fileDirectory/.mkdir.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..37480d0a233f9da32d727ed0f94892c23eee770d GIT binary patch literal 12288 zcmeI&KTq307zXec_WluU?a;$8Boc`OWk71ggrFG^$RUMFm5>{s;RHEH=QARrzyRN( z8xjlR161mORAxHz0T|&+^gSm;EfutD)#s!qw%>bmj(?k#`&fBbTM*ObN#gOG==kJv z?Rs#Gs_%)UQB7C=)2N3wGum~RX65$7-TSV^uiGr@D81g$YNw{HG;!FJ{*)i9Ejg=< zY}&+h{MKfqO!=>F|L_el0|5wh1u`3#rk~L3$%%?9jCvztc&Gs#2tWV=5P$##AOHaf zK;S}k_bvs`x>CQVG{H?g+D2&`7d+GA9ppk9lb8?M zE4oP)7xHSs^EZ-~^z;4bR;Jc>ANiJZuPCabQWU$y{oVEN7R={=TQILaw&1u3R#t +#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 0000000000000000000000000000000000000000..65f57c2ee985713476ac0b6e3483e6fe472e2176 GIT binary patch literal 256 LcmZQz7})>-0RR92 literal 0 HcmV?d00001 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"); + } +}