2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 /**********************************************************
35 * OS Testing - Silicon Graphics, Inc.
37 * FUNCTION NAME : rmobj()
39 * FUNCTION TITLE : Remove an object
42 * int rmobj(char *obj, char **errmsg)
44 * AUTHOR : Kent Rogers
46 * INITIAL RELEASE : UNICOS 7.0
49 * This routine will remove the specified object. If the specified
50 * object is a directory, it will recursively remove the directory
51 * and everything underneath it. It assumes that it has privilege
52 * to remove everything that it tries to remove. If rmobj() encounters
53 * any problems, and errmsg is not NULL, errmsg is set to point to a
54 * string explaining the error.
56 * DETAILED DESCRIPTION
57 * Allocate space for the directory and its contents
58 * Open the directory to get access to what is in it
59 * Loop through the objects in the directory:
60 * If the object is not "." or "..":
61 * Determine the file type by calling lstat()
62 * If the object is not a directory:
63 * Remove the object with unlink()
65 * Call rmobj(object) to remove the object's contents
66 * Determine the link count on object by calling lstat()
67 * If the link count >= 3:
68 * Remove the directory with unlink()
70 * Remove the directory with rmdir()
71 * Close the directory and free the pointers
74 * If there are any problems, rmobj() will set errmsg (if it was not
75 * NULL) and return -1. Otherwise it will return 0.
77 ************************************************************/
78 #include <errno.h> /* for errno */
79 #include <stdio.h> /* for NULL */
80 #include <stdlib.h> /* for malloc() */
81 #include <string.h> /* for string function */
82 #include <limits.h> /* for PATH_MAX */
83 #include <sys/types.h> /* for opendir(), readdir(), closedir(), stat() */
84 #include <sys/stat.h> /* for [l]stat() */
85 #include <dirent.h> /* for opendir(), readdir(), closedir() */
86 #include <unistd.h> /* for rmdir(), unlink() */
89 #define SYSERR strerror(errno)
92 rmobj(char *obj
, char **errmsg
)
94 int ret_val
= 0; /* return value from this routine */
95 DIR *dir
; /* pointer to a directory */
96 struct dirent
*dir_ent
; /* pointer to directory entries */
97 char dirobj
[PATH_MAX
]; /* object inside directory to modify */
98 struct stat statbuf
; /* used to hold stat information */
99 static char err_msg
[1024]; /* error message */
101 /* Determine the file type */
102 if ( lstat(obj
, &statbuf
) < 0 ) {
103 if ( errmsg
!= NULL
) {
104 sprintf(err_msg
, "lstat(%s) failed; errno=%d: %s",
111 /* Take appropriate action, depending on the file type */
112 if ( (statbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
113 /* object is a directory */
115 /* Do NOT perform the request if the directory is "/" */
116 if ( !strcmp(obj
, "/") ) {
117 if ( errmsg
!= NULL
) {
118 sprintf(err_msg
, "Cannot remove /");
124 /* Open the directory to get access to what is in it */
125 if ( (dir
= opendir(obj
)) == NULL
) {
126 if ( rmdir(obj
) != 0 ) {
127 if ( errmsg
!= NULL
) {
128 sprintf(err_msg
, "rmdir(%s) failed; errno=%d: %s",
138 /* Loop through the entries in the directory, removing each one */
139 for ( dir_ent
= (struct dirent
*)readdir(dir
);
141 dir_ent
= (struct dirent
*)readdir(dir
)) {
143 /* Don't remove "." or ".." */
144 if ( !strcmp(dir_ent
->d_name
, ".") || !strcmp(dir_ent
->d_name
, "..") )
147 /* Recursively call this routine to remove the current entry */
148 sprintf(dirobj
, "%s/%s", obj
, dir_ent
->d_name
);
149 if ( rmobj(dirobj
, errmsg
) != 0 )
153 /* Close the directory */
156 /* If there were problems removing an entry, don't attempt to
157 remove the directory itself */
161 /* Get the link count, now that all the entries have been removed */
162 if ( lstat(obj
, &statbuf
) < 0 ) {
163 if ( errmsg
!= NULL
) {
164 sprintf(err_msg
, "lstat(%s) failed; errno=%d: %s",
171 /* Remove the directory itself */
172 if ( statbuf
.st_nlink
>= 3 ) {
173 /* The directory is linked; unlink() must be used */
174 if ( unlink(obj
) < 0 ) {
175 if ( errmsg
!= NULL
) {
176 sprintf(err_msg
, "unlink(%s) failed; errno=%d: %s",
183 /* The directory is not linked; rmdir() can be used */
184 if ( rmdir(obj
) < 0 ) {
185 if ( errmsg
!= NULL
) {
186 sprintf(err_msg
, "remove(%s) failed; errno=%d: %s",
194 /* object is not a directory; just use unlink() */
195 if ( unlink(obj
) < 0 ) {
196 if ( errmsg
!= NULL
) {
197 sprintf(err_msg
, "unlink(%s) failed; errno=%d: %s",
203 } /* if obj is a directory */
206 * Everything must have went ok.