Back to index

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