stripslash.c 1.53 KB
Newer Older
1
/* stripslash.c -- remove redundant trailing slashes from a file name
Paul Eggert's avatar
Paul Eggert committed
2

Paul Eggert's avatar
Paul Eggert committed
3
   Copyright (C) 1990, 2001, 2003-2006, 2009-2016 Free Software Foundation,
4
   Inc.
Jim Meyering's avatar
Jim Meyering committed
5

6
   This program is free software: you can redistribute it and/or modify
Jim Meyering's avatar
Jim Meyering committed
7
   it under the terms of the GNU General Public License as published by
8 9
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
Jim Meyering's avatar
Jim Meyering committed
10 11 12 13 14 15 16

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
17
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Meyering's avatar
Jim Meyering committed
18

19
#include <config.h>
Jim Meyering's avatar
Jim Meyering committed
20

21
#include "dirname.h"
22

Paul Eggert's avatar
Paul Eggert committed
23 24 25 26 27 28
/* Remove trailing slashes from FILE.  Return true if a trailing slash
   was removed.  This is useful when using file name completion from a
   shell that adds a "/" after directory names (such as tcsh and
   bash), because on symlinks to directories, several system calls
   have different semantics according to whether a trailing slash is
   present.  */
Jim Meyering's avatar
Jim Meyering committed
29

30
bool
Paul Eggert's avatar
Paul Eggert committed
31
strip_trailing_slashes (char *file)
Jim Meyering's avatar
Jim Meyering committed
32
{
Paul Eggert's avatar
Paul Eggert committed
33 34 35 36 37
  char *base = last_component (file);
  char *base_lim;
  bool had_slash;

  /* last_component returns "" for file system roots, but we need to turn
38
     "///" into "/".  */
Paul Eggert's avatar
Paul Eggert committed
39 40 41 42
  if (! *base)
    base = file;
  base_lim = base + base_len (base);
  had_slash = (*base_lim != '\0');
43 44
  *base_lim = '\0';
  return had_slash;
Jim Meyering's avatar
Jim Meyering committed
45
}