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