C Programming

C GetCWD Function Usage

Whenever you are using your operating system, you would have noticed that all your actions take place within a specific directory. This directory is known as the current working directory of your system, and it is usually set up by default. However, at times, you might want to change this directory, i.e., you want to change the current working directory of your system. But before doing so, you must know about your current working directory. The “getcwd()” function of the C programming language is used to extract the current working directory of your system. In this article, we will have a detailed discussion about this function. We will highlight the purpose of using this function, followed by its accepted arguments. We will talk about the return type of this function and the errors that can arise due to calling this function wrongly. Finally, we will share a simple example that will depict the usage of the “getcwd()” function in the C programming language.

Purpose of the GetCWD Function in C:

At times, while working with your system, you intentionally change the current working directory. In this situation, it is essential to cross-check whether the path of your current working directory has been changed to your desired path or not. This is where the getcwd() function comes into play. This function helps us by extracting the current working directory of our system. In this way, we can instantly know whether our desired change has taken place successfully or not. Moreover, even if we have not changed our current working directory, we can still know about the default one by using the getcwd() function of the C programming language.

Arguments of the GetCWD Function in C:

The getcwd() function of the C programming language accepts two arguments. The first argument is an array that holds the name of the current working directory, whereas the second one holds the size of that array in bytes. The general syntax of the getcwd() function looks something like this:

char *getcwd(char *CWD, sizeof(CWD))

Return Type of the GetCWD Function in C:

The getcwd() function returns the title of the current working directory that is, in fact, the complete path to this directory. This path or name is returned in the form of a character array.

To which Header File does the GetCWD Function in C belong?

The getcwd() function is implemented within the “unistd.h” header file, i.e., once you will include this file within your C program, you will easily be able to use the getcwd() function.

Possible Errors Associated with the GetCWD Function in C:

The getcwd() function sometimes terminates by displaying some error message without extracting the current working directory. Some specific errors are associated with this behavior of the getcwd() function. We are going to elaborate the most significant of those errors one by one below:

  • EINVAL: The size of the arguments provided is below or equal to zero.
  • ERANGE: The size of the arguments is greater than zero but is still less than the name of the current working directory. In this case, the character array will not hold the name or the path of your current working directory correctly.
  • EACCES: The search or read permissions for the file path or any of its components are denied by your system. Because of this, the getcwd() function will not be able to extract the name or path of your present working directory correctly.
  • ENOMEM: The storage space of your system is insufficient.
  • EIO: Your system has encountered an input or output error.
  • ENOENT: Some component of the current working directory’s pathname does not exist on your system.
  • ENOTDIR: A directory component of your current working directory is, in fact, not a directory.
  • ELOOP: There are so many levels of symbolic links created on your system that the getcwd() function gets stuck into a loop. In this case, the getcwd() function will keep trying to resolve this loop but will fail to do so in the stipulated time because of which your program might terminate due to timeout.
  • ENOSYS: The getcwd() function is not implemented for the file system that is specified in the name of your current working directory.

Any of the errors mentioned above will lead to the termination of your programming without displaying the name or path of your current working directory.

Usage of the GetCWD Function in C:

To elaborate the usage of the getcwd() function in C, we have implemented a real example whose code is shown in the image below:

In this code, we have first included the “unistd.h” header file along with the regular “stdio.h” file since this file contains the implementation of the “getcwd()” function of the C programming language. Then, within our driver function, we have declared a character array named “CWD” of size 256 bytes. This array will contain the name of our current working directory. Then, we have an “if” statement that checks the value returned by the “getcwd()” function and whether it is equal to “NULL” or not.

This function accepts the “CWD” character array and its size as arguments. If the value returned by this function is equal to “NULL” then, an error message will be printed on the terminal, and the program will terminate without displaying the current working directory on the terminal. Then, we have an “else” statement that will be executed only when the current working directory is extracted successfully. Within this “else” statement, the current working directory will be printed on the terminal. Finally, we wrapped up the program with the “return 0” statement.

To compile this code, you need to run the command that follows:

$ gcc GetCWD.c –o GetCWD

For executing this code, you need to run the command given below:

$ ./GetCWD

The current working directory of our system is shown in the following image:

Conclusion:

This article was dedicated to discussing the “getcwd()” function of the C programming language. We threw light on the importance of this function and discussed its parameters and return type. We also shared the name of the header file to which the getcwd() function belongs, i.e., “unistd.h”. After that, we briefly discussed the errors that can arise after calling this function. Then, we shared a simple example with you that demonstrated this function’s usage in C. Once you follow the example shared in this guide, you will easily extract the current working directory of your system. Moreover, this function will also prove to be helpful for you if you would have changed your current working directory recently to confirm if the said change has taken place successfully or not.

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.048561096191406