paul@299 | 1 | /* |
paul@299 | 2 | * Generic file access functions. |
paul@299 | 3 | * |
paul@299 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@299 | 5 | * |
paul@299 | 6 | * This program is free software; you can redistribute it and/or |
paul@299 | 7 | * modify it under the terms of the GNU General Public License as |
paul@299 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@299 | 9 | * the License, or (at your option) any later version. |
paul@299 | 10 | * |
paul@299 | 11 | * This program is distributed in the hope that it will be useful, |
paul@299 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@299 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@299 | 14 | * GNU General Public License for more details. |
paul@299 | 15 | * |
paul@299 | 16 | * You should have received a copy of the GNU General Public License |
paul@299 | 17 | * along with this program; if not, write to the Free Software |
paul@299 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@299 | 19 | * Boston, MA 02110-1301, USA |
paul@299 | 20 | */ |
paul@299 | 21 | |
paul@299 | 22 | #include <stdio.h> |
paul@299 | 23 | #include <string.h> |
paul@299 | 24 | #include <sys/stat.h> |
paul@299 | 25 | #include <sys/types.h> |
paul@299 | 26 | |
paul@299 | 27 | #include "file.h" |
paul@299 | 28 | |
paul@299 | 29 | |
paul@299 | 30 | |
paul@299 | 31 | /* Test object types in the external environment. */ |
paul@299 | 32 | |
paul@299 | 33 | int isdir(const char *name) |
paul@299 | 34 | { |
paul@299 | 35 | struct stat st; |
paul@299 | 36 | |
paul@299 | 37 | if (!lstat(name, &st)) |
paul@299 | 38 | return S_ISDIR(st.st_mode); |
paul@299 | 39 | else |
paul@299 | 40 | return 0; |
paul@299 | 41 | } |
paul@299 | 42 | |
paul@299 | 43 | int isdir_dirname(const char *name) |
paul@299 | 44 | { |
paul@299 | 45 | char dirname[strlen(name) + 1]; |
paul@299 | 46 | char *s; |
paul@299 | 47 | |
paul@299 | 48 | strcpy(dirname, name); |
paul@299 | 49 | s = strrchr(dirname, (int) '/'); |
paul@299 | 50 | |
paul@299 | 51 | if (s != NULL) |
paul@299 | 52 | *s = '\0'; |
paul@299 | 53 | |
paul@299 | 54 | return isdir(dirname); |
paul@299 | 55 | } |
paul@299 | 56 | |
paul@299 | 57 | int isfile(const char *name) |
paul@299 | 58 | { |
paul@299 | 59 | struct stat st; |
paul@299 | 60 | |
paul@299 | 61 | if (!lstat(name, &st)) |
paul@299 | 62 | return S_ISREG(st.st_mode); |
paul@299 | 63 | else |
paul@299 | 64 | return 0; |
paul@299 | 65 | } |
paul@299 | 66 | |
paul@299 | 67 | /* Open a file in the external environment. */ |
paul@299 | 68 | |
paul@299 | 69 | FILE *open_file_in_dir(const char *dirname, const char *basename, |
paul@299 | 70 | const char *mode) |
paul@299 | 71 | { |
paul@299 | 72 | char pathname[strlen(dirname) + strlen(basename) + 1]; |
paul@299 | 73 | |
paul@299 | 74 | strcpy(pathname, dirname); |
paul@299 | 75 | strcat(pathname, "/"); |
paul@299 | 76 | strcat(pathname, basename); |
paul@299 | 77 | |
paul@299 | 78 | return fopen(pathname, mode); |
paul@299 | 79 | } |
paul@299 | 80 | |
paul@299 | 81 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@299 | 82 | */ |