]> git.sur5r.net Git - cc65/blobdiff - src/common/searchpath.c
Fixed LinuxDoc Tools issues in some verbatim blocks in the Atari document.
[cc65] / src / common / searchpath.c
index bd3a1f60363ef975ec8c7a4af90113eb8a61ddc5..ca7017e6fd4a404e296ba5b4a4a4ff91c221e4bd 100644 (file)
 
 #include <stdlib.h>
 #include <string.h>
+#if defined(_WIN32)
+#  include <windows.h>
+#endif
 #if defined(_MSC_VER)
 /* Microsoft compiler */
 #  include <io.h>
-#  pragma warning(disable : 4996)
 #else
 /* Anyone else */
 #  include <unistd.h>
@@ -53,7 +55,7 @@
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -69,7 +71,7 @@ static char* CleanupPath (const char* Path)
 
     /* Check for a trailing path separator and remove it */
     if (Len > 0 && (Path[Len-1] == '\\' || Path[Len-1] == '/')) {
-       --Len;
+        --Len;
     }
 
     /* Allocate memory for the new string */
@@ -83,7 +85,7 @@ static char* CleanupPath (const char* Path)
 
 
 
-static void Add (SearchPath* P, const char* New)
+static void Add (SearchPaths* 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 */
@@ -92,7 +94,7 @@ static void Add (SearchPath* P, const char* New)
 
 
 
-SearchPath* NewSearchPath (void)
+SearchPaths* NewSearchPath (void)
 /* Create a new, empty search path list */
 {
     return NewCollection ();
@@ -100,7 +102,7 @@ SearchPath* NewSearchPath (void)
 
 
 
-void AddSearchPath (SearchPath* P, const char* NewPath)
+void AddSearchPath (SearchPaths* P, const char* NewPath)
 /* Add a new search path to the end of an existing list */
 {
     /* Allow a NULL path */
@@ -111,27 +113,27 @@ void AddSearchPath (SearchPath* P, const char* NewPath)
 
 
 
-void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar)
+void AddSearchPathFromEnv (SearchPaths* P, const char* EnvVar)
 /* Add a search path from an environment variable to the end of an existing
- * list.
- */
+** list.
+*/
 {
     AddSearchPath (P, getenv (EnvVar));
 }
 
 
 
-void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* SubDir)
+void AddSubSearchPathFromEnv (SearchPaths* P, const char* EnvVar, const char* SubDir)
 /* Add a search path from an environment variable, adding a subdirectory to
- * the environment variable value.
- */
+** the environment variable value.
+*/
 {
     StrBuf Dir = AUTO_STRBUF_INITIALIZER;
 
     const char* EnvVal = getenv (EnvVar);
     if (EnvVal == 0) {
-       /* Not found */
-       return;
+        /* Not found */
+        return;
     }
 
     /* Copy the environment variable to the buffer */
@@ -139,9 +141,9 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub
 
     /* Add a path separator if necessary */
     if (SB_NotEmpty (&Dir)) {
-       if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
-           SB_AppendChar (&Dir, '/');
-       }
+        if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
+            SB_AppendChar (&Dir, '/');
+        }
     }
 
     /* Add the subdirectory and terminate the string */
@@ -157,21 +159,20 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub
 
 
 
-void AddSubSearchPathFromWinBin (SearchPath* P, const char* SubDir)
+void AddSubSearchPathFromWinBin (SearchPaths* P, const char* SubDir)
 {
 /* Windows only:
- * Add a search path from the running binary, adding a subdirectory to
- * the parent directory of the directory containing the binary.
- */
-#if defined(_MSC_VER)
+** Add a search path from the running binary, adding a subdirectory to
+** the parent directory of the directory containing the binary.
+*/
+#if defined(_WIN32)
 
     char Dir[_MAX_PATH];
     char* Ptr;
 
-    if (_get_pgmptr (&Ptr) != 0) {
+    if (GetModuleFileName (NULL, Dir, _MAX_PATH) == 0) {
         return;
     }
-    strcpy (Dir, Ptr);
 
     /* Remove binary name */
     Ptr = strrchr (Dir, '\\');
@@ -204,11 +205,11 @@ void AddSubSearchPathFromWinBin (SearchPath* P, const char* SubDir)
 }
 
 
-int PushSearchPath (SearchPath* P, const char* NewPath)
+int PushSearchPath (SearchPaths* 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.
- */
+** 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);   
@@ -227,7 +228,7 @@ int PushSearchPath (SearchPath* P, const char* NewPath)
 
 
 
-void PopSearchPath (SearchPath* P)
+void PopSearchPath (SearchPaths* P)
 /* Remove a search path from the head of an existing search path list */
 {
     /* Remove the path at position 0 */
@@ -237,10 +238,22 @@ void PopSearchPath (SearchPath* P)
 
 
 
-char* SearchFile (const SearchPath* P, const char* File)
+char* GetSearchPath (SearchPaths* P, unsigned Index)
+/* Return the search path at the given index, if the index is valid, return an
+** empty string otherwise.
+*/
+{
+    if (Index < CollCount (P))
+        return CollAtUnchecked (P, Index);
+    return "";
+}
+
+
+
+char* SearchFile (const SearchPaths* P, const char* File)
 /* 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.
- */
+** area that contains the complete path, if found, return 0 otherwise.
+*/
 {
     char* Name = 0;
     StrBuf PathName = AUTO_STRBUF_INITIALIZER;
@@ -252,25 +265,22 @@ char* SearchFile (const SearchPath* P, const char* File)
         /* 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));
+        /* 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;
-       }
+        }
     }
 
     /* Cleanup and return the result of the search */
     SB_Done (&PathName);
     return Name;
 }
-
-
-