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