From Jeff Foster: add support for Cisco HDLC captures.
[obnox/wireshark/wip.git] / fileset.c
index d0f020004416f9844da9f7a2fe043e8c380162aa..08201f7ee33d5f446b34cf472d342f13de892600 100644 (file)
--- a/fileset.c
+++ b/fileset.c
@@ -3,8 +3,8 @@
  *
  * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
@@ -26,8 +26,8 @@
 # include "config.h"
 #endif
 
-#ifdef HAVE_IO_H
-#include <io.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
 #endif
 
 #ifdef HAVE_FCNTL_H
@@ -51,6 +51,7 @@
 
 #include <glib.h>
 
+#include <wiretap/file_util.h>
 #include "globals.h"
 
 #include <epan/filesystem.h>
@@ -83,8 +84,8 @@ fileset_filename_match_pattern(const char *fname)
 
     /* test_00001_20050418010750.cap */
     pfx = strrchr(filename, '.');
-    if(pfx == NULL) {
-        return FALSE;
+    if(pfx == NULL) {  /* suffix is optional */
+        pfx = filename + strlen(filename);
     }
     /* test_00001_20050418010750 */
     *pfx = '\0';
@@ -110,7 +111,7 @@ fileset_filename_match_pattern(const char *fname)
     while(minlen--) {
         baselen--;
 
-        if(!isdigit(filename[baselen])) {
+        if(!isdigit( (int) filename[baselen])) {
             g_free(filename);
             return FALSE;
         }
@@ -124,6 +125,7 @@ fileset_filename_match_pattern(const char *fname)
 
 
 /* test, if both files could be in the same file set */
+/* (the filenames must already be in correct shape) */
 gboolean
 fileset_is_file_in_set(const char *fname1, const char *fname2)
 {
@@ -134,12 +136,20 @@ fileset_is_file_in_set(const char *fname1, const char *fname2)
     int         minlen = strlen("_00001_20050418010750");
 
 
+    /* just to be sure ... */
+    g_assert(fileset_filename_match_pattern(fname1));
+    g_assert(fileset_filename_match_pattern(fname2));
+
     dup_f1 = g_strdup(fname1);
     dup_f2 = g_strdup(fname2);
 
     pfx1 = strrchr(dup_f1, '.');
     pfx2 = strrchr(dup_f2, '.');
+    /* suffix is optional */
+    if (!pfx1) pfx1 = dup_f1 + strlen(dup_f1);
+    if (!pfx2) pfx2 = dup_f2 + strlen(dup_f2);
 
+    /* the optional suffix (file extension) must be equal */
     if(strcmp(pfx1, pfx2) != 0) {
         g_free(dup_f1);
         g_free(dup_f2);
@@ -162,7 +172,7 @@ fileset_is_file_in_set(const char *fname1, const char *fname2)
 
 
 /* we know this file is part of the set, so add it */
-fileset_entry *
+static fileset_entry *
 fileset_add_file(const char *dirname, const char *fname, gboolean current)
 {
     int fh, result;
@@ -173,7 +183,7 @@ fileset_add_file(const char *dirname, const char *fname, gboolean current)
 
     path = g_strdup_printf("%s%s", dirname, fname);
 
-    fh = open( path, O_RDONLY );
+    fh = eth_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
     if(fh !=  -1) {
 
         /* Get statistics */
@@ -193,7 +203,7 @@ fileset_add_file(const char *dirname, const char *fname, gboolean current)
             set.entries = g_list_append(set.entries, entry);
         }
 
-        close(fh);
+        eth_close(fh);
     }
 
     g_free(path);
@@ -202,14 +212,14 @@ fileset_add_file(const char *dirname, const char *fname, gboolean current)
 }
 
 
-/* compare two list entries by creation date/time */
-gint
-fileset_compare_creation(gconstpointer a, gconstpointer b)
+/* compare two list entries by creation date/time (through filename) */
+static gint
+fileset_sort_compare(gconstpointer a, gconstpointer b)
 {
     const fileset_entry *entry_a = a;
     const fileset_entry *entry_b = b;
 
-    return entry_a->ctime - entry_b->ctime;
+       return strcmp(entry_a->name, entry_b->name);
 }
 
 
@@ -232,15 +242,9 @@ void fileset_update_dlg(void)
 void
 fileset_add_dir(const char *fname)
 {
-#if GLIB_MAJOR_VERSION < 2
-    DIR           *dir;             /* scanned directory */
-    struct dirent *file;            /* current file */
-    gchar         *name;
-#else /* GLIB 2 */
-    GDir          *dir;             /* scanned directory */
-    GError        **dummy;
+    ETH_DIR       *dir;             /* scanned directory */
+    ETH_DIRENT    *file;            /* current file */
     const char    *name;
-#endif
     fileset_entry *entry;
     GString       *dirname;
     gchar         *fname_dup;
@@ -255,37 +259,29 @@ fileset_add_dir(const char *fname)
     
     dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
 
-    dummy = g_malloc(sizeof(GError *));
-    *dummy = NULL;
-
-    /* if the current file can't be part of any fileset, do nothing */
-    if(!fileset_filename_match_pattern(fname)) {
+    /* is the current file probably a part of any fileset? */
+    if(fileset_filename_match_pattern(fname)) {
+        /* yes, go through the files in the directory and check if the file in question is part of the current file set */
+        if ((dir = eth_dir_open(dirname->str, 0, NULL)) != NULL) {
+               while ((file = eth_dir_read_name(dir)) != NULL) {
+                   name = eth_dir_get_name(file);
+                if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
+                    fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
+                }
+            } /* while */
+
+            eth_dir_close(dir);
+        } /* if */
+    } else {
+        /* no, this is a "standalone file", just add this one */
         entry = fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
-        if(entry) {
-            fileset_dlg_add_file(entry);
-        }
-    }
-
-    /* go through the files in the directory and check if it's part of the current file set */
-#if GLIB_MAJOR_VERSION < 2
-    if ((dir = opendir(dirname)) != NULL) {
-           while ((file = readdir(dir)) != NULL) {
-               name = (gchar *)file->d_name;
-#else
-    if ((dir = g_dir_open(dirname->str, 0, dummy)) != NULL) {
-        while ((name = g_dir_read_name(dir)) != NULL) {
-#endif
-            if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
-                fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
-            }
-        }
+        /* don't add the file to the dialog here, this will be done in fileset_update_dlg() below */
     }
 
-    g_free(dummy);
     g_string_free(dirname, TRUE /* free_segment */);
 
     /* sort entries by creation time */
-    set.entries = g_list_sort(set.entries, fileset_compare_creation);
+    set.entries = g_list_sort(set.entries, fileset_sort_compare);
 
     fileset_update_dlg();
 }
@@ -300,7 +296,7 @@ fileset_get_dirname(void)
 
 
 /* get the current list entry, or NULL */
-GList *
+static GList *
 fileset_get_current(void)
 {
     GList         *le;
@@ -364,7 +360,7 @@ fileset_get_previous(void)
 
 
 /* delete a single entry */
-void fileset_entry_delete(gpointer data, gpointer user_data)
+static void fileset_entry_delete(gpointer data, gpointer user_data _U_)
 {
     fileset_entry *entry = data;