diff -U3 buildtorrent-0.8/buildtorrent.c buildtorrent-0.8-wormfood_mod/buildtorrent.c
--- buildtorrent-0.8/buildtorrent.c	2010-01-31 18:01:30.000000000 +0800
+++ buildtorrent-0.8-wormfood_mod/buildtorrent.c	2010-02-07 20:44:35.000000000 +0800
@@ -18,6 +18,9 @@
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
+/* define this as GNU source, so that the versionsort will work with scandir */
+#define _GNU_SOURCE
+
 #include "config.h"
 
 #include <getopt.h>
@@ -844,20 +847,40 @@
 returns an error flag
 ******************************************************************************/
 bt_error bt_find_files(
-  DIR *directory, size_t maxlength, char *path, bt_data pathlist, bt_file_list files
+  char sort, size_t maxlength, char *path, bt_data pathlist, bt_file_list files
 ) {
-  struct dirent *entry;
+  struct dirent **entry;
   bt_data filename = NULL;
   bt_data filepath = NULL;
   bt_error err = BT_ERROR_NONE;
-  while ((entry = readdir(directory))) {
-    if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
+  int i,n;
+
+  // replaced readdir with scandir, so we can get directory listings in alphabetical order
+  switch(sort)
+  {
+    default:
+#ifdef _GNU_SOURCE
+    case 'v':
+      i = scandir(path, &entry, 0, versionsort); // read directory entry in "version" order, ie. 1 2 10, not 1 10 2
+      break;
+#endif
+    case 'a':
+    case 's':
+      i = scandir(path, &entry, 0, alphasort); // read directory entry in "version" order, ie. 1 2 10, not 1 10 2
+      break;
+    case 'u':
+      i = scandir(path, &entry, 0, 0); // read directory entry normal order (directory order, seemingly random)
+      break;
+  }
+  if (i >=0 )  { // read directory entry in "version" order, ie. 1 2 10, not 1 10 2
+  for(n=0;n<i;n++) {
+    if (strcmp(entry[n]->d_name, ".") && strcmp(entry[n]->d_name, "..")) {
       struct stat s;
       size_t oldlength = strlen(path);
-      if (!(err = bt_joinpath(maxlength, path, entry->d_name))) {
+      if (!(err = bt_joinpath(maxlength, path, entry[n]->d_name))) {
         if (!stat(path, &s)) {
           if (S_ISREG(s.st_mode)) {
-            if ((filename = bt_string(strlen(entry->d_name), entry->d_name))) {
+            if ((filename = bt_string(strlen(entry[n]->d_name), entry[n]->d_name))) {
               if ((filepath = bt_copy(pathlist))) {
                 if (!(err = bt_list_append(filepath, filename))) {
                   err = bt_file_list_prepend(files, path, filepath);
@@ -869,12 +892,10 @@
               err = BT_ERROR_MEMORY;
             }
           } else if (S_ISDIR(s.st_mode)) {
-            DIR* dir;
-            if ((dir = opendir(path))) {
-              if ((filename = bt_string(strlen(entry->d_name), entry->d_name))) {
+              if ((filename = bt_string(strlen(entry[n]->d_name), entry[n]->d_name))) {
                 if ((filepath = bt_copy(pathlist))) {
                   if (!(err = bt_list_append(filepath, filename))) {
-                    err = bt_find_files(dir, maxlength, path, filepath, files);
+                    err = bt_find_files(sort, maxlength, path, filepath, files);
                   }
                 } else {
                   err = BT_ERROR_MEMORY;
@@ -882,10 +903,6 @@
               } else {
                 err = BT_ERROR_MEMORY;
               }
-              closedir(dir);
-            } else {
-              err = BT_ERROR_IO;
-            }
           } else {
             /* FIXME neither regular file nor directory, what to do? */
           }
@@ -899,6 +916,9 @@
       break;
     }
   }
+  } else {
+    err = BT_ERROR_IO;
+  }
   return err;
 }
 
@@ -1370,9 +1390,16 @@
     "--nodate          -D               : omit 'creation date' field\n"
     "--nocreator       -C               : omit 'created by' field\n"
     "--md5sum          -m               : add an 'md5sum' field for each file\n"
-    "--show            -s               : show generated torrent structure\n"
-    "--showall         -S               : show pieces too (implies '-s')\n"
+//    "--show            -s               : show generated torrent structure\n"
+//    "--showall         -S               : show pieces too (implies '-s')\n"
+    "--sort            -s <sortorder>   : sort files. [U]nsorted, "
+#ifdef _GNU_SOURCE
+    "[A]lpha and [V]ersion\n"
+#else
+    "and [A]lpha\n"
+#endif
     "--quiet           -q               : quiet operation\n"
+    "--verbose         -v               : verbose. More -v means more verbose\n"
     "--version         -V               : show version of buildtorrent\n"
     "--help            -h               : show this help screen\n"
   );
@@ -1392,6 +1419,11 @@
   char *outfile = NULL;
   char *commentstr = NULL;
   char *filelistfilename = NULL;
+#ifdef _GNU_SOURCE
+  char sort = 'v'; // default to version sort, if available
+#else
+  char sort = 'a'; // otherwise default to alpha sort
+#endif
   int lplen = -1;
   unsigned int plen = 262144;
   int verbose = 1;
@@ -1404,7 +1436,6 @@
   int slen;
   int i;
 
-  DIR *dir = NULL;
   FILE *output = NULL;
   bt_data torrent = NULL;
   bt_data announce = NULL;
@@ -1443,17 +1474,19 @@
       { "piecesize", 1, 0, 'L' },
       { "comment", 1, 0, 'c' },
       { "private", 1, 0, 'p' },
+      { "sort", 1, 0, 's' },
       { "nodate", 0, 0, 'D' },
       { "nocreator", 0, 0, 'C' },
       { "md5sum", 0, 0, 'm' },
-      { "show", 0, 0, 's' },
-      { "showpieces", 0, 0, 'S' },
+//      { "show", 0, 0, 's' },
+//      { "showpieces", 0, 0, 'S' },
       { "quiet", 0, 0, 'q' },
+      { "verbose", 0, 0, 'v' },
       { "version", 0, 0, 'V' },
       { "help", 0, 0, 'h' },
       { 0, 0, 0, 0 }
     };
-    char c = getopt_long(argc, argv, "hVqSsmCDa:f:n:A:w:l:L:c:p:", options, &optidx );
+    char c = getopt_long(argc, argv, "hVqvmCDs:a:f:n:A:w:l:L:c:p:", options, &optidx );
     if (c == -1) {
       break;
     }
@@ -1488,6 +1521,9 @@
       privated = 1;
       privateopt = (strcmp(optarg, "0") == 0) ? 0 : 1;
       break;
+    case ('s'):
+      sort=tolower(optarg[0]);
+      break;
     case ('D'):
       nodate = 1;
       break;
@@ -1497,11 +1533,10 @@
     case ('m'):
       domd5sum = 1;
       break;
-    case ('s'):
-      show = 1;
-      break;
-    case ('S'):
-      show = 2;
+    case ('v'):
+      show++;
+      if(show > 2)
+        show=2;
       break;
     case ('q'):
       verbose = 0;
@@ -1647,20 +1682,15 @@
   if (inname && S_ISDIR(s.st_mode)) {
 
     multifile = 1;
-    if (!(dir = opendir(inname))) {
-      fprintf(stderr, "buildtorrent: couldn't open directory\n");
-      return 1;
-    }
     if (!(pathlist = bt_list())) {
       fprintf(stderr, "buildtorrent: couldn't path list\n");
       return 1;
     }
     memcpy(path, inname, strlen(inname) + 1);
-    if (bt_find_files(dir, 8192, path, pathlist, flist)) {
+    if (bt_find_files(sort, 8192, path, pathlist, flist)) {
       fprintf(stderr, "buildtorrent: error finding files\n");
       return 1;
     }
-    closedir(dir);
 
   } else if (inname && S_ISREG(s.st_mode)) {
