[421b7d2] | 1 | /* osdepend.c |
---|
[d1b1380] | 2 | * OS dependent functions |
---|
[affaeee] | 3 | * Copyright (C) 1993-2003,2004,2005 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[a420b49] | 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
[d1b1380] | 23 | |
---|
| 24 | #include <string.h> |
---|
| 25 | #include "whichos.h" |
---|
| 26 | #include "useful.h" |
---|
| 27 | #include "osdepend.h" |
---|
| 28 | |
---|
[affaeee] | 29 | #if OS_WIN32 |
---|
[d1b1380] | 30 | # include <ctype.h> /* needed for isalpha */ |
---|
| 31 | |
---|
[a420b49] | 32 | /* NB "c:fred" isn't relative. Eg "c:\data\c:fred" won't work */ |
---|
| 33 | bool |
---|
| 34 | fAbsoluteFnm(const char *fnm) |
---|
| 35 | { |
---|
[bd1913f] | 36 | /* <drive letter>: or \<path> or /<path> |
---|
| 37 | * or \\<host>\... or //<host>/... */ |
---|
[0580c6a] | 38 | unsigned char ch = (unsigned char)*fnm; |
---|
[a420b49] | 39 | return ((fnm[1] == ':' && isalpha(ch)) || ch == '\\' || ch == '/'); |
---|
[d1b1380] | 40 | } |
---|
| 41 | |
---|
[affaeee] | 42 | #elif OS_UNIX |
---|
[d1b1380] | 43 | |
---|
[a420b49] | 44 | bool |
---|
| 45 | fAbsoluteFnm(const char *fnm) |
---|
| 46 | { |
---|
| 47 | return (fnm[0] == '/'); |
---|
[d1b1380] | 48 | } |
---|
| 49 | |
---|
| 50 | #endif |
---|
| 51 | |
---|
| 52 | /* fDirectory( fnm ) returns fTrue if fnm is a directory; fFalse if fnm is a |
---|
| 53 | * file, doesn't exist, or another error occurs (eg disc not in drive, ...) |
---|
[0804fbe] | 54 | * NB If fnm has a trailing directory separator (e.g. “/” or “/home/olly/” |
---|
[8a01812] | 55 | * then it's assumed to be a directory even if it doesn't exist (as is an |
---|
| 56 | * empty string). |
---|
[d1b1380] | 57 | */ |
---|
| 58 | |
---|
[affaeee] | 59 | #if OS_UNIX || OS_WIN32 |
---|
[d1b1380] | 60 | |
---|
| 61 | # include <sys/types.h> |
---|
| 62 | # include <sys/stat.h> |
---|
| 63 | # include <stdio.h> |
---|
| 64 | |
---|
[a420b49] | 65 | bool |
---|
| 66 | fDirectory(const char *fnm) |
---|
| 67 | { |
---|
[2d9e394f] | 68 | struct stat buf; |
---|
[8a01812] | 69 | if (!fnm[0] || fnm[strlen(fnm) - 1] == FNM_SEP_LEV |
---|
| 70 | #ifdef FNM_SEP_LEV2 |
---|
| 71 | || fnm[strlen(fnm) - 1] == FNM_SEP_LEV2 |
---|
| 72 | #endif |
---|
| 73 | ) return 1; |
---|
[affaeee] | 74 | #ifdef HAVE_LSTAT |
---|
[df97cc9] | 75 | /* On Unix, dereference any symlinks we might encounter */ |
---|
| 76 | if (lstat(fnm, &buf) != 0) return 0; |
---|
| 77 | #else |
---|
[a420b49] | 78 | if (stat(fnm, &buf) != 0) return 0; |
---|
[df97cc9] | 79 | #endif |
---|
[be97baf] | 80 | #ifdef S_ISDIR |
---|
| 81 | /* POSIX way */ |
---|
| 82 | return S_ISDIR(buf.st_mode); |
---|
| 83 | #else |
---|
| 84 | /* BSD way */ |
---|
[2d9e394f] | 85 | return ((buf.st_mode & S_IFMT) == S_IFDIR); |
---|
[be97baf] | 86 | #endif |
---|
[d1b1380] | 87 | } |
---|
| 88 | |
---|
| 89 | #else |
---|
| 90 | # error Unknown OS |
---|
| 91 | #endif |
---|