12a6b7db3Sskrll /* unlink-if-ordinary.c - remove link to a file unless it is special
2*cb63e24eSchristos Copyright (C) 2004-2024 Free Software Foundation, Inc.
32a6b7db3Sskrll
42a6b7db3Sskrll This file is part of the libiberty library. This library is free
52a6b7db3Sskrll software; you can redistribute it and/or modify it under the
62a6b7db3Sskrll terms of the GNU General Public License as published by the
72a6b7db3Sskrll Free Software Foundation; either version 2, or (at your option)
82a6b7db3Sskrll any later version.
92a6b7db3Sskrll
102a6b7db3Sskrll This library is distributed in the hope that it will be useful,
112a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
122a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
132a6b7db3Sskrll GNU General Public License for more details.
142a6b7db3Sskrll
152a6b7db3Sskrll You should have received a copy of the GNU General Public License
162a6b7db3Sskrll along with GNU CC; see the file COPYING. If not, write to
172a6b7db3Sskrll the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
182a6b7db3Sskrll
192a6b7db3Sskrll As a special exception, if you link this library with files
202a6b7db3Sskrll compiled with a GNU compiler to produce an executable, this does not cause
212a6b7db3Sskrll the resulting executable to be covered by the GNU General Public License.
222a6b7db3Sskrll This exception does not however invalidate any other reasons why
232a6b7db3Sskrll the executable file might be covered by the GNU General Public License. */
242a6b7db3Sskrll
252a6b7db3Sskrll /*
262a6b7db3Sskrll
272a6b7db3Sskrll @deftypefn Supplemental int unlink_if_ordinary (const char*)
282a6b7db3Sskrll
292a6b7db3Sskrll Unlinks the named file, unless it is special (e.g. a device file).
302a6b7db3Sskrll Returns 0 when the file was unlinked, a negative value (and errno set) when
312a6b7db3Sskrll there was an error deleting the file, and a positive value if no attempt
322a6b7db3Sskrll was made to unlink the file because it is special.
332a6b7db3Sskrll
342a6b7db3Sskrll @end deftypefn
352a6b7db3Sskrll
362a6b7db3Sskrll */
372a6b7db3Sskrll
382a6b7db3Sskrll #ifdef HAVE_CONFIG_H
392a6b7db3Sskrll #include "config.h"
402a6b7db3Sskrll #endif
412a6b7db3Sskrll
422a6b7db3Sskrll #include <sys/types.h>
432a6b7db3Sskrll
442a6b7db3Sskrll #ifdef HAVE_UNISTD_H
452a6b7db3Sskrll #include <unistd.h>
462a6b7db3Sskrll #endif
472a6b7db3Sskrll #if HAVE_SYS_STAT_H
482a6b7db3Sskrll #include <sys/stat.h>
492a6b7db3Sskrll #endif
502a6b7db3Sskrll
512a6b7db3Sskrll #include "libiberty.h"
522a6b7db3Sskrll
532a6b7db3Sskrll #ifndef S_ISLNK
542a6b7db3Sskrll #ifdef S_IFLNK
552a6b7db3Sskrll #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
562a6b7db3Sskrll #else
572a6b7db3Sskrll #define S_ISLNK(m) 0
582a6b7db3Sskrll #define lstat stat
592a6b7db3Sskrll #endif
602a6b7db3Sskrll #endif
612a6b7db3Sskrll
622a6b7db3Sskrll int
unlink_if_ordinary(const char * name)632a6b7db3Sskrll unlink_if_ordinary (const char *name)
642a6b7db3Sskrll {
652a6b7db3Sskrll struct stat st;
662a6b7db3Sskrll
672a6b7db3Sskrll if (lstat (name, &st) == 0
682a6b7db3Sskrll && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode)))
692a6b7db3Sskrll return unlink (name);
702a6b7db3Sskrll
712a6b7db3Sskrll return 1;
722a6b7db3Sskrll }
73