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