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