misc/runlink.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
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 }

Generated on Tue Jun 19 20:38:26 2007 for lucid by  doxygen 1.5.2