Purpose
- Familiar with the functions of file descriptors to manipulate files;
- Master the application of non-blocking file operations;
- Master the file lock application;
- Master the operation of directory files and symbolic link files.
content
- At the end of Chapter 5 of the textbook, page 213, questions 1, 2, 3, 4, and 7.
- Design a program that requires to open the file "pass", if there is no such file, create a new file, and set the permissions to only the owner has read-only permissions.
-
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd; if((fd=open("pass",O_CREAT)<0)) { printf("file is not exist.\n"); } printf("createFile pass,fd:%d\n",fd); chmod("pass",S_IRUSR); return 0; }
2. Design a program that requires a new file "hello", and use the write function to write the string "C software design under Linux" into the file.
-
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> int main() { int fd,w; if((fd=open("hello",O_TRUNC|O_WRONLY,0666))<0) { printf("打开文件出错"); return 1; } char str[]="Linux下C软件设计"; if((w=write(fd,str,sizeof(str)))<0) { printf("写入文件出错"); return 1; } close(fd); return 0; }
3. Design a program that requires the read function to read the system file "/etc/passwd" and display the output in the terminal
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd,r,w;
char str[50];
if((fd=open("/etc/passwd",O_RDONLY))<0)
{
printf("读取文件失败!");
return 1;
}
while((r=read(fd,str,50))>0)
{
if((w=write(STDOUT_FILENO,str,r))<0)
printf("写入文件出错");
}
close(fd);
return 0;
}
Design a program that requires to open the file "pass", if there is no such file, create a new file, read the system file "/etc/passwd", and write the contents of the file into the "pass" file.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fdsrc,fddes,cnt,w;
char str[30];
if((fddes=open("pass",O_CREAT|O_TRUNC|O_WRONLY,0600))<0)
{
printf("打开文件pass失败!");
return 1;
}
if((fdsrc=open("/etc/passwd",O_RDONLY))<0)
{
printf("打开文件失败!");
return 1;
}
while((cnt=read(fdsrc,str,30))>0)
{
if((w=write(fddes,str,30))<0)
printf("写入文件失败");
}
close(fdsrc);
close(fddes);
return 0;
}
Question: I haven't found out why the file pass keeps failing to open?
⑦ Design a program that requires to create a soft link "ls1" and a hard link "ls2" for the "/bin/ls" file, and view the two link files and the "/bin/ls" file.
#include <stdlib.h>
#include <unistd.h>
int main()
{
symlink("/bin/ls","ls1");
link("/bin/ls","ls2");
system("ls -l /bin/ls");
system("ls -l ls1");
system("ls -l ls2");
return 0;
}
- Programmatically implement the basic functions of ls and implement the -l parameter function; such as:
- If no parameter is specified, all visible files in the current directory are displayed;
- If a parameter is specified, the parameter specifies the file to display the file information, and if it is a directory, the file information in the directory is displayed;
- The -l parameter format is the same as ls -l;
- The specified parameter can be multiple;
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include<time.h> #include<pwd.h> #include<grp.h> void filebuf(mode_t mode,char *rs){ //判断文件类型以及所有者,组和其他的权限; if(S_ISDIR(mode)){ rs[0]='d'; } else if(S_ISREG(mode)){ rs[0]='-'; } else if(S_ISFIFO(mode)){ rs[0]='p'; } else if(S_ISLNK(mode)){ rs[0]='l'; } else if(S_ISBLK(mode)){ rs[0]='b'; } else if(S_ISSOCK(mode)){ rs[0]='s'; } else if(S_ISCHR(mode)){ rs[0]='c'; } if(mode & S_IRUSR) rs[1]='r'; else rs[1]='-'; if(mode & S_IWUSR) rs[2]='w'; else rs[2]='-'; if(mode & S_IXUSR) rs[3]='x'; else rs[3]='-'; if(mode & S_IRGRP) rs[4]='r'; else rs[4]='-'; if(mode & S_IWGRP) rs[5]='w'; else rs[5]='-'; if(mode & S_IXGRP) rs[6]='x'; else rs[6]='-'; if(mode & S_IROTH) rs[7]='r'; else rs[7]='-'; if(mode & S_IWOTH) rs[8]='w'; else rs[8]='-'; if(mode & S_IXOTH) rs[9]='x'; else rs[9]='-'; rs[10]='\0'; } int main(int argc, char *argv[]) { struct stat fst; struct tm *mytime=(struct tm *)malloc(sizeof(struct tm)); char rs[12]; if(argc!2){ fprintf(stderr,"Usage: %s <pathname>\n",argv[0]); exit(EXIT_FAILURE); } if(stat(argv[1],&fst)==-1) { perror("stat"); exit(EXIT_FAILURE); } filebuf(fst.st_mode,rs); printf("%s",rs); //输出文件类型与权限信息; printf(" %d",fst.st_nlink);//输出文件的硬链接数; printf(" %s",getpwuid(fst.st_uid)->pw_name);//输出所属用户名; printf(" %s",getgrgid(fst.st_gid)->gr_name);//输出用户所在组; printf(" %1d",fst.st_size);//输出文件大小; mytime=localtime(&fst.st_mtime);//获取文件修改时间; printf("%d-%02d-%02d%02d:%02d",mytime->tm_year+1900,mytime-> tm_mon+1,mytime->tm_mday,mytime->tm_hour,mytime->tm_min); printf(" %s",argv[1]);//输出文件名; printf("\n"); return 0; }