Blame lib/stripslash.c

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