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