实现linux mkdir、rmdir和rm命令

 linuxcommand: 实现一些linux command (gitee.com)

1.实现mkdir命令,对于程序的说明,请看注释(只有少量)

/**
 * @copyright Copyright (c) 2022 Jiawshi
 * @author    Jiawshi ([email protected])
 * 
 * @file      mkdirr.c
 * @brief     
 * 
 * @version   V0.01
 * @date      2022-11-26
 * 
 * @note      历史记录 :
 *            - [2022-11-26] [Jiawshi] 创建初始版本:
 * @warning   
 * @par       修改记录: 
 * 
 * 
 * 
 * 
date Version Author Description
2022-11-26 V0.01 Jiawshi 创建初始版本
*/ #include #include "mkdirn.h" #include #include #include #include #include #include /** * @fn * @brief * * @param [in] argc * @param [in] argv * * @return int */ int main(int argc, char **argv) { int ch,pflag=0,vflag=0,i,err; char *modeStat = NULL; mode_t mode; while((ch=getopt(argc,argv,"m:pvh")) != -1) { switch(ch) { case 'm': modeStat = optarg; break; case 'p': pflag = 1; break; case 'v': vflag = 1; break; case 'h': print_help(); exit(1); default: usage(); break; } } argc -=optind; argv +=optind; if(modeStat == NULL) { mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; } for(i=0;i
/**
 * @copyright Copyright (c) 2022 Jiawshi
 * @author    Jiawshi ([email protected])
 * 
 * @file      mkdirn.h
 * @brief     
 * 
 * @version   V0.01
 * @date      2022-11-26
 * 
 * @note      历史记录 :
 *            - [2022-11-26] [Jiawshi] 创建初始版本:
 * @warning   
 * @par       修改记录: 
 * 
 * 
 * 
 * 
date Version Author Description
2022-11-26 V0.01 Jiawshi 创建初始版本
*/ #ifndef MKDIRL_H__ #define MKDIRN_H__ #include #include void printf_err(int); void usage(void); void print_help(void); void flag(int ,char *); int mkDir(char *,mode_t); int createDir(char *,mode_t); #endif
/**
 * @copyright Copyright (c) 2022 Jiawshi
 * @author    Jiawshi ([email protected])
 * 
 * @file      mkdirn.c
 * @brief     
 * 
 * @version   V0.01
 * @date      2022-11-26
 * 
 * @note      历史记录 :
 *            - [2022-11-26] [Jiawshi] 创建初始版本:
 * @warning   
 * @par       修改记录: 
 * 
 * 
 * 
 * 
date Version Author Description
2022-11-26 V0.01 Jiawshi 创建初始版本
*/ #include #include #include #include #include #include #include #include "mkdirn.h" /** * @fn * @brief print err if create file failure * * @param [in] err err = errno * */ void printf_err(int err) { if(err == EEXIST) fprintf(stderr,"%s\n",strerror(err)); else { fprintf(stderr,"create err! use help\n"); } } /** * @fn * @brief * * @param [in] vflag -v option symbol * @param [in] dir dirname * */ void flag(int vflag,char *dir) { if(vflag) { printf("mkdir: created directory '%s'\n",dir); } } void usage(void) { fprintf(stderr,"usage....,you can use mkdir -h to print help.\n"); } void print_help(void) { printf("-m set file mode,it need a mode after -m \ -p make parnet directories \ -v print a message for each directory that be created\ -h print help\n"); } /** * @fn * @brief * * @param [in] path dirname which need be create * @param [in] mode permission for the dirname * * @return int */ int mkDir(char *path,mode_t mode) { if((mkdir(path,mode)) < 0) { if((mkdir(path,mode)) < 0) { if(errno == EEXIST) return EEXIST; else { return -1; } } } return 0; } int createDir(char *path,mode_t mode) { int first,end,err; char *dirPath = path; if(*dirPath == '/') { dirPath++; } for(first=0,end=0;first <= strlen(path);first++) { if(dirPath[first] == '\0') { end = 1; } else if(dirPath[first] != '/') { continue; } dirPath[first] = '\0'; err = mkDir(path,mode); if(err != 0) { if(err == EEXIST) { if(end) { return EEXIST; } } else return -1; } if(!end) { dirPath[first] = '/'; } if(end) break; } return 0; }

makefile文件

all:main

main:mkdirn.o mkdirr.o
	gcc $^ -o $@

2.rmdir的实现,对于程序的说明,请看注释(只有少量)

