unlink-if-ordinary.c revision 214082
128009Sdyson/* unlink-if-ordinary.c - remove link to a file unless it is special 228009Sdyson Copyright (C) 2004, 2005 Free Software Foundation, Inc. 328009Sdyson 428009SdysonThis file is part of the libiberty library. This library is free 528009Sdysonsoftware; you can redistribute it and/or modify it under the 628009Sdysonterms of the GNU General Public License as published by the 728009SdysonFree Software Foundation; either version 2, or (at your option) 828009Sdysonany later version. 928009Sdyson 1028009SdysonThis library is distributed in the hope that it will be useful, 1128009Sdysonbut WITHOUT ANY WARRANTY; without even the implied warranty of 1228009SdysonMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1328009SdysonGNU General Public License for more details. 1428009Sdyson 1528009SdysonYou should have received a copy of the GNU General Public License 1628009Sdysonalong with GNU CC; see the file COPYING. If not, write to 1728009Sdysonthe Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 1828009Sdyson 1928009SdysonAs a special exception, if you link this library with files 2028009Sdysoncompiled with a GNU compiler to produce an executable, this does not cause 2128009Sdysonthe resulting executable to be covered by the GNU General Public License. 2228009SdysonThis exception does not however invalidate any other reasons why 2328009Sdysonthe executable file might be covered by the GNU General Public License. */ 2428009Sdyson 2528009Sdyson/* 2650477Speter 2728009Sdyson@deftypefn Supplemental int unlink_if_ordinary (const char*) 2828009Sdyson 2928009SdysonUnlinks the named file, unless it is special (e.g. a device file). 3028009SdysonReturns 0 when the file was unlinked, a negative value (and errno set) when 3128009Sdysonthere was an error deleting the file, and a positive value if no attempt 3228009Sdysonwas made to unlink the file because it is special. 3328009Sdyson 3428009Sdyson@end deftypefn 3528009Sdyson 3628009Sdyson*/ 3728009Sdyson 3828009Sdyson#ifdef HAVE_CONFIG_H 3928009Sdyson#include "config.h" 4028009Sdyson#endif 4128009Sdyson 4228009Sdyson#include <sys/types.h> 4328009Sdyson 4428009Sdyson#ifdef HAVE_UNISTD_H 4528009Sdyson#include <unistd.h> 4654188Sluoqi#endif 4754188Sluoqi#if HAVE_SYS_STAT_H 4854188Sluoqi#include <sys/stat.h> 4954188Sluoqi#endif 5054188Sluoqi 5154188Sluoqi#include "libiberty.h" 5254188Sluoqi 5354188Sluoqi#ifndef S_ISLNK 5454188Sluoqi#ifdef S_IFLNK 5554188Sluoqi#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 5654188Sluoqi#else 5754188Sluoqi#define S_ISLNK(m) 0 5854188Sluoqi#define lstat stat 5954188Sluoqi#endif 6054188Sluoqi#endif 6154188Sluoqi 6254188Sluoqiint 6354188Sluoqiunlink_if_ordinary (const char *name) 6428009Sdyson{ 65 struct stat st; 66 67 if (lstat (name, &st) == 0 68 && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode))) 69 return unlink (name); 70 71 return 1; 72} 73