1d6f907dcSJoerg Wunsch /*- 2d6f907dcSJoerg Wunsch * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>. 3d6f907dcSJoerg Wunsch * All rights reserved. 4d6f907dcSJoerg Wunsch * 5d6f907dcSJoerg Wunsch * Redistribution and use in source and binary forms, with or without 6d6f907dcSJoerg Wunsch * modification, are permitted provided that the following conditions 7d6f907dcSJoerg Wunsch * are met: 8d6f907dcSJoerg Wunsch * 1. Redistributions of source code must retain the above copyright 9d6f907dcSJoerg Wunsch * notice, this list of conditions and the following disclaimer as 10d6f907dcSJoerg Wunsch * the first lines of this file unmodified. 11d6f907dcSJoerg Wunsch * 2. Redistributions in binary form must reproduce the above copyright 12d6f907dcSJoerg Wunsch * notice, this list of conditions and the following disclaimer in the 13d6f907dcSJoerg Wunsch * documentation and/or other materials provided with the distribution. 14d6f907dcSJoerg Wunsch * 3. All advertising materials mentioning features or use of this software 15d6f907dcSJoerg Wunsch * must display the following acknowledgement: 16d6f907dcSJoerg Wunsch * This product includes software developed by David L. Nugent. 17d6f907dcSJoerg Wunsch * 4. The name of the author may not be used to endorse or promote products 18d6f907dcSJoerg Wunsch * derived from this software without specific prior written permission. 19d6f907dcSJoerg Wunsch * 20d6f907dcSJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND 21d6f907dcSJoerg Wunsch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22d6f907dcSJoerg Wunsch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23d6f907dcSJoerg Wunsch * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE 24d6f907dcSJoerg Wunsch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25d6f907dcSJoerg Wunsch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26d6f907dcSJoerg Wunsch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27d6f907dcSJoerg Wunsch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28d6f907dcSJoerg Wunsch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29d6f907dcSJoerg Wunsch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30d6f907dcSJoerg Wunsch * SUCH DAMAGE. 31d6f907dcSJoerg Wunsch * 32d6f907dcSJoerg Wunsch * $Id$ 33d6f907dcSJoerg Wunsch */ 34d6f907dcSJoerg Wunsch 35d6f907dcSJoerg Wunsch #include <stdio.h> 36d6f907dcSJoerg Wunsch #include <stdlib.h> 37d6f907dcSJoerg Wunsch #include <sys/types.h> 38d6f907dcSJoerg Wunsch #include <sys/stat.h> 39d6f907dcSJoerg Wunsch #include <sys/param.h> 40d6f907dcSJoerg Wunsch #include <unistd.h> 41d6f907dcSJoerg Wunsch #include <dirent.h> 42d6f907dcSJoerg Wunsch 43d6f907dcSJoerg Wunsch #include "pwupd.h" 44d6f907dcSJoerg Wunsch 45d6f907dcSJoerg Wunsch void 46d6f907dcSJoerg Wunsch rm_r(char const * dir, uid_t uid) 47d6f907dcSJoerg Wunsch { 48d6f907dcSJoerg Wunsch DIR *d = opendir(dir); 49d6f907dcSJoerg Wunsch 50d6f907dcSJoerg Wunsch if (d != NULL) { 51d6f907dcSJoerg Wunsch struct dirent *e; 52d6f907dcSJoerg Wunsch struct stat st; 53d6f907dcSJoerg Wunsch char file[MAXPATHLEN]; 54d6f907dcSJoerg Wunsch 55d6f907dcSJoerg Wunsch while ((e = readdir(d)) != NULL) { 56d6f907dcSJoerg Wunsch if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) { 57d6f907dcSJoerg Wunsch sprintf(file, "%s/%s", dir, e->d_name); 58d6f907dcSJoerg Wunsch if (lstat(file, &st) == 0) { /* Need symlinks, not 59d6f907dcSJoerg Wunsch * linked file */ 60d6f907dcSJoerg Wunsch if (S_ISDIR(st.st_mode)) /* Directory - recurse */ 61d6f907dcSJoerg Wunsch rm_r(file, uid); 62d6f907dcSJoerg Wunsch else { 63d6f907dcSJoerg Wunsch if (S_ISLNK(st.st_mode) || st.st_uid == uid) 64d6f907dcSJoerg Wunsch remove(file); 65d6f907dcSJoerg Wunsch } 66d6f907dcSJoerg Wunsch } 67d6f907dcSJoerg Wunsch } 68d6f907dcSJoerg Wunsch } 69d6f907dcSJoerg Wunsch closedir(d); 70d6f907dcSJoerg Wunsch if (lstat(dir, &st) == 0) { 71d6f907dcSJoerg Wunsch if (S_ISLNK(st.st_mode)) 72d6f907dcSJoerg Wunsch remove(dir); 73d6f907dcSJoerg Wunsch else if (st.st_uid == uid) 74d6f907dcSJoerg Wunsch rmdir(dir); 75d6f907dcSJoerg Wunsch } 76d6f907dcSJoerg Wunsch } 77d6f907dcSJoerg Wunsch } 78