From a1c89d9aed34328b37363f9209e88c52f9cf5742 Mon Sep 17 00:00:00 2001 From: cuz Date: Tue, 30 May 2000 06:31:09 +0000 Subject: [PATCH] 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 --- src/cc65/include.c | 44 ++++++++++++++++++++------------------------ src/cc65/include.h | 4 ++-- src/cc65/main.c | 3 +-- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/cc65/include.c b/src/cc65/include.c index 2488d7196..e5d798e39 100644 --- a/src/cc65/include.c +++ b/src/cc65/include.c @@ -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 */ { - unsigned Len, NewLen; + unsigned OrigLen, NewLen; char* NewPath; - /* Check for a trailing path separator and remove it */ - NewLen = strlen (New); - if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) { - New [--NewLen] = '\0'; - } + /* Get the length of the original string */ + OrigLen = Orig? strlen (Orig) : 0; - /* Calculate the length of the combined paths */ - if (Orig) { - Len = strlen (Orig) + NewLen; - } else { - Len = NewLen; + /* Get the length of the new path */ + NewLen = strlen (New); + + /* Check for a trailing path separator and remove it */ + if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) { + --NewLen; } /* Allocate memory for the new string */ - NewPath = xmalloc (Len + 2); + NewPath = xmalloc (OrigLen + NewLen + 2); /* Copy the strings */ - if (Orig) { - strcpy (NewPath, Orig); - } else { - NewPath [0] = '\0'; - } - strcat (NewPath, New); - strcat (NewPath, ";"); + memcpy (NewPath, Orig, OrigLen); + memcpy (NewPath+OrigLen, New, NewLen); + NewPath [OrigLen+NewLen+0] = ';'; + NewPath [OrigLen+NewLen+1] = '\0'; /* Delete the original path */ 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 * name including the path in a malloced data area, if not found, return 0. */ { - char* P; + const char* P; unsigned Count; int 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 */ { /* 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 * the complete path, if found, return 0 otherwise. */ @@ -160,3 +155,4 @@ char* FindInclude (char* Name, unsigned Where) + diff --git a/src/cc65/include.h b/src/cc65/include.h index e8131b3d4..9d0b074fb 100644 --- a/src/cc65/include.h +++ b/src/cc65/include.h @@ -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 */ -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 * the complete path, if found, return 0 otherwise. */ diff --git a/src/cc65/main.c b/src/cc65/main.c index 296d29731..78090fd13 100644 --- a/src/cc65/main.c +++ b/src/cc65/main.c @@ -445,8 +445,7 @@ static void Compile (void) AddIncludePath ("", INC_USER); /* Current directory */ AddIncludePath ("include", INC_SYS); #ifdef CC65_INC - /* Allow modifications of the given string by dup'ing it */ - AddIncludePath (xstrdup (CC65_INC), INC_SYS); + AddIncludePath (CC65_INC, INC_SYS); #else AddIncludePath ("/usr/lib/cc65/include", INC_SYS); #endif