最简单的一些linux命令的基本的实现

来源:互联网 发布:汇航亿胜网络推手公司 编辑:程序博客网 时间:2024/06/06 01:03

//特别简单的ls命令的实现,没有对参数等的功能进行判断,等以后进行完善


#include<stdio.h>

#include<unistd.h>

#include<dirent.h>

#include<sys/types.h>

int main(int argc ,char *argv[])

{

DIR *dir1;

struct dirent *dir2;        //指针格式

if(!dir1=opendir(argv[1]))//这边的参数不能够加上引号

{

perror("opendir ");

}

       while(dir2=readdir(dir1))

{

printf("%s\n",dir2->d_name);

}

closedir(dir1);

}


///简单cp命令的实现
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>


int main()
{
int fd1,fd2;
char *p;
char p2[1000];
char pp[10];
int len ;
int ret ;
struct stat stat_point;
ret = 0;
stat("./test1.c",&stat_point);
len = stat_point.st_size;
  
fd1 = open("./test1.c",O_RDWR);
fd2 = open("./m4.c",O_RDWR | O_CREAT,0677);
if(fd1 == -1)
{
perror("open");
exit(0);
}
if(fd2 == -1)
{
perror("create");
exit(0);
}
while(len!=0 && (ret=read(fd1,pp,sizeof(pp))!=0))
{

write(fd2,pp,sizeof(pp));
printf("\n%s\n",pp);
len -= sizeof(pp);
//write(fd2,pp,sizeof(pp));  天哪, 我这边没注释掉,然后在那边百思不得其解,到底为什么写了之后文件变大了啊啊啊啊 啊啊啊啊啊啊
printf("\n***************************************************************\n");
}

close(fd1);
close(fd2);

return 0;
}


原创粉丝点击