xref: /netbsd-src/external/gpl3/gcc.old/dist/libiberty/unlink-if-ordinary.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* unlink-if-ordinary.c - remove link to a file unless it is special
2*8feb0f0bSmrg    Copyright (C) 2004-2020 Free Software Foundation, Inc.
31debfc3dSmrg 
41debfc3dSmrg This file is part of the libiberty library.  This library is free
51debfc3dSmrg software; you can redistribute it and/or modify it under the
61debfc3dSmrg terms of the GNU General Public License as published by the
71debfc3dSmrg Free Software Foundation; either version 2, or (at your option)
81debfc3dSmrg any later version.
91debfc3dSmrg 
101debfc3dSmrg This library is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131debfc3dSmrg GNU General Public License for more details.
141debfc3dSmrg 
151debfc3dSmrg You should have received a copy of the GNU General Public License
161debfc3dSmrg along with GNU CC; see the file COPYING.  If not, write to
171debfc3dSmrg the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
181debfc3dSmrg 
191debfc3dSmrg As a special exception, if you link this library with files
201debfc3dSmrg compiled with a GNU compiler to produce an executable, this does not cause
211debfc3dSmrg the resulting executable to be covered by the GNU General Public License.
221debfc3dSmrg This exception does not however invalidate any other reasons why
231debfc3dSmrg the executable file might be covered by the GNU General Public License. */
241debfc3dSmrg 
251debfc3dSmrg /*
261debfc3dSmrg 
271debfc3dSmrg @deftypefn Supplemental int unlink_if_ordinary (const char*)
281debfc3dSmrg 
291debfc3dSmrg Unlinks the named file, unless it is special (e.g. a device file).
301debfc3dSmrg Returns 0 when the file was unlinked, a negative value (and errno set) when
311debfc3dSmrg there was an error deleting the file, and a positive value if no attempt
321debfc3dSmrg was made to unlink the file because it is special.
331debfc3dSmrg 
341debfc3dSmrg @end deftypefn
351debfc3dSmrg 
361debfc3dSmrg */
371debfc3dSmrg 
381debfc3dSmrg #ifdef HAVE_CONFIG_H
391debfc3dSmrg #include "config.h"
401debfc3dSmrg #endif
411debfc3dSmrg 
421debfc3dSmrg #include <sys/types.h>
431debfc3dSmrg 
441debfc3dSmrg #ifdef HAVE_UNISTD_H
451debfc3dSmrg #include <unistd.h>
461debfc3dSmrg #endif
471debfc3dSmrg #if HAVE_SYS_STAT_H
481debfc3dSmrg #include <sys/stat.h>
491debfc3dSmrg #endif
501debfc3dSmrg 
511debfc3dSmrg #include "libiberty.h"
521debfc3dSmrg 
531debfc3dSmrg #ifndef S_ISLNK
541debfc3dSmrg #ifdef S_IFLNK
551debfc3dSmrg #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
561debfc3dSmrg #else
571debfc3dSmrg #define S_ISLNK(m) 0
581debfc3dSmrg #define lstat stat
591debfc3dSmrg #endif
601debfc3dSmrg #endif
611debfc3dSmrg 
621debfc3dSmrg int
unlink_if_ordinary(const char * name)631debfc3dSmrg unlink_if_ordinary (const char *name)
641debfc3dSmrg {
651debfc3dSmrg   struct stat st;
661debfc3dSmrg 
671debfc3dSmrg   if (lstat (name, &st) == 0
681debfc3dSmrg       && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode)))
691debfc3dSmrg     return unlink (name);
701debfc3dSmrg 
711debfc3dSmrg   return 1;
721debfc3dSmrg }
73