实验四 Linux下的C语言编程
时间:2025-05-04
时间:2025-05-04
实验四Linux 下的 C 语言编程
四、实验内容
本实验要求在LINUX/UNIX环境下用C语言编写三个具体的SHELL命令,基本涉及了LINUX/UNIX文件系统中较为常用的有关文件操作的系统调用。内容如下:
1、编程实现 copy 命令。执行格式:copy file1file2file3
功能:将 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 file3\n");
exit(1);
}
file1=open(argv[1],O_RDONLY);
file2=open(argv[2],O_RDONLY);
file3=open(argv[3],O_CREAT|O_RDWR,S_IRWXU);
int n;
char buf[1024];
while (( n=read(file1,buf,1024))>0)
if (write(file3,buf,n)!=n)
printf("write error\n");
if (n<0)
printf("read %s error\nErrno= %d\n",argv[1],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 error\n");
if (n<0)
printf("read %s error\nErrno= %d\n",argv[2],errno);
exit(0);
close(file1);
close(file2);
close(file3);
printf("success\n");
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 newfile\n");
exit(1);
}
if(stat(argv[1],&buf1) == -1) {
printf("star error\nerrno is %d\n",errno);
exit(1);
}
printf("使用stat()显示文件%s的信息\n",argv[1]);
printf("%s大小-->%d\n",argv[1],(int)buf1.st_size);
printf("%s索引节点号-->%d\n",argv[1],(int)buf1.st_ino);
printf("%s最后一次修改时间--
>%d\n",argv[1],(int)buf1.st_mtime);
printf("--------------------------------------------------
\n");
if(rename(argv[1],argv[2])==-1){
printf("rename error\nErrno %d\n",errno);
exit(1);
}
printf("-------------------rename success------------------
\n");
if(stat(argv[2],&buf2) == -1) {
printf("star error\nErrno is %d\n",errno);
exit(1);
}
printf("使用stat()显示文件%s的信息\n",argv[2]);
printf("%s大小-->%d\n",argv[2],(int)buf2.st_size);
printf("%s索引节点号-->%d\n",argv[2],(int)buf2.st_ino);
printf("%s最后一次修改时间--
>%d\n",argv[2],(int)buf2.st_mtime);
printf("--------------------------------------------------
\n");
return 0;
}
3、编程实现 lnk 命令,执行格式:lnk f1f2f3。具体要求如下:
⑴分别使用 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 symlinkfn\n");
exit(1);
}
if (link(argv[1],argv[2])==-1) {
printf("link error\nErrno= %d\n", errno);
}
if (symlink(argv[1],argv[3])==-1) {
printf("symlink error\nErrno= %d\n", errno);
}
if((stat(argv[2],&buf1) == -1)|(stat(argv[3],&buf2) == -1)) { printf("star error\nerrno is %d\n",errno);
exit(1);
}
printf("使用stat()显示文件%s和%s的信息\n",argv[2],argv[3]);
printf("%s大小-->%d\n",argv[2],(int)buf1.st_size);
printf("%s大小-->%d\n",argv[3],(int)buf2.st_size);
printf("%s索引节点号-->%d\n",argv[2],(int)buf1.st_ino);
printf("%s索引节点号-->%d\n",argv[3],(int)buf2.st_ino);
printf("%s权限-->%d\n",argv[2],(int)buf1.st_mode);
printf("%s权限-->%d\n",argv[3],(int)buf2.st_mode);
printf("%s文件所在设备号-->%d\n",argv[2],(int)buf1.st_dev);
printf("%s文件所在设备号-->%d\n",argv[3],(int)buf2.st_dev);
printf("%s最后一次修改时间--