wordpress 回收站在哪,哪些网站可以做外链,中国最早做网站是谁,佛山网站建设专业定制观看本文之前请先阅读Linux Day10的相关内容
1.touch
1.1 open系统调用
int open(const char*path,int oflags,mode_t mode);
oflags参数#xff1a;
O_APPEND:把写入数据追加在文件的末尾
O_TRUNC:把文件长度设置为0#xff0c;丢弃已有的内容 O_CREAT:如果需要#…观看本文之前请先阅读Linux Day10的相关内容
1.touch
1.1 open系统调用
int open(const char*path,int oflags,mode_t mode);
oflags参数
O_APPEND:把写入数据追加在文件的末尾
O_TRUNC:把文件长度设置为0丢弃已有的内容 O_CREAT:如果需要就按参数mode给出的访问模式创建文件 O_EXCL:与O_CREAT一起使用确保调用者创建出文件。
mode参数 1.2 代码实现 所以创建目录文件就可以用ope系统调用 if(strcmp(cmd,touch)0){int fdwopen(myargv[1],O_CREAT,S_IRUSR|S_IXOTH);close(fdw);continue;}2.mv
2.1 文件读取操作
2.1.1 open
详细参考1.1open操作
2.1.2 read
系统调用read的作用是从与文件描述符fildes相关联的文件里读入nbytes个字节的数据并把它放在数据区buf中它返回实际读入的字节数这可能小于请求的字节数如果read调用返回0就表示未读入任何数据已达到文件尾同样返回-1表示read调用出现了错误。
size_t read(int fildes,void*buf,size_t nbytes);
2.1.3 write
系统调用write的作用是把缓冲区的前nbytes个字节写入与文件描述符fildes关联的文件中。它返回实际写入的字节数。如果文件描述符有错误或者底层的设备驱动程序对数据长度比较敏感该返回值可能小于nbytes。如果这个函数返回0就表示未写入任何数据如果它返回-1就是表示write调用中出现了错误错误的代码保存在全局变量errno里。
size_t wirite(int fildes,const void *buf,size_t nbytes);
2.1.4 remove
remove()函数用于删除指定的文件其原型如下
头文件#include stdio.h int remove(char * filename);
【参数】filename为要删除的文件名可以为一目录。如果参数filename 为一文件则调用unlink()处理若参数filename 为一目录则调用rmdir()来处理。
【返回值】成功则返回0失败则返回-1错误原因存于errno。
2.1.5 realpath
realpath()库函数对pathname中的所有符合链接一一解引用从而成功一个以空字符结尾的字符串内涵相应的绝对路径名
NAMErealpath - 返回规范化的绝对路径名 SYNOPSIS#include limits.h#include stdlib.hchar *realpath(const char *path, char *resolved_path);
2.1.6 chdir
程序可以像用户在文件系统那样来浏览目录就像在shell里使用cd命令来切换目录一样程序使用chdir系统调用
int chdir(const char*path);
2.2 代码实现
初步思路这个实现方法比较复杂我的想法就是因为mv如果后面两个参数在同一个普通文件那么mv命令相当于cp命令如果不一样那么就将原先的内容转移到另外一个。
所以我先判断是否在一个文件中如果在执行cp,如果不在先将第一个参数的文件位置记录在一个数组中然后打开第二个参数的文件位置使用open函数创建一个目录文件然后将之前打开的参数一的文件内容读出来算读算写入新创建的目录文件类似cp当一切都执行完毕我们关闭两个文件然后将路径切换至第一个参数文件位置这个我们在前面的数组中已经保存了然后将第一个文件删除即可。
if(strcmp(cmd,mv)0){char rp1[128]{0};char rp2[128]{0};realpath(myargv[1], rp1);realpath(myargv[2],rp2);if(strcmp(rp1,rp2)0){cmdcp;}else{printf(%s\n,myargv[1]);printf(%s\n,myargv[2]);int fdw1open(myargv[1],O_RDONLY);char curr_dir[128]{0};if(getcwd(curr_dir,128)NULL){printf(getcwd err\n);}if(myargv[2]!NULL){if(chdir(myargv[2])-1){perror(cd err\n);}int fdw2open(myargv[1],O_CREAT,S_IRUSR|S_IXOTH);char buff[128]{0};int num0;while(numread(fdw1,buff,128)){write(fdw2,buff,num);}close(fdw1);close(fdw2);chdir(curr_dir);}remove(myargv[1]);}continue;}3.cp
3.1 文件读取操作
3.1.1 open
3.1.2 read
3.1.3 write
3.2 代码实现 if(strcmp(cmd,cp)0){char*ptr1myargv[1];char*ptr2myargv[2];assert(ptr1!NULL);int fdw1open(ptr1,O_RDONLY,0600);int fdw2open(ptr2,O_CREAT|O_RDWR,0600);char buff[128]{0};int num0;while(numread(fdw1,buff,128)){write(fdw2,buff,num);} close(fdw1);close(fdw2);continue;}4.cat
4.1 文件读取操作
4.1.1 open
详细参考1.1open操作
4.1.2 read
4.1.3 write
4.2 代码实现
所以cat实现起来就变得简单多了我们先将文件读出来存进一个字符数组这里我们用定长的数组所以cat文字有限然后遍历字符数组即可 if(strcmp(cmd,cat)0){int fdwopen(myargv[1],O_RDONLY,0600);if(fdw0){printf(fopen err\n);continue;}char buff[256]{0};int num0;int i0;while(numread(fdw,buff,256)){for(int i0;inum;i){printf(%c,buff[i]);fflush(stdout);}}continue;}5.ls
int main(int argc ,char *argv[])
{char buff[128] {0};if(strncnp(. ,argv[1],1) || strncmp (/ ,argv[1],1))strcpy(buff ,argv[1]);elseif(getcwd(buff, 128)NULL) exit(1);DIR* pdir opendir(buff);if(pdir-NULL) exit(1);struct dirent* s NULL ;while( (sreaddir(pdir))!NULL){if(strncmp(argv[1],-a ,2)0){struct stat filestat;stat(s-d_ nane ,fllestat);if(S_ ISDIR(filestat.st mode))printf( 1033[1;34nxs\033[0m ,S-d nane);elseif(filestat.st_ mode (S_ IXUSR|S_ IXGRPIS. IXOTH))printf( 1033[1;32mxs\033[0m,S-xd nane);elseprintf(%s s-d name);}else if(strncmp(argv[1],-i ,2)0) {if(strncmp(. s-d nane ,1)) continue;struct stat filestat; stat(s-d_ nane ,fLestat);if(5_ ISDIR(filestat.st mode))printf(%ld 1033[1;34m%s\033[0m ,5-d ino,s-d nane);elseif(filestat.st_ mode (S_ IXUSRIS_ IXGRPIS_ IXOTH)){printf(%ld \033[1;327m%s\033[0m ,s-d. _ino,s-d_ nane);else printf(%ld %s ,s-d_ ino,S-d nane);}else if(strncmp(argv[1],-l ,2)0) {if(strncmp(.,s-d nane,1)0) continue;}else{if(strncmp(. ,S-d nane,1)--0) continue;struct stat filestat;stat(s-d nane ,fiLestat);if(s_ISDIR(filestat.st mode))printf( \033[1;34m%s\033[m” ,s-d. nane);elseif(filestat.st mode (S_ IXUSRIS IXGRPIS IXOTH))printf(”\033[1;32m%s\033[0m” ,s-d. nane);else printf(%s ,s-d nane);}}printf(\n);closedir(pdir);exit(0);
}好了这里mbash实现的差不多只不过各个命令的实现还是比较复杂等过段时间将这些方式再优化优化找到最优解。