Fix coding style
[survex.git] / src / osdepend.c
blob865eaf069b9eb4cb08404f8cfa9c80c93fffac1d
1 /* osdepend.c
2 * OS dependent functions
3 * Copyright (C) 1993-2003,2004,2005,2014 Olly Betts
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <string.h>
25 #include "whichos.h"
26 #include "useful.h"
27 #include "osdepend.h"
29 #if OS_WIN32
31 /* NB "c:fred" isn't relative. Eg "c:\data\c:fred" won't work */
32 bool
33 fAbsoluteFnm(const char *fnm)
35 /* <drive letter>: or \<path> or /<path>
36 * or \\<host>\... or //<host>/... */
37 unsigned char ch = (unsigned char)*fnm;
38 return ch == '/' || ch == '\\' ||
39 (ch && fnm[1] == ':' && (ch | 32) >= 'a' && (ch | 32) <= 'z');
42 #elif OS_UNIX
44 bool
45 fAbsoluteFnm(const char *fnm)
47 return (fnm[0] == '/');
50 #endif
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).
59 #if OS_UNIX || OS_WIN32
61 # include <sys/types.h>
62 # include <sys/stat.h>
63 # include <stdio.h>
65 bool
66 fDirectory(const char *fnm)
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 #ifdef HAVE_LSTAT
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
89 #else
90 # error Unknown OS
91 #endif