1 /* -*- buffer-read-only: t -*- vi: set ro: */ 2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */ 3 /* basename.c -- return the last element in a file name 4 5 Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2019 Free Software 6 Foundation, Inc. 7 8 This program is free software: you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 20 21 #include <config.h> 22 23 #include "dirname.h" 24 25 #include <string.h> 26 27 /* Return the address of the last file name component of NAME. If 28 NAME has no relative file name components because it is a file 29 system root, return the empty string. */ 30 31 char * last_component(char const * name)32last_component (char const *name) 33 { 34 char const *base = name + FILE_SYSTEM_PREFIX_LEN (name); 35 char const *p; 36 bool saw_slash = false; 37 38 while (ISSLASH (*base)) 39 base++; 40 41 for (p = base; *p; p++) 42 { 43 if (ISSLASH (*p)) 44 saw_slash = true; 45 else if (saw_slash) 46 { 47 base = p; 48 saw_slash = false; 49 } 50 } 51 52 return (char *) base; 53 } 54 55 /* Return the length of the basename NAME. Typically NAME is the 56 value returned by base_name or last_component. Act like strlen 57 (NAME), except omit all trailing slashes. */ 58 59 size_t base_len(char const * name)60base_len (char const *name) 61 { 62 size_t len; 63 size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name); 64 65 for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--) 66 continue; 67 68 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1 69 && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2]) 70 return 2; 71 72 if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len 73 && len == prefix_len && ISSLASH (name[prefix_len])) 74 return prefix_len + 1; 75 76 return len; 77 } 78