-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.c
110 lines (88 loc) · 2.07 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <dirent.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
char cwdir[1000];
int main(int argc, char* args[]){
if(args[1] != NULL && strcmp(args[1],"--help") == 0){
getcwd(cwdir,sizeof(cwdir));
strcat(cwdir,"/");
strcat(cwdir,"ls.txt");
FILE *fp = fopen(cwdir, "r");
if(!fp) {
perror("fopen");
exit(1);
}
char ch;
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
return 0;
}
void nameFile(struct dirent* name,char* followup)
{
if(name->d_type == DT_REG)
{
printf("%s ",name->d_name);
}
else if(name->d_type == DT_DIR)
{
printf("%s ",name->d_name);
}
else
{
printf("%s ",name->d_name);
}
}
if(args[1] != NULL && strcmp(args[1],"-1") == 0){
int i=0;
struct dirent **listr;
int listn = scandir(".", &listr, 0, alphasort);
if (listn >= 0)
{
for(i = 0; i < listn; i++ )
{
if(strcmp(listr[i]->d_name,".")==0 || strcmp(listr[i]->d_name,"..")==0)
{
continue;
}
else nameFile(listr[i]," ");
printf("\n");
}
}
else
{
perror ("+--- Error in ls ");
}
return 0;
}
int i=0;
struct dirent **listr;
int listn = scandir(".", &listr, 0, alphasort);
if (listn >= 0)
{
for(i = 0; i < listn; i++ )
{
if(strcmp(listr[i]->d_name,".")==0 || strcmp(listr[i]->d_name,"..")==0)
{
continue;
}
else nameFile(listr[i]," ");
if(i%8==0) printf("\n");
}
printf("\n");
}
else
{
perror ("+--- Error in ls ");
}
return 0;
}