C Programming

C: CHDIR Function Usage

We have already talked about the “CHDIR” function of the C programming language briefly while discussing the “getcwd” function. However, this article will specifically revolve around the “CHDIR” function mainly used to alter the current working directory of a system. We will especially try to explore how the behavior of this function changes once it is used within a C script. To do that, we will first introduce you to the purpose of this function, followed by its syntax. Then, we will discuss the impact of this function on the current working directory of the shell in Linux. Finally, we will conclude our discussion by talking about some of the errors that can be returned from executing the “CHDIR” function of the C programming language.

What is the CHDIR Function in the C Programming Language?

The “CHDIR” function is used to change the current working directory of a system. You might want to alter the current working directory of your system in several different situations. For example, you wish to execute a C script located in a directory rather than your current working directory. Therefore, you will have to change your current working directory to execute that particular C script. Moreover, the change of the current working directory can easily be found by making use of the “getcwd” function of the C programming language.

The syntax of the “CHDIR” function of the C programming language is stated below:

$ int chdir(const char *path);

The return type of the “CHDIR” function is “int,” which means that it always returns an integer value. This function basically returns a “0” upon success and a “-1” upon failure. Then, the parameter “const char *path” refers to the path of the new working directory that you want to set while using the change directory function.

Does the CHDIR Function also Change the present Working Directory of the Shell?

Here, we need to understand that changing the current working directory of a running program is different from changing the current working directory of the current shell. Whenever the “CHDIR” function is used within a C script, it only attempts to modify the current working directory of that particular program and not of the shell in which it is running. It implies that the “CHDIR” function has no impact on the current working directory of the current shell.

Usage of the CHDIR Function in the C Programming Language:

The “CHDIR” function of the C programming language is simply used to change the current working directory of your system. To understand its correct usage, you must follow the C script that uses the “CHDIR” function. In this example, we first intend to display the current working directory on the terminal, then change it to the default directory. After doing that, display the current working directory on the terminal again.

Before explaining this C script to you, we would like to share a few things. First, we have created this C script file in our system’s “Documents” directory and not in the “Home” directory. We demonstrated how the “CHDIR” function actually works in the C programming language. Moreover, for running this C script, we also had to change the default path of our shell to the “Documents” directory. Additionally, this example will confirm that the “CHDIR” function only changes the current working directory of the running program and not the current working directory or path of the current shell. You can go through the following description to understand the example C script.

In this C program, we have included the “stdio.h” and “unistd.h” header files. The former header file handles all the input and output operations. In contrast, the latter header file contains the implementation of the “CHDIR” function that we will use in this example. In our “main()” function, we have declared a character type array of size “100”. This array will be used to hold the name of the current working directory of our system. Then, we have used the “printf” statement along with the “getcwd” function for printing the current working directory on the terminal before actually changing it.

After that, we have used the “CHDIR” function followed by “…”. We have used it in this way because we want to change our current working directory back to the default directory of our system. Then again, we have used the “printf” statement along with the “getcwd” function to check if our current working directory has been changed successfully or not. Finally, we have concluded our C script with the “return 0” statement.

For compiling this example C script, we first changed the path of our terminal and navigated to the Documents directory since this C script file resides within the Documents directory. Then, we executed the following command:

$ gcc chdir.c –o chdir

After that, we executed this example C script with the help of the command given below:

$ ./chdir

You can easily visualize from the following output that two different current working directories have been printed on the terminal due to executing this example C script. The first one represents the current working directory of our system before executing the “CHDIR” command. In contrast, the second one represents the current working directory of our system after executing the “CHDIR” command. Moreover, you can also confirm that even the execution of the “CHDIR” command within our C script did not have any impact on the path of our shell, i.e., it remained the same as it was before, which shows that the “CHDIR” command only changes the current working directory of the running program and not of the current shell.

Possible Errors Returned by the CHDIR Function in the C Programming Language:

Some of the most common errors returned by the “CHDIR” function of the C programming language are as follows:

  • EACCES: Refers to the denied search permissions for one or more components of the provided path.
  • EFAULT: Indicates that the provided path lies outside the permissible address space.
  • ENAMETOOLONG: The name of the provided path is too long to be resolved.
  • ENOTDIR: Any one or more components of the provided path are not directories.

All of the errors discussed above, along with a few others, will lead to the “-1” return value of the “CHDIR” function.

Conclusion:

In this article, we talked in detail about the “CHDIR” function of the C programming language. We first talked about the purpose of this function and explained all its components while stating its syntax. This was followed by a discussion on whether this function also has any impact on the current working directory of the shell or not. After that, we shared a C script for explaining the usage of the “CHDIR” function. Finally, we talked about some errors that can be returned from executing this function. After going through this detailed discussion on the “CHDIR” function, you will not find any ambiguities while using this function in your C programs.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.050306081771851