Skip to content

Commit

Permalink
Fixed Unix build.
Browse files Browse the repository at this point in the history
There was a couple of strncpy() and other calls missing the len argument. Revised Dir_ExpandHome() to use ddstring.
  • Loading branch information
skyjake committed Jun 7, 2009
1 parent 7affb53 commit 23166dd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
2 changes: 2 additions & 0 deletions doomsday/engine/portable/include/m_string.h
Expand Up @@ -66,5 +66,7 @@ void Str_Strip(ddstring_t *ds);
const char *Str_GetLine(ddstring_t *ds, const char *src);
const char *Str_CopyDelim(ddstring_t* dest, const char* src, char delim);
int Str_CompareIgnoreCase(ddstring_t *ds, const char *text);
char Str_At(ddstring_t* str, int index);
char Str_RAt(ddstring_t* str, int reverseIndex);

#endif
34 changes: 34 additions & 0 deletions doomsday/engine/portable/src/m_string.c
Expand Up @@ -395,3 +395,37 @@ const char* Str_CopyDelim(ddstring_t* dest, const char* src, char delim)
// Skip past the delimiter.
return src + 1;
}

/**
* Retrieves a character in the string.
*
* @param str String to get the character from.
* @param index Index of the character.
*
* @return The character at @c index, or 0 if the index is not in range.
*/
char Str_At(ddstring_t* str, int index)
{
if(index < 0 || index >= str->length)
{
return 0;
}
return str->str[index];
}

/**
* Retrieves a character in the string. Indices start from the end of the string.
*
* @param str String to get the character from.
* @param reverseIndex Index of the character, where 0 is the last character of the string.
*
* @return The character at @c index, or 0 if the index is not in range.
*/
char Str_RAt(ddstring_t* str, int reverseIndex)
{
if(reverseIndex < 0 || reverseIndex >= str->length)
{
return 0;
}
return str->str[str->length - 1 - reverseIndex];
}
40 changes: 26 additions & 14 deletions doomsday/engine/portable/src/sys_direc.c
Expand Up @@ -122,7 +122,13 @@ void Dir_MakeDir(const char* path, directory_t* dir)
void Dir_FileDir(const char* str, directory_t* dir)
{
filename_t temp, pth;


if(!str)
{
// Nothing to do.
return;
}

M_TranslatePath(pth, str, FILENAME_T_MAXLEN);

_fullpath(temp, pth, FILENAME_T_MAXLEN);
Expand Down Expand Up @@ -213,29 +219,32 @@ void Dir_FixSlashes(char* path, size_t len)
* If the path begins with a tilde, replace it with either the value
* of the HOME environment variable or a user's home directory (from
* passwd).
*
* @param str Path to expand. Overwritten by the result.
* @param len Maximum length of str.
*/
void Dir_ExpandHome(char* str)
void Dir_ExpandHome(char* str, size_t len)
{
char buf[PATH_MAX];
ddstring_t *buf = NULL;

if(str[0] != '~')
return;

memset(buf, 0, sizeof(buf));
buf = Str_New();

if(str[1] == '/')
{
// Replace it with the HOME environment variable.
strncpy(buf, getenv("HOME"));
if(LAST_CHAR(buf) != '/')
strcat(buf, "/");
Str_Set(buf, getenv("HOME"));
if(Str_RAt(buf, 0) != '/')
Str_Append(buf, "/");

// Append the rest of the original path.
strcat(buf, str + 2);
Str_Append(buf, str + 2);
}
else
{
char userName[PATH_MAX], *end = NULL;
char userName[PATH_MAX], *end = NULL;
struct passwd *pw;

end = strchr(str + 1, '/');
Expand All @@ -244,16 +253,19 @@ void Dir_ExpandHome(char* str)

if((pw = getpwnam(userName)) != NULL)
{
strcpy(buf, pw->pw_dir);
if(LAST_CHAR(buf) != '/')
strcat(buf, "/");
Str_Set(buf, pw->pw_dir);
if(Str_RAt(buf, 0) != '/')
Str_Append(buf, "/");
}

strcat(buf, str + 1);
Str_Append(buf, str + 1);
}

// Replace the original.
strcpy(str, buf);
str[len - 1] = 0;
strncpy(str, Str_Text(buf), len - 1);

Str_Free(buf);
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/unix/src/dd_uinit.c
Expand Up @@ -114,11 +114,11 @@ static void determineGlobalPaths(application_t *app)
if(ArgCheckWith("-basedir", 1))
{
strcpy(ddBasePath, ArgNext());
Dir_ValidDir(ddBasePath);
Dir_ValidDir(ddBasePath, FILENAME_T_MAXLEN);
}

Dir_MakeAbsolute(ddBasePath);
Dir_ValidDir(ddBasePath);
Dir_MakeAbsolute(ddBasePath, FILENAME_T_MAXLEN);
Dir_ValidDir(ddBasePath, FILENAME_T_MAXLEN);

printf("determineGlobalPaths: Base path = %s\n", ddBasePath);
}
Expand Down

0 comments on commit 23166dd

Please sign in to comment.