1.编写程序 mycat.c,实现命令 cat 的功能
2.编写程序 mycp.c,实现命令 cp 的功能
3.要求使用 open/read/write/close 等系统调用实现以上程序
mycat.c思路
首先,我们得了解cat命令的作用——cat("concatenate"的缩写)命令是 Linux/Unix 操作系统中最常用的命令之一。cat命令允许我们创建单个或多个文件,查看文件的内容,连接文件并在终端或文件中重定向输出。cat 命令将文件内容显示到屏幕上。Cat 命令将标准输入连接到标准输出。当没有文件或者文件是 - 那么它读取标准输入?它执行与操作文本文件相关的三个主要角色:创建它们,显示它们和组合它们。而我们主要实现的就是cat的显示功能,举个例子:
f1.txt文件内容如下:
我们调用cat f1.txt即可显示文件内容:
同理,cat还支持显示多个文件,如cat f1.txt f2.txt:
所以,对于用户的输入,用fgets函数,特别注意,Linux和windows的读键盘输入略有不同,亲测scanf和cin、gets会有各种问题,不一一展开细说了,感兴趣自己去尝试。如果对fgets函数不了解的建议去看一下函数原型,这个函数比较万能,可以读写键盘或文件,非常实用。
输入的cat命令我们得先按空格进行分词操作,主要用到字符串函数strtok,不熟悉的也建议去看一下函数原型;分词后用一个结构体进行保存,
请记住这个 这个结构体,因为后面的第二次作业还会用到。
分词后,就是对第一个词,也就是commands.agrv[0]进行检测是不是“cat”,再决定要不要继续。
接着就是打开文件的操作,这里用的是open函数,对于初次用linux进行编程的同学可以先看看函数原型,这里不过多赘述。读文件和显示文件用read和write,对于要显示多个文件用一个for循环就行。
多说无益,直接上源码:
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
struct Command{
int agrc; //命令的单词数
char *agrv[16];
}commands; //输入命令的结构体
void parse_command(char *command){ //分词函数,建议记住这个函数,后面还会用到
char*line=(char *)malloc(sizeof*(command));
strcpy(line,command);
commands.agrc=0;
for(int i=0;i<16;i++)
{
commands.agrv[i]=NULL;
}
char *word;
word=strtok(line," "); //第一次使用时,第一个参数是要分词的串,第二个参数是按什么进行分词
while(word!=NULL)
{
commands.agrv[commands.agrc++]=word;
word=strtok(NULL," "); //再次使用时,第一个参数直接NULL
}
}
void mycat(char *command){ //cat实现
parse_command(command);
char buf[1024];
int n;
if(strcmp(commands.agrv[0],"cat")==0){
for(int i=1;i<commands.agrc;i++){ //用for循环实现多个文件的显示
int fp=open(commands.agrv[i],O_RDONLY); //以只读方式打开文件
if(fp==-1){
printf("cannot open the file!\n");
return ;
}
else{
while((n=read(fp,&buf,1024))!=0){ //读取文件1024个字节内容到buf
write(1,buf,n); //参数1表示将buf内容输出到屏幕
}
}
}}else{
printf("error!please put 'cat'!\n");
return ;
}
}
int main(){
char str[50];
printf(">");
fgets(str,50,stdin); //读50个字节的用户输入
str[strlen(str)-1]=0; //这步操作很关键,也困扰了我很久,如果不执行,将导致strcmp函数出错
mycat(str);
}
mycp.c思路:
有了上面的基础,mycp.c就很容易实现了,主要是实现将源文件copy到目标文件,如果目标文件不存在就建立一个目标文件,其实换而言之就是把源文件内容copy到目标文件中。其实就是把mycat.c中,读文件到屏幕改为读文件后写入目标文件中。
直接上源码:
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>struct Command{
int agrc;
char *agrv[16];
}commands;
void parse_command(char *command){
char*line=(char *)malloc(sizeof*(command));
strcpy(line,command);
commands.agrc=0;
for(int i=0;i<16;i++)
{
commands.agrv[i]=NULL;
}char *word;
word=strtok(line," ");
while(word!=NULL)
{
commands.agrv[commands.agrc++]=word;
word=strtok(NULL," ");
}
}
void mycp(char *command){
parse_command(command);
char buf[1024];
int n;
int fp1,fp2;if(strcmp(commands.agrv[0],"cp")==0&&commands.agrv[0]!=NULL){
fp1=open(commands.agrv[1],O_RDONLY);
if(fp1==-1){
printf("cannot open the %s\n",commands.agrv[1]);
return ;
}
fp2=open(commands.agrv[2],O_RDWR|O_CREAT|O_TRUNC);
while((n=read(fp1,&buf,1024))!=0){
if(n<0){
printf("read error!\n");
break;
}
write(fp2,buf,n);
}
}
else{
printf("error!please input 'cp'!\n");
return ;
}
close(fp1);
close(fp2);
}
int main(){
char str[50];
printf(">");
fgets(str,50,stdin);
str[strlen(str)-1]=0;
mycp(str);
}
最后,以上都是个人思路,如有不足请多多指教,如果喜欢或是对你有帮助麻烦帮忙点个赞、加个收藏,感谢 阅读!!!