rm_r.c revision 20302
120253Sjoerg/*- 220302Sjoerg * Copyright (C) 1996 320302Sjoerg * David L. Nugent. All rights reserved. 420253Sjoerg * 520253Sjoerg * Redistribution and use in source and binary forms, with or without 620253Sjoerg * modification, are permitted provided that the following conditions 720253Sjoerg * are met: 820253Sjoerg * 1. Redistributions of source code must retain the above copyright 920302Sjoerg * notice, this list of conditions and the following disclaimer. 1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1120253Sjoerg * notice, this list of conditions and the following disclaimer in the 1220253Sjoerg * documentation and/or other materials provided with the distribution. 1320253Sjoerg * 1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1720302Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2420253Sjoerg * SUCH DAMAGE. 2520253Sjoerg * 2620302Sjoerg * $Id: rm_r.c,v 1.1.1.1 1996/12/09 14:05:35 joerg Exp $ 2720253Sjoerg */ 2820253Sjoerg 2920253Sjoerg#include <stdio.h> 3020253Sjoerg#include <stdlib.h> 3120253Sjoerg#include <sys/types.h> 3220253Sjoerg#include <sys/stat.h> 3320253Sjoerg#include <sys/param.h> 3420253Sjoerg#include <unistd.h> 3520253Sjoerg#include <dirent.h> 3620253Sjoerg 3720253Sjoerg#include "pwupd.h" 3820253Sjoerg 3920253Sjoergvoid 4020253Sjoergrm_r(char const * dir, uid_t uid) 4120253Sjoerg{ 4220253Sjoerg DIR *d = opendir(dir); 4320253Sjoerg 4420253Sjoerg if (d != NULL) { 4520253Sjoerg struct dirent *e; 4620253Sjoerg struct stat st; 4720253Sjoerg char file[MAXPATHLEN]; 4820253Sjoerg 4920253Sjoerg while ((e = readdir(d)) != NULL) { 5020253Sjoerg if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) { 5120253Sjoerg sprintf(file, "%s/%s", dir, e->d_name); 5220253Sjoerg if (lstat(file, &st) == 0) { /* Need symlinks, not 5320253Sjoerg * linked file */ 5420253Sjoerg if (S_ISDIR(st.st_mode)) /* Directory - recurse */ 5520253Sjoerg rm_r(file, uid); 5620253Sjoerg else { 5720253Sjoerg if (S_ISLNK(st.st_mode) || st.st_uid == uid) 5820253Sjoerg remove(file); 5920253Sjoerg } 6020253Sjoerg } 6120253Sjoerg } 6220253Sjoerg } 6320253Sjoerg closedir(d); 6420253Sjoerg if (lstat(dir, &st) == 0) { 6520253Sjoerg if (S_ISLNK(st.st_mode)) 6620253Sjoerg remove(dir); 6720253Sjoerg else if (st.st_uid == uid) 6820253Sjoerg rmdir(dir); 6920253Sjoerg } 7020253Sjoerg } 7120253Sjoerg} 72