xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/unlink-if-ordinary.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
116dce513Schristos /* unlink-if-ordinary.c - remove link to a file unless it is special
2*e992f068Schristos    Copyright (C) 2004-2022 Free Software Foundation, Inc.
316dce513Schristos 
416dce513Schristos This file is part of the libiberty library.  This library is free
516dce513Schristos software; you can redistribute it and/or modify it under the
616dce513Schristos terms of the GNU General Public License as published by the
716dce513Schristos Free Software Foundation; either version 2, or (at your option)
816dce513Schristos any later version.
916dce513Schristos 
1016dce513Schristos This library is distributed in the hope that it will be useful,
1116dce513Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1216dce513Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1316dce513Schristos GNU General Public License for more details.
1416dce513Schristos 
1516dce513Schristos You should have received a copy of the GNU General Public License
1616dce513Schristos along with GNU CC; see the file COPYING.  If not, write to
1716dce513Schristos the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1816dce513Schristos 
1916dce513Schristos As a special exception, if you link this library with files
2016dce513Schristos compiled with a GNU compiler to produce an executable, this does not cause
2116dce513Schristos the resulting executable to be covered by the GNU General Public License.
2216dce513Schristos This exception does not however invalidate any other reasons why
2316dce513Schristos the executable file might be covered by the GNU General Public License. */
2416dce513Schristos 
2516dce513Schristos /*
2616dce513Schristos 
2716dce513Schristos @deftypefn Supplemental int unlink_if_ordinary (const char*)
2816dce513Schristos 
2916dce513Schristos Unlinks the named file, unless it is special (e.g. a device file).
3016dce513Schristos Returns 0 when the file was unlinked, a negative value (and errno set) when
3116dce513Schristos there was an error deleting the file, and a positive value if no attempt
3216dce513Schristos was made to unlink the file because it is special.
3316dce513Schristos 
3416dce513Schristos @end deftypefn
3516dce513Schristos 
3616dce513Schristos */
3716dce513Schristos 
3816dce513Schristos #ifdef HAVE_CONFIG_H
3916dce513Schristos #include "config.h"
4016dce513Schristos #endif
4116dce513Schristos 
4216dce513Schristos #include <sys/types.h>
4316dce513Schristos 
4416dce513Schristos #ifdef HAVE_UNISTD_H
4516dce513Schristos #include <unistd.h>
4616dce513Schristos #endif
4716dce513Schristos #if HAVE_SYS_STAT_H
4816dce513Schristos #include <sys/stat.h>
4916dce513Schristos #endif
5016dce513Schristos 
5116dce513Schristos #include "libiberty.h"
5216dce513Schristos 
5316dce513Schristos #ifndef S_ISLNK
5416dce513Schristos #ifdef S_IFLNK
5516dce513Schristos #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
5616dce513Schristos #else
5716dce513Schristos #define S_ISLNK(m) 0
5816dce513Schristos #define lstat stat
5916dce513Schristos #endif
6016dce513Schristos #endif
6116dce513Schristos 
6216dce513Schristos int
unlink_if_ordinary(const char * name)6316dce513Schristos unlink_if_ordinary (const char *name)
6416dce513Schristos {
6516dce513Schristos   struct stat st;
6616dce513Schristos 
6716dce513Schristos   if (lstat (name, &st) == 0
6816dce513Schristos       && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode)))
6916dce513Schristos     return unlink (name);
7016dce513Schristos 
7116dce513Schristos   return 1;
7216dce513Schristos }
73