1 /* 2 * Path functions. 3 * 4 * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <string.h> 23 24 #include "path.h" 25 26 /* Return the basename from a path. */ 27 28 const char *path_basename(const char *path) 29 { 30 char *s = strrchr(path, (int) '/'); 31 32 if (s == NULL) 33 return path; 34 else 35 return s + 1; 36 } 37 38 /* Return the end of the first component in the given path. */ 39 40 const char *path_component_end(const char *path) 41 { 42 char *end = strchr(path, (int) '/'); 43 44 if (end == NULL) 45 return path + strlen(path); 46 else 47 return end; 48 } 49 50 /* Return the start of the next component in a path given the end of the 51 previous one. */ 52 53 const char *path_component_next(const char *end) 54 { 55 if (*end) 56 return end + 1; 57 else 58 return end; 59 } 60 61 /* Return whether the path lacks a path separator. */ 62 63 int path_is_leafname(const char *path) 64 { 65 return strchr(path, (int) '/') == NULL; 66 } 67 68 /* Split a path into directory and basename components, returning a pointer to 69 the basename component. */ 70 71 char *path_split(char *path) 72 { 73 char *s = strrchr(path, (int) '/'); 74 75 if (s != NULL) 76 { 77 *s = '\0'; 78 return s + 1; 79 } 80 else 81 return path; 82 }