]> git.sur5r.net Git - cc65/blobdiff - src/common/searchpath.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / src / common / searchpath.c
index 78d2cce4c9e4a9bf349377312b2d3391a3b6ebf8..16945f36d4ab344a2a57533286cc6de4561be477 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2010, Ullrich von Bassewitz                                      */
+/* (C) 2000-2013, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -53,7 +53,7 @@
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -69,7 +69,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 */
@@ -130,8 +130,8 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub
 
     const char* EnvVal = getenv (EnvVar);
     if (EnvVal == 0) {
-       /* Not found */
-       return;
+        /* Not found */
+        return;
     }
 
     /* Copy the environment variable to the buffer */
@@ -139,9 +139,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,6 +157,53 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub
 
 
 
+void AddSubSearchPathFromWinBin (SearchPath* 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)
+
+    char Dir[_MAX_PATH];
+    char* Ptr;
+
+    if (_get_pgmptr (&Ptr) != 0) {
+        return;
+    }
+    strcpy (Dir, Ptr);
+
+    /* Remove binary name */
+    Ptr = strrchr (Dir, '\\');
+    if (Ptr == 0) {
+        return;
+    }
+    *Ptr = '\0';
+
+    /* Check for 'bin' directory */
+    Ptr = strrchr (Dir, '\\');
+    if (Ptr == 0) {
+        return;
+    }
+    if (strcmp (Ptr++, "\\bin") != 0) {
+        return;
+    }
+
+    /* Append SubDir */
+    strcpy (Ptr, SubDir);
+
+    /* Add the search path */
+    AddSearchPath (P, Dir);
+
+#else
+
+    (void) P;
+    (void) SubDir;
+
+#endif
+}
+
+
 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,
@@ -190,18 +237,6 @@ void PopSearchPath (SearchPath* P)
 
 
 
-void ForgetSearchPath (SearchPath* P)
-/* Forget all search paths in the given list */
-{
-    unsigned I;
-    for (I = 0; I < CollCount (P); ++I) {
-        xfree (CollAt (P, I));
-    }
-    CollDeleteAll (P);
-}
-
-
-
 char* SearchFile (const SearchPath* 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.
@@ -217,19 +252,19 @@ 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 */