Example 1:
Within our first example, we will take a look at the simple C code of using the “realpath” function to get the real and whole path of some files from the system’s current working directory. We need to use a total of 6 basic header files at the start of this code included with the use of the “#include” keyword. Some libraries are standard libraries to use the input output stream in the code, some are here to utilize the string values, and some are utilized to perform the exception handling. This code’s main() function defines a character-type array called “buffer” with a built-in size of “PATH MAX” or 0. This array is utilized to save the real and full path of some directories.
The realpath() function takes the “SomeFile.txt” file name with the “buffer” array that is passed to it as an argument for storing the whole path. A pointer “path” of the character type is initialized to point towards the address of a “buffer” variable containing the path of a file passed by the realpath() function.
Here, comes the “if-else” statement to check whether we got the correct reference for the file path or if the “path” pointer is empty. If it returns the correct reference, the printf function is used to display the text “The complete file path of SomeFile.txt:” along with the real path of the particular file. Otherwise, the else statement is executed and the printf statement used within it will throw an error using the “Strerror” function, taking the “errno” as an argument. The perror function is used to display the same error once again with the “realpath” error name. To get the error, the exit function of this code terminates the program as soon as possible. Let’s save our first example and compile it to see its results.
We use the gcc compiler to compile the realpath.c file with the –o option to generate its object file named “realPath.out” within the same working directory. This command is successful and we got the same name object file in the home “src” directory as per the output of the “ls” instruction.
Now, we execute the object file we just created with the “./” characters in the same working directory. The output of this command shows us the complete path for this particular file starting from the root folder like the “/home/linux/src/SomeFile.txt”.
Example 2:
Let’s get started with another example to utilize the realpath() function to get the real path of some files. Within this C code, we added the same 6 header files with the “#include” keyword. After that, the main() function is initialized with the character type pointer variable “link path” that stores some path like the “/src/SomeFile.txt”. An “actual path” array is used to store the real path of the “SomeFile.txt” file with “+1” to fetch the path of a file mentioned after the second “/” character. Another “ptr” pointer of the character type is defined.
We pass the link path pointer variable and the actual path array to the realpath() function to get the whole path of the “/src/SomeFile.txt” to the “actual path” array. The returned result is saved to the “ptr” pointer variable. Within the “if” part, if the “ptr” return the result by the realpath() function that contains some reference number, the printf statement displays the actual path via the actual path array. Otherwise, the else part of this code is executed and generates an error via the use of the strerror function using the “errno” variable as an argument. The exit function is to terminate this program.
We compiled this C code with the gcc compiler and created its “realpath1.out” object file preceded by the –o option.
After running its object file with the “./” character, we got the error because we haven’t provided the correct format of the file path “/src/SomeFile.txt”.
Let’s update the code once again to avoid this error shortly after executing the object file. We update its first line within the main function,. The character type pointer “link path” that contains the specific file with one folder below in it is updated. Now, we add the name of a “SomeFile.txt” file that resides in the current working directory “src” instead of the “/src/SomeFile.txt” path to the working directory folder. The rest of the code is used without any change. Save and run this code to see the results.
After compiling this C file with the gcc compiler and creating its “realPath1.out” object file with the –o object, it is ready to be executed.
After the execution of the C object file, the real and full path of the specific file “SomeFile.txt” is shown on the terminal screen.
Conclusion
This article is all about the use of the realpath method of the POSIX C library to show the real and full path of some specific file. For this, we tried the two C examples in the Kali Linux platform. Within our examples, we looked at how this function reacts to a single file name or with a specific path.