unlink-if-ordinary.c revision 1.1
150276Speter/* unlink-if-ordinary.c - remove link to a file unless it is special 2166124Srafan Copyright (C) 2004, 2005 Free Software Foundation, Inc. 350276Speter 450276SpeterThis file is part of the libiberty library. This library is free 550276Spetersoftware; you can redistribute it and/or modify it under the 650276Speterterms of the GNU General Public License as published by the 750276SpeterFree Software Foundation; either version 2, or (at your option) 850276Speterany later version. 950276Speter 1050276SpeterThis library is distributed in the hope that it will be useful, 1150276Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of 1250276SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1350276SpeterGNU General Public License for more details. 1450276Speter 1550276SpeterYou should have received a copy of the GNU General Public License 1650276Speteralong with GNU CC; see the file COPYING. If not, write to 1750276Speterthe Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 1850276Speter 1950276SpeterAs a special exception, if you link this library with files 2050276Spetercompiled with a GNU compiler to produce an executable, this does not cause 2150276Speterthe resulting executable to be covered by the GNU General Public License. 2250276SpeterThis exception does not however invalidate any other reasons why 2350276Speterthe executable file might be covered by the GNU General Public License. */ 2450276Speter 2550276Speter/* 2650276Speter 2750276Speter@deftypefn Supplemental int unlink_if_ordinary (const char*) 2850276Speter 2950276SpeterUnlinks the named file, unless it is special (e.g. a device file). 3050276SpeterReturns 0 when the file was unlinked, a negative value (and errno set) when 3150276Speterthere was an error deleting the file, and a positive value if no attempt 32166124Srafanwas made to unlink the file because it is special. 3350276Speter 3450276Speter@end deftypefn 3550276Speter 3650276Speter*/ 3750276Speter 3850276Speter#ifdef HAVE_CONFIG_H 3950276Speter#include "config.h" 4050276Speter#endif 4150276Speter 4250276Speter#include <sys/types.h> 4350276Speter 4450276Speter#ifdef HAVE_UNISTD_H 4550276Speter#include <unistd.h> 4650276Speter#endif 4750276Speter#if HAVE_SYS_STAT_H 4850276Speter#include <sys/stat.h> 4950276Speter#endif 5050276Speter 5150276Speter#include "libiberty.h" 5297049Speter 5397049Speter#ifndef S_ISLNK 5497049Speter#ifdef S_IFLNK 5550276Speter#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 5697049Speter#else 5750276Speter#define S_ISLNK(m) 0 5897049Speter#define lstat stat 5950276Speter#endif 6050276Speter#endif 6150276Speter 6250276Speterint 6350276Speterunlink_if_ordinary (const char *name) 6450276Speter{ 6550276Speter struct stat st; 6650276Speter 6750276Speter if (lstat (name, &st) == 0 6850276Speter && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode))) 6950276Speter return unlink (name); 7050276Speter 7150276Speter return 1; 7297049Speter} 7350276Speter