Expanding environment variables first.
git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@1188 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
@@ -172,11 +172,12 @@ getHomeDir (void)
|
|||||||
int
|
int
|
||||||
expandPath (char *dest, size_t len, const char *src, int what)
|
expandPath (char *dest, size_t len, const char *src, int what)
|
||||||
{
|
{
|
||||||
char *destend;
|
char *destptr, *destend;
|
||||||
char *destptr;
|
char *buf, *bufptr, *bufend;
|
||||||
|
const char *srcend;
|
||||||
|
|
||||||
#define CHECKLEN(n) \
|
#define CHECKLEN(bufname, n) \
|
||||||
if (destptr + (n) >= destend) \
|
if (bufname##ptr + (n) >= bufname##end) \
|
||||||
{ \
|
{ \
|
||||||
errno = ENAMETOOLONG; \
|
errno = ENAMETOOLONG; \
|
||||||
return -1; \
|
return -1; \
|
||||||
@@ -187,60 +188,11 @@ expandPath (char *dest, size_t len, const char *src, int what)
|
|||||||
destptr = dest;
|
destptr = dest;
|
||||||
destend = dest + len;
|
destend = dest + len;
|
||||||
|
|
||||||
if (what & EP_HOME)
|
|
||||||
{
|
|
||||||
if (src[0] == '~')
|
|
||||||
{
|
|
||||||
const char *home;
|
|
||||||
size_t homelen;
|
|
||||||
|
|
||||||
if (src[1] != '/')
|
|
||||||
{
|
|
||||||
errno = EINVAL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
home = getHomeDir ();
|
|
||||||
if (home == NULL)
|
|
||||||
{
|
|
||||||
errno = ENOENT;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
homelen = strlen (home);
|
|
||||||
|
|
||||||
if (what & EP_ABSOLUTE) {
|
|
||||||
dest = expandPathAbsolute (dest, destend - dest,
|
|
||||||
home, what);
|
|
||||||
if (dest == NULL)
|
|
||||||
{
|
|
||||||
// errno is set
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
what &= ~EP_ABSOLUTE;
|
|
||||||
// The part after the '~' should not be seen
|
|
||||||
// as absolute.
|
|
||||||
}
|
|
||||||
|
|
||||||
CHECKLEN (homelen);
|
|
||||||
memcpy (destptr, home, homelen);
|
|
||||||
destptr += homelen;
|
|
||||||
src++; /* skip the ~ */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (what & EP_ABSOLUTE)
|
|
||||||
{
|
|
||||||
destptr = expandPathAbsolute (destptr, destend - destptr, src,
|
|
||||||
what);
|
|
||||||
if (destptr == NULL)
|
|
||||||
{
|
|
||||||
// errno is set
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (what & EP_ENVVARS)
|
if (what & EP_ENVVARS)
|
||||||
{
|
{
|
||||||
|
buf = alloca (len);
|
||||||
|
bufptr = buf;
|
||||||
|
bufend = buf + len;
|
||||||
while (*src != '\0')
|
while (*src != '\0')
|
||||||
{
|
{
|
||||||
switch (*src)
|
switch (*src)
|
||||||
@@ -290,11 +242,12 @@ expandPath (char *dest, size_t len, const char *src, int what)
|
|||||||
{
|
{
|
||||||
#define APPDATA_STRING "\\Application Data"
|
#define APPDATA_STRING "\\Application Data"
|
||||||
envVarLen = strlen (envVar);
|
envVarLen = strlen (envVar);
|
||||||
CHECKLEN (envVarLen + sizeof (APPDATA_STRING) - 1);
|
CHECKLEN (buf,
|
||||||
strcpy (destptr, envVar);
|
envVarLen + sizeof (APPDATA_STRING) - 1);
|
||||||
destptr += envVarLen;
|
strcpy (bufptr, envVar);
|
||||||
strcpy (destptr, APPDATA_STRING);
|
bufptr += envVarLen;
|
||||||
destptr += sizeof (APPDATA_STRING) - 1;
|
strcpy (bufptr, APPDATA_STRING);
|
||||||
|
bufptr += sizeof (APPDATA_STRING) - 1;
|
||||||
src = end + 1;
|
src = end + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -304,9 +257,9 @@ expandPath (char *dest, size_t len, const char *src, int what)
|
|||||||
"Falling back to \"..\\userdata\" for %%APPDATA%%"
|
"Falling back to \"..\\userdata\" for %%APPDATA%%"
|
||||||
"\n");
|
"\n");
|
||||||
#define APPDATA_FALLBACK_STRING "..\\userdata"
|
#define APPDATA_FALLBACK_STRING "..\\userdata"
|
||||||
CHECKLEN (sizeof (APPDATA_FALLBACK_STRING) - 1);
|
CHECKLEN (buf, sizeof (APPDATA_FALLBACK_STRING) - 1);
|
||||||
strcpy (destptr, APPDATA_FALLBACK_STRING);
|
strcpy (bufptr, APPDATA_FALLBACK_STRING);
|
||||||
destptr += sizeof (APPDATA_FALLBACK_STRING) - 1;
|
bufptr += sizeof (APPDATA_FALLBACK_STRING) - 1;
|
||||||
src = end + 1;
|
src = end + 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -318,9 +271,9 @@ expandPath (char *dest, size_t len, const char *src, int what)
|
|||||||
}
|
}
|
||||||
|
|
||||||
envVarLen = strlen (envVar);
|
envVarLen = strlen (envVar);
|
||||||
CHECKLEN (envVarLen);
|
CHECKLEN (buf, envVarLen);
|
||||||
strcpy (destptr, envVar);
|
strcpy (bufptr, envVar);
|
||||||
destptr += envVarLen;
|
bufptr += envVarLen;
|
||||||
src = end + 1;
|
src = end + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -333,18 +286,76 @@ expandPath (char *dest, size_t len, const char *src, int what)
|
|||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
CHECKLEN(1);
|
CHECKLEN(buf, 1);
|
||||||
*(destptr++) = *(src++);
|
*(bufptr++) = *(src++);
|
||||||
break;
|
break;
|
||||||
} // switch
|
} // switch
|
||||||
} // while
|
} // while
|
||||||
*destptr = '\0';
|
*bufptr = '\0';
|
||||||
|
src = buf;
|
||||||
|
srcend = bufptr;
|
||||||
} // if (what & EP_ENVVARS)
|
} // if (what & EP_ENVVARS)
|
||||||
else
|
else
|
||||||
|
srcend = src + strlen (src);
|
||||||
|
|
||||||
|
|
||||||
|
if (what & EP_HOME)
|
||||||
{
|
{
|
||||||
strcpy (destptr, src);
|
if (src[0] == '~')
|
||||||
|
{
|
||||||
|
const char *home;
|
||||||
|
size_t homelen;
|
||||||
|
|
||||||
|
if (src[1] != '/')
|
||||||
|
{
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
home = getHomeDir ();
|
||||||
|
if (home == NULL)
|
||||||
|
{
|
||||||
|
errno = ENOENT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
homelen = strlen (home);
|
||||||
|
|
||||||
|
if (what & EP_ABSOLUTE) {
|
||||||
|
destptr = expandPathAbsolute (dest, destend - dest,
|
||||||
|
home, what);
|
||||||
|
if (destptr == NULL)
|
||||||
|
{
|
||||||
|
// errno is set
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
what &= ~EP_ABSOLUTE;
|
||||||
|
// The part after the '~' should not be seen
|
||||||
|
// as absolute.
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECKLEN (dest, homelen);
|
||||||
|
memcpy (destptr, home, homelen);
|
||||||
|
destptr += homelen;
|
||||||
|
src++; /* skip the ~ */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (what & EP_ABSOLUTE)
|
||||||
|
{
|
||||||
|
destptr = expandPathAbsolute (destptr, destend - destptr, src,
|
||||||
|
what);
|
||||||
|
if (destptr == NULL)
|
||||||
|
{
|
||||||
|
// errno is set
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECKLEN (dest, srcend - src);
|
||||||
|
memcpy (destptr, src, srcend - src + 1);
|
||||||
|
// The +1 is for the '\0'. It is already taken into account by
|
||||||
|
// CHECKLEN.
|
||||||
|
|
||||||
if (what & EP_SLASHES)
|
if (what & EP_SLASHES)
|
||||||
{
|
{
|
||||||
/* Replacing backslashes in path by slashes. */
|
/* Replacing backslashes in path by slashes. */
|
||||||
|
|||||||
Reference in New Issue
Block a user