-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistrem.c
More file actions
153 lines (146 loc) · 3.21 KB
/
listrem.c
File metadata and controls
153 lines (146 loc) · 3.21 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <termios.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/*
Gets the path of the file and returns whether the file is regular
path: string of the path
returns 0 if success, -1 otherwise
*/
int is_regular_file(const char *path){
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
/*
returns a list of the files and folders in a directory
paramaters: a - includes files and folders with '.'
m - puts commas between each directory
Q - puts quotation marks around each directory
*/
void list(int argc, char* argv[]){
int a = 0;
int m = 0;
int Q = 0;
char ch;
while((ch = getopt(argc, argv, "amQ")) != EOF){
switch(ch) {
case 'a':
a = 1;
continue;
case 'm':
m = 1;
continue;
case 'Q':
Q = 1;
continue;
default:
printf("Not a valid argument.");
}
argc -= optind;
argv += optind;
}
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("\033[0m");
if(dir->d_name[0] != '.' || a){
if(dir->d_type == 8){
if(Q){printf("\"");}
printf("%s", dir->d_name);
if(Q){printf("\"");}
if(m){printf(",");}
printf(" ");
}
}
}
rewinddir(d);
while ((dir = readdir(d)) != NULL)
{
printf("\033[1;34m");
if(dir->d_name[0] != '.' || a){
if(dir->d_type == 4){
if(Q){printf("\"");}
printf("%s", dir->d_name);
if(Q){printf("\"");}
if(m){printf(",");}
printf(" ");
}
}
}
printf("\n");
closedir(d);
}
}
/*
removes the file specified
parameters: r - removes directories as well as files.
*/
int removeFile(int argc, char* argv[]){
int r = 0;
char ch;
while((ch = getopt(argc, argv, "r")) != EOF){
switch(ch) {
case 'r':
r = 1;
break;
default:
printf("Those aren't valid arguments. \n");
}
argc -= optind;
argv += optind;
}
printf("%s\n", argv[argc-1]);
DIR *d;
struct dirent *dir;
char* array[2];
int status;
char directory[strlen(argv[argc-1])+3];
strcpy(directory,"./");
strcat(directory,argv[argc-1]);
if(is_regular_file(argv[argc-1])){
status = remove(argv[argc-1]);
printf("%d\n", status);
if(status){
printf("rm: cannot remove '%s': No such file\n",argv[argc-1]);
}
}
else{
if(r){
d = opendir(directory);
while ((dir = readdir(d)) != NULL){
if(dir->d_name[0] != '.'){
char nextDir[1+strlen(argv[argc-1])+strlen(dir->d_name)];
strcpy(nextDir,argv[argc-1]);
strcat(nextDir,"/");
strcat(nextDir,dir->d_name);
array[0] = "./a.out";
array[1] = "-r";
array[2] = nextDir;
removeFile(3,array);
}
}
status = remove(argv[argc-1]);
}
else{
status = remove(argv[argc-1]);
if(status){
printf("rm: cannot remove '%s': Is a directory\n",argv[argc-1]);
}
}
}
return 0;
}