1*fae548d3Szrj /* unlink-if-ordinary.c - remove link to a file unless it is special
2*fae548d3Szrj Copyright (C) 2004-2020 Free Software Foundation, Inc.
3*fae548d3Szrj
4*fae548d3Szrj This file is part of the libiberty library. This library is free
5*fae548d3Szrj software; you can redistribute it and/or modify it under the
6*fae548d3Szrj terms of the GNU General Public License as published by the
7*fae548d3Szrj Free Software Foundation; either version 2, or (at your option)
8*fae548d3Szrj any later version.
9*fae548d3Szrj
10*fae548d3Szrj This library is distributed in the hope that it will be useful,
11*fae548d3Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
12*fae548d3Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*fae548d3Szrj GNU General Public License for more details.
14*fae548d3Szrj
15*fae548d3Szrj You should have received a copy of the GNU General Public License
16*fae548d3Szrj along with GNU CC; see the file COPYING. If not, write to
17*fae548d3Szrj the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18*fae548d3Szrj
19*fae548d3Szrj As a special exception, if you link this library with files
20*fae548d3Szrj compiled with a GNU compiler to produce an executable, this does not cause
21*fae548d3Szrj the resulting executable to be covered by the GNU General Public License.
22*fae548d3Szrj This exception does not however invalidate any other reasons why
23*fae548d3Szrj the executable file might be covered by the GNU General Public License. */
24*fae548d3Szrj
25*fae548d3Szrj /*
26*fae548d3Szrj
27*fae548d3Szrj @deftypefn Supplemental int unlink_if_ordinary (const char*)
28*fae548d3Szrj
29*fae548d3Szrj Unlinks the named file, unless it is special (e.g. a device file).
30*fae548d3Szrj Returns 0 when the file was unlinked, a negative value (and errno set) when
31*fae548d3Szrj there was an error deleting the file, and a positive value if no attempt
32*fae548d3Szrj was made to unlink the file because it is special.
33*fae548d3Szrj
34*fae548d3Szrj @end deftypefn
35*fae548d3Szrj
36*fae548d3Szrj */
37*fae548d3Szrj
38*fae548d3Szrj #ifdef HAVE_CONFIG_H
39*fae548d3Szrj #include "config.h"
40*fae548d3Szrj #endif
41*fae548d3Szrj
42*fae548d3Szrj #include <sys/types.h>
43*fae548d3Szrj
44*fae548d3Szrj #ifdef HAVE_UNISTD_H
45*fae548d3Szrj #include <unistd.h>
46*fae548d3Szrj #endif
47*fae548d3Szrj #if HAVE_SYS_STAT_H
48*fae548d3Szrj #include <sys/stat.h>
49*fae548d3Szrj #endif
50*fae548d3Szrj
51*fae548d3Szrj #include "libiberty.h"
52*fae548d3Szrj
53*fae548d3Szrj #ifndef S_ISLNK
54*fae548d3Szrj #ifdef S_IFLNK
55*fae548d3Szrj #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
56*fae548d3Szrj #else
57*fae548d3Szrj #define S_ISLNK(m) 0
58*fae548d3Szrj #define lstat stat
59*fae548d3Szrj #endif
60*fae548d3Szrj #endif
61*fae548d3Szrj
62*fae548d3Szrj int
unlink_if_ordinary(const char * name)63*fae548d3Szrj unlink_if_ordinary (const char *name)
64*fae548d3Szrj {
65*fae548d3Szrj struct stat st;
66*fae548d3Szrj
67*fae548d3Szrj if (lstat (name, &st) == 0
68*fae548d3Szrj && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode)))
69*fae548d3Szrj return unlink (name);
70*fae548d3Szrj
71*fae548d3Szrj return 1;
72*fae548d3Szrj }
73