-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist_client.c
More file actions
200 lines (163 loc) · 3.64 KB
/
list_client.c
File metadata and controls
200 lines (163 loc) · 3.64 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <sys/sysmacros.h> // major(), minor()
#include "common.h"
/*
List files in current working directory on client side.
*/
/* Return result of character comparison(Ignoring LETTER CASE)*/
int mycompare(const void *a, const void *b)
{
return strcasecmp(*(const char **)a,*(const char **)b);
}
/* `ls` comand in linux. List files in current working directory without details.*/
void ls_dir(char *dir_name)
{
DIR *fd;
char *arg[MAXSZ];
int i = INITIALISE;
int n;
struct dirent *entry;
fd = opendir(dir_name);/* Open directory */
if(fd == NULL)
{
perror("Error");
return;
}
while((entry = readdir(fd))!=NULL)/* Ignore hidden files */
{
if(entry->d_name[0] != '.' && strcmp(entry->d_name,".") != 0 && strcmp(entry->d_name,"..") != 0)
{
*(arg + i) = entry->d_name;
i++;
}
}
n = i;
qsort(arg,n,sizeof(const char *),mycompare);/* Sort file names in alphabetical order ignoring LETTER CASE*/
i = 0;
while(i < n)
{
printf("%s\n",*(arg + i));
i++;
}
printf("\n");
closedir(fd);
}
void ls_l_dir(char *dir_name)
{
char time_buff[MAXSZ];
char *arg[MAXSZ];
int i = INITIALISE;
int n;
int val = INITIALISE;
int temp;
struct group *gp;/* structure contating group details */
struct passwd *pw;/* structure containing passwd file */
struct tm *info;
struct stat buff;
struct dirent *entry;
DIR *fd;
fd = opendir(dir_name);/* Open directory */
if(fd == NULL)
{
perror("Error");
return;
}
while((entry = readdir(fd))!= NULL)/* Read directory */
{
/* Call kiya tha */
if(strcmp(entry->d_name,".") != 0 && strcmp(entry->d_name,"..") != 0 && entry->d_name[0]!= '.')
{
*(arg + i) = entry->d_name;
i++;
}
}
n = i;
qsort(arg,n,sizeof(const char *),mycompare);
i = 0;
while(i < n)
{
lstat(*(arg + i),&buff);
gp = getgrgid(buff.st_gid);
pw = getpwuid(buff.st_uid);
info = localtime(&(buff.st_mtime));
strftime(time_buff,sizeof(time_buff),"%b %d %H:%M",info);
if(buff.st_size % 1024 == 0)
{
temp = (int)buff.st_size / 1024;
val += temp ;
}
else
{
temp = ((int)buff.st_size / 1024);
val += (temp + (4 - (temp % 4)));
}
/* Selecting file type */
switch(buff.st_mode & S_IFMT)
{
case S_IFCHR:
printf("c");
break;
case S_IFBLK:
printf("b");
break;
case S_IFDIR:
printf("d");
break;
case S_IFLNK:
printf("l");
break;
case S_IFIFO:
printf("p");
break;
case S_IFSOCK:
printf("s");
break;
default:
printf("-");
break;
}
/* File permissions */
if(buff.st_mode & S_IRUSR)
printf("r");
else
printf("-");
if(buff.st_mode & S_IWUSR)
printf("w");
else
printf("-");
if(buff.st_mode & S_IXUSR)
printf("x");
else
printf("-");
if(buff.st_mode & S_IRGRP)
printf("r");
else
printf("-");
if(buff.st_mode & S_IWGRP)
printf("w");
else
printf("-");
if(buff.st_mode & S_IXGRP)
printf("x");
else
printf("-");
if(buff.st_mode & S_IROTH)
printf("r");
else
printf("-");
if(buff.st_mode & S_IWOTH)
printf("w");
else
printf("-");
if(buff.st_mode & S_IXOTH)
printf("x");
else
printf("-");
if(((buff.st_mode & S_IFMT)^S_IFCHR) == 0 || ((buff.st_mode & S_IFMT)^S_IFBLK) ==0)
printf(" %6d %8s %8s %5d, %5d %13s %s\n",(int)buff.st_nlink,pw->pw_name,gp->gr_name, major(buff.st_rdev), minor(buff.st_rdev),time_buff,*(arg + i));
else
printf(" %6d %8s %8s %12u %13s %s\n",(int)buff.st_nlink,pw->pw_name,gp->gr_name,(unsigned int)buff.st_size,time_buff,*(arg + i));
i++;
}
printf("total %d\n\n",val);
closedir(fd);
}