Blame lib/stripslash.c

Packit 709fb3
/* stripslash.c -- remove redundant trailing slashes from a file name
Packit 709fb3
Packit 709fb3
   Copyright (C) 1990, 2001, 2003-2006, 2009-2017 Free Software Foundation,
Packit 709fb3
   Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include "dirname.h"
Packit 709fb3
Packit 709fb3
/* Remove trailing slashes from FILE.  Return true if a trailing slash
Packit 709fb3
   was removed.  This is useful when using file name completion from a
Packit 709fb3
   shell that adds a "/" after directory names (such as tcsh and
Packit 709fb3
   bash), because on symlinks to directories, several system calls
Packit 709fb3
   have different semantics according to whether a trailing slash is
Packit 709fb3
   present.  */
Packit 709fb3
Packit 709fb3
bool
Packit 709fb3
strip_trailing_slashes (char *file)
Packit 709fb3
{
Packit 709fb3
  char *base = last_component (file);
Packit 709fb3
  char *base_lim;
Packit 709fb3
  bool had_slash;
Packit 709fb3
Packit 709fb3
  /* last_component returns "" for file system roots, but we need to turn
Packit 709fb3
     "///" into "/".  */
Packit 709fb3
  if (! *base)
Packit 709fb3
    base = file;
Packit 709fb3
  base_lim = base + base_len (base);
Packit 709fb3
  had_slash = (*base_lim != '\0');
Packit 709fb3
  *base_lim = '\0';
Packit 709fb3
  return had_slash;
Packit 709fb3
}