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