-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmrubbish.c
125 lines (115 loc) · 2.43 KB
/
rmrubbish.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
/*
gcc -c -Wall -Wshadow rmrubbish.c
gcc -o 8. rmrubbish rmrubbish.o
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <err.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h> /* PATH_MAX */
#define FORMAT ".rubbish"
int
static is_dir(char *d_name){
struct stat st;
stat(d_name, &st);
return ((st.st_mode & S_IFMT)==S_IFDIR);
}
int
static is_file(char *d_name){
struct stat st;
stat(d_name, &st);
return ((st.st_mode & S_IFMT)==S_IFREG);
}
/*Retorna 0 si no es .rubbish, retorna 1 sí es .rubbish*/
int
get_format(char *name){
int len = 0;
char *rubbish;
rubbish = strstr(name, FORMAT);
if(rubbish)
len = strlen(rubbish);
return ((rubbish) && (len == 8));
}
char *
built_path(char *s1, char *path){
char * aux;
int n1, n2;
n1 = strlen(s1);
n2 = strlen(path);
aux = malloc (n1+n2+2);
sprintf(aux, "%s/%s", path, s1);
return aux;
}
void
search_in_dir(char* dir, int pid){
DIR *d = opendir(dir);
struct dirent *de;
int counter=0;
if(d == NULL) {
err(1,"%s" ,dir);
}
char *buf =malloc(sizeof (PATH_MAX));
while((de = readdir(d)) != NULL) {
buf= built_path(de->d_name,dir);
if (is_file(buf)){
if(get_format(de->d_name)==1){
if (unlink(buf)<0){
printf("%d : error removing %s\n", pid ,de->d_name);
}else {
counter++;
printf("%d : %s ok \n",pid ,de->d_name );
}
}
}else{
if(is_dir(buf)){
if((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..")!=0)){
search_in_dir(buf,pid);
}
}else{
printf("\t%s It in not a valid file or directory\n", buf);
}
if ((counter==0) && (strcmp(de->d_name, ".") == 0))
printf("%d : no files to remove in :%s\n", pid ,dir);
}
}
free(buf);
closedir(d);
}
int
main(int argc,char*argv[])
{
int i,sts,pid,np;
np=0;//numero de procesos creados//
if (argc >= 1){
for (i = 1; i < argc ; i++){
printf("%d\n", i);
pid=fork();
switch(pid){
case -1:
err(1, "fork failed");
break;
case 0: // esto es el hijo
search_in_dir(argv[i], getpid());
break;
default:
while(wait(&sts) != pid){
if (sts == 1){
printf("%d\n",sts );
np++;//Sumo porque ha ido bien todo//
}
}
}
}
if (np == (argc))
err(1,"All processes were successful ");
if (np!=(argc))
err(1,"%d processes failed dsvfx",(argc-np));
}
exit(0);
}