-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
48 lines (47 loc) · 1.02 KB
/
ls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
if(argv[1] == NULL) {
struct dirent **list;
int totalFiles;
totalFiles = scandir(".", &list, 0, alphasort);
while(totalFiles--) {
char *name = list[totalFiles]->d_name;
if(name[0] != '.') {
printf("%s \t\t", name);
}
}
free(list);
printf("\n");
}
else if(strcmp(argv[1], "-a") == 0) {
struct dirent **list;
int totalFiles;
totalFiles = scandir(".", &list, 0, alphasort);
while(totalFiles--) {
char *name = list[totalFiles]->d_name;
printf("%s \t\t", name);
}
free(list);
printf("\n");
}
else if(strcmp(argv[1], "-A") == 0) {
struct dirent **list;
int totalFiles;
totalFiles = scandir(".", &list, 0, alphasort);
while(totalFiles--) {
char *name = list[totalFiles]->d_name;
if(strcmp(name, ".") != 0 && strcmp(name, "..") != 0) {
printf("%s \t\t", name);
}
}
free(list);
printf("\n");
}
else {
printf("Flag not handled. \n");
}
return(0);
}