rmdir.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Work around rmdir bugs.
  2. Copyright (C) 1988, 1990, 1999, 2003-2006, 2009-2013 Free Software
  3. Foundation, Inc.
  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
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include "dosname.h"
  19. #undef rmdir
  20. /* Remove directory DIR.
  21. Return 0 if successful, -1 if not. */
  22. int
  23. rpl_rmdir (char const *dir)
  24. {
  25. /* Work around cygwin 1.5.x bug where rmdir("dir/./") succeeds. */
  26. size_t len = strlen (dir);
  27. int result;
  28. while (len && ISSLASH (dir[len - 1]))
  29. len--;
  30. if (len && dir[len - 1] == '.' && (1 == len || ISSLASH (dir[len - 2])))
  31. {
  32. errno = EINVAL;
  33. return -1;
  34. }
  35. result = rmdir (dir);
  36. /* Work around mingw bug, where rmdir("file/") fails with EINVAL
  37. instead of ENOTDIR. We've already filtered out trailing ., the
  38. only reason allowed by POSIX for EINVAL. */
  39. if (result == -1 && errno == EINVAL)
  40. errno = ENOTDIR;
  41. return result;
  42. }