实验四 Linux下的C语言编程(5页).doc
-实验四Linux 下的 C 语言编程四、实验内容本实验要求在 LINUX/UNIX 环境下用 C 语言编写三个具体的 SHELL 命令,基本涉及了 LINUX/UNIX 文件系统中较为常用的有关文件操作的系统调用。内容如下:1、编程实现 copy 命令。执行格式:copyfile1file2file3功能:将 file1、file2 两文件的内容合并拷入 file3 中,其中间应有 30 个字节的空洞(调试成功后可将空洞调大到几十 MB)。程序执行后用 du 命令显示其占用磁盘空间,观察其大小,分析原因。 程序可能涉及到的系统调用: read(), write(), open(), creat(),close(), lseek()#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>#include <stdlib.h> #include <errno.h> #include <stdio.h> #include <unistd.h> int main(int argc, char const *argv) int file1,file2,file3; if (argc!= 4) printf("Usage: copy file1 file2 file3n"); exit(1); file1=open(argv1,O_RDONLY); file2=open(argv2,O_RDONLY); file3=open(argv3,O_CREAT|O_RDWR,S_IRWXU); int n; char buf1024; while ( n=read(file1,buf,1024)>0) if (write(file3,buf,n)!=n) printf("write errorn"); if (n<0) printf("read %s errornErrno= %dn",argv1,errno); if (lseek(file3,30,SEEK_END)=-1) printf("lseek error "); while ( n=read(file2,buf,1024)>0)if (write(file3,buf,n)!=n) printf("write errorn"); if (n<0) printf("read %s errornErrno= %dn",argv2,errno); exit(0); close(file1); close(file2); close(file3); printf("successn"); return 0; 2、编程实现 renam(即 LINUX 下的 rename)命令,功能是实现文件的重命名。执行格式:renam filea fileb;其中 filea 为源文件,fileb 为目标文件。程序执行时应显示出命令行的所有参数,并给出重命名前后两个文件的大小、索引节点号及最近一次状态改变的时间。程序可能涉及到的系统调用:read(), write(), open(), stat(), close(), link(), unlink()#include <sys/stat.h> #include <unistd.h>#include <stdlib.h> #include <stdio.h> #include <errno.h>#include <time.h> extern int errno;int main(int argc,const char* argv) struct stat buf1,buf2; if (argc!= 3) printf("Usage: rename oldfile newfilen"); exit(1); if(stat(argv1,&buf1) = -1) printf("star errornerrno is %dn",errno);exit(1);printf("使用stat()显示文件%s的信息n",argv1);printf("%s大小->%dn",argv1,(int)buf1.st_size);printf("%s索引节点号->%dn",argv1,(int)buf1.st_ino);printf("%s最后一次修改时间->%dn",argv1,(int)buf1.st_mtime); printf("-n");if(rename(argv1,argv2)=-1)printf("rename errornErrno %dn",errno); exit(1); printf("-rename success-n");if(stat(argv2,&buf2) = -1) printf("star errornErrno is %dn",errno);exit(1);printf("使用stat()显示文件%s的信息n",argv2);printf("%s大小->%dn",argv2,(int)buf2.st_size);printf("%s索引节点号->%dn",argv2,(int)buf2.st_ino);printf("%s最后一次修改时间->%dn",argv2,(int)buf2.st_mtime); printf("-n"); return 0; 3、编程实现 lnk 命令,执行格式:lnkf1f2f3。具体要求如下:分别使用 link()和 symlink()为文件 f1 创建其硬链接文件 f2 和符号链接文件 f3。分别使用 stat()和 lstat()调用给出文件 f2 和 f3 大小、索引节点号、权限、存放该文件的设备号及文件修改时间,比较其异同。说明原因。#include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>#include <time.h>#include <errno.h>extern int errno; int main(int argc,char* argv) struct stat buf1,buf2,buf3,buf4;if (argc!= 4) printf("Usage: link_exam oldfile linkfn symlinkfnn"); exit(1); if (link(argv1,argv2)=-1) printf("link errornErrno= %dn", errno); if (symlink(argv1,argv3)=-1) printf("symlink errornErrno= %dn", errno); if(stat(argv2,&buf1) = -1)|(stat(argv3,&buf2) = -1) printf("star errornerrno is %dn",errno);exit(1);printf("使用stat()显示文件%s和%s的信息n",argv2,argv3);printf("%s大小->%dn",argv2,(int)buf1.st_size);printf("%s大小->%dn",argv3,(int)buf2.st_size);printf("%s索引节点号->%dn",argv2,(int)buf1.st_ino);printf("%s索引节点号->%dn",argv3,(int)buf2.st_ino);printf("%s权限->%dn",argv2,(int)buf1.st_mode);printf("%s权限->%dn",argv3,(int)buf2.st_mode);printf("%s文件所在设备号->%dn",argv2,(int)buf1.st_dev);printf("%s文件所在设备号->%dn",argv3,(int)buf2.st_dev);printf("%s最后一次修改时间->%dn",argv2,(int)buf1.st_mtime);printf("%s最后一次修改时间->%dn",argv3,(int)buf2.st_mtime);printf("*#&&&& STAT END &&&&*n");if(lstat(argv2,&buf3) = -1)|(lstat(argv3,&buf4) = -1) printf("lstar errornerrno is %dn",errno);exit(1);printf("-n");printf("使用lstat()显示文件%s和%s的信息n",argv2,argv3);printf("%s大小是->%dn",argv2,(int)buf3.st_size);printf("%s大小是->%dn",argv3,(int)buf4.st_size);printf("%s索引节点号->%dn",argv2,(int)buf3.st_ino);printf("%s索引节点号->%dn",argv3,(int)buf4.st_ino);printf("%s权限->%dn",argv2,(int)buf3.st_mode);printf("%s权限->%dn",argv3,(int)buf4.st_mode);printf("%s文件所在设备号->%dn",argv2,(int)buf3.st_dev);printf("%s文件所在设备号->%dn",argv3,(int)buf4.st_dev);printf("%s最后一次修改时间->%dn",argv2,(int)buf3.st_mtime);printf("%s最后一次修改时间->%dn",argv3,(int)buf4.st_mtime);printf("*#&&&& LSTAT END &&&&*n");-第 5 页-3