xref: /dflybsd-src/contrib/cvs-1.12/lib/stripslash.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* stripslash.c -- remove redundant trailing slashes from a file name
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino    Copyright (C) 1990, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify
686d7f5d3SJohn Marino    it under the terms of the GNU General Public License as published by
786d7f5d3SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
886d7f5d3SJohn Marino    any later version.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
1186d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1286d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1386d7f5d3SJohn Marino    GNU General Public License for more details.
1486d7f5d3SJohn Marino 
1586d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
1686d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
1786d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
1886d7f5d3SJohn Marino 
1986d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
2086d7f5d3SJohn Marino # include <config.h>
2186d7f5d3SJohn Marino #endif
2286d7f5d3SJohn Marino 
2386d7f5d3SJohn Marino #include "dirname.h"
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino /* Remove trailing slashes from FILE.
2686d7f5d3SJohn Marino    Return true if a trailing slash was removed.
2786d7f5d3SJohn Marino    This is useful when using file name completion from a shell that
2886d7f5d3SJohn Marino    adds a "/" after directory names (such as tcsh and bash), because
2986d7f5d3SJohn Marino    the Unix rename and rmdir system calls return an "Invalid argument" error
3086d7f5d3SJohn Marino    when given a file that ends in "/" (except for the root directory).  */
3186d7f5d3SJohn Marino 
3286d7f5d3SJohn Marino bool
strip_trailing_slashes(char * file)3386d7f5d3SJohn Marino strip_trailing_slashes (char *file)
3486d7f5d3SJohn Marino {
3586d7f5d3SJohn Marino   char *base = base_name (file);
3686d7f5d3SJohn Marino   char *base_lim = base + base_len (base);
3786d7f5d3SJohn Marino   bool had_slash = (*base_lim != '\0');
3886d7f5d3SJohn Marino   *base_lim = '\0';
3986d7f5d3SJohn Marino   return had_slash;
4086d7f5d3SJohn Marino }
41