Ladies and gentlemen, crossing, the last time we were talking about DIY date command example, this time we say the example is:DIY pwd command . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!
Crossing, when you switch directories frequently, there is a feeling that you do not know which directory you are in, and you need to use the PWD command to show which directory you are currently in. Today, we come together to DIY the PWD command.
Here are the steps to the DIY pwd command:
- 1. Use the GETCWD function to get the current path;
- 2. Output the path obtained in step 1.
Below is the code of our DIY pwd command, please refer to:
int pwds(){ char buf[PATH_SIZE]; *res; res = getcwd(buf,PATH_SIZE); if(res) { printf("%s\n",buf); return0; } else return1;}
Relatively speaking, the program is relatively simple. But there are some caveats, and let's emphasize that:
When using the GETCWD function, you need a large enough memory space to hold the acquired path. If the space is not large enough, it is possible to obtain a failure, so we need to determine the execution result of the function and confirm whether the current path can be displayed by the result.
Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use.
Here is the result of the program running:
|->pwd/home/talk8/Shell
Below is the operation result of the PWD command in the system, please compare with the above results:
pwd/home/talk8/Shell
You crossing, the example of the DIY pwd 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 (73rd back: C language Instance--diy pwd command)