Add files.

This commit is contained in:
Saturneric 2020-12-07 12:17:01 +08:00
parent d33313e906
commit c36f15dc80
18 changed files with 323 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

19
4/fileDirectory/access.c Normal file
View File

@ -0,0 +1,19 @@
#include <unistd.h>
#include <stdio.h>
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;
}

6
4/fileDirectory/chdir.c Normal file
View File

@ -0,0 +1,6 @@
#include <unistd.h>
int main(void) {
// TODO
return 0;
}

22
4/fileDirectory/chmod.c Normal file
View File

@ -0,0 +1,22 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#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;
}

8
4/fileDirectory/chown.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
int main(void){
printf("%ld\n", pathconf("./chown.c",_POSIX_CHOWN_RESTRICTED));
return 0;
}

23
4/fileDirectory/dirent.c Normal file
View File

@ -0,0 +1,23 @@
#include <dirent.h>
#include <stdio.h>
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;
}

View File

@ -0,0 +1,52 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
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;
}

60
4/fileDirectory/links.c Normal file
View File

@ -0,0 +1,60 @@
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
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;
}

18
4/fileDirectory/mkdir.c Normal file
View File

@ -0,0 +1,18 @@
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#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;
}

10
4/fileDirectory/rename.c Normal file
View File

@ -0,0 +1,10 @@
#include <fcntl.h>
#include <stdio.h>
int main(void) {
if(renameat(AT_FDCWD, "test/test2", AT_FDCWD, "test/test3") < 0) {
printf("rename error\n");
return -1;
}
return 0;
}

45
4/fileDirectory/stat.c Normal file
View File

@ -0,0 +1,45 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
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;
}

View File

@ -0,0 +1,34 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
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;
}

BIN
4/fileDirectory/test/test1 Normal file

Binary file not shown.

View File

9
4/fileDirectory/trunc.c Normal file
View File

@ -0,0 +1,9 @@
#include <unistd.h>
#include <stdio.h>
int main(void){
if(truncate("./test/test1", 256) < 0) {
printf("truncate error for file test1\n");
}
return 0;
}

17
4/fileDirectory/umask.c Normal file
View File

@ -0,0 +1,17 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#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");
}
}