-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.c
145 lines (139 loc) · 3.45 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
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
#include "headers.h"
DIR * directory_stream;
struct dirent * file;
void open_directory(char * directory)
{
if(directory == NULL)
directory_stream = opendir(".");
else
{
directory_stream = opendir(directory);
if(directory_stream == NULL)
{
perror("Error:");
return;
}
}
}
void without_flag_l(int choice, char * directory)
{
open_directory(directory);
if(directory_stream == NULL)
return;
file = readdir(directory_stream);
while(file != NULL)
{
if((choice % 4 == 0 && file->d_name[0] != '.') || choice % 4 == 1)
printf("%s\n", file->d_name);
file = readdir(directory_stream);
}
}
void flag_l(int choice, char * directory)
{
open_directory(directory);
if(directory_stream == NULL)
return;
file = readdir(directory_stream);
while(file != NULL)
{
// printf("Yes\n");
if(!((choice % 4 == 2 && file->d_name[0] != '.') || choice % 4 == 3))
{
file = readdir(directory_stream);
continue;
}
struct stat file_details;
stat(file->d_name,&file_details);
//print file permissions
printf( (S_ISDIR(file_details.st_mode)) ? "d" : "-");
printf( (file_details.st_mode & S_IRUSR) ? "r" : "-");
printf( (file_details.st_mode & S_IWUSR) ? "w" : "-");
printf( (file_details.st_mode & S_IXUSR) ? "x" : "-");
printf( (file_details.st_mode & S_IRGRP) ? "r" : "-");
printf( (file_details.st_mode & S_IWGRP) ? "w" : "-");
printf( (file_details.st_mode & S_IXGRP) ? "x" : "-");
printf( (file_details.st_mode & S_IROTH) ? "r" : "-");
printf( (file_details.st_mode & S_IWOTH) ? "w" : "-");
printf( (file_details.st_mode & S_IXOTH) ? "x\t" : "-\t");
//number of hard links to the file
printf(" %3d", (int)file_details.st_nlink);
//owner
struct passwd * uname = getpwuid(file_details.st_uid);
printf(" %s",uname->pw_name);
//group
struct group * ugroup = getgrgid(file_details.st_gid);
printf(" %s",ugroup->gr_name);
//number of bytes
printf(" %10d", (int)file_details.st_size);
//time lasst modified & file name
struct tm *time;
char datestring[256];
time = localtime(&file_details.st_mtime);
strftime(datestring, sizeof(datestring), "%b %d %H:%M", time);
printf(" %s %s\n", datestring, file->d_name);
file = readdir(directory_stream);
}
}
void ls(char * command)
{
char * save_command;
char command_copy[1000];
strcpy(command_copy, command);
// printf("No\n");
char * parameter = strtok_r(command_copy, " ", &save_command);
char * previours_parameter;
parameter = strtok_r(NULL, " ", &save_command);
int flag_choice = 0;
/*
0 = flagless
1 = -a
2 = -l
3 = -la
4 = flagless with directory
5 = -a with directory
6 = -l with directory
7 = -la with directory
*/
while(parameter != NULL)
{
if(strcmp(parameter, "-l") == 0)
{
if(flag_choice == 0)
flag_choice = 2;
else
flag_choice = 3;
}
else if(strcmp(parameter, "-a") == 0)
{
if(flag_choice == 0)
flag_choice = 1;
else
flag_choice = 3;
}
else if(strcmp(parameter, "-la") == 0 || strcmp(parameter, "-al") == 0)
flag_choice = 3;
else
flag_choice += 4;
previours_parameter = parameter;
parameter = strtok_r(NULL, " ", &save_command);
}
switch(flag_choice)
{
case 0:
case 1:
without_flag_l(flag_choice, NULL);
break;
case 2:
case 3:
flag_l(flag_choice, NULL);
break;
case 4:
case 5:
without_flag_l(flag_choice, previours_parameter);
break;
case 6:
case 7:
flag_l(flag_choice, previours_parameter);
break;
}
}