When a naïve user starts to learn about the operating system, he/she gets to know about the file system of that system. A directory is essentially a special type of file that behaves like a database of any operating system like Windows, Linux, Unix, etc. The file system of the media defines how the files are categorized and accessed. Similarly, all that information is stored in the directory including the file’s physical location, name, date and timestamps, privileges, and other required details.
The readdir() function of POSIX in the Linux system has been widely known for reading all the files and folders from some specific directory. Within this tutorial, we will look at the use of the readdir function in Kali Linux.
We need to utilize the “dirent.h” header file. Before reading the data from a particular directory, you need to open it. Use the opendir() function to gain an access to a directory. It’s written as follows in the header file “dirent.h”. But, we’re talking about the readdir() function here. To acquire an access to the directory that we’re going to read, we need to utilize the opendir() function. The header file “dirent.h” contains the definitions for both functions. Before proceeding, we must be familiar with the readdir() function’s syntax. The following are the syntax and required header files:
- #include <sys/types.h>
- #include <dirent.h>
- struct dirent *readdir(DIR *dirp);
Example 1:
Use VIM or any other editor of your preference to build or open a file. In this instance, readdirFile.c is used as the filename and the VIM editor is employed. When we type this command and hit the enter button on the keyboard, the VIM editor opens the file. The following cited command is executed in Kali Linux:
To open the insert mode in VIM, press the “Escape” key on the keyboard and then press the ‘i’ button to bring up the insert mode. Then, as shown on the following screen, begin writing the code in the file. We simply include the header files, define, and undefine the POSIX source in the first six lines of code.
The main function then starts to construct a DIR pointer named directory, which is required since the opendir() method returns a DIR pointer. This function requires a string path to a directory. In the following line, we define the entry direct structure point which is later utilized in the readdir() function to show the name of the directory in the specified current directory. As you can see in the if statement, we applied the opendir function which takes the ‘/’ character and points to the current directory’s root directory.
We check if the root directory isn’t NULL in the conditional statement. If it is, we display an error using the perror function. The “Contents of root:” line is printed in the else statement. Then, using a while loop on the readdir function, check that it is not returning NULL, indicating that there are no more directories in the specified directory or folder. The name of the directory is printed using a pointer variable entry inside the while loop block. After you’ve done whatever you want with the directory, you can use the closedir() function of POSIX outside the while loop block to close the opened directory. Let’s save the following code first:
Type the command as shown in the following image to compile the C code with the GCC compiler and save the output to the required output or object file. If you don’t type the command following the –o flag, the compiler will save your file as an “a.out” file by default. If the event that the output file already exists in the directory, this command will overwrite it. We built a “readdirFile.out” output file in our scenario. If it compiles correctly with no errors or warnings in the command line, just run it. Otherwise, fix the code and recompile it on the shell. The following cited command is executed in Kali Linux:
To run the output file, use the following command which displays the file’s output on the screen or any other commands contained in the code. The readdir method in our code displays the names of the root folder’s directories. This also displays the files that exist in the directory. The following cited command is executed in Kali Linux:
Example 2:
Now, use the VIM editor to create a new file called “readdireFile2.c”. This will create a new file and open it in the editor. Press “escape” and hit the ‘i’ key on the keyboard to enable the insert/edit mode in the editor. The following cited command is executed in Kali Linux:
After that, copy the following code lines. The initial code is the same, but the variable names are changed. The current working directory path is supplied to the opendir function using the dot(.) character in this code. After that, see if it returns NULL or not. If it returns NULL, use the perror function to display the error and exit the main function. The int files variable is used to show the file and folder numbering in the current directory. We incremented this variable by 1 in the while loop, then used the printf command to print its value along with the name of the file or folder. Outside the while loop, close the directory using the closedir method.
Now, compile the code and store the output in the “reddirFile2.out” file. The following cited command is executed in Kali Linux.
The “ls” command can be utilized to show a list of all the files and folders in a directory. The following cited command is executed in Kali Linux:
After executing the output file, the compiler displays all the files in the terminal that resides in the current directory. The following cited command is executed in Kali Linux:
Conclusion
Any file system can consist many files and directories that hold more data within them. This article is all about the use of the readdir function of C to read all the files and folders of the current working directory of the system.