Setting up a working directory starting from the Linux root directory opens up the possibility of accessing and working with the entire file structure in the various system and user folders.
The C language provides the chdir() function to set up a working directory and also has a function to query the directory that you are working in.
In this Linux Hint article, you will learn how to use the getcwd() function to query the directory in which you are currently working. We will explain the theoretical workings of this function, its syntax, the types of its input and output arguments, and each type of data.
To use this function in a practical way, we will apply everything we learned through a practical example where you learn step-by-step on how to use this function to retrieve the current working directory.
Syntax of the Getcwd() Function in C Language
Description of the Getcwd() Function in C Language
The getcwd() function queries the current working directory of the calling program or process. This function returns the pointer to a “buf of size” character array which contains a string with the absolute path of the current working directory.
If the call to the getcwd() function is successful, it returns the pointer to the string with the path of the working directory. If an error occurs, the result is a null pointer. In this case, the error identification code is automatically stored in the errno global variable.
Both the declaration of the errno variable and the definitions of the error codes are in the “errno.h” header. In the following example, we will see a special section which explains how to detect and identify the errors.
The getcwd() function is defined in the “unistd.h” header. To use it, we have to include it in our “.c” file as follows:
How to Query the Current Working Directory with the Getcwd() Function in the C Language
In this example, we will explain how to retrieve the current working directory and display it in the command console.
The first step to do this is to include the necessary headers. In this case, they are the “unistd.h” and “stdio.h” files. Then, we open a main() function which returns an empty return value and in which we declare an array path with 128 elements of type char. In this array, the getcwd() function stores the string with the directory.
Once the necessary variables are declared, we call the getcwd() function. We pass the “buf” input argument as the pointer to the array path and the size of the buffer as argument size, as 128 in this case.
Then, using the printf() function, we print the “The actual directory is:” message followed by the directory path that is returned by the getcwd() function. Here is the complete code for this example:
#include <unistd.h>
void main ()
{
char path [128];
getcwd (path, 128);
printf ("\n\nThe actual directory is: %s\n", path);
}
Next, we see an image with the compilation and execution of this code. In this case, getcwd() returns the absolute path of the user’s home directory in session:
The same pointer that getcwd() returns in the “buf” argument returns the following result:
This allows the getcwd() function to be called from the input arguments of another function. The following code shows the call from the input arguments of printf():
#include <unistd.h>
void main ()
{
char path [128];
printf ("\n\nThe actual directory is: %s\n", getcwd (path, 128));
}
In this case, printf() takes the pointer to the string that is returned by getcwd() and prints it to the command console with only one line of code.
The following image shows that this method has the same effect as the program in the previous example:
Errors When Using the Getcwd() Function: How to Detect and Identify Them
If an error occurs when calling the getcwd() function, it returns a null pointer as a result. The easiest way to detect an error is to use an “if” condition where the input condition is determined by a result that is equal to NULL. The following is a code snippet that uses this method for error detection:
When an error occurs, a numeric code that identifies it is automatically stored in the errno global variable which is declared in the “errno.h” header.
This header also defines the error codes and their numeric representation.
You can see the errors that the getcwd() function can generate, their numeric representation which is returned in the errno variable, and a brief description of each one:
Definition | Value in errno | Description |
ENOENT | 2 | No such file or directory. |
EACCES | 13 | Permission denied |
EFAULT | 14 | Bad address. |
EINVAL | 22 | Invalid argument. |
ENAMETOOLONG | 36 | File name or path too long. |
The easiest way to identify an error is to open a conditional switch where the jump condition is the errno global variable and each case is an error definition. We see the code for this detection method in the following:
The variables and error codes are defined in the “errno.h” header. To use them, we must include them in our “.c” file as follows:
Conclusion
In this Linux Hint article, we explained how to use the getcwd() function to determine the current working directory of the calling program.
We looked at a theoretical section about this function, what its input and output arguments are, and what kind of data it uses. Then, we applied what we learned through the practical examples with images and code snippets that show how to declare the necessary variables and call the getcwd() function.
We also included a special section where we list the different errors that this function can generate and explained how to detect them.