linux system function learning_( 5)access function, chmod function, chown function, truncate function, rename function and link related functions
access() function
Function: test whether the specified file has certain permissions
Prototype: int access(const char *pathname, int mode);
Parameters:
Pathname - > file name
Mode - > permission category: R_OK, do you have read permission, W_OK, do you have write permission, X_OK whether there is execution permission, F_OK tests whether a file exists
Return value:
0 - > all permissions to be checked have passed the check - 1 - > permissions are prohibited
// access.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char* argv[]) { if(argc < 2) { printf("a.out filename\n"); exit(1); } int ret = access(argv[1], W_OK); if(ret == -1) { perror("access"); exit(1); } printf("you can write this file.\n"); return 0; }
chmod() function
Function: change the permissions of the file
Prototype: int chmod(const char *filename, int pmode);
Parameters: filename - > file name pmode - > permission: must be an octal number
Return value: 0 - > change succeeded - 1 - > failed
// chmod.c #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> int main(int argc, char* argv[]) { if(argc < 2) { printf("a.out filename\n"); exit(1); } int ret = chmod(argv[1], 0755); if(ret == -1) { perror("chmod"); exit(1); } return 0; }
chown() function
Function: change the owner of the file. chown() will change the owner of the file specified by the parameter path to the user represented by the parameter owner, and change the group of the file to the parameter group. If the parameter owner or group is - 1, the corresponding owner or group will not be changed.
Prototype: int chown (const char * path, uid)_ t owner, gid_ t group);
Return value: 0 - > success - 1 - > failure
// chown.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { if(argc < 2) { printf("a.out filename!\n"); exit(1); } // user->ftp group->ftp int ret = chown(argv[1], 116, 125); if(ret == -1) { perror("chown"); exit(1); } return 0; }
truncate() function
Function: change the file size specified by the parameter path to the size specified by the parameter length. If the original file size is larger than the parameter length, the exceeding part will be deleted.
Prototype: int truncate (const char * path, off)_ t length);
Return value: 0 - > success - 1 - > failure
// truncate.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> int main(int argc, char* argv[]) { if(argc < 3) { printf("a.out filename 111\n"); exit(1); } long int len = strtol(argv[2], NULL, 10); int aa = truncate(argv[1], len); if(aa == -1) { perror("truncate"); exit(1); } return 0; }
rename() function
Role: File renaming
Prototype: int rename(const char *oldpath, const char *newpath);
Return value: 0 - > success - 1 - > failure
// rename.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { if(argc < 3) { printf("a.out oldName newName\n"); exit(1); } int ret = rename(argv[1], argv[2]); if(ret == -1) { perror("rename"); exit(1); } return 0; }
link
link() function -- function: create a hard link; Prototype: int link(const char *oldpath, const char *newpath);
// link.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char* argv[]) { if(argc < 3) { printf("a.out oldpath newpath\n"); exit(0); } int ret = link(argv[1], argv[2]); if(ret == -1) { perror("link"); exit(1); } return 0; }
symlink() function -- function: create a soft link
// symlink.c #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { if(argc < 3) { printf("a.out oldpath newpath\n"); exit(1); } int ret = symlink(argv[1], argv[2]); if(ret == -1) { perror("symlink"); exit(1); } return 0; }
readlink() function -- function: read the file name corresponding to the soft connection, not the content
// readlink.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char* argv[]) { if(argc < 2) { printf("a.out softlink\n"); exit(1); } char buf[512]; int ret = readlink(argv[1], buf, sizeof(buf)); if(ret == -1) { perror("readlink"); exit(1); } buf[ret] = 0; printf("buf = %s\n", buf); return 0; }
unlink() function:
Function: delete the directory entry of a file and reduce its number of links. If it is successful, it returns 0; otherwise, it returns - 1. The error reason is stored in errno. If you want to successfully delete a file by calling this function, you must have write and execute permissions in the directory to which the file belongs.
Use: 1. If it is a symbolic link, delete the symbolic link; 2. If it is a hard link, the number of hard links is reduced by 1. When it is reduced to 0, the data block and inode are released; 3. If the number of hard links of the file is 0, but a process has opened the file and holds the file descriptor, the kernel will not delete the file until the process closes the file (use this feature to create a temporary file, open or create a file first, and unlink the file immediately).
// unlink.c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(void) { int fd = open("tempfile", O_CREAT | O_RDWR, 0755); if(fd == -1) { perror("open"); exit(1); } int ret = unlink("tempfile"); if(ret == -1) { perror("unlink"); exit(1); } char buf[512]; write(fd, "hello", 5); lseek(fd, 0, SEEK_SET); int len = read(fd, buf, sizeof(buf)); write(STDOUT_FILENO, buf, len); close(fd); return 0; }
reference resources
Reference dark horse programmer linux system programming materials