1169695Skan/* unlink-if-ordinary.c - remove link to a file unless it is special 2169695Skan Copyright (C) 2004, 2005 Free Software Foundation, Inc. 3169695Skan 4169695SkanThis file is part of the libiberty library. This library is free 5169695Skansoftware; you can redistribute it and/or modify it under the 6169695Skanterms of the GNU General Public License as published by the 7169695SkanFree Software Foundation; either version 2, or (at your option) 8169695Skanany later version. 9169695Skan 10169695SkanThis library is distributed in the hope that it will be useful, 11169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 12169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13169695SkanGNU General Public License for more details. 14169695Skan 15169695SkanYou should have received a copy of the GNU General Public License 16169695Skanalong with GNU CC; see the file COPYING. If not, write to 17169695Skanthe Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 18169695Skan 19169695SkanAs a special exception, if you link this library with files 20169695Skancompiled with a GNU compiler to produce an executable, this does not cause 21169695Skanthe resulting executable to be covered by the GNU General Public License. 22169695SkanThis exception does not however invalidate any other reasons why 23169695Skanthe executable file might be covered by the GNU General Public License. */ 24169695Skan 25169695Skan/* 26169695Skan 27169695Skan@deftypefn Supplemental int unlink_if_ordinary (const char*) 28169695Skan 29169695SkanUnlinks the named file, unless it is special (e.g. a device file). 30169695SkanReturns 0 when the file was unlinked, a negative value (and errno set) when 31169695Skanthere was an error deleting the file, and a positive value if no attempt 32169695Skanwas made to unlink the file because it is special. 33169695Skan 34169695Skan@end deftypefn 35169695Skan 36169695Skan*/ 37169695Skan 38169695Skan#ifdef HAVE_CONFIG_H 39169695Skan#include "config.h" 40169695Skan#endif 41169695Skan 42169695Skan#include <sys/types.h> 43169695Skan 44169695Skan#ifdef HAVE_UNISTD_H 45169695Skan#include <unistd.h> 46169695Skan#endif 47169695Skan#if HAVE_SYS_STAT_H 48169695Skan#include <sys/stat.h> 49169695Skan#endif 50169695Skan 51169695Skan#include "libiberty.h" 52169695Skan 53169695Skan#ifndef S_ISLNK 54169695Skan#ifdef S_IFLNK 55169695Skan#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 56169695Skan#else 57169695Skan#define S_ISLNK(m) 0 58169695Skan#define lstat stat 59169695Skan#endif 60169695Skan#endif 61169695Skan 62169695Skanint 63169695Skanunlink_if_ordinary (const char *name) 64169695Skan{ 65169695Skan struct stat st; 66169695Skan 67169695Skan if (lstat (name, &st) == 0 68169695Skan && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode))) 69169695Skan return unlink (name); 70169695Skan 71169695Skan return 1; 72169695Skan} 73