-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.c
65 lines (65 loc) · 1.37 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
extern int errno;
int main(int noOfArguements, char *args[])
{
int flaga = 0;
int flagA = 0;
DIR *dir;
for (int a = 0; a < noOfArguements; a++)
{
if (strcmp(args[a], "-A") == 0)
flagA = 1;
if (strcmp(args[a], "-a") == 0)
flaga = 1;
}
if (flaga + flagA + 1 == noOfArguements)
{
args[1] = (char *)malloc(2 * sizeof(char));
args[1][0] = '.';
args[1][1] = '\0';
}
if (noOfArguements == 1)
noOfArguements = 2;
for (int a = 1; a < noOfArguements; a++)
{
if (strcmp(args[a], "-A") == 0 || strcmp(args[a], "-a") == 0)
continue;
dir = opendir(args[a]);
if (dir == NULL)
{
printf("Error no %d\n",errno );
perror("ls");
return 0;
}
struct dirent *fileHere;
fileHere = readdir(dir);
if (fileHere == NULL)
perror("ls");
if (flagA + flaga + 2 != noOfArguements)
printf("%s : \n ", args[a]);
while (fileHere != NULL)
{
char tmp[1024];
if (fileHere->d_name[0] == '.')
{
if (flaga)
printf("%s ", fileHere->d_name);
else if (flagA && strcmp(fileHere->d_name, ".") && strcmp(fileHere->d_name, ".."))
printf("%s ", fileHere->d_name);
}
else
printf("%s ", fileHere->d_name);
fileHere = readdir(dir);
}
printf("\n");
}
return 0;
}