2.2 文件和目录
链接文件
软链接文件(符号链接):ln -s file.txt a.txt
硬链接文件:ln file.txt b.txt
======================================================================
opendir.c
1 #include <sys/types.h> 2 #include <stdio.h> 3 #include <dirent.h> 4 5 int main(void) 6 { 7 DIR* dp = NULL; 8 struct dirent *pp = NULL; 9 if(NULL == (dp = opendir("."))) 10 { 11 perror("fail to opendir!"); 12 return -1; 13 } 14 15 while(NULL != (pp = readdir(dp))) 16 { 17 if(pp->d_name[0] == '.') 18 continue; 19 printf("%-8s ",pp->d_name); 20 } 21 printf("\n"); 22 closedir(dp); 23 24 return 0; 25 } 26 27 28 29 /* 30 #include <sys/types.h> 31 #include <dirent.h> 32 DIR *opendir(const char *name); 33 功能:打开一个目录获得一个目录流指针 34 参数:name:要打开目录的路径 35 返回值:成功返回目录流指针,失败返回NULL 36 37 #include <dirent.h> 38 struct dirent *readdir(DIR *dirp); 39 功能:从目录流中读取一个文件的信息 40 参数:dirp:目录流指针 41 返回值:成功返回一个struct dirent*结构体;失败/读到目录流末尾返回NULL 42 43 //man 3 readdir 44 On Linux, the dirent structure is defined as follows: 45 struct dirent { 46 ino_t d_ino; // inode number 47 off_t d_off; // offset to the next dirent 48 unsigned short d_reclen; // length of this record 49 unsigned char d_type; // type of file; not supported 50 // by all file system types 51 char d_name[256]; // filename 52 }; 53 54 */
mkdir.c
1 #include <unistd.h> 2 #include <stdio.h> 3 int main(void) 4 { 5 char buff[] = "mkdir_file";//文件名 6 char dir[1024] = {0};//路径名 7 8 if(NULL == getcwd(dir, 1024))//获取当前工作的绝对路径 9 { 10 perror("fail to getcwd!"); 11 return -1; 12 } 13 puts(dir);//显示当前工作的绝对路径 14 15 if(-1 == chdir("/root/mytest/"))//切换到目的工作路径 这个 16 { 17 perror("fail to chdir!"); 18 return -1; 19 } 20 if(NULL == getcwd(dir, 1024))//获取当前工作的绝对路径 21 { 22 perror("fail to getcwd!"); 23 return -1; 24 } 25 puts(dir);//显示当前工作的绝对路径 26 27 if(-1 == mkdir(buff, 0777))//创建文件目录 28 { 29 perror("fail to mkdir!"); 30 return -1; 31 } 32 getchar();//暂停,看看是否创建了文件目录 33 34 if(-1 == rmdir(buff))//删除该文件目录 35 { 36 perror("fail to rmdir!"); 37 return -1; 38 } 39 40 return 0; 41 } 42 43 /* 44 #include <sys/stat.h> 45 #include <sys/types.h> 46 int mkdir(const char *pathname, mode_t mode); 47 功能:创建一个目录 48 参数:pathname:创建目录的路径 49 mode:创建目录的权限 50 返回值:成功返回0;失败返回-1 51 52 #include <unistd.h> 53 int rmdir(const char *pathname); 54 功能:删除一个空目录 55 参数:pathname:要删除目录的路径 56 返回值:成功返回0;失败返回-1 57 58 #include <unistd.h> 59 int chdir(const char *path); 60 功能:切换当前的工作路径 61 参数:path:要切换到的路径 62 返回值:成功返回0;失败返回-1 63 64 ////////////////////////////////////////// 65 #include <unistd.h> 66 char *getcwd(char *buf, size_t size); 67 功能:获得当前工作的绝对路径 68 参数:buf:路径存放空间的首地址 69 size:存放数据的大小 70 返回值:成功返回路径字符串;失败返回NULL 71 72 #include <sys/types.h> 73 #include <sys/stat.h> 74 #include <unistd.h> 75 int stat(const char *path, struct stat *buf); 76 功能:获得文件的属性放入buf中 77 参数:path:文件的路径; 78 buf:存放文件属性空间的首地址 79 返回值:成功返回0;失败返回-1 80 81 82 83 */
stat1.c
1 #include <stdio.h> 2 #include <sys/stat.h> 3 #include <sys/types.h> 4 #include <unistd.h> 5 int main(int argc, const char *argv[]) 6 { 7 if(argc != 2) { 8 fprintf(stderr,"Usage:./a.out filename\n"); 9 return -1; 10 } 11 12 struct stat buf; 13 if(-1 == lstat(argv[1], &buf)) { 14 perror("fail to stat!"); 15 return -1; 16 } 17 //获取文件类型 18 if(S_ISBLK(buf.st_mode)) printf("b\n"); 19 else if(S_ISCHR(buf.st_mode)) printf("c\n"); 20 else if(S_ISDIR(buf.st_mode)) printf("d\n"); 21 else if(S_ISREG(buf.st_mode)) printf("-\n"); 22 else if(S_ISLNK(buf.st_mode)) printf("l\n"); 23 else if(S_ISSOCK(buf.st_mode)) printf("s\n"); 24 else if(S_ISFIFO(buf.st_mode)) printf("p\n"); 25 26 //获取文件权限 27 if(buf.st_mode & S_IRUSR) putchar('r');//owner's permission 28 else putchar('-'); 29 if(buf.st_mode & S_IWUSR) putchar('w'); 30 else putchar('-'); 31 if(buf.st_mode & S_IXUSR) putchar('x'); 32 else putchar('-'); 33 34 if(buf.st_mode & S_IRGRP) putchar('r');//group's permission 35 else putchar('-'); 36 if(buf.st_mode & S_IWGRP) putchar('w'); 37 else putchar('-'); 38 if(buf.st_mode & S_IXGRP) putchar('x'); 39 else putchar('-'); 40 41 if(buf.st_mode & S_IROTH) putchar('r');//others's permission 42 else putchar('-'); 43 if(buf.st_mode & S_IWOTH) putchar('w'); 44 else putchar('-'); 45 if(buf.st_mode & S_IXOTH) putchar('x'); 46 else putchar('-'); 47 48 printf("\n"); 49 50 return 0; 51 } 52 53 /* 54 #include <sys/types.h> 55 #include <sys/stat.h> 56 #include <unistd.h> 57 58 int stat(const char *path, struct stat *buf); 59 功能:获得文件的属性放入buf中 60 参数:path:文件的路径; 61 buf:存放文件属性空间的首地址 62 返回值:成功返回0;失败返回-1 63 64 //int fstat(int fd, struct stat *buf); 65 //int lstat(const char *path, struct stat *buf); 66 67 ////////////////////////////////////////////////// 68 struct stat { 69 dev_t st_dev; // ID of device containing file 70 ino_t st_ino; // inode number 71 mode_t st_mode; // protection 72 nlink_t st_nlink; // number of hard links 73 uid_t st_uid; // user ID of owner 74 gid_t st_gid; // group ID of owner 75 dev_t st_rdev; // device ID (if special file) 76 off_t st_size; // total size, in bytes 77 blksize_t st_blksize; // blocksize for file system I/O 78 blkcnt_t st_blocks; // number of 512B blocks allocated 79 time_t st_atime; // time of last access 80 time_t st_mtime; // time of last modification 81 time_t st_ctime; // time of last status change 82 }; 83 ///////////////////////////////////////////////////// 84 The following POSIX macros are defined to check the file type using the 85 st_mode field: 86 87 S_ISREG(m) is it a regular file? 88 89 S_ISDIR(m) directory? 90 91 S_ISCHR(m) character device? 92 93 S_ISBLK(m) block device? 94 95 S_ISFIFO(m) FIFO (named pipe)? 96 97 S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) 98 99 S_ISSOCK(m) socket? (Not in POSIX.1-1996.) 100 101 102 The following flags are defined for the st_mode field: 103 104 S_IFMT 0170000 bit mask for the file type bit fields 105 S_IFSOCK 0140000 socket 106 S_IFLNK 0120000 symbolic link 107 S_IFREG 0100000 regular file 108 S_IFBLK 0060000 block device 109 S_IFDIR 0040000 directory 110 S_IFCHR 0020000 character device 111 S_IFIFO 0010000 FIFO 112 S_ISUID 0004000 set UID bit 113 S_ISGID 0002000 set-group-ID bit (see below) 114 S_ISVTX 0001000 sticky bit (see below) 115 S_IRWXU 00700 mask for file owner permissions 116 S_IRUSR 00400 owner has read permission 117 S_IWUSR 00200 owner has write permission 118 S_IXUSR 00100 owner has execute permission 119 S_IRWXG 00070 mask for group permissions 120 S_IRGRP 00040 group has read permission 121 S_IWGRP 00020 group has write permission 122 S_IXGRP 00010 group has execute permission 123 S_IRWXO 00007 mask for permissions for others (not in group) 124 S_IROTH 00004 others have read permission 125 S_IWOTH 00002 others have write permission 126 S_IXOTH 00001 others have execute permission 127 128 129 */
ls.c
1 /* 2 #include <sys/types.h> 3 #include <pwd.h> 4 struct passwd *getpwuid(uid_t uid); 5 功能:通过uid来获得该uid对应的用户信息 6 参数:uid:用户的id号 7 返回值:成功返回用户的信息struct passwd;失败返回NULL 8 9 /////////////////////////////////////////////////////// 10 The passwd structure is defined in <pwd.h> as follows: 11 struct passwd { 12 char *pw_name; // username 13 char *pw_passwd; // user password 14 uid_t pw_uid; // user ID 15 gid_t pw_gid; // group ID 16 char *pw_gecos; // real name 17 char *pw_dir; // home directory 18 char *pw_shell; // shell program 19 }; 20 21 #include <sys/types.h> 22 #include <grp.h> 23 struct group *getgrgid(gid_t gid); 24 功能:通过组id来获得该uid对应的用户信息 25 参数:gid:组对应的id号 26 返回值:成功返回用户的信息struct group;失败返回NULL 27 28 //////////////////////////////////////////////////////// 29 The group structure is defined in <grp.h> as follows: 30 struct group { 31 char *gr_name; // group name 32 char *gr_passwd; // group password 33 gid_t gr_gid; // group ID 34 char **gr_mem; // group members 35 }; 36 37 */
===================================================================
浙公网安备 33010602011771号