1 | /* osdepend.c |
---|
2 | * OS dependent functions |
---|
3 | * Copyright (C) 1993-2003,2004 Olly Betts |
---|
4 | * |
---|
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. |
---|
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 |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
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 |
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <string.h> |
---|
25 | #include "whichos.h" |
---|
26 | #include "useful.h" |
---|
27 | #include "osdepend.h" |
---|
28 | |
---|
29 | #if (OS==WIN32) |
---|
30 | # include <ctype.h> /* needed for isalpha */ |
---|
31 | |
---|
32 | /* NB "c:fred" isn't relative. Eg "c:\data\c:fred" won't work */ |
---|
33 | bool |
---|
34 | fAbsoluteFnm(const char *fnm) |
---|
35 | { |
---|
36 | /* <drive letter>: or \<path> or /<path> |
---|
37 | * or \\<host>\... or //<host>/... */ |
---|
38 | unsigned char ch = (unsigned char)*fnm; |
---|
39 | return ((fnm[1] == ':' && isalpha(ch)) || ch == '\\' || ch == '/'); |
---|
40 | } |
---|
41 | |
---|
42 | #elif (OS==UNIX) |
---|
43 | |
---|
44 | bool |
---|
45 | fAbsoluteFnm(const char *fnm) |
---|
46 | { |
---|
47 | return (fnm[0] == '/'); |
---|
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, ...) |
---|
54 | * NB If fnm has a trailing directory separator (e.g. `/' or `/home/olly/' |
---|
55 | * then it's assumed to be a directory even if it doesn't exist (as is an |
---|
56 | * empty string). |
---|
57 | */ |
---|
58 | |
---|
59 | #if ((OS==UNIX) || (OS==WIN32)) |
---|
60 | |
---|
61 | # include <sys/types.h> |
---|
62 | # include <sys/stat.h> |
---|
63 | # include <stdio.h> |
---|
64 | |
---|
65 | bool |
---|
66 | fDirectory(const char *fnm) |
---|
67 | { |
---|
68 | struct stat buf; |
---|
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; |
---|
74 | #if (OS == UNIX) |
---|
75 | /* On Unix, dereference any symlinks we might encounter */ |
---|
76 | if (lstat(fnm, &buf) != 0) return 0; |
---|
77 | #else |
---|
78 | if (stat(fnm, &buf) != 0) return 0; |
---|
79 | #endif |
---|
80 | #ifdef S_ISDIR |
---|
81 | /* POSIX way */ |
---|
82 | return S_ISDIR(buf.st_mode); |
---|
83 | #else |
---|
84 | /* BSD way */ |
---|
85 | return ((buf.st_mode & S_IFMT) == S_IFDIR); |
---|
86 | #endif |
---|
87 | } |
---|
88 | |
---|
89 | #else |
---|
90 | # error Unknown OS |
---|
91 | #endif |
---|