Support drive letters and UNC paths on some non-Windows platforms.

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3043 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
Meep-Eep
2008-08-22 19:57:35 +00:00
parent 3ec51c7c0f
commit 5ed92e6942
3 changed files with 59 additions and 23 deletions
+14
View File
@@ -503,5 +503,19 @@ typedef unsigned int wint_t;
# define UNBUFFERED_LOGFILE # define UNBUFFERED_LOGFILE
#endif #endif
#if defined(WIN32) || defined(__SYMBIAN32__)
// HAVE_DRIVE_LETTERS is defined to signify that DOS/Windows style drive
// letters are to be recognised on this platform.
# define HAVE_DRIVE_LETTERS
// HAVE_UNC_PATHS is defined to signify that Universal Naming Convention
// style paths are to be recognised on this platform.
# define HAVE_UNC_PATHS
// BACKSLASH_IS_PATH_SEPARATOR is defined to signify that the backslash
// character is to be recognised as a path separator on this platform.
// This does not affect the acceptance of forward slashes as path
// separators.
# define BACKSLASH_IS_PATH_SEPARATOR
#endif
#endif /* _PORT_H */ #endif /* _PORT_H */
+4 -4
View File
@@ -72,16 +72,16 @@ int copyFile (uio_DirHandle *srcDir, const char *srcName,
uio_DirHandle *dstDir, const char *newName); uio_DirHandle *dstDir, const char *newName);
bool fileExists (const char *name); bool fileExists (const char *name);
bool fileExists2(uio_DirHandle *dir, const char *fileName); bool fileExists2(uio_DirHandle *dir, const char *fileName);
#ifdef WIN32 #ifdef HAVE_UNC_PATHS
size_t skipUNCServerShare(const char *inPath); size_t skipUNCServerShare(const char *inPath);
#endif #endif /* HAVE_UNC_PATHS */
#ifdef WIN32 #ifdef HAVE_DRIVE_LETTERS
static inline int isDriveLetter(int c) static inline int isDriveLetter(int c)
{ {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
} }
#endif #endif /* HAVE_DRIVE_LETTERS */
#endif /* _FILE_H */ #endif /* _FILE_H */
+41 -19
View File
@@ -29,11 +29,16 @@
#include "misc.h" #include "misc.h"
#include "libs/log.h" #include "libs/log.h"
#ifdef HAVE_DRIVE_LETTERS
# include <ctype.h>
// For tolower()
#endif /* HAVE_DRIVE_LETTERS */
#ifdef WIN32 #ifdef WIN32
# include <direct.h> # include <direct.h>
# include <ctype.h> // For _getdcwd()
#else #else
# include <pwd.h> # include <pwd.h>
// For getpwuid()
#endif #endif
/* Try to find a suitable value for %APPDATA% if it isn't defined on /* Try to find a suitable value for %APPDATA% if it isn't defined on
@@ -72,7 +77,7 @@ mkdirhier (const char *path)
ptr = buf; ptr = buf;
pathstart = path; pathstart = path;
#ifdef WIN32 #ifdef HAVE_DRIVE_LETTERS
if (isDriveLetter(pathstart[0]) && pathstart[1] == ':') if (isDriveLetter(pathstart[0]) && pathstart[1] == ':')
{ {
// Driveletter + semicolon on Windows. // Driveletter + semicolon on Windows.
@@ -87,8 +92,13 @@ mkdirhier (const char *path)
log_add (log_Error, "Can't stat \"%s\": %s", buf, strerror (errno)); log_add (log_Error, "Can't stat \"%s\": %s", buf, strerror (errno));
goto err; goto err;
} }
} else if (pathstart[0] == '\\' && pathstart[1] == '\\') { }
// Windows UNC path. (\\server\share\...) else
#endif /* HAVE_DRIVE_LETTERS */
#ifdef HAVE_UNC_PATHS
if (pathstart[0] == '\\' && pathstart[1] == '\\')
{
// Universal Naming Convention path. (\\server\share\...)
// Copy the server part as is; don't try to create directories for // Copy the server part as is; don't try to create directories for
// it, or stat it. Don't create a dir for the share either. // it, or stat it. Don't create a dir for the share either.
*(ptr++) = *(pathstart++); *(ptr++) = *(pathstart++);
@@ -119,7 +129,12 @@ mkdirhier (const char *path)
goto err; goto err;
} }
} }
#endif #else
{
// Making sure that there is an 'else' case if HAVE_DRIVE_LETTERS is
// defined.
}
#endif /* HAVE_UNC_PATHS */
if (*pathstart == '/') if (*pathstart == '/')
*(ptr++) = *(pathstart++); *(ptr++) = *(pathstart++);
@@ -487,9 +502,9 @@ expandPath (char *dest, size_t len, const char *src, int what)
{ {
/* Replacing backslashes in path by slashes. */ /* Replacing backslashes in path by slashes. */
destptr = dest; destptr = dest;
#ifdef WIN32 #ifdef HAVE_UNC_PATHS
{ {
// A Windows UNC path should always start with two backslashes // A UNC path should always start with two backslashes
// and have a backslash in between the server and share part. // and have a backslash in between the server and share part.
size_t skip = skipUNCServerShare (destptr); size_t skip = skipUNCServerShare (destptr);
if (skip != 0) if (skip != 0)
@@ -500,7 +515,7 @@ expandPath (char *dest, size_t len, const char *src, int what)
destptr += skip; destptr += skip;
} }
} }
#endif #endif /* HAVE_UNC_PATHS */
while (*destptr != '\0') while (*destptr != '\0')
{ {
if (*destptr == '\\') if (*destptr == '\\')
@@ -521,17 +536,24 @@ expandPath (char *dest, size_t len, const char *src, int what)
char *endPart; char *endPart;
pathStart = dest; pathStart = dest;
#ifdef WIN32 #ifdef HAVE_DRIVE_LETTERS
if (isDriveLetter(pathStart[0]) && (pathStart[1] == ':')) if (isDriveLetter(pathStart[0]) && (pathStart[1] == ':'))
{ {
pathStart += 2; pathStart += 2;
} }
else else
#endif /* HAVE_DRIVE_LETTERS */
#ifdef HAVE_UNC_PATHS
{ {
// Test for a UNC path. // Test for a Universal Naming Convention path.
pathStart += skipUNCServerShare(pathStart); pathStart += skipUNCServerShare(pathStart);
} }
#endif #else
{
// Making sure that there is an 'else' case if HAVE_DRIVE_LETTERS is
// defined.
}
#endif /* HAVE_UNC_PATHS */
if (pathStart[0] == '/') if (pathStart[0] == '/')
pathStart++; pathStart++;
@@ -619,7 +641,7 @@ err:
return -1; return -1;
} }
#ifdef WIN32 #ifdef HAVE_DRIVE_LETTERS
// letter is 0 based: 0 = A, 1 = B, ... // letter is 0 based: 0 = A, 1 = B, ...
bool bool
driveLetterExists(int letter) driveLetterExists(int letter)
@@ -630,7 +652,7 @@ driveLetterExists(int letter)
return ((drives >> letter) & 1) != 0; return ((drives >> letter) & 1) != 0;
} }
#endif #endif /* HAVE_DRIVE_LETTERS */
// helper for expandPath, expanding an absolute path // helper for expandPath, expanding an absolute path
// returns a pointer to the end of the filled in part of dest. // returns a pointer to the end of the filled in part of dest.
@@ -648,7 +670,7 @@ expandPathAbsolute (char *dest, size_t destLen, const char *src,
} }
orgSrc = src; orgSrc = src;
#ifdef WIN32 #ifdef HAVE_DRIVE_LETTERS
if (isDriveLetter(src[0]) && (src[1] == ':')) if (isDriveLetter(src[0]) && (src[1] == ':'))
{ {
int letter; int letter;
@@ -686,7 +708,7 @@ expandPathAbsolute (char *dest, size_t destLen, const char *src,
src += 2; src += 2;
} }
else else
#endif #endif /* HAVE_DRIVE_LETTERS */
{ {
// Relative dir // Relative dir
if (getcwd (dest, destLen) == NULL) if (getcwd (dest, destLen) == NULL)
@@ -709,9 +731,9 @@ expandPathAbsolute (char *dest, size_t destLen, const char *src,
destLen -= tempLen; destLen -= tempLen;
} }
if (dest[-1] != '/' if (dest[-1] != '/'
#ifdef WIN32 #ifdef BACKSLASH_IS_PATH_SEPARATOR
&& dest[-1] != '\\' && dest[-1] != '\\'
#endif #endif /* BACKSLASH_IS_PATH_SEPARATOR */
) )
{ {
// Need to add a slash. // Need to add a slash.
@@ -738,7 +760,7 @@ strrchr2(const char *start, int c, const char *end) {
} }
} }
#ifdef WIN32 #ifdef HAVE_UNC_PATHS
// returns 0 if the path is not a valid UNC path. // returns 0 if the path is not a valid UNC path.
// Does not skip trailing slashes. // Does not skip trailing slashes.
size_t size_t
@@ -766,6 +788,6 @@ skipUNCServerShare(const char *inPath) {
return (size_t) (path - inPath); return (size_t) (path - inPath);
} }
#endif #endif /* HAVE_UNC_PATHS */