xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/unlink-if-ordinary.c (revision 7e120ff03ede3fe64e2c8620c01465d528502ddb)
198b9484cSchristos /* unlink-if-ordinary.c - remove link to a file unless it is special
2*7e120ff0Schristos    Copyright (C) 2004-2024 Free Software Foundation, Inc.
398b9484cSchristos 
498b9484cSchristos This file is part of the libiberty library.  This library is free
598b9484cSchristos software; you can redistribute it and/or modify it under the
698b9484cSchristos terms of the GNU General Public License as published by the
798b9484cSchristos Free Software Foundation; either version 2, or (at your option)
898b9484cSchristos any later version.
998b9484cSchristos 
1098b9484cSchristos This library is distributed in the hope that it will be useful,
1198b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1298b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1398b9484cSchristos GNU General Public License for more details.
1498b9484cSchristos 
1598b9484cSchristos You should have received a copy of the GNU General Public License
1698b9484cSchristos along with GNU CC; see the file COPYING.  If not, write to
1798b9484cSchristos the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1898b9484cSchristos 
1998b9484cSchristos As a special exception, if you link this library with files
2098b9484cSchristos compiled with a GNU compiler to produce an executable, this does not cause
2198b9484cSchristos the resulting executable to be covered by the GNU General Public License.
2298b9484cSchristos This exception does not however invalidate any other reasons why
2398b9484cSchristos the executable file might be covered by the GNU General Public License. */
2498b9484cSchristos 
2598b9484cSchristos /*
2698b9484cSchristos 
2798b9484cSchristos @deftypefn Supplemental int unlink_if_ordinary (const char*)
2898b9484cSchristos 
2998b9484cSchristos Unlinks the named file, unless it is special (e.g. a device file).
3098b9484cSchristos Returns 0 when the file was unlinked, a negative value (and errno set) when
3198b9484cSchristos there was an error deleting the file, and a positive value if no attempt
3298b9484cSchristos was made to unlink the file because it is special.
3398b9484cSchristos 
3498b9484cSchristos @end deftypefn
3598b9484cSchristos 
3698b9484cSchristos */
3798b9484cSchristos 
3898b9484cSchristos #ifdef HAVE_CONFIG_H
3998b9484cSchristos #include "config.h"
4098b9484cSchristos #endif
4198b9484cSchristos 
4298b9484cSchristos #include <sys/types.h>
4398b9484cSchristos 
4498b9484cSchristos #ifdef HAVE_UNISTD_H
4598b9484cSchristos #include <unistd.h>
4698b9484cSchristos #endif
4798b9484cSchristos #if HAVE_SYS_STAT_H
4898b9484cSchristos #include <sys/stat.h>
4998b9484cSchristos #endif
5098b9484cSchristos 
5198b9484cSchristos #include "libiberty.h"
5298b9484cSchristos 
5398b9484cSchristos #ifndef S_ISLNK
5498b9484cSchristos #ifdef S_IFLNK
5598b9484cSchristos #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
5698b9484cSchristos #else
5798b9484cSchristos #define S_ISLNK(m) 0
5898b9484cSchristos #define lstat stat
5998b9484cSchristos #endif
6098b9484cSchristos #endif
6198b9484cSchristos 
6298b9484cSchristos int
6398b9484cSchristos unlink_if_ordinary (const char *name)
6498b9484cSchristos {
6598b9484cSchristos   struct stat st;
6698b9484cSchristos 
6798b9484cSchristos   if (lstat (name, &st) == 0
6898b9484cSchristos       && (S_ISREG (st.st_mode) || S_ISLNK (st.st_mode)))
6998b9484cSchristos     return unlink (name);
7098b9484cSchristos 
7198b9484cSchristos   return 1;
7298b9484cSchristos }
73