]> git.cworth.org Git - tar/blob - lib/rmdir.c
Imported Upstream version 1.21
[tar] / lib / rmdir.c
1 /* BSD compatible remove directory function for System V
2
3    Copyright (C) 1988, 1990, 1999, 2003, 2004, 2005, 2006 Free
4    Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24
25 /* rmdir adapted from GNU tar.  */
26
27 /* Remove directory DIR.
28    Return 0 if successful, -1 if not.  */
29
30 int
31 rmdir (char const *dir)
32 {
33   pid_t cpid;
34   int status;
35   struct stat statbuf;
36
37   if (stat (dir, &statbuf) != 0)
38     return -1;                  /* errno already set */
39
40   if (!S_ISDIR (statbuf.st_mode))
41     {
42       errno = ENOTDIR;
43       return -1;
44     }
45
46   cpid = fork ();
47   switch (cpid)
48     {
49     case -1:                    /* cannot fork */
50       return -1;                /* errno already set */
51
52     case 0:                     /* child process */
53       execl ("/bin/rmdir", "rmdir", dir, (char *) 0);
54       _exit (1);
55
56     default:                    /* parent process */
57
58       /* Wait for kid to finish.  */
59
60       while (wait (&status) != cpid)
61         /* Do nothing.  */ ;
62
63       if (status)
64         {
65
66           /* /bin/rmdir failed.  */
67
68           errno = EIO;
69           return -1;
70         }
71       return 0;
72     }
73 }