paul@236 | 1 | /* |
paul@236 | 2 | * File opening support. |
paul@236 | 3 | * |
paul@236 | 4 | * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@236 | 5 | * |
paul@236 | 6 | * This program is free software; you can redistribute it and/or |
paul@236 | 7 | * modify it under the terms of the GNU General Public License as |
paul@236 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@236 | 9 | * the License, or (at your option) any later version. |
paul@236 | 10 | * |
paul@236 | 11 | * This program is distributed in the hope that it will be useful, |
paul@236 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@236 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@236 | 14 | * GNU General Public License for more details. |
paul@236 | 15 | * |
paul@236 | 16 | * You should have received a copy of the GNU General Public License |
paul@236 | 17 | * along with this program; if not, write to the Free Software |
paul@236 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@236 | 19 | * Boston, MA 02110-1301, USA |
paul@236 | 20 | */ |
paul@236 | 21 | |
paul@236 | 22 | #include <stdlib.h> |
paul@236 | 23 | #include <string.h> |
paul@236 | 24 | |
paul@236 | 25 | #include "file_opening.h" |
paul@236 | 26 | |
paul@236 | 27 | |
paul@236 | 28 | |
paul@236 | 29 | /* Obtain the identifier for any active parent directory for a path. */ |
paul@236 | 30 | |
paul@236 | 31 | long FileOpening::get_parent(const char *path, fileid_t *fileid) |
paul@236 | 32 | { |
paul@236 | 33 | char *sep = strrchr(path, (int) '/'); |
paul@236 | 34 | long err; |
paul@236 | 35 | |
paul@236 | 36 | /* For top-level paths, use the empty string to get the root directory. |
paul@236 | 37 | Otherwise, obtain the parent directory path to obtain the file |
paul@236 | 38 | identifier. */ |
paul@236 | 39 | |
paul@236 | 40 | if (sep == NULL) |
paul@236 | 41 | err = get_fileid("", 0, fileid); |
paul@236 | 42 | else |
paul@236 | 43 | { |
paul@236 | 44 | char *parent = strndup(path, sep - path + 1); |
paul@236 | 45 | |
paul@236 | 46 | parent[sep - path] = '\0'; |
paul@236 | 47 | err = get_fileid(parent, 0, fileid); |
paul@236 | 48 | free(parent); |
paul@236 | 49 | } |
paul@236 | 50 | |
paul@236 | 51 | return err; |
paul@236 | 52 | } |
paul@236 | 53 | |
paul@236 | 54 | // vim: tabstop=4 expandtab shiftwidth=4 |