-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
144 lines (112 loc) · 2.31 KB
/
Copy pathls.c
File metadata and controls
144 lines (112 loc) · 2.31 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "ucode.c"
char *t1 = "xwrxwrxwr-------";
char *t2 = "----------------";
struct stat mystat, *sp;
int ls_file(char *fname, char *name) //lists a single file
{
char * ret;
char ch = '/';
struct stat fstat, *sp;
int r, i, in;
char ftime[64];
char tempName[100];
sp = &fstat;
r= stat(fname, &fstat);
if (r<0)
{
printf("Stat Error ");
prints(fname);
printf("\n\r");
exit(1);
}
if ((sp->st_mode & 0170000)==0100000)//if (sp->st_mode==0X8124) // file
{
printc('-');
}
if((sp->st_mode & 0170000)==0040000)//if(sp->st_mode==0X41ED) // dir
{
printc('d');
}
if((sp->st_mode & 0170000)==0120000) //symbol link
{
printc('l');
}
for(i=8; i>=0; i--)
{
if(sp->st_mode &(1<<i))
{
printc(t1[i]);
}
else
{
printc(t2[i]);
}
}
printf(" %d ",sp->st_nlink);
printf(" %d ",sp->st_uid);
printf(" %d ",sp->st_gid);
printf(" %d ",sp->st_size);
i = 0;
while(name[i]!='\0')
{
i++;
}
strcpy(tempName,name);
tempName[i]=0;
prints(tempName);
printf("\n\r");
}
int ls_dir(char *dname)
{
printf("SCHEFF~LS~\n\r");
char name[256], tempName[256]; // EXT2 filename: 1-255 chars
char buf[1024];
struct dirent *ep;
char *cp;
DIR *dp;
int dir_fd;
int i =0;
// open DIR to read names
dir_fd = open(dname,O_RDONLY); //get directory file descriptor
read(dir_fd,buf,1024); //read dir into buf
cp = buf;
dp = (DIR *) cp;
while(cp < &buf[1024])
{
strncpy(tempName,dp->name,dp->name_len);
strcpy(name, dname);
strcat(name,"/");
strcat(name,tempName);
ls_file(name,tempName);
cp+= dp->rec_len;
dp=(DIR*)cp;
}
//prints(dp->name);
close (dir_fd);
printf("-----ls dir done\n\r");
}
int main (int argc, char *argv[])
{
struct stat mystat, *sp = &mystat; //stat file status info
int r;
char *s;
char filename[1024], cwd[1024], path[1024];
if (argc==1) // no parameter
strcpy(filename,"./");
else
{
strcpy(path, argv[1]);
}
if ((stat(filename, sp)) < 0) { //stat() syscall
print2f("Error when stat'ing in ls"); exit(1);
}
if((sp->st_mode & 0xF000) == 0x4000) //if (S_ISDIR(sp->st_mode))
{
ls_dir(filename);
}
else if ((sp->st_mode & 0xF000) == 0x8000)
{
ls_file(filename,filename);
}
exit(0);
}