-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive.c
More file actions
185 lines (147 loc) · 4.14 KB
/
recursive.c
File metadata and controls
185 lines (147 loc) · 4.14 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
/*
* recursive.c
* This file is part of jbtex2
*
* Copyright (C) 2012 - Giacomo Bergami <giacomo90@libero.it>
*
* jbtex2 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* jbtex2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with jbtex2; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "recursive.h"
/** recursiveDelete():
* Effettua l'eliminazione ricorsiva degli elementi all'interno della directory
* @param dirname: Percorso della cartella da svuotare ricorsivamente
*/
void recursiveDelete(char* dirname) {
char* tmp;
DIR *direct;
struct dirent *dir;
char *abs_filename;
asprintf(&tmp, "%s", dirname);
while ((tmp[strlen(tmp)-1]=='/')&&(strlen(tmp)))
tmp[strlen(tmp)-1] = '\0';
if (!strlen(tmp))
return;
printf("%s",tmp);
direct = opendir (tmp);
if (direct) {
while (dir = readdir (direct)) {
struct stat stt;
asprintf(&abs_filename, "%s/%s", tmp, dir->d_name);
stat(abs_filename, &stt);
if(S_ISDIR(stt.st_mode)) {
if(strcmp(dir->d_name, ".") &&
strcmp(dir->d_name, "..")) {
printf("Folder %s....\n", abs_filename);
recursiveDelete(abs_filename);
}
} else {
printf("Deleting %s....\n", abs_filename);
unlink(abs_filename);
}
free(abs_filename);
}
closedir(direct);
remove(tmp);
}
free(tmp);
}
/** check_extension():
* Checks the file extension
*/
char check_extension(char* file, char* end) {
if (strlen(end)>strlen(file)) return (char)0;
char toret;
int len = strlen(file)-strlen(end);
toret = (char)(strcmp(&file[len],end)==0);
if (toret) printf("%d %s %s\n", (int)toret, file, end);
return toret;
}
/** file_exist():
* Checks the file existance
* @param filename Given File name
* @param time Returns the creation time
* @param retbuf Eventually copies the file's stat
* @param pfd Eventually returns the file's File Descriptor, otherwise
* the fd will be closed
*/
char file_exists(char * filename, unsigned int *time, struct stat *retbuf, int *pfd)
{
int fd = open(filename, O_RDWR);
char test = (char)0;
if ((filename)&&(fd>-1)) {
struct stat buf;
char val;
fstat(fd, &buf);
if (time)
*time = (unsigned int)buf.st_ctime;
if (S_ISDIR(buf.st_mode)) test=(char)2;
else test=(char)1;
if (!pfd) close(fd); else *pfd = fd;
if (retbuf) *retbuf = buf;
return test;
}
return (char)0;
}
/** folder_empty():
* Checks if the given folder is empty
*/
char folder_empty(char *dirname)
{
int n = 0;
DIR *dir = opendir(dirname);
/* If the folder doesn't exists */
if (!(dir = opendir(dirname)))
return (char)-1;
/* Checks for . and .. */
while (readdir(dir)) {
if(++n > 2) break;
}
closedir(dir);
/* If there are only . and .., then return false */
return ((char)(n <= 2));
}
/** runcommand():
* Does the fork, executes the command, and waits for the child
*/
void runcommand(char* command[]) {
pid_t child;
int retnum;
if ((child=fork())>0) {
waitpid(child, &retnum, 0);
} else {
execv(command[0],command);
exit(0);
}
return;
}
/** do_argv():
* given a parametric string, returns an allocated array, where all the
* arguments are stored */
void do_argv(int n, ...)
{
va_list ap;
if (n>=ARG_MAX) n = ARG_MAX;
char** args=(char**)malloc(sizeof(char**)*n);
int i = 0, j= 0;
va_start(ap, n);
for (; n; n--)
args[i++] = va_arg(ap, char*);
va_end(ap);
args[i] = NULL;
runcommand(args);
free(args);
return;
}