source: git/src/osdepend.c @ a72ed95

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since a72ed95 was 8bc1384, checked in by Olly Betts <olly@…>, 10 years ago

src/osdepend.c: Don't use isalpha() for checking drive letters as
it's locale dependent.

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/* osdepend.c
2 * OS dependent functions
3 * Copyright (C) 1993-2003,2004,2005,2014 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  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
31/* NB "c:fred" isn't relative. Eg "c:\data\c:fred" won't work */
32bool
33fAbsoluteFnm(const char *fnm)
34{
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');
40}
41
42#elif OS_UNIX
43
44bool
45fAbsoluteFnm(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
65bool
66fDirectory(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#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
87}
88
89#else
90# error Unknown OS
91#endif
Note: See TracBrowser for help on using the repository browser.