misc/runlink.c

Go to the documentation of this file.
00001 // Copyright 2006 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (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
00015 // Free Software Foundation, Inc.,
00016 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 }

Generated on Sun Dec 3 17:45:53 2006 for lucid by  doxygen 1.5.1