Back to index

nagios-plugins  1.4.16
basename-lgpl.c
Go to the documentation of this file.
00001 /* basename.c -- return the last element in a file name
00002 
00003    Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2010 Free Software
00004    Foundation, 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 #include <string.h>
00024 
00025 /* Return the address of the last file name component of NAME.  If
00026    NAME has no relative file name components because it is a file
00027    system root, return the empty string.  */
00028 
00029 char *
00030 last_component (char const *name)
00031 {
00032   char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
00033   char const *p;
00034   bool saw_slash = false;
00035 
00036   while (ISSLASH (*base))
00037     base++;
00038 
00039   for (p = base; *p; p++)
00040     {
00041       if (ISSLASH (*p))
00042         saw_slash = true;
00043       else if (saw_slash)
00044         {
00045           base = p;
00046           saw_slash = false;
00047         }
00048     }
00049 
00050   return (char *) base;
00051 }
00052 
00053 /* Return the length of the basename NAME.  Typically NAME is the
00054    value returned by base_name or last_component.  Act like strlen
00055    (NAME), except omit all trailing slashes.  */
00056 
00057 size_t
00058 base_len (char const *name)
00059 {
00060   size_t len;
00061   size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
00062 
00063   for (len = strlen (name);  1 < len && ISSLASH (name[len - 1]);  len--)
00064     continue;
00065 
00066   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1
00067       && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
00068     return 2;
00069 
00070   if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len
00071       && len == prefix_len && ISSLASH (name[prefix_len]))
00072     return prefix_len + 1;
00073 
00074   return len;
00075 }