[xiph-commits] r16953 - in trunk/vorbis-tools: . ogg123

conrad at svn.xiph.org conrad at svn.xiph.org
Sat Mar 6 19:06:29 PST 2010


Author: conrad
Date: 2010-03-06 19:06:29 -0800 (Sat, 06 Mar 2010)
New Revision: 16953

Modified:
   trunk/vorbis-tools/configure.ac
   trunk/vorbis-tools/ogg123/playlist.c
Log:
Add directory contents to ogg123 in sorted order

Patch by Ray Kohler in this thread:
http://lists.xiph.org/pipermail/vorbis-dev/2010-March/020109.html

This patch adds a configure check for scandir and alphasort, and
if present it sorts directory contents, eg. when a directory name
is given on the commandline.

Tested on Linux with these functions present, and also tested that
the old codepath still works.

Modified: trunk/vorbis-tools/configure.ac
===================================================================
--- trunk/vorbis-tools/configure.ac	2010-03-07 02:17:10 UTC (rev 16952)
+++ trunk/vorbis-tools/configure.ac	2010-03-07 03:06:29 UTC (rev 16953)
@@ -317,7 +317,7 @@
 
 AC_FUNC_ALLOCA
 AM_ICONV
-AC_CHECK_FUNCS(atexit on_exit fcntl select stat chmod)
+AC_CHECK_FUNCS(atexit on_exit fcntl select stat chmod alphasort scandir)
 AM_LANGINFO_CODESET
 
 dnl --------------------------------------------------

Modified: trunk/vorbis-tools/ogg123/playlist.c
===================================================================
--- trunk/vorbis-tools/ogg123/playlist.c	2010-03-07 02:17:10 UTC (rev 16952)
+++ trunk/vorbis-tools/ogg123/playlist.c	2010-03-07 03:06:29 UTC (rev 16953)
@@ -120,8 +120,63 @@
 }
 
 /* Recursively adds files from the directory and subdirectories */
+#if defined(HAVE_ALPHASORT) && defined(HAVE_SCANDIR)
 int playlist_append_directory(playlist_t *list, char *dirname)
 {
+  int dir_len = strlen(dirname);
+  int num_entries = 0, i = 0;
+  struct dirent **entries;
+  struct stat stat_buf;
+  char nextfile[NAME_MAX + 1];
+
+  num_entries = scandir(dirname, &entries, 0, alphasort);
+
+  if (num_entries < 0) {
+    return 0;
+  }
+
+  for (i=0; i<num_entries; i++) {
+    int sub_len = strlen(entries[i]->d_name);
+
+    /* Make sure full pathname is within limits and we don't parse the
+       relative directory entries. */
+    if (dir_len + sub_len + 1 < NAME_MAX 
+        && strcmp(entries[i]->d_name, ".") != 0  
+        && strcmp(entries[i]->d_name, "..") != 0  ) {
+
+      /* Build the new full pathname */
+      strcpy(nextfile, dirname);
+      strcat(nextfile, "/");
+      strcat(nextfile, entries[i]->d_name);
+
+      if (stat(nextfile, &stat_buf) == 0) {
+        
+        /* Decide what type of entry this is */
+        if (S_ISDIR(stat_buf.st_mode)) {
+          
+        /* Recursively follow directories */
+          if ( playlist_append_directory(list, nextfile) == 0 ) {
+            fprintf(stderr,
+                  _("Warning: Could not read directory %s.\n"), 
+                    nextfile);
+          }
+        } else {
+        /* Assume everything else is a file of some sort */
+          playlist_append_file(list, nextfile);
+        }
+      }
+    }
+
+    free(entries[i]);
+  }
+
+  free(entries);
+
+  return 1;
+}
+#else
+int playlist_append_directory(playlist_t *list, char *dirname)
+{
   DIR *dir;
   int dir_len = strlen(dirname);
   struct dirent *entry;
@@ -174,6 +229,7 @@
 
   return 1;
 }
+#endif
 
 
 /* Opens a file containing filenames, one per line, and adds them to the



More information about the commits mailing list