summaryrefslogtreecommitdiff
path: root/lib/remove.c
blob: d604c8304f5b10ea03376840292687294be862e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Remove a file or directory.
   Copyright (C) 2009-2023 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* written by Eric Blake */

#include <config.h>

#include <stdio.h>

#include <errno.h>
#include <unistd.h>

#undef remove

/* Remove NAME from the file system.  This works around C89 platforms
   that don't handle directories like POSIX requires; it also works
   around Solaris 9 bugs with trailing slash.  */
int
rpl_remove (char const *name)
{
  /* It is faster to just try rmdir, and fall back on unlink, than it
     is to use lstat to see what we are about to remove.  Technically,
     it is more likely that we want unlink, not rmdir, but we cannot
     guarantee the safety of unlink on directories.  Trailing slash
     bugs are handled by our rmdir and unlink wrappers.  */
  int result = rmdir (name);
  if (result && errno == ENOTDIR)
    result = unlink (name);
  return result;
}