shithub: sox

Download patch

ref: 3d55a193330e1da375d33d394275e56602e67d7e
parent: cb3ed3c66705de48de46142afff3d58053754267
author: Mans Rullgard <mans@mansr.com>
date: Wed Aug 19 07:48:32 EDT 2020

build: simplify win32 glob selection

Move the files to a subdirectory so the header can be activated by
simply adding a -I flag, avoiding ugly ifdefs.  Use this version if
a native glob is missing and FindFirstFileA() exists.  This is more
accurate than matching the system name since some mingw versions do
have a glob implementation.

--- a/configure.ac
+++ b/configure.ac
@@ -13,10 +13,7 @@
 AC_CONFIG_SRCDIR(sox.1)
 AC_CONFIG_HEADERS([src/soxconfig.h])
 
-dnl Find target architecture
-AC_CANONICAL_TARGET
-
-AM_INIT_AUTOMAKE
+AM_INIT_AUTOMAKE([subdir-objects])
 AM_SILENT_RULES([yes])
 
 LT_INIT([win32-dll])
@@ -62,16 +59,12 @@
 AC_OPENMP
 CFLAGS="$CFLAGS $OPENMP_CFLAGS"
 
-using_win32_glob="no"
-case $target in
-  *mingw*)
-  using_win32_glob="yes"
-   ;;
-esac
-if test "$using_win32_glob" = yes; then
-   AC_DEFINE([HAVE_WIN32_GLOB_H], 1, [Define to 1 to use win32 glob])
+if test "$ac_cv_header_glob_h" != yes; then
+    AC_CHECK_FUNC([FindFirstFileA], [
+        AC_DEFINE([HAVE_GLOB_H], [1], [Define if glob.h exists])
+        HAVE_WIN32_GLOB=yes])
 fi
-AM_CONDITIONAL(HAVE_WIN32_GLOB, test x$using_win32_glob = xyes)
+AM_CONDITIONAL(HAVE_WIN32_GLOB, test "$HAVE_WIN32_GLOB" = yes)
 
 AC_ARG_WITH(dyn-default,AS_HELP_STRING([--with-dyn-default],[Default to loading optional formats dynamically]),opt_default=dyn,opt_default=yes)
 
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -27,7 +27,8 @@
 include_HEADERS = sox.h
 sox_SOURCES = sox.c
 if HAVE_WIN32_GLOB
-sox_SOURCES += win32-glob.c win32-glob.h
+sox_SOURCES += win32/glob.c win32/glob.h
+AM_CPPFLAGS += -I$(srcdir)/win32
 endif
 sox_LDADD = libsox.la
 example0_SOURCES = example0.c
--- a/src/sox.c
+++ b/src/sox.c
@@ -37,10 +37,7 @@
 #include <sys/types.h>
 #include <time.h>
 
-#if defined(HAVE_WIN32_GLOB_H)
-  #include "win32-glob.h"
-  #define HAVE_GLOB_H 1
-#elif defined(HAVE_GLOB_H)
+#if defined(HAVE_GLOB_H)
   #include <glob.h>
 #endif
 
