-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
73 lines (61 loc) · 1.39 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
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
void ls(char *arr[])
{
char cwd[10000];
getcwd(cwd, 10000);
struct dirent **files;
int size = scandir(cwd, &files, NULL, alphasort);
if (arr[1] != NULL)
{
if (!strcmp(arr[1], "-a\n") || !strcmp(arr[1], "-a"))
{
for (int i = 0; i < size; i++)
{
char f[20];
strncpy(f, files[i]->d_name, 20);
printf("%s ", f);
}
printf("\n");
}
else if (!strcmp(arr[1], "-m\n") || !strcmp(arr[1], "-m"))
{
for (int i = 0; i < size; i++)
{
char f[20];
strncpy(f, files[i]->d_name, 20);
if ((int)f[0] != 46)
{
printf("%s, ", f);
}
}
printf("\n");
}
else
{
printf("ls: invalid option %s\n", arr[1]);
}
}
else
{
for (int i = 0; i < size; i++)
{
char f[20];
strncpy(f, files[i]->d_name, 20);
if ((int)f[0] != 46)
{
printf("%s ", f);
}
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
char *arr[] = {argv[1], argv[2]};
ls(arr);
return 0;
}