-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
121 lines (107 loc) · 2.28 KB
/
ls.c
File metadata and controls
121 lines (107 loc) · 2.28 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
#include<stdio.h>
#include<string.h>
#include<dirent.h>
#include<sys/stat.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
void sfile(char const filename[]){
struct stat sfile;
if(stat(filename,&sfile)==-1){
perror("Error");
}
}
void ls(char tokens[10][100],int args)
{
int a=0,l=0;
char *path =(char*)malloc(100*sizeof(char));
strcpy(path,".");
if(args>1&&tokens[args-1][0]!='-')
{
strcpy(path,tokens[args-1]);
}
else if(args>1&&tokens[1][0]=='-')
{
if(tokens[1][1]=='a')
{
a=1;
if(tokens[1][2]=='l')
{
l=1;
}
}
if(tokens[1][1]=='l')
{
l=1;
if(tokens[1][2]=='a')
{
a=1;
}
}
}
char *file = (char*)malloc(100*sizeof(char));
char perm[15]="----------";
struct dirent *dir;
DIR *curr = opendir(path);
if(curr==NULL)
{
perror("Error:Cant open the directory");
}
dir=readdir(curr);
while(dir!=NULL)
{
file=dir->d_name;
if(file[0]=='.'&&a==0)
{
dir=readdir(curr);
continue;
}
struct stat sfile;
int temp=stat(file,&sfile);
// if(temp==-1){
// perror("Error");
// }
if(l==1)
{
if(S_ISDIR(sfile.st_mode))
perm[0] = 'd';
if(sfile.st_mode & S_IRUSR)
perm[1] = 'r';
if(sfile.st_mode & S_IWUSR)
perm[2] = 'w';
if(sfile.st_mode & S_IXUSR)
perm[3] = 'x';
if(sfile.st_mode & S_IRGRP)
perm[4] = 'r';
if(sfile.st_mode & S_IWGRP)
perm[5] = 'w';
if(sfile.st_mode & S_IXGRP)
perm[6] = 'x';
if(sfile.st_mode & S_IROTH)
perm[7] = 'r';
if(sfile.st_mode & S_IWOTH)
perm[8] = 'w';
if(sfile.st_mode & S_IXOTH)
perm[9] = 'x';
printf("%s %ld ",perm,sfile.st_nlink);
//user
struct passwd *user;
user = getpwuid(sfile.st_uid);
printf("%s ", user->pw_name);
//Group
struct group *grp;
grp = getgrgid(sfile.st_gid);
printf("%s ",grp->gr_name);
char* timestr=(char *)malloc(100*sizeof(char));
timestr=ctime(&sfile.st_mtime);
timestr[strlen(timestr)-1]='\0';
//Assumption: the number of digits of memory is <=8
printf("%8zu %s %s\n",sfile.st_size,timestr,file );
}
else
{
printf("%s\n",file);
}
dir=readdir(curr);
}
}