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