2003-05-28 21:11:47 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* searchpath.h */
|
|
|
|
/* */
|
2010-05-09 10:54:15 +00:00
|
|
|
/* Handling of search paths */
|
2003-05-28 21:11:47 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2010-05-01 14:44:14 +00:00
|
|
|
/* (C) 2000-2010, Ullrich von Bassewitz */
|
2009-09-22 11:29:13 +00:00
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
2003-05-28 21:11:47 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
/* Microsoft compiler */
|
|
|
|
# include <io.h>
|
|
|
|
#else
|
|
|
|
/* Anyone else */
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* common */
|
2010-05-01 14:44:14 +00:00
|
|
|
#include "coll.h"
|
2003-05-28 21:11:47 +00:00
|
|
|
#include "searchpath.h"
|
2006-08-22 20:27:59 +00:00
|
|
|
#include "strbuf.h"
|
2003-05-28 21:11:47 +00:00
|
|
|
#include "xmalloc.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2010-05-09 10:54:15 +00:00
|
|
|
/* Code */
|
2003-05-28 21:11:47 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-28 11:22:44 +00:00
|
|
|
static char* CleanupPath (const char* Path)
|
|
|
|
/* Prepare and return a clean copy of Path */
|
2003-05-28 21:11:47 +00:00
|
|
|
{
|
2010-05-28 11:22:44 +00:00
|
|
|
unsigned Len;
|
2010-05-01 14:44:14 +00:00
|
|
|
char* NewPath;
|
2003-05-28 21:11:47 +00:00
|
|
|
|
2010-05-28 11:22:44 +00:00
|
|
|
/* Get the length of the path */
|
|
|
|
Len = strlen (Path);
|
2003-05-28 21:11:47 +00:00
|
|
|
|
|
|
|
/* Check for a trailing path separator and remove it */
|
2010-05-28 11:22:44 +00:00
|
|
|
if (Len > 0 && (Path[Len-1] == '\\' || Path[Len-1] == '/')) {
|
|
|
|
--Len;
|
2003-05-28 21:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory for the new string */
|
2010-05-28 11:22:44 +00:00
|
|
|
NewPath = (char*) xmalloc (Len + 1);
|
|
|
|
|
|
|
|
/* Copy the path and terminate it, then return the copy */
|
|
|
|
memcpy (NewPath, Path, Len);
|
|
|
|
NewPath [Len] = '\0';
|
|
|
|
return NewPath;
|
|
|
|
}
|
|
|
|
|
2003-05-28 21:11:47 +00:00
|
|
|
|
|
|
|
|
2010-05-28 11:22:44 +00:00
|
|
|
static void Add (SearchPath* P, const char* New)
|
|
|
|
/* Cleanup a new search path and add it to the list */
|
|
|
|
{
|
|
|
|
/* Add a clean copy of the path to the collection */
|
|
|
|
CollAppend (P, CleanupPath (New));
|
2003-05-28 21:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-09 10:54:15 +00:00
|
|
|
SearchPath* NewSearchPath (void)
|
|
|
|
/* Create a new, empty search path list */
|
|
|
|
{
|
|
|
|
return NewCollection ();
|
2003-05-28 21:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-09 10:54:15 +00:00
|
|
|
void AddSearchPath (SearchPath* P, const char* NewPath)
|
|
|
|
/* Add a new search path to the end of an existing list */
|
2003-05-28 21:11:47 +00:00
|
|
|
{
|
|
|
|
/* Allow a NULL path */
|
|
|
|
if (NewPath) {
|
2010-05-09 10:54:15 +00:00
|
|
|
Add (P, NewPath);
|
2003-05-28 21:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-09 10:54:15 +00:00
|
|
|
void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar)
|
|
|
|
/* Add a search path from an environment variable to the end of an existing
|
|
|
|
* list.
|
|
|
|
*/
|
2003-05-29 09:19:01 +00:00
|
|
|
{
|
2010-05-09 10:54:15 +00:00
|
|
|
AddSearchPath (P, getenv (EnvVar));
|
2003-05-29 09:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-09 10:54:15 +00:00
|
|
|
void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* SubDir)
|
2009-09-22 11:29:13 +00:00
|
|
|
/* Add a search path from an environment variable, adding a subdirectory to
|
|
|
|
* the environment variable value.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
StrBuf Dir = AUTO_STRBUF_INITIALIZER;
|
|
|
|
|
|
|
|
const char* EnvVal = getenv (EnvVar);
|
|
|
|
if (EnvVal == 0) {
|
|
|
|
/* Not found */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the environment variable to the buffer */
|
|
|
|
SB_CopyStr (&Dir, EnvVal);
|
|
|
|
|
|
|
|
/* Add a path separator if necessary */
|
|
|
|
if (SB_NotEmpty (&Dir)) {
|
|
|
|
if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
|
|
|
|
SB_AppendChar (&Dir, '/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 10:54:15 +00:00
|
|
|
/* Add the subdirectory and terminate the string */
|
2009-09-22 11:29:13 +00:00
|
|
|
SB_AppendStr (&Dir, SubDir);
|
|
|
|
SB_Terminate (&Dir);
|
|
|
|
|
|
|
|
/* Add the search path */
|
2010-05-09 10:54:15 +00:00
|
|
|
AddSearchPath (P, SB_GetConstBuf (&Dir));
|
2009-09-22 11:29:13 +00:00
|
|
|
|
|
|
|
/* Free the temp buffer */
|
|
|
|
SB_Done (&Dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-28 11:56:01 +00:00
|
|
|
int PushSearchPath (SearchPath* P, const char* NewPath)
|
|
|
|
/* Add a new search path to the head of an existing search path list, provided
|
|
|
|
* that it's not already there. If the path is already at the first position,
|
|
|
|
* return zero, otherwise return a non zero value.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Generate a clean copy of NewPath */
|
|
|
|
char* Path = CleanupPath (NewPath);
|
|
|
|
|
|
|
|
/* If we have paths, check if Path is already at position zero */
|
|
|
|
if (CollCount (P) > 0 && strcmp (CollConstAt (P, 0), Path) == 0) {
|
|
|
|
/* Match. Delete the copy and return to the caller */
|
|
|
|
xfree (Path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert a clean copy of the path at position 0, return success */
|
|
|
|
CollInsert (P, Path, 0);
|
|
|
|
return 1;
|
2010-05-28 11:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PopSearchPath (SearchPath* P)
|
|
|
|
/* Remove a search path from the head of an existing search path list */
|
|
|
|
{
|
|
|
|
/* Remove the path at position 0 */
|
|
|
|
xfree (CollAt (P, 0));
|
|
|
|
CollDelete (P, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-09 10:54:15 +00:00
|
|
|
void ForgetSearchPath (SearchPath* P)
|
|
|
|
/* Forget all search paths in the given list */
|
2003-08-03 11:51:12 +00:00
|
|
|
{
|
|
|
|
unsigned I;
|
2010-05-09 10:54:15 +00:00
|
|
|
for (I = 0; I < CollCount (P); ++I) {
|
|
|
|
xfree (CollAt (P, I));
|
2003-08-03 11:51:12 +00:00
|
|
|
}
|
2010-05-09 10:54:15 +00:00
|
|
|
CollDeleteAll (P);
|
2003-08-03 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-09 10:54:15 +00:00
|
|
|
char* SearchFile (const SearchPath* P, const char* File)
|
2003-05-28 21:11:47 +00:00
|
|
|
/* Search for a file in a list of directories. Return a pointer to a malloced
|
|
|
|
* area that contains the complete path, if found, return 0 otherwise.
|
|
|
|
*/
|
|
|
|
{
|
2010-05-09 10:54:15 +00:00
|
|
|
char* Name = 0;
|
|
|
|
StrBuf PathName = AUTO_STRBUF_INITIALIZER;
|
|
|
|
|
|
|
|
/* Start the search */
|
2003-05-28 21:11:47 +00:00
|
|
|
unsigned I;
|
2010-05-09 10:54:15 +00:00
|
|
|
for (I = 0; I < CollCount (P); ++I) {
|
|
|
|
|
|
|
|
/* Copy the next path element into the buffer */
|
|
|
|
SB_CopyStr (&PathName, CollConstAt (P, I));
|
|
|
|
|
|
|
|
/* Add a path separator and the filename */
|
|
|
|
if (SB_NotEmpty (&PathName)) {
|
|
|
|
SB_AppendChar (&PathName, '/');
|
|
|
|
}
|
|
|
|
SB_AppendStr (&PathName, File);
|
|
|
|
SB_Terminate (&PathName);
|
|
|
|
|
|
|
|
/* Check if this file exists */
|
|
|
|
if (access (SB_GetBuf (&PathName), 0) == 0) {
|
|
|
|
/* The file exists, we're done */
|
|
|
|
Name = xstrdup (SB_GetBuf (&PathName));
|
|
|
|
break;
|
|
|
|
}
|
2003-05-28 21:11:47 +00:00
|
|
|
}
|
2010-05-09 10:54:15 +00:00
|
|
|
|
|
|
|
/* Cleanup and return the result of the search */
|
|
|
|
SB_Done (&PathName);
|
|
|
|
return Name;
|
2003-05-28 21:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|