Blame lib/stripslash.c

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