rm_r.c revision 256281
1132956Smarkm/*-
2132956Smarkm * Copyright (C) 1996
3132956Smarkm *	David L. Nugent.  All rights reserved.
4132956Smarkm *
5132956Smarkm * Redistribution and use in source and binary forms, with or without
6132956Smarkm * modification, are permitted provided that the following conditions
7132956Smarkm * are met:
8132956Smarkm * 1. Redistributions of source code must retain the above copyright
9132956Smarkm *    notice, this list of conditions and the following disclaimer.
10132956Smarkm * 2. Redistributions in binary form must reproduce the above copyright
11132956Smarkm *    notice, this list of conditions and the following disclaimer in the
12132956Smarkm *    documentation and/or other materials provided with the distribution.
13132956Smarkm *
14132956Smarkm * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15132956Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16132956Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132956Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18132956Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132956Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20132956Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132956Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132956Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132956Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132956Smarkm * SUCH DAMAGE.
25132956Smarkm */
26132956Smarkm
27132956Smarkm#ifndef lint
28207329Sattiliostatic const char rcsid[] =
29207329Sattilio  "$FreeBSD: stable/10/usr.sbin/pw/rm_r.c 243897 2012-12-05 13:56:52Z eadler $";
30132956Smarkm#endif /* not lint */
31207329Sattilio
32207329Sattilio#include <stdio.h>
33207329Sattilio#include <stdlib.h>
34207329Sattilio#include <string.h>
35207329Sattilio#include <sys/types.h>
36207329Sattilio#include <sys/stat.h>
37207329Sattilio#include <sys/param.h>
38207329Sattilio#include <unistd.h>
39207329Sattilio#include <dirent.h>
40207329Sattilio
41207329Sattilio#include "pwupd.h"
42207329Sattilio
43207329Sattiliovoid
44207329Sattiliorm_r(char const * dir, uid_t uid)
45207329Sattilio{
46207329Sattilio	DIR            *d = opendir(dir);
47
48	if (d != NULL) {
49		struct dirent  *e;
50		struct stat     st;
51		char            file[MAXPATHLEN];
52
53		while ((e = readdir(d)) != NULL) {
54			if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) {
55				snprintf(file, sizeof(file), "%s/%s", dir, e->d_name);
56				if (lstat(file, &st) == 0) {	/* Need symlinks, not
57								 * linked file */
58					if (S_ISDIR(st.st_mode))	/* Directory - recurse */
59						rm_r(file, uid);
60					else {
61						if (S_ISLNK(st.st_mode) || st.st_uid == uid)
62							remove(file);
63					}
64				}
65			}
66		}
67		closedir(d);
68		if (lstat(dir, &st) == 0) {
69			if (S_ISLNK(st.st_mode))
70				remove(dir);
71			else if (st.st_uid == uid)
72				rmdir(dir);
73		}
74	}
75}
76