1408
|
1 /* rmdir.c -- BSD compatible remove directory function for System V |
|
2 Copyright (C) 1988, 1990 Free Software Foundation, Inc. |
|
3 |
|
4 This program is free software; you can redistribute it and/or modify |
|
5 it under the terms of the GNU General Public License as published by |
7016
|
6 the Free Software Foundation; either version 2 of the License, or |
|
7 (at your option) any later version. |
1408
|
8 |
|
9 This program is distributed in the hope that it will be useful, |
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 GNU General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU General Public License |
|
15 along with this program; if not, write to the Free Software |
5307
|
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
17 02110-1301 USA. */ |
1408
|
18 |
|
19 #ifdef HAVE_CONFIG_H |
|
20 #include <config.h> |
|
21 #endif |
|
22 |
|
23 #ifndef HAVE_RMDIR |
|
24 |
2443
|
25 #ifdef HAVE_SYS_TYPES_H |
1408
|
26 #include <sys/types.h> |
2443
|
27 #endif |
1408
|
28 #include <sys/stat.h> |
|
29 |
|
30 #include <errno.h> |
|
31 #ifndef errno |
|
32 extern int errno; |
|
33 #endif |
|
34 |
|
35 #ifdef STAT_MACROS_BROKEN |
|
36 #undef S_ISDIR |
|
37 #endif |
|
38 |
|
39 #if !defined(S_ISDIR) && defined(S_IFDIR) |
|
40 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
|
41 #endif |
|
42 |
|
43 #include "safe-stat.h" |
|
44 |
|
45 /* rmdir adapted from GNU tar. */ |
|
46 |
|
47 /* Remove directory DPATH. |
|
48 Return 0 if successful, -1 if not. */ |
|
49 |
|
50 int |
|
51 rmdir (dpath) |
|
52 char *dpath; |
|
53 { |
|
54 int cpid, status; |
|
55 struct stat statbuf; |
|
56 |
|
57 if (SAFE_STAT (dpath, &statbuf) != 0) |
|
58 return -1; /* errno already set */ |
|
59 |
|
60 if (!S_ISDIR (statbuf.st_mode)) |
|
61 { |
|
62 errno = ENOTDIR; |
|
63 return -1; |
|
64 } |
|
65 |
|
66 cpid = fork (); |
|
67 switch (cpid) |
|
68 { |
|
69 case -1: /* cannot fork */ |
|
70 return -1; /* errno already set */ |
|
71 |
|
72 case 0: /* child process */ |
|
73 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0); |
|
74 _exit (1); |
|
75 |
|
76 default: /* parent process */ |
|
77 |
|
78 /* Wait for kid to finish. */ |
|
79 |
|
80 while (wait (&status) != cpid) |
|
81 /* Do nothing. */ ; |
|
82 |
|
83 if (status & 0xFFFF) |
|
84 { |
|
85 |
|
86 /* /bin/rmdir failed. */ |
|
87 |
|
88 errno = EIO; |
|
89 return -1; |
|
90 } |
|
91 return 0; |
|
92 } |
|
93 } |
|
94 |
|
95 #endif |