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
#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 */
+4 -4
View File
@@ -72,16 +72,16 @@ int copyFile (uio_DirHandle *srcDir, const char *srcName,
uio_DirHandle *dstDir, const char *newName);
bool fileExists (const char *name);
bool fileExists2(uio_DirHandle *dir, const char *fileName);
#ifdef WIN32
#ifdef HAVE_UNC_PATHS
size_t skipUNCServerShare(const char *inPath);
#endif
#endif /* HAVE_UNC_PATHS */
#ifdef WIN32
#ifdef HAVE_DRIVE_LETTERS
static inline int isDriveLetter(int c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
#endif
#endif /* HAVE_DRIVE_LETTERS */
#endif /* _FILE_H */
+41 -19
View File
@@ -29,11 +29,16 @@
#include "misc.h"
#include "libs/log.h"
#ifdef HAVE_DRIVE_LETTERS
# include <ctype.h>
// For tolower()
#endif /* HAVE_DRIVE_LETTERS */
#ifdef WIN32
# include <direct.h>
# include <ctype.h>
// For _getdcwd()
#else
# include <pwd.h>
// For getpwuid()
#endif
/* Try to find a suitable value for %APPDATA% if it isn't defined on
@@ -72,7 +77,7 @@ mkdirhier (const char *path)
ptr = buf;
pathstart = path;
#ifdef WIN32
#ifdef HAVE_DRIVE_LETTERS
if (isDriveLetter(pathstart[0]) && pathstart[1] == ':')
{
// Driveletter + semicolon on Windows.
@@ -87,8 +92,13 @@ mkdirhier (const char *path)
log_add (log_Error, "Can't stat \"%s\": %s", buf, strerror (errno));
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
// it, or stat it. Don't create a dir for the share either.
*(ptr++) = *(pathstart++);
@@ -119,7 +129,12 @@ mkdirhier (const char *path)
goto err;
}
}
#endif
#else
{
// Making sure that there is an 'else' case if HAVE_DRIVE_LETTERS is
// defined.
}
#endif /* HAVE_UNC_PATHS */
if (*pathstart == '/')
*(ptr++) = *(pathstart++);
@@ -487,9 +502,9 @@ expandPath (char *dest, size_t len, const char *src, int what)
{
/* Replacing backslashes in path by slashes. */
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.
size_t skip = skipUNCServerShare (destptr);
if (skip != 0)
@@ -500,7 +515,7 @@ expandPath (char *dest, size_t len, const char *src, int what)
destptr += skip;
}
}
#endif
#endif /* HAVE_UNC_PATHS */
while (*destptr != '\0')
{
if (*destptr == '\\')
@@ -521,17 +536,24 @@ expandPath (char *dest, size_t len, const char *src, int what)
char *endPart;
pathStart = dest;
#ifdef WIN32
#ifdef HAVE_DRIVE_LETTERS
if (isDriveLetter(pathStart[0]) && (pathStart[1] == ':'))
{
pathStart += 2;
}
else
#endif /* HAVE_DRIVE_LETTERS */
#ifdef HAVE_UNC_PATHS
{
// Test for a UNC path.
// Test for a Universal Naming Convention path.
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] == '/')
pathStart++;
@@ -619,7 +641,7 @@ err:
return -1;
}
#ifdef WIN32
#ifdef HAVE_DRIVE_LETTERS
// letter is 0 based: 0 = A, 1 = B, ...
bool
driveLetterExists(int letter)
@@ -630,7 +652,7 @@ driveLetterExists(int letter)
return ((drives >> letter) & 1) != 0;
}
#endif
#endif /* HAVE_DRIVE_LETTERS */
// helper for expandPath, expanding an absolute path
// 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;
#ifdef WIN32
#ifdef HAVE_DRIVE_LETTERS
if (isDriveLetter(src[0]) && (src[1] == ':'))
{
int letter;
@@ -686,7 +708,7 @@ expandPathAbsolute (char *dest, size_t destLen, const char *src,
src += 2;
}
else
#endif
#endif /* HAVE_DRIVE_LETTERS */
{
// Relative dir
if (getcwd (dest, destLen) == NULL)
@@ -709,9 +731,9 @@ expandPathAbsolute (char *dest, size_t destLen, const char *src,
destLen -= tempLen;
}
if (dest[-1] != '/'
#ifdef WIN32
#ifdef BACKSLASH_IS_PATH_SEPARATOR
&& dest[-1] != '\\'
#endif
#endif /* BACKSLASH_IS_PATH_SEPARATOR */
)
{
// 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.
// Does not skip trailing slashes.
size_t
@@ -766,6 +788,6 @@ skipUNCServerShare(const char *inPath) {
return (size_t) (path - inPath);
}
#endif
#endif /* HAVE_UNC_PATHS */