-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsFile.c
More file actions
57 lines (56 loc) · 1.84 KB
/
Copy pathlsFile.c
File metadata and controls
57 lines (56 loc) · 1.84 KB
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
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char const *argv[])
{
if (argc == 1) {
struct dirent *dirEnt;
DIR *dir = opendir(".");
if (dir == NULL) {
printf("Error : Current working directory can't be opened");
exit(1);
}
while ((dirEnt = readdir(dir)) != NULL) {
if ((strcmp(dirEnt->d_name, ".") != 0) && (strcmp(dirEnt->d_name, "..") != 0)) {
printf("%s\n", dirEnt->d_name);
}
}
closedir(dir);
exit(0);
}
else if (argc == 2 && (strcmp(argv[1], "-F") == 0)) {
struct dirent *dirEnt;
DIR *dir = opendir(".");
if (dir == NULL) {
printf("Error : Current working directory can't be opened");
exit(1);
}
while ((dirEnt = readdir(dir)) != NULL) {
if ((strcmp(dirEnt->d_name, ".") != 0) && (strcmp(dirEnt->d_name, "..") != 0)) {
if (dirEnt->d_type == DT_DIR) {
printf("%s/\n", dirEnt->d_name);
}
else {
printf("%s\n", dirEnt->d_name);
}
}
}
closedir(dir);
exit(0);
}
else if (argc == 2 && (strcmp(argv[1], "--help") == 0)) {
printf("ls : Prints the files and folders in working directory.\n");
printf("Options:\n");
printf("-F : Prints the files and fodders in working directory. The folder name will be ended by forward slash\n");
printf("–help : Prints information about this command\n");
exit(0);
}
else {
printf("Error: There is an error in the arguments or options.\n");
exit(1);
}
}