-
-
Save bittorf/c098d2ff9f792a23934aafb0353db7a0 to your computer and use it in GitHub Desktop.
Simple ls command implementation in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment