unlink-if-ordinary.c revision 1.1.1.1
1198090Srdivacky/* unlink-if-ordinary.c - remove link to a file unless it is special 2198090Srdivacky Copyright (C) 2004, 2005 Free Software Foundation, Inc. 3198090Srdivacky 4198090SrdivackyThis file is part of the libiberty library. This library is free 5198090Srdivackysoftware; you can redistribute it and/or modify it under the 6198090Srdivackyterms of the GNU General Public License as published by the 7198090SrdivackyFree Software Foundation; either version 2, or (at your option) 8198090Srdivackyany later version. 9198090Srdivacky 10198090SrdivackyThis library is distributed in the hope that it will be useful, 11198090Srdivackybut WITHOUT ANY WARRANTY; without even the implied warranty of 12198090SrdivackyMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13198090SrdivackyGNU General Public License for more details. 14198090Srdivacky 15198090SrdivackyYou should have received a copy of the GNU General Public License 16198090Srdivackyalong with GNU CC; see the file COPYING. If not, write to 17198090Srdivackythe Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 18198090Srdivacky 19198090SrdivackyAs a special exception, if you link this library with files 20198090Srdivackycompiled with a GNU compiler to produce an executable, this does not cause 21198090Srdivackythe resulting executable to be covered by the GNU General Public License. 22198090SrdivackyThis exception does not however invalidate any other reasons why 23198090Srdivackythe executable file might be covered by the GNU General Public License. */ 24198090Srdivacky 25198090Srdivacky/* 26198090Srdivacky 27198892Srdivacky@deftypefn Supplemental int unlink_if_ordinary (const char*) 28198892Srdivacky 29198090SrdivackyUnlinks the named file, unless it is special (e.g. a device file). 30198090SrdivackyReturns 0 when the file was unlinked, a negative value (and errno set) when 31198090Srdivackythere was an error deleting the file, and a positive value if no attempt 32198090Srdivackywas made to unlink the file because it is special. 33198090Srdivacky 34198090Srdivacky@end deftypefn 35202878Srdivacky 36202878Srdivacky*/ 37202878Srdivacky 38202878Srdivacky#ifdef HAVE_CONFIG_H 39202878Srdivacky#include "config.h" 40202878Srdivacky#endif 41202878Srdivacky 42202878Srdivacky#include <sys/types.h> 43202878Srdivacky 44202878Srdivacky#ifdef HAVE_UNISTD_H 45198090Srdivacky#include <unistd.h> 46198090Srdivacky#endif 47198090Srdivacky#if HAVE_SYS_STAT_H 48198090Srdivacky#include <sys/stat.h> 49198090Srdivacky#endif 50198090Srdivacky 51198892Srdivacky#include "libiberty.h" 52198090Srdivacky 53198090Srdivacky#ifndef S_ISLNK 54198090Srdivacky#ifdef S_IFLNK 55198090Srdivacky#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 56198090Srdivacky#else 57198090Srdivacky#define S_ISLNK(m) 0 58198090Srdivacky#define lstat stat 59198090Srdivacky#endif 60198090Srdivacky#endif 61198090Srdivacky 62198090Srdivackyint 63198090Srdivackyunlink_if_ordinary (const char *name) 64198090Srdivacky{ 65198090Srdivacky struct stat st; 66198090Srdivacky 67198090Srdivacky if (lstat (name, &st) == 0 68198090Srdivacky && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode))) 69198090Srdivacky return unlink (name); 70198090Srdivacky 71198090Srdivacky return 1; 72198090Srdivacky} 73198090Srdivacky