]> git.cworth.org Git - tar/blob - gnu/rmdir.c
5b1c09554a207b3d300385d799ee96f81015c350
[tar] / gnu / rmdir.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Work around rmdir bugs.
4
5    Copyright (C) 1988, 1990, 1999, 2003-2006, 2009-2010 Free Software
6    Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <config.h>
22
23 #include <unistd.h>
24
25 #include <errno.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #undef rmdir
31
32 /* Remove directory DIR.
33    Return 0 if successful, -1 if not.  */
34
35 int
36 rpl_rmdir (char const *dir)
37 {
38 #if HAVE_RMDIR
39   /* Work around cygwin 1.5.x bug where rmdir("dir/./") succeeds.  */
40   size_t len = strlen (dir);
41   int result;
42   while (len && ISSLASH (dir[len - 1]))
43     len--;
44   if (len && dir[len - 1] == '.' && (1 == len || ISSLASH (dir[len - 2])))
45     {
46       errno = EINVAL;
47       return -1;
48     }
49   result = rmdir (dir);
50   /* Work around mingw bug, where rmdir("file/") fails with EINVAL
51      instead of ENOTDIR.  We've already filtered out trailing ., the
52      only reason allowed by POSIX for EINVAL.  */
53   if (result == -1 && errno == EINVAL)
54     errno = ENOTDIR;
55   return result;
56
57 #else /* !HAVE_RMDIR */
58   /* rmdir adapted from GNU tar.  FIXME: Delete this implementation in
59      2010 if no one reports a system with missing rmdir.  */
60   pid_t cpid;
61   int status;
62   struct stat statbuf;
63
64   if (stat (dir, &statbuf) != 0)
65     return -1;                  /* errno already set */
66
67   if (!S_ISDIR (statbuf.st_mode))
68     {
69       errno = ENOTDIR;
70       return -1;
71     }
72
73   cpid = fork ();
74   switch (cpid)
75     {
76     case -1:                    /* cannot fork */
77       return -1;                /* errno already set */
78
79     case 0:                     /* child process */
80       execl ("/bin/rmdir", "rmdir", dir, (char *) 0);
81       _exit (1);
82
83     default:                    /* parent process */
84
85       /* Wait for kid to finish.  */
86
87       while (wait (&status) != cpid)
88         /* Do nothing.  */ ;
89
90       if (status)
91         {
92
93           /* /bin/rmdir failed.  */
94
95           errno = EIO;
96           return -1;
97         }
98       return 0;
99     }
100 #endif /* !HAVE_RMDIR */
101 }