18dffb485Schristos /* basename.c -- return the last element in a file name 28dffb485Schristos 3*4b169a6bSchristos Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2022 Free Software 48dffb485Schristos Foundation, Inc. 58dffb485Schristos 6*4b169a6bSchristos This file is free software: you can redistribute it and/or modify 7*4b169a6bSchristos it under the terms of the GNU Lesser General Public License as 8*4b169a6bSchristos published by the Free Software Foundation; either version 2.1 of the 9*4b169a6bSchristos License, or (at your option) any later version. 108dffb485Schristos 11*4b169a6bSchristos This file is distributed in the hope that it will be useful, 128dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 138dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*4b169a6bSchristos GNU Lesser General Public License for more details. 158dffb485Schristos 16*4b169a6bSchristos You should have received a copy of the GNU Lesser General Public License 178dffb485Schristos along with this program. If not, see <https://www.gnu.org/licenses/>. */ 188dffb485Schristos 198dffb485Schristos #include <config.h> 208dffb485Schristos 21*4b169a6bSchristos /* Specification. */ 22*4b169a6bSchristos #include "basename-lgpl.h" 238dffb485Schristos 24*4b169a6bSchristos #include <stdbool.h> 258dffb485Schristos #include <string.h> 268dffb485Schristos 27*4b169a6bSchristos #include "filename.h" 288dffb485Schristos 298dffb485Schristos char * last_component(char const * name)308dffb485Schristoslast_component (char const *name) 318dffb485Schristos { 328dffb485Schristos char const *base = name + FILE_SYSTEM_PREFIX_LEN (name); 338dffb485Schristos char const *p; 34*4b169a6bSchristos bool last_was_slash = false; 358dffb485Schristos 368dffb485Schristos while (ISSLASH (*base)) 378dffb485Schristos base++; 388dffb485Schristos 398dffb485Schristos for (p = base; *p; p++) 408dffb485Schristos { 418dffb485Schristos if (ISSLASH (*p)) 42*4b169a6bSchristos last_was_slash = true; 43*4b169a6bSchristos else if (last_was_slash) 448dffb485Schristos { 458dffb485Schristos base = p; 46*4b169a6bSchristos last_was_slash = false; 478dffb485Schristos } 488dffb485Schristos } 498dffb485Schristos 508dffb485Schristos return (char *) base; 518dffb485Schristos } 528dffb485Schristos 538dffb485Schristos size_t base_len(char const * name)548dffb485Schristosbase_len (char const *name) 558dffb485Schristos { 568dffb485Schristos size_t len; 578dffb485Schristos size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name); 588dffb485Schristos 598dffb485Schristos for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--) 608dffb485Schristos continue; 618dffb485Schristos 628dffb485Schristos if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1 638dffb485Schristos && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2]) 648dffb485Schristos return 2; 658dffb485Schristos 668dffb485Schristos if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len 678dffb485Schristos && len == prefix_len && ISSLASH (name[prefix_len])) 688dffb485Schristos return prefix_len + 1; 698dffb485Schristos 708dffb485Schristos return len; 718dffb485Schristos } 72