195b7b453SJohn Marino /* stripslash.c -- remove redundant trailing slashes from a file name
295b7b453SJohn Marino
3*09d4459fSDaniel Fojt Copyright (C) 1990, 2001, 2003-2006, 2009-2020 Free Software Foundation,
495b7b453SJohn Marino Inc.
595b7b453SJohn Marino
695b7b453SJohn Marino This program is free software: you can redistribute it and/or modify
795b7b453SJohn Marino it under the terms of the GNU General Public License as published by
895b7b453SJohn Marino the Free Software Foundation; either version 3 of the License, or
995b7b453SJohn Marino (at your option) any later version.
1095b7b453SJohn Marino
1195b7b453SJohn Marino This program is distributed in the hope that it will be useful,
1295b7b453SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
1395b7b453SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1495b7b453SJohn Marino GNU General Public License for more details.
1595b7b453SJohn Marino
1695b7b453SJohn Marino You should have received a copy of the GNU General Public License
17*09d4459fSDaniel Fojt along with this program. If not, see <https://www.gnu.org/licenses/>. */
1895b7b453SJohn Marino
1995b7b453SJohn Marino #include <config.h>
2095b7b453SJohn Marino
2195b7b453SJohn Marino #include "dirname.h"
2295b7b453SJohn Marino
2395b7b453SJohn Marino /* Remove trailing slashes from FILE. Return true if a trailing slash
2495b7b453SJohn Marino was removed. This is useful when using file name completion from a
2595b7b453SJohn Marino shell that adds a "/" after directory names (such as tcsh and
2695b7b453SJohn Marino bash), because on symlinks to directories, several system calls
2795b7b453SJohn Marino have different semantics according to whether a trailing slash is
2895b7b453SJohn Marino present. */
2995b7b453SJohn Marino
3095b7b453SJohn Marino bool
strip_trailing_slashes(char * file)3195b7b453SJohn Marino strip_trailing_slashes (char *file)
3295b7b453SJohn Marino {
3395b7b453SJohn Marino char *base = last_component (file);
3495b7b453SJohn Marino char *base_lim;
3595b7b453SJohn Marino bool had_slash;
3695b7b453SJohn Marino
3795b7b453SJohn Marino /* last_component returns "" for file system roots, but we need to turn
38cf28ed85SJohn Marino "///" into "/". */
3995b7b453SJohn Marino if (! *base)
4095b7b453SJohn Marino base = file;
4195b7b453SJohn Marino base_lim = base + base_len (base);
4295b7b453SJohn Marino had_slash = (*base_lim != '\0');
4395b7b453SJohn Marino *base_lim = '\0';
4495b7b453SJohn Marino return had_slash;
4595b7b453SJohn Marino }
46