00001 /* 00002 * Copyright 2005, 2006 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* unlink-if-ordinary.c - remove link to a file unless it is special 00006 Copyright (C) 2004 Free Software Foundation, Inc. 00007 00008 This file is part of the libiberty library. This library is free 00009 software; you can redistribute it and/or modify it under the 00010 terms of the GNU General Public License as published by the 00011 Free Software Foundation; either version 2, or (at your option) 00012 any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with GNU CC; see the file COPYING. If not, write to 00021 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 00023 As a special exception, if you link this library with files 00024 compiled with a GNU compiler to produce an executable, this does not cause 00025 the resulting executable to be covered by the GNU General Public License. 00026 This exception does not however invalidate any other reasons why 00027 the executable file might be covered by the GNU General Public License. */ 00028 00029 /* 00030 00031 @deftypefn Supplemental int unlink_if_ordinary (const char*) 00032 00033 Unlinks the named file, unless it is special (e.g. a device file). 00034 Returns 0 when the file was unlinked, a negative value (and errno set) when 00035 there was an error deleting the file, and a positive value if no attempt 00036 was made to unlink the file because it is special. 00037 00038 @end deftypefn 00039 00040 */ 00041 00042 #ifdef HAVE_CONFIG_H 00043 #include "config.h" 00044 #endif 00045 00046 #ifdef HAVE_UNISTD_H 00047 #include <unistd.h> 00048 #endif 00049 #if HAVE_SYS_STAT_H 00050 #include <sys/stat.h> 00051 #endif 00052 00053 #include "libiberty.h" 00054 00055 #ifndef S_ISLNK 00056 #ifdef S_IFLNK 00057 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 00058 #else 00059 #define S_ISLNK(m) 0 00060 #define lstat stat 00061 #endif 00062 #endif 00063 00064 int 00065 unlink_if_ordinary (name) 00066 const char *name; 00067 { 00068 struct stat st; 00069 00070 if (lstat (name, &st) == 0 00071 && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode))) 00072 return unlink (name); 00073 00074 return 1; 00075 }
1.5.6