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