Patch for the Khronos-distributed http://www.khronos.org/registry/cl/ ICD Loader
to make it usable for regression testing in pocl development:
- Allow overriding the .icd search path with env OCL_ICD_VENDORS (like ocl-icd does)
- Do not fail in case the directory contains non .icd files.
- Allow paths without '/' suffix.
- Load drivers in guaranteed order.

--- icd/icd_linux.c
+++ icd/icd_linux.c
@@ -55,21 +55,26 @@
 // go through the list of vendors in the two configuration files
 void khrIcdOsVendorsEnumerate(void)
 {
-    DIR *dir = NULL;
-    struct dirent *dirEntry = NULL;
-    char *vendorPath = "/etc/OpenCL/vendors/";
-
-    // open the directory
-    dir = opendir(vendorPath);
-    if (NULL == dir) 
+    struct dirent **dirEntries = NULL;
+    int i;
+    char *vendorPath = getenv("OCL_ICD_VENDORS");
+    if (vendorPath == NULL || strncmp(vendorPath, "", 1) == 0)
+    {
+        vendorPath = "/etc/OpenCL/vendors";
+    }
+
+    // read the directory in guaranteed order
+    const int numEntries = scandir(vendorPath, &dirEntries, NULL, alphasort);
+    if (numEntries < 0)
     {
         KHR_ICD_TRACE("Failed to open path %s\n", vendorPath);
         goto Cleanup;
     }
 
     // attempt to load all files in the directory
-    for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir) )
+    for (i = 0; i < numEntries; ++i)
     {
+        const struct dirent *dirEntry = dirEntries[i];
         switch(dirEntry->d_type)
         {
         case DT_UNKNOWN:
@@ -85,21 +90,21 @@
                 // make sure the file name ends in .icd
                 if (strlen(extension) > strlen(dirEntry->d_name) )
                 {
-                    break;
+                    continue;
                 }
                 if (strcmp(dirEntry->d_name + strlen(dirEntry->d_name) - strlen(extension), extension) ) 
                 {
-                    break;
+                    continue;
                 }
 
                 // allocate space for the full path of the vendor library name
-                fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 1);
+                fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 2);
                 if (!fileName) 
                 {
                     KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
                     break;
                 }
-                sprintf(fileName, "%s%s", vendorPath, dirEntry->d_name);
+                sprintf(fileName, "%s/%s", vendorPath, dirEntry->d_name);
 
                 // open the file and read its contents
                 fin = fopen(fileName, "r");
@@ -146,9 +151,13 @@
 Cleanup:
 
     // free resources and exit
-    if (dir) 
+    if (dirEntries)
     {
-        closedir(dir);
+        for (i = 0; i < numEntries; ++i)
+        {
+            free(dirEntries[i]);
+        }
+        free(dirEntries);
     }
 }
 
