-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeek.c
More file actions
185 lines (178 loc) · 5.82 KB
/
peek.c
File metadata and controls
185 lines (178 loc) · 5.82 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
#include "headers.h"
//all done except ~ and -
void get_Directories(char* cmd, int hid, int info)
{
struct dirent **dir;
int curr_dir_no;
char abs_path[1000];
if(!(cmd[0] == '/')){
getcwd(abs_path, sizeof(abs_path));
strcat(abs_path, "/");
strcat(abs_path, cmd);
}
else{
strcpy(abs_path, cmd);
}
curr_dir_no = scandir(cmd, &dir, NULL, alphasort);
if (curr_dir_no == -1)
{
perror("scandir");
return;
}
for (int l = 0; l < curr_dir_no; l++)
{
if (dir[l]->d_name[0] != '.' || hid == 1)
{
struct stat space;
char path[1000];
strcpy(path, abs_path);
strcat(path, "/");
strcat(path, dir[l]->d_name);
if (stat(path, &space) != -1)
{
char perms[12];
perms[0] = (S_ISDIR(space.st_mode)) ? 'd' : '-';
perms[1] = (space.st_mode & S_IRUSR) ? 'r' : '-';
perms[2] = (space.st_mode & S_IWUSR) ? 'w' : '-';
perms[3] = (space.st_mode & S_IXUSR) ? 'x' : '-';
perms[4] = (space.st_mode & S_IRGRP) ? 'r' : '-';
perms[5] = (space.st_mode & S_IWGRP) ? 'w' : '-';
perms[6] = (space.st_mode & S_IXGRP) ? 'x' : '-';
perms[7] = (space.st_mode & S_IROTH) ? 'r' : '-';
perms[8] = (space.st_mode & S_IWOTH) ? 'w' : '-';
perms[9] = (space.st_mode & S_IXOTH) ? 'x' : '-';
perms[10] = '\0';
struct passwd *pw = getpwuid(space.st_uid);
struct group *gr = getgrgid(space.st_gid);
struct tm *time_info = localtime(&space.st_mtime);
char date_time[80];
strftime(date_time, sizeof(date_time), "%b %d %H:%M", time_info);
if (S_ISDIR(space.st_mode))
{
if(info == 1){
printf("\e[36m%s %3ld %s %s %8ld %s %s\e[m\n", perms, (long int)space.st_nlink, pw ? pw->pw_name : "unknown", gr ? gr->gr_name : "unknown", space.st_size, date_time, dir[l]->d_name);
}
else{
printf("\e[36m%s\e[m\n", dir[l]->d_name);
}
}
else if (S_IXUSR & space.st_mode)
{
if(info == 1){
printf("\e[32m%s %3ld %s %s %8ld %s %s\e[m\n", perms, (long int)space.st_nlink, pw ? pw->pw_name : "unknown", gr ? gr->gr_name : "unknown", space.st_size , date_time, dir[l]->d_name);
}
else{
printf("\e[32m%s\e[m\n", dir[l]->d_name);
}
}
else
{
if(info == 1){
printf("%s %3ld %s %s %8ld %s %s\n", perms, (long int)space.st_nlink, pw ? pw->pw_name : "unknown", gr ? gr->gr_name : "unknown", space.st_size, date_time, dir[l]->d_name);
}
else{
printf("%s\n", dir[l]->d_name);
}
}
}
}
free(dir[l]);
}
free(dir);
}
int peek(char **command, int bg, int cmd_len,int* pipe_fd,int* pipe_fd2, int active_pipe)
{
int hid = 0;
int info = 0;
char cmd[100];
getcwd(cmd, sizeof(cmd));
if (cmd_len == 1)
{
get_Directories(cmd, 0, 0);
}
else if (cmd_len == 2)
{
if (!strcmp(command[1], "-a") || !strcmp(command[1], "-al") || !strcmp(command[1], "-la"))
{
hid = 1;
}
if (!strcmp(command[1], "-l") || !strcmp(command[1], "-al") || !strcmp(command[1], "-la"))
{
info = 1;
}
if (!strcmp(command[1], "."))
{
get_Directories(cmd, 0, 0);
}
else if(!strcmp(command[1], "~")){
get_Directories(gethome_len(), 0, 0);
}
else if (!strcmp(command[1], ".."))
{
char *temp = strrchr(cmd, '/');
*temp = '\0';
get_Directories(cmd, 0, 0);
}
else if (hid || info)
{
get_Directories(cmd, hid, info);
}
else
{
get_Directories(command[1], 0, 0);
}
}
else if (cmd_len == 3)
{
if (!strcmp(command[1], "-a") || !strcmp(command[1], "-al") || !strcmp(command[1], "-la") || !strcmp(command[2], "-a"))
{
hid = 1;
}
if (!strcmp(command[1], "-l") || !strcmp(command[1], "-al") || !strcmp(command[1], "-la") || !strcmp(command[2], "-l"))
{
info = 1;
}
if (!strcmp(command[2], "."))
{
get_Directories(cmd, hid, info);
}
else if (!strcmp(command[2], "~"))
{
get_Directories(gethome_len(), hid, info);
}
else if (!strcmp(command[2], ".."))
{
char *temp = strrchr(cmd, '/');
*temp = '\0';
get_Directories(cmd, hid, info);
}
else if (hid && info)
{
if (strlen(command[1]) == 3)
{
get_Directories(command[2], hid, info);
}
else
{
get_Directories(cmd, hid, info);
}
}
else if (hid || info)
{
get_Directories(command[2], hid, info);
}
}
else if (cmd_len == 4)
{
if (!strcmp(command[1], "-a") || !strcmp(command[1], "-al") || !strcmp(command[1], "-la") || !strcmp(command[2], "-a"))
{
hid = 1;
}
if (!strcmp(command[1], "-l") || !strcmp(command[1], "-al") || !strcmp(command[1], "-la") || !strcmp(command[2], "-l"))
{
info = 1;
}
get_Directories(command[3], hid, info);
}
return 0;
}