1 1.1 christos /* unlink-if-ordinary.c - remove link to a file unless it is special 2 1.1.1.6 christos Copyright (C) 2004-2024 Free Software Foundation, Inc. 3 1.1 christos 4 1.1 christos This file is part of the libiberty library. This library is free 5 1.1 christos software; you can redistribute it and/or modify it under the 6 1.1 christos terms of the GNU General Public License as published by the 7 1.1 christos Free Software Foundation; either version 2, or (at your option) 8 1.1 christos any later version. 9 1.1 christos 10 1.1 christos This library is distributed in the hope that it will be useful, 11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 1.1 christos GNU General Public License for more details. 14 1.1 christos 15 1.1 christos You should have received a copy of the GNU General Public License 16 1.1 christos along with GNU CC; see the file COPYING. If not, write to 17 1.1 christos the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 18 1.1 christos 19 1.1 christos As a special exception, if you link this library with files 20 1.1 christos compiled with a GNU compiler to produce an executable, this does not cause 21 1.1 christos the resulting executable to be covered by the GNU General Public License. 22 1.1 christos This exception does not however invalidate any other reasons why 23 1.1 christos the executable file might be covered by the GNU General Public License. */ 24 1.1 christos 25 1.1 christos /* 26 1.1 christos 27 1.1 christos @deftypefn Supplemental int unlink_if_ordinary (const char*) 28 1.1 christos 29 1.1 christos Unlinks the named file, unless it is special (e.g. a device file). 30 1.1 christos Returns 0 when the file was unlinked, a negative value (and errno set) when 31 1.1 christos there was an error deleting the file, and a positive value if no attempt 32 1.1 christos was made to unlink the file because it is special. 33 1.1 christos 34 1.1 christos @end deftypefn 35 1.1 christos 36 1.1 christos */ 37 1.1 christos 38 1.1 christos #ifdef HAVE_CONFIG_H 39 1.1 christos #include "config.h" 40 1.1 christos #endif 41 1.1 christos 42 1.1 christos #include <sys/types.h> 43 1.1 christos 44 1.1 christos #ifdef HAVE_UNISTD_H 45 1.1 christos #include <unistd.h> 46 1.1 christos #endif 47 1.1 christos #if HAVE_SYS_STAT_H 48 1.1 christos #include <sys/stat.h> 49 1.1 christos #endif 50 1.1 christos 51 1.1 christos #include "libiberty.h" 52 1.1 christos 53 1.1 christos #ifndef S_ISLNK 54 1.1 christos #ifdef S_IFLNK 55 1.1 christos #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 56 1.1 christos #else 57 1.1 christos #define S_ISLNK(m) 0 58 1.1 christos #define lstat stat 59 1.1 christos #endif 60 1.1 christos #endif 61 1.1 christos 62 1.1 christos int 63 1.1 christos unlink_if_ordinary (const char *name) 64 1.1 christos { 65 1.1 christos struct stat st; 66 1.1 christos 67 1.1 christos if (lstat (name, &st) == 0 68 1.1 christos && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode))) 69 1.1 christos return unlink (name); 70 1.1 christos 71 1.1 christos return 1; 72 1.1 christos } 73