/**
 * @copyright Copyright (c) 2022 Jiawshi
 * @author    Jiawshi ([email protected])
 * 
 * @file      rmd.c
 * @brief     主程序
 * 
 * @version   V0.01
 * @date      2022-11-26
 * 
 * @note      历史记录 :
 *            - [2022-11-26] [Jiawshi] 创建初始版本:
 * @warning   
 * @par       修改记录: 
 * 
 * 
 * 
 * 
date Version Author Description
2022-11-26 V0.01 Jiawshi 创建初始版本
*/ #include #include #include #include #include "rmdi.h" #include int main(int argc, char **argv) { int ch,i,err; int pflag = 0; int ignore = 0; int help = 0; int longindex; printf_usage(argc); struct option longopts[] = {{"ignore",0,0,'i'},{"parents",0,0,'p'}, {"verbose",0,0,'v'}, {"help",0,0,'h'}, {0,0,0,0}}; longindex = 4; while((ch = getopt_long(argc, argv,"pv",longopts,&longindex)) != -1) { switch(ch) { case 'p': pflag = 1; break; case 'v': v_flag = 1; break; case 'h': printf_help();; break; case 'i': ignore = 1; break; default : printf("Try 'rmdir --help' for more information\n"); exit(1); } } argc -= optind; argv += optind; for(i=0;i
/**
 * @copyright Copyright (c) 2022 Jiawshi
 * @author    Jiawshi ([email protected])
 * 
 * @file      rmdi.c
 * @brief     实现函数的功能
 * 
 * @version   V0.01
 * @date      2022-11-26
 * 
 * @note      历史记录 :
 *            - [2022-11-26] [Jiawshi] 创建初始版本:
 * @warning   
 * @par       修改记录: 
 * 
 * 
 * 
 * 
date Version Author Description
2022-11-26 V0.01 Jiawshi 创建初始版本
*/ #include #include #include "rmdi.h" #include #include #include #include #include static int _flag = 0; int v_flag = 0; static int strlong; void printf_err(int err) { fprintf(stderr,"%s\n",strerror(err)); } void printf_usage(int num) { if(num < 2 ) { fprintf(stderr,"rmdir:missing operand\nTry 'rmdir --help' \ for more information\n"); } } /** * @fn vFlag * @brief 处理 -v --verbose * * @param [in] flag vflag * @param [in] path dirname * */ void vFlag(int flag,char *path) { if(flag) { printf("rmdir: removing directory,'%s'\n",path); } } void printf_help(void) { printf("--ignore :ignore when error happen \ -p --parents :remove include partent dir \ -v --verbose :view result \ --help:print help\n"); } /** * @fn rmParent * @brief deal with -p --parent parameter * * @param [in] dirName * * @return int success yes(return 0) or no(errno) */ int rmParent(char *dirName) { char *temPath = dirName; int retVal; int i; DIR *dirp; struct dirent *dirn; strlong = strlen(temPath); if(temPath[strlong-1] == '/') { _flag = 1; temPath[strlong-1] = '\0'; } while(temPath != NULL) { // if(strcmp(temPath,".") == 0) // { // temPath = NULL; // return 0; // } if(strcmp(temPath,"./") ==0 || strcmp(temPath,"..") ==0||strcmp(temPath,".")==0) { temPath = NULL; return 0; } retVal = rmDir(temPath); if(retVal < 0) { //do something return errno; } strlong = strlen(temPath); for(i = strlong -1;i>=0;i--) { if(temPath[i] == '/'&& i != 0) { //printf("hhh\n"); temPath[i] = '\0'; break; } if(i == 0) { temPath = NULL; } } } return 0; } /** * @fn rmDir * @brief remove dirname * * @param [in] dirName * * @return int success yes(return 0) or no(errno) */ int rmDir(char *dirName) { char *temPath = dirName; int reVal; reVal = rmdir(temPath); if(reVal==0) { if(_flag) { temPath[strlong-1] = '/'; } vFlag(v_flag,temPath); if(_flag) { _flag = 0; temPath[strlong-1] = '\0'; } } return reVal; }
/**
 * @copyright Copyright (c) 2022 Jiawshi
 * @author    Jiawshi ([email protected])
 * 
 * @file      rmdi.h
 * @brief     
 * 
 * @version   V0.01
 * @date      2022-11-26
 * 
 * @note      历史记录 :
 *            - [2022-11-26] [Jiawshi] 创建初始版本:
 * @warning   
 * @par       修改记录: 
 * 
 * 
 * 
 * 
date Version Author Description
2022-11-26 V0.01 Jiawshi 创建初始版本
*/ #ifndef RMDI_H__ #define RMDI_H__ extern int v_flag; void printf_err(int); void printf_usage(int); void printf_help(void); void vFlag(int,char *); int rmParent(char *); int rmDir(char *); #endif

makefile

all:main

main:rmd.o rmdi.o
	gcc $^ -o $@


.PHONY:clean
clean:
	rm -rf  *.o  main

rm命令在一开始的仓库链接里面。

对于这两个命令如何在你的电脑上运行。你需要花点时间自己看一下。如果你遇到了bug,请与我联系[email protected] 或者给我留言。

你可能感兴趣的:(linux,linux,c++)