Blame lib/stripslash.c

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