1 /* 2 * File opening support. 3 * 4 * Copyright (C) 2021, 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 <stdlib.h> 23 #include <string.h> 24 25 #include "file_opening.h" 26 27 28 29 /* Obtain the identifier for any active parent directory for a path. */ 30 31 long FileOpening::get_parent(const char *path, fileid_t *fileid) 32 { 33 char *sep = strrchr(path, (int) '/'); 34 long err; 35 36 /* For top-level paths, use the empty string to get the root directory. 37 Otherwise, obtain the parent directory path to obtain the file 38 identifier. */ 39 40 if (sep == NULL) 41 err = get_fileid("", 0, fileid); 42 else 43 { 44 char *parent = strndup(path, sep - path + 1); 45 46 parent[sep - path] = '\0'; 47 err = get_fileid(parent, 0, fileid); 48 free(parent); 49 } 50 51 return err; 52 } 53 54 // vim: tabstop=4 expandtab shiftwidth=4