00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <unistd.h>
00018 #include <errno.h>
00019 #include <dirent.h>
00020 #include <sys/stat.h>
00021
00022 #include "mem.h"
00023 #include "misc.h"
00024 #include "printf.h"
00025
00026 int runlink(const char *path)
00027 {
00028 struct stat sb;
00029
00030 DIR *dp;
00031 struct dirent *d;
00032
00033 int status = 0;
00034 char *p, *new_path;
00035
00036 if (lstat(path, &sb) == -1) {
00037 if (errno == ENOENT)
00038 return 0;
00039 else
00040 return -1;
00041 }
00042
00043 if (S_ISDIR(sb.st_mode)) {
00044 if (!(dp = opendir(path)))
00045 return -1;
00046
00047 while ((d = readdir(dp))) {
00048 p = d->d_name;
00049
00050 if (p && p[0] == '.' && (!p[1] || (p[1] == '.' && !p[2])))
00051 continue;
00052
00053 _lucid_asprintf(&new_path, "%s/%s", path, d->d_name);
00054
00055 if (runlink(new_path) == -1)
00056 status = -1;
00057
00058 mem_free(new_path);
00059 }
00060
00061 if (closedir(dp) == -1)
00062 return -1;
00063
00064 if (rmdir(path) == -1)
00065 return -1;
00066
00067 return status;
00068 }
00069
00070 if (unlink(path) == -1)
00071 return -1;
00072
00073 return 0;
00074 }