Skip to content

Instantly share code, notes, and snippets.

@svdamani
Created April 15, 2015 16:56
Show Gist options
  • Save svdamani/cd9d2a5bb19228d912dc to your computer and use it in GitHub Desktop.
Save svdamani/cd9d2a5bb19228d912dc to your computer and use it in GitHub Desktop.
Simple ls command implementation in C
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#define GREEN "\x1b[32m"
#define BLUE "\x1b[34m"
#define WHITE "\x1b[37m"
void Usage() {
fprintf(stderr, "\nUsage: exec [OPTION]... [DIR]...\n");
fprintf(stderr, "List DIR's (directory) contents\n");
fprintf(stderr, "\nOptions\n-R\tlist subdirectories recursively\n");
return;
}
void RecDir(char * path, int flag) {
DIR * dp = opendir(path);
if(!dp) {
perror(path);
return;
}
struct dirent * ep;
char newdir[512];
printf(BLUE "\n%s :\n" WHITE, path);
while((ep = readdir(dp)))
if(strncmp(ep->d_name, ".", 1))
printf(GREEN "\t%s\n" WHITE, ep->d_name);
closedir(dp);
dp = opendir(path);
while((ep = readdir(dp))) if(strncmp(ep->d_name, ".", 1)) {
if(flag && ep->d_type == 4) {
sprintf(newdir, "%s/%s", path, ep->d_name);
RecDir(newdir, 1);
}
}
closedir(dp);
}
int main(int argc, char ** argv) {
switch(argc) {
case 2:
if(strcmp(argv[1], "-R") == 0) Usage();
else RecDir(argv[1], 0);
break;
case 3:
if(strcmp(argv[1], "-R") == 0) RecDir(argv[2], 1);
else Usage();
break;
default: Usage();
}
return 0;
}
@Callen44
Copy link

Callen44 commented May 8, 2025

Usually on linux when I run ls, it prints the files in the “.” Directory. Is that not necessary for POSIX, or is there some other particular reason why this one prints out usage instructions instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment