Back to index

nagios-plugins  1.4.16
stripslash.c
Go to the documentation of this file.
00001 /* stripslash.c -- remove redundant trailing slashes from a file name
00002 
00003    Copyright (C) 1990, 2001, 2003-2006, 2009-2010 Free Software Foundation,
00004    Inc.
00005 
00006    This program is free software: you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 #include <config.h>
00020 
00021 #include "dirname.h"
00022 
00023 /* Remove trailing slashes from FILE.  Return true if a trailing slash
00024    was removed.  This is useful when using file name completion from a
00025    shell that adds a "/" after directory names (such as tcsh and
00026    bash), because on symlinks to directories, several system calls
00027    have different semantics according to whether a trailing slash is
00028    present.  */
00029 
00030 bool
00031 strip_trailing_slashes (char *file)
00032 {
00033   char *base = last_component (file);
00034   char *base_lim;
00035   bool had_slash;
00036 
00037   /* last_component returns "" for file system roots, but we need to turn
00038      `///' into `/'.  */
00039   if (! *base)
00040     base = file;
00041   base_lim = base + base_len (base);
00042   had_slash = (*base_lim != '\0');
00043   *base_lim = '\0';
00044   return had_slash;
00045 }