Ladies and gentlemen, crossing, the last time we were talking about the example of a DIY pwd command, this time we said the example is:DIY cd command . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!
Crossing, in the Linux system, the CD command provided by the terminal is one of our most commonly used commands, I believe you must be very familiar with it. Today, let's join the DIY CD command. Here are the specific steps to be done:
- 1. Extract the name of the path from the user-entered content;
- 2. Pass the path name you just acquired to the CHDIR function, using the function to switch to the directory where the name resides;
- 3. Confirm the result of function running, if cannot switch, give User a prompt.
Here is the code of our DIY CD command, please refer to:
intCDsConst Char*P) {CharPath[path_size];Char*start;Char*end;intResintn=0;memset(Path,' + ', path_size);//Must init, ohtrway there is a wrong result with @Start =STRCHR(P,"'); End =STRCHR(P,' \ n ');if(!start | |!end) {printf("can ' t support this format \ n");return 1; }strncpy(path,p+3, end-start-1);//Get the path in inputting commandres = chdir (path);//change dir if(Res! =0)printf("%s is nod a path,please check again \ n", path);returnRes;}
From the code above, we can see that in the process of extracting paths from user input, we use spaces and newline characters as the extracted tokens. There is a space behind the CD, which is the format of the CD command. In addition, we used the fgets to get the content entered by the user. The function adds a line break and a null character after the user enters the content. Given these two features, we took full advantage of the space and newline characters in the code to extract the paths in the user input. If the user input does not have these two characteristics, then the input format is not correct, you need to prompt the user.
In addition, let's talk about the ChDir function, which is a function provided by the Linux system that switches the current working directory to the directory referred to by the parameters passed to it. Returns a value other than 0 if the switch successfully returns zero.
Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use. This function needs to be used in conjunction with MAIN.C and Main.h in the previous chapter.
The following is the results of the operation of the program, please refer to:
| -Pwd//Use the PWD command to display the current path first/home/talk8/shell//Show Current Path | -Cd/etc/ //Switch to/etc/In the Catalog | -Pwd//Use the PWD command to display the current path/etc//current path is/etc/, indicating successful switchover | -Cd/hello/ //Open the wrong path/hello/ isNod a path,please check again//Prompt for path error | - //Wait for the user to re-enter
Everybody crossing, the example of DIY CD command We're going to talk about here. I want to know what the following example, and listen to tell.
Together talk C Chestnut Bar (74th back: C language Instance--diy CD command)