]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
util.h: removed dependency from PATH_MAX
authorinikep <inikep@gmail.com>
Tue, 13 Sep 2016 11:13:10 +0000 (13:13 +0200)
committerinikep <inikep@gmail.com>
Tue, 13 Sep 2016 11:13:10 +0000 (13:13 +0200)
programs/util.h

index ee130f4bd82dd5f47b8f2a156f67f155e3658ecc..645b615072858ebb9aacd1bbbd4454e58c02d41f 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
+ * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
  * All rights reserved.
  *
  * This source code is licensed under the BSD-style license found in the
@@ -205,42 +205,49 @@ UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
 
 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
 {
-    char path[MAX_PATH];
-    int pathLength, nbFiles = 0;
+    char* path;
+    int dirLength, fnameLength, pathLength, nbFiles = 0;
     WIN32_FIND_DATA cFile;
     HANDLE hFile;
 
-    pathLength = snprintf(path, MAX_PATH, "%s\\*", dirName);
-    if (pathLength < 0 || pathLength >= MAX_PATH) {
-        fprintf(stderr, "Path length has got too long.\n");
-        return 0;
-    }
+    dirLength = strlen(dirName);
+    path = (char*) malloc(dirLength + 3);
+    if (!path) return 0;
+
+    memcpy(path, dirName, dirLength);
+    path[dirLength] = '\\';
+    path[dirLength+1] = '*';
+    path[dirLength+2] = 0;
 
     hFile=FindFirstFile(path, &cFile);
     if (hFile == INVALID_HANDLE_VALUE) {
         fprintf(stderr, "Cannot open directory '%s'\n", dirName);
         return 0;
     }
+    free(path);
 
     do {
-        pathLength = snprintf(path, MAX_PATH, "%s\\%s", dirName, cFile.cFileName);
-        if (pathLength < 0 || pathLength >= MAX_PATH) {
-            fprintf(stderr, "Path length has got too long.\n");
-            continue;
-        }
+        fnameLength = strlen(cFile.cFileName);
+        path = (char*) malloc(dirLength + fnameLength + 2);
+        if (!path) { FindClose(hFile); return 0; }
+        memcpy(path, dirName, dirLength);
+        path[dirLength] = '\\';
+        memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
+        pathLength = dirLength+1+fnameLength;
+        path[pathLength] = 0;
         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
             if (strcmp (cFile.cFileName, "..") == 0 ||
                 strcmp (cFile.cFileName, ".") == 0) continue;
 
             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd);  /* Recursively call "UTIL_prepareFileList" with the new path. */
-            if (*bufStart == NULL) { FindClose(hFile); return 0; }
+            if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
         }
         else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
             if (*bufStart + *pos + pathLength >= *bufEnd) {
                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
                 *bufStart = (char*)realloc(*bufStart, newListSize);
                 *bufEnd = *bufStart + newListSize;
-                if (*bufStart == NULL) { FindClose(hFile); return 0; }
+                if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
             }
             if (*bufStart + *pos + pathLength < *bufEnd) {
                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
@@ -248,6 +255,7 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
                 nbFiles++;
             }
         }
+        free(path);
     } while (FindNextFile(hFile, &cFile));
 
     FindClose(hFile);
@@ -257,39 +265,43 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
 #elif (defined(__unix__) || defined(__unix) || defined(__midipix__) || (defined(__APPLE__) && defined(__MACH__))) && defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) /* snprintf, opendir */
 #  define UTIL_HAS_CREATEFILELIST
 #  include <dirent.h>       /* opendir, readdir */
-#  include <limits.h>       /* PATH_MAX */
 #  include <errno.h>
 
 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
 {
     DIR *dir;
     struct dirent *entry;
-    char path[PATH_MAX];
-    int pathLength, nbFiles = 0;
+    char* path;
+    int dirLength, fnameLength, pathLength, nbFiles = 0;
 
     if (!(dir = opendir(dirName))) {
         fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
         return 0;
     }
 
+    dirLength = strlen(dirName);
     errno = 0;
     while ((entry = readdir(dir)) != NULL) {
         if (strcmp (entry->d_name, "..") == 0 ||
             strcmp (entry->d_name, ".") == 0) continue;
-        pathLength = snprintf(path, PATH_MAX, "%s/%s", dirName, entry->d_name);
-        if (pathLength < 0 || pathLength >= PATH_MAX) {
-            fprintf(stderr, "Path length has got too long.\n");
-            continue;
-        }
+        fnameLength = strlen(entry->d_name);
+        path = (char*) malloc(dirLength + fnameLength + 2);
+        if (!path) { closedir(dir); return 0; }
+        memcpy(path, dirName, dirLength);
+        path[dirLength] = '/';
+        memcpy(path+dirLength+1, entry->d_name, fnameLength);
+        pathLength = dirLength+1+fnameLength;
+        path[pathLength] = 0;
+
         if (UTIL_isDirectory(path)) {
             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd);  /* Recursively call "UTIL_prepareFileList" with the new path. */
-            if (*bufStart == NULL) { closedir(dir); return 0; }
+            if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
         } else {
             if (*bufStart + *pos + pathLength >= *bufEnd) {
                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
                 *bufStart = (char*)realloc(*bufStart, newListSize);
                 *bufEnd = *bufStart + newListSize;
-                if (*bufStart == NULL) { closedir(dir); return 0; }
+                if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
             }
             if (*bufStart + *pos + pathLength < *bufEnd) {
                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
@@ -297,6 +309,7 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
                 nbFiles++;
             }
         }
+        free(path);
         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
     }