--- a/src/win32-glob.c
+++ /dev/null
@@ -1,170 +1,0 @@
-/* libSoX minimal glob for MS-Windows: (c) 2009 SoX contributors
- *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or (at
- * your option) any later version.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#include "win32-glob.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include <errno.h>
-#define WIN32_LEAN_AND_MEAN 1
-#include <windows.h>
-
-typedef struct file_entry
-{
-    char name[MAX_PATH];
-    struct file_entry *next;
-} file_entry;
-
-static int
-insert(
-    const char* path,
-    const char* name,
-    file_entry** phead)
-{
-    int len;
-    file_entry* cur = malloc(sizeof(file_entry));
-    if (!cur)
-    {
-        return ENOMEM;
-    }
-
-    len = _snprintf(cur->name, MAX_PATH, "%s%s", path, name);
-    cur->name[MAX_PATH - 1] = 0;
-    cur->next = *phead;
-    *phead = cur;
-
-    return len < 0 || len >= MAX_PATH ? ENAMETOOLONG : 0;
-}
-
-static int
-entry_comparer(
-    const void* pv1,
-    const void* pv2)
-{
-	const file_entry* const * pe1 = pv1;
-	const file_entry* const * pe2 = pv2;
-    return _stricmp((*pe1)->name, (*pe2)->name);
-}
-
-int
-glob(
-    const char *pattern,
-    int flags,
-    void *unused,
-    glob_t *pglob)
-{
-    char path[MAX_PATH];
-    file_entry *head = NULL;
-    int err = 0;
-    size_t len;
-    unsigned entries = 0;
-    WIN32_FIND_DATAA finddata;
-    HANDLE hfindfile;
-
-    if (!pattern || flags != (flags & GLOB_FLAGS) || unused || !pglob)
-    {
-        errno = EINVAL;
-        return EINVAL;
-    }
-
-    path[MAX_PATH - 1] = 0;
-    strncpy(path, pattern, MAX_PATH);
-    if (path[MAX_PATH - 1] != 0)
-    {
-        errno = ENAMETOOLONG;
-        return ENAMETOOLONG;
-    }
-
-    len = strlen(path);
-    while (len > 0 && path[len - 1] != '/' && path[len - 1] != '\\')
-        len--;
-    path[len] = 0;
-
-    hfindfile = FindFirstFileA(pattern, &finddata);
-    if (hfindfile == INVALID_HANDLE_VALUE)
-    {
-        if (flags & GLOB_NOCHECK)
-        {
-            err = insert("", pattern, &head);
-            entries++;
-        }
-    }
-    else
-    {
-        do
-        {
-            err = insert(path, finddata.cFileName, &head);
-            entries++;
-        } while (!err && FindNextFileA(hfindfile, &finddata));
-
-        FindClose(hfindfile);
-    }
-
-    if (err == 0)
-    {
-        pglob->gl_pathv = malloc((entries + 1) * sizeof(char*));
-        if (pglob->gl_pathv)
-        {
-            pglob->gl_pathc = entries;
-            pglob->gl_pathv[entries] = NULL;
-            for (; head; head = head->next, entries--)
-                pglob->gl_pathv[entries - 1] = (char*)head;
-            qsort(pglob->gl_pathv, pglob->gl_pathc, sizeof(char*), entry_comparer);
-        }
-        else
-        {
-            pglob->gl_pathc = 0;
-            err = ENOMEM;
-        }
-    }
-    else if (pglob)
-    {
-        pglob->gl_pathc = 0;
-        pglob->gl_pathv = NULL;
-    }
-
-    if (err)
-    {
-        file_entry *cur;
-        while (head)
-        {
-            cur = head;
-            head = head->next;
-            free(cur);
-        }
-
-        errno = err;
-    }
-
-    return err;
-}
-
-void
-globfree(
-    glob_t* pglob)
-{
-    if (pglob)
-    {
-        char** cur;
-        for (cur = pglob->gl_pathv; *cur; cur++)
-        {
-            free(*cur);
-        }
-
-        pglob->gl_pathc = 0;
-        pglob->gl_pathv = NULL;
-    }
-}
--- a/src/win32-glob.h
+++ /dev/null
@@ -1,49 +1,0 @@
-/* libSoX minimal glob for MS-Windows: (c) 2009 SoX contributors
- *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or (at
- * your option) any later version.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#ifndef GLOB_H
-#define GLOB_H 1
-
-#define GLOB_NOCHECK (16)
-#define GLOB_FLAGS (GLOB_NOCHECK)
-
-typedef struct glob_t
-{
-    unsigned gl_pathc;
-    char **gl_pathv;
-} glob_t;
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int
-glob(
-    const char *pattern,
-    int flags,
-    void *unused,
-    glob_t *pglob);
-
-void
-globfree(
-    glob_t* pglob);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* ifndef GLOB_H */
--- /dev/null
+++ b/src/win32/glob.c
@@ -1,0 +1,170 @@
+/* libSoX minimal glob for MS-Windows: (c) 2009 SoX contributors
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "glob.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#define WIN32_LEAN_AND_MEAN 1
+#include <windows.h>
+
+typedef struct file_entry
+{
+    char name[MAX_PATH];
+    struct file_entry *next;
+} file_entry;
+
+static int
+insert(
+    const char* path,
+    const char* name,
+    file_entry** phead)
+{
+    int len;
+    file_entry* cur = malloc(sizeof(file_entry));
+    if (!cur)
+    {
+        return ENOMEM;
+    }
+
+    len = _snprintf(cur->name, MAX_PATH, "%s%s", path, name);
+    cur->name[MAX_PATH - 1] = 0;
+    cur->next = *phead;
+    *phead = cur;
+
+    return len < 0 || len >= MAX_PATH ? ENAMETOOLONG : 0;
+}
+
+static int
+entry_comparer(
+    const void* pv1,
+    const void* pv2)
+{
+	const file_entry* const * pe1 = pv1;
+	const file_entry* const * pe2 = pv2;
+    return _stricmp((*pe1)->name, (*pe2)->name);
+}
+
+int
+glob(
+    const char *pattern,
+    int flags,
+    void *unused,
+    glob_t *pglob)
+{
+    char path[MAX_PATH];
+    file_entry *head = NULL;
+    int err = 0;
+    size_t len;
+    unsigned entries = 0;
+    WIN32_FIND_DATAA finddata;
+    HANDLE hfindfile;
+
+    if (!pattern || flags != (flags & GLOB_FLAGS) || unused || !pglob)
+    {
+        errno = EINVAL;
+        return EINVAL;
+    }
+
+    path[MAX_PATH - 1] = 0;
+    strncpy(path, pattern, MAX_PATH);
+    if (path[MAX_PATH - 1] != 0)
+    {
+        errno = ENAMETOOLONG;
+        return ENAMETOOLONG;
+    }
+
+    len = strlen(path);
+    while (len > 0 && path[len - 1] != '/' && path[len - 1] != '\\')
+        len--;
+    path[len] = 0;
+
+    hfindfile = FindFirstFileA(pattern, &finddata);
+    if (hfindfile == INVALID_HANDLE_VALUE)
+    {
+        if (flags & GLOB_NOCHECK)
+        {
+            err = insert("", pattern, &head);
+            entries++;
+        }
+    }
+    else
+    {
+        do
+        {
+            err = insert(path, finddata.cFileName, &head);
+            entries++;
+        } while (!err && FindNextFileA(hfindfile, &finddata));
+
+        FindClose(hfindfile);
+    }
+
+    if (err == 0)
+    {
+        pglob->gl_pathv = malloc((entries + 1) * sizeof(char*));
+        if (pglob->gl_pathv)
+        {
+            pglob->gl_pathc = entries;
+            pglob->gl_pathv[entries] = NULL;
+            for (; head; head = head->next, entries--)
+                pglob->gl_pathv[entries - 1] = (char*)head;
+            qsort(pglob->gl_pathv, pglob->gl_pathc, sizeof(char*), entry_comparer);
+        }
+        else
+        {
+            pglob->gl_pathc = 0;
+            err = ENOMEM;
+        }
+    }
+    else if (pglob)
+    {
+        pglob->gl_pathc = 0;
+        pglob->gl_pathv = NULL;
+    }
+
+    if (err)
+    {
+        file_entry *cur;
+        while (head)
+        {
+            cur = head;
+            head = head->next;
+            free(cur);
+        }
+
+        errno = err;
+    }
+
+    return err;
+}
+
+void
+globfree(
+    glob_t* pglob)
+{
+    if (pglob)
+    {
+        char** cur;
+        for (cur = pglob->gl_pathv; *cur; cur++)
+        {
+            free(*cur);
+        }
+
+        pglob->gl_pathc = 0;
+        pglob->gl_pathv = NULL;
+    }
+}
--- /dev/null
+++ b/src/win32/glob.h
@@ -1,0 +1,49 @@
+/* libSoX minimal glob for MS-Windows: (c) 2009 SoX contributors
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef GLOB_H
+#define GLOB_H 1
+
+#define GLOB_NOCHECK (16)
+#define GLOB_FLAGS (GLOB_NOCHECK)
+
+typedef struct glob_t
+{
+    unsigned gl_pathc;
+    char **gl_pathv;
+} glob_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int
+glob(
+    const char *pattern,
+    int flags,
+    void *unused,
+    glob_t *pglob);
+
+void
+globfree(
+    glob_t* pglob);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ifndef GLOB_H */