186d7f5d3SJohn Marino /* unlink-if-ordinary.c - remove link to a file unless it is special
286d7f5d3SJohn Marino Copyright (C) 2004, 2005 Free Software Foundation, Inc.
386d7f5d3SJohn Marino
486d7f5d3SJohn Marino This file is part of the libiberty library. This library is free
586d7f5d3SJohn Marino software; you can redistribute it and/or modify it under the
686d7f5d3SJohn Marino terms of the GNU General Public License as published by the
786d7f5d3SJohn Marino Free Software Foundation; either version 2, or (at your option)
886d7f5d3SJohn Marino any later version.
986d7f5d3SJohn Marino
1086d7f5d3SJohn Marino This library is distributed in the hope that it will be useful,
1186d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
1286d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1386d7f5d3SJohn Marino GNU General Public License for more details.
1486d7f5d3SJohn Marino
1586d7f5d3SJohn Marino You should have received a copy of the GNU General Public License
1686d7f5d3SJohn Marino along with GNU CC; see the file COPYING. If not, write to
1786d7f5d3SJohn Marino the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1886d7f5d3SJohn Marino
1986d7f5d3SJohn Marino As a special exception, if you link this library with files
2086d7f5d3SJohn Marino compiled with a GNU compiler to produce an executable, this does not cause
2186d7f5d3SJohn Marino the resulting executable to be covered by the GNU General Public License.
2286d7f5d3SJohn Marino This exception does not however invalidate any other reasons why
2386d7f5d3SJohn Marino the executable file might be covered by the GNU General Public License. */
2486d7f5d3SJohn Marino
2586d7f5d3SJohn Marino /*
2686d7f5d3SJohn Marino
2786d7f5d3SJohn Marino @deftypefn Supplemental int unlink_if_ordinary (const char*)
2886d7f5d3SJohn Marino
2986d7f5d3SJohn Marino Unlinks the named file, unless it is special (e.g. a device file).
3086d7f5d3SJohn Marino Returns 0 when the file was unlinked, a negative value (and errno set) when
3186d7f5d3SJohn Marino there was an error deleting the file, and a positive value if no attempt
3286d7f5d3SJohn Marino was made to unlink the file because it is special.
3386d7f5d3SJohn Marino
3486d7f5d3SJohn Marino @end deftypefn
3586d7f5d3SJohn Marino
3686d7f5d3SJohn Marino */
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
3986d7f5d3SJohn Marino #include "config.h"
4086d7f5d3SJohn Marino #endif
4186d7f5d3SJohn Marino
4286d7f5d3SJohn Marino #include <sys/types.h>
4386d7f5d3SJohn Marino
4486d7f5d3SJohn Marino #ifdef HAVE_UNISTD_H
4586d7f5d3SJohn Marino #include <unistd.h>
4686d7f5d3SJohn Marino #endif
4786d7f5d3SJohn Marino #if HAVE_SYS_STAT_H
4886d7f5d3SJohn Marino #include <sys/stat.h>
4986d7f5d3SJohn Marino #endif
5086d7f5d3SJohn Marino
5186d7f5d3SJohn Marino #include "libiberty.h"
5286d7f5d3SJohn Marino
5386d7f5d3SJohn Marino #ifndef S_ISLNK
5486d7f5d3SJohn Marino #ifdef S_IFLNK
5586d7f5d3SJohn Marino #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
5686d7f5d3SJohn Marino #else
5786d7f5d3SJohn Marino #define S_ISLNK(m) 0
5886d7f5d3SJohn Marino #define lstat stat
5986d7f5d3SJohn Marino #endif
6086d7f5d3SJohn Marino #endif
6186d7f5d3SJohn Marino
6286d7f5d3SJohn Marino int
unlink_if_ordinary(const char * name)6386d7f5d3SJohn Marino unlink_if_ordinary (const char *name)
6486d7f5d3SJohn Marino {
6586d7f5d3SJohn Marino struct stat st;
6686d7f5d3SJohn Marino
6786d7f5d3SJohn Marino if (lstat (name, &st) == 0
6886d7f5d3SJohn Marino && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode)))
6986d7f5d3SJohn Marino return unlink (name);
7086d7f5d3SJohn Marino
7186d7f5d3SJohn Marino return 1;
7286d7f5d3SJohn Marino }
73