1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-15 17:30:06 +00:00

Change the implementation of Add() so it won't modify it's argument.

git-svn-id: svn://svn.cc65.org/cc65/trunk@14 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2000-05-30 06:31:09 +00:00
parent bca9ccd5b8
commit a1c89d9aed
3 changed files with 23 additions and 28 deletions

View File

@ -32,36 +32,31 @@ static char* UserIncludePath = 0;
static char* Add (char* Orig, char* New) static char* Add (char* Orig, const char* New)
/* Create a new path from Orig and New, delete Orig, return the result */ /* Create a new path from Orig and New, delete Orig, return the result */
{ {
unsigned Len, NewLen; unsigned OrigLen, NewLen;
char* NewPath; char* NewPath;
/* Check for a trailing path separator and remove it */ /* Get the length of the original string */
NewLen = strlen (New); OrigLen = Orig? strlen (Orig) : 0;
if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
New [--NewLen] = '\0';
}
/* Calculate the length of the combined paths */ /* Get the length of the new path */
if (Orig) { NewLen = strlen (New);
Len = strlen (Orig) + NewLen;
} else { /* Check for a trailing path separator and remove it */
Len = NewLen; if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) {
--NewLen;
} }
/* Allocate memory for the new string */ /* Allocate memory for the new string */
NewPath = xmalloc (Len + 2); NewPath = xmalloc (OrigLen + NewLen + 2);
/* Copy the strings */ /* Copy the strings */
if (Orig) { memcpy (NewPath, Orig, OrigLen);
strcpy (NewPath, Orig); memcpy (NewPath+OrigLen, New, NewLen);
} else { NewPath [OrigLen+NewLen+0] = ';';
NewPath [0] = '\0'; NewPath [OrigLen+NewLen+1] = '\0';
}
strcat (NewPath, New);
strcat (NewPath, ";");
/* Delete the original path */ /* Delete the original path */
xfree (Orig); xfree (Orig);
@ -72,12 +67,12 @@ static char* Add (char* Orig, char* New)
static char* Find (char* Path, char* File) static char* Find (const char* Path, const char* File)
/* Search for a file in a list of directories. If found, return the complete /* Search for a file in a list of directories. If found, return the complete
* name including the path in a malloced data area, if not found, return 0. * name including the path in a malloced data area, if not found, return 0.
*/ */
{ {
char* P; const char* P;
unsigned Count; unsigned Count;
int Max; int Max;
char PathName [FILENAME_MAX]; char PathName [FILENAME_MAX];
@ -126,7 +121,7 @@ static char* Find (char* Path, char* File)
void AddIncludePath (char* NewPath, unsigned Where) void AddIncludePath (const char* NewPath, unsigned Where)
/* Add a new include path to the existing one */ /* Add a new include path to the existing one */
{ {
/* Allow a NULL path */ /* Allow a NULL path */
@ -142,7 +137,7 @@ void AddIncludePath (char* NewPath, unsigned Where)
char* FindInclude (char* Name, unsigned Where) char* FindInclude (const char* Name, unsigned Where)
/* Find an include file. Return a pointer to a malloced area that contains /* Find an include file. Return a pointer to a malloced area that contains
* the complete path, if found, return 0 otherwise. * the complete path, if found, return 0 otherwise.
*/ */
@ -160,3 +155,4 @@ char* FindInclude (char* Name, unsigned Where)

View File

@ -28,10 +28,10 @@
void AddIncludePath (char* NewPath, unsigned Where); void AddIncludePath (const char* NewPath, unsigned Where);
/* Add a new include path to the existing one */ /* Add a new include path to the existing one */
char* FindInclude (char* Name, unsigned Where); char* FindInclude (const char* Name, unsigned Where);
/* Find an include file. Return a pointer to a malloced area that contains /* Find an include file. Return a pointer to a malloced area that contains
* the complete path, if found, return 0 otherwise. * the complete path, if found, return 0 otherwise.
*/ */

View File

@ -445,8 +445,7 @@ static void Compile (void)
AddIncludePath ("", INC_USER); /* Current directory */ AddIncludePath ("", INC_USER); /* Current directory */
AddIncludePath ("include", INC_SYS); AddIncludePath ("include", INC_SYS);
#ifdef CC65_INC #ifdef CC65_INC
/* Allow modifications of the given string by dup'ing it */ AddIncludePath (CC65_INC, INC_SYS);
AddIncludePath (xstrdup (CC65_INC), INC_SYS);
#else #else
AddIncludePath ("/usr/lib/cc65/include", INC_SYS); AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
#endif #endif