1214082Sdim/* unlink-if-ordinary.c - remove link to a file unless it is special 2214082Sdim Copyright (C) 2004, 2005 Free Software Foundation, Inc. 3214082Sdim 4214082SdimThis file is part of the libiberty library. This library is free 5214082Sdimsoftware; you can redistribute it and/or modify it under the 6214082Sdimterms of the GNU General Public License as published by the 7214082SdimFree Software Foundation; either version 2, or (at your option) 8214082Sdimany later version. 9214082Sdim 10214082SdimThis library is distributed in the hope that it will be useful, 11214082Sdimbut WITHOUT ANY WARRANTY; without even the implied warranty of 12214082SdimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13214082SdimGNU General Public License for more details. 14214082Sdim 15214082SdimYou should have received a copy of the GNU General Public License 16214082Sdimalong with GNU CC; see the file COPYING. If not, write to 17214082Sdimthe Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 18214082Sdim 19214082SdimAs a special exception, if you link this library with files 20214082Sdimcompiled with a GNU compiler to produce an executable, this does not cause 21214082Sdimthe resulting executable to be covered by the GNU General Public License. 22214082SdimThis exception does not however invalidate any other reasons why 23214082Sdimthe executable file might be covered by the GNU General Public License. */ 24214082Sdim 25214082Sdim/* 26214082Sdim 27214082Sdim@deftypefn Supplemental int unlink_if_ordinary (const char*) 28214082Sdim 29214082SdimUnlinks the named file, unless it is special (e.g. a device file). 30214082SdimReturns 0 when the file was unlinked, a negative value (and errno set) when 31214082Sdimthere was an error deleting the file, and a positive value if no attempt 32214082Sdimwas made to unlink the file because it is special. 33214082Sdim 34214082Sdim@end deftypefn 35214082Sdim 36214082Sdim*/ 37214082Sdim 38214082Sdim#ifdef HAVE_CONFIG_H 39214082Sdim#include "config.h" 40214082Sdim#endif 41214082Sdim 42214082Sdim#include <sys/types.h> 43214082Sdim 44214082Sdim#ifdef HAVE_UNISTD_H 45214082Sdim#include <unistd.h> 46214082Sdim#endif 47214082Sdim#if HAVE_SYS_STAT_H 48214082Sdim#include <sys/stat.h> 49214082Sdim#endif 50214082Sdim 51214082Sdim#include "libiberty.h" 52214082Sdim 53214082Sdim#ifndef S_ISLNK 54214082Sdim#ifdef S_IFLNK 55214082Sdim#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 56214082Sdim#else 57214082Sdim#define S_ISLNK(m) 0 58214082Sdim#define lstat stat 59214082Sdim#endif 60214082Sdim#endif 61214082Sdim 62214082Sdimint 63214082Sdimunlink_if_ordinary (const char *name) 64214082Sdim{ 65214082Sdim struct stat st; 66214082Sdim 67214082Sdim if (lstat (name, &st) == 0 68214082Sdim && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode))) 69214082Sdim return unlink (name); 70214082Sdim 71214082Sdim return 1; 72214082Sdim} 73