-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
69 lines (66 loc) · 1.34 KB
/
utils.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
char* concat(const char *s1, const char *s2) {
char *ret = malloc(strlen(s1) + strlen(s2) + 1);
if (ret == NULL) {
perror("malloc");
return NULL;
}
strcpy(ret, s1);
strcat(ret, s2);
return ret;
}
int rm_r(const char *path) {
DIR *d = opendir(path);
size_t path_len = strlen(path);
if (d == NULL) {
perror("opendir");
return -1;
}
struct dirent *dir;
errno = 0;
while ((dir = readdir(d)) != NULL) {
if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) {
continue;
}
size_t len = path_len + strlen(dir->d_name) + 2;
char *buf = malloc(len);
if (buf == NULL) {
perror("malloc");
return -1;
}
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, dir->d_name);
if (!stat(buf, &statbuf)){
if (S_ISDIR(statbuf.st_mode)) {
if (rm_r(buf) < 0) {
return -1;
}
} else {
if (unlink(buf) < 0) {
perror("unlink");
return -1;
}
}
}
free(buf);
}
if (errno) {
perror("readdir");
return -1;
}
if (closedir(d) < 0) {
perror("closedir");
return -1;
}
if (rmdir(path) < 0) {
perror("rmdir");
return -1;
}
return 0;
}