Cloning ls command in C and Java programming languages

Default featured post

Long time ago I wanted to write one file manager as a fun project in either C or Java programming languages but unfortunately due to some issues and having no free time, the project was unsuccessful and I had to give it up in the middle of doing it. Although, I could not write my own version of file manager/explorer, I gained lot of experience during developing the code which I will share small portion of it in this post as writing your own version ls command or in other word simulating or cloning ‘ls’ command for educational purposes.

Firstly, in C programming for getting list of available files and directories based on given path is possible with either using some POSIX libraries or Windows APIs. ANSI C does not contain any libraries, functions to help for developing such command and if you want to use pure ANSI C, you should start developing your code from scratch or in other word, reinvent the wheel.

For developing the code I utilized “dirnet.h” header file which is POSIX header file and it could be found in all POSIX or Unix like (including Linux) operating systems. The code is quite simple with using mentioned header file as you can see below.

#include <dirent.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
DIR *dir;
struct dirent *de;
if (argc > 1)
dir = opendir(argv[1]); /*your directory*/
else
dir = opendir("."); /* Default: current path */
while (dir)
{
de = readdir(dir);
if (!de)
break;
printf("%s\n", de->d_name);
}
closedir(dir);
return 0;
}
view raw ls.c hosted with ❤ by GitHub

In the above code firstly, you should get the path name from the user which here is assumed to be argv[0] variable and if the user did not enter anything then current path is considered as the target path. Current path is shown with . symbol. Then in the next step, list of all files and directories are pushed to dir variable which is from DIR type and it is supposed to be array of some kind of struct. Then in the while loop, all elements will be printed out which is name of directories and files.

You also could sort list of files and folder and put them into order. Furthermore, you could make distinction between files and directories with dedicating various color to each of them. In the code, I did not apply extra things to keep the code simple to understand but in the next example which is developed in Java, coloring and sorting is added.

Now if you want to develop the same application in Java, you do not have some compatibility issues that you faced in C. In fact, in Java there is one cross platform library for this task which is called as java.io.File. This library gives you more facilities than C version counterpart and base on my experience it is much easier to use with high flexibility. Java version of the code is given below.

import java.io.File;
import java.util.Arrays;
public class LsClone {
public static void main(String[] args) {
String path;
if (args.length == 0)
path = ".";
else
path = args[0];
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
Arrays.sort(listOfFiles);
for (int i = 0; i < listOfFiles.length; i++)
if (listOfFiles[i].isDirectory() && listOfFiles[i].isHidden() == false)
System.out.println("\u001B[34m" + listOfFiles[i].getName());
else if (listOfFiles[i].isFile() && listOfFiles[i].isHidden() == false)
System.out.println("\u001B[32m" + listOfFiles[i].getName());
System.out.print("\u001B[0m");
}
}
view raw LsClone.java hosted with ❤ by GitHub

Structure of the code is exactly the same as C version, except, sorting feature and also coloring feature is added to this version and in addition, Java code is more tangible and has more understand-ability in comparison with the C one. In order to distinguish between files and folder, coloring is defined in the loop and it checks type of file. If it is directory then the name of directory is printed in green, otherwise, it prints name of files in blue. At the end of the program, the terminal/console color restored to the original color. In this version also hidden files are not shown in the output.

The code is just basic code for expressing how to make clone version ls command and it is not complete command with all available switches. If you are interested in developing the current version, you could add some switches with designing simple parser to parse different given switches by the user.

For more information please refer to following links,