replace *a lot* of file related calls by their GLib counterparts. This is necessary...
[obnox/wireshark/wip.git] / epan / filesystem.c
index 40337f82010ed8d8050b1509cade9932009e5a6c..b77c7d07933aef6eab08ce92806b101bbdbe62da 100644 (file)
 #include <unistd.h>
 #endif
 
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #ifdef HAVE_WINDOWS_H
 #include <windows.h>
 #endif
 
-#ifdef HAVE_DIRECT_H
-#include <direct.h>            /* to declare "mkdir()" on Windows */
-#endif
-
 #ifndef _WIN32
 #include <pwd.h>
 #endif
 
 #include "filesystem.h"
+#include "file_util.h"
 
 /*
  * Given a pathname, return a pointer to the last pathname separator
@@ -184,7 +177,7 @@ test_for_directory(const char *path)
 {
        struct stat statb;
 
-       if (stat(path, &statb) < 0)
+       if (eth_stat(path, &statb) < 0)
                return errno;
 
        if (S_ISDIR(statb.st_mode))
@@ -198,7 +191,7 @@ test_for_fifo(const char *path)
 {
        struct stat statb;
 
-       if (stat(path, &statb) < 0)
+       if (eth_stat(path, &statb) < 0)
                return errno;
 
        if (S_ISFIFO(statb.st_mode))
@@ -351,7 +344,7 @@ get_persconffile_dir(void)
        char *appdatadir;
        char *userprofiledir;
 #else
-       char *homedir;
+       const char *homedir;
        struct passwd *pwd;
 #endif
        static char *pf_dir = NULL;
@@ -374,9 +367,8 @@ get_persconffile_dir(void)
                /*
                 * Concatenate %APPDATA% with "\Ethereal".
                 */
-               pf_dir = g_malloc(strlen(appdatadir) + strlen(PF_DIR) + 2);
-               sprintf(pf_dir, "%s" G_DIR_SEPARATOR_S "%s", appdatadir,
-                   PF_DIR);
+               pf_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", 
+                       appdatadir, PF_DIR);
        } else {
                /*
                 * OK, %APPDATA% wasn't set, so use
@@ -384,17 +376,14 @@ get_persconffile_dir(void)
                 */
                userprofiledir = getenv("USERPROFILE");
                if (userprofiledir != NULL) {
-                       pf_dir = g_malloc(strlen(userprofiledir) +
-                           strlen("Application Data") + strlen(PF_DIR) + 3);
-                       sprintf(pf_dir,
+                       pf_dir = g_strdup_printf(
                            "%s" G_DIR_SEPARATOR_S "Application Data" G_DIR_SEPARATOR_S "%s",
                            userprofiledir, PF_DIR);
                } else {
                        /*
                         * Give up and use "C:".
                         */
-                       pf_dir = g_malloc(strlen("C:") + strlen(PF_DIR) + 2);
-                       sprintf(pf_dir, "C:" G_DIR_SEPARATOR_S "%s", PF_DIR);
+                       pf_dir = g_strdup_printf("C:" G_DIR_SEPARATOR_S "%s", PF_DIR);
                }
        }
 #else
@@ -418,8 +407,7 @@ get_persconffile_dir(void)
                } else
                        homedir = "/tmp";
        }
-       pf_dir = g_malloc(strlen(homedir) + strlen(PF_DIR) + 2);
-       sprintf(pf_dir, "%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR);
+       pf_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR);
 #endif
 
        return pf_dir;
@@ -444,7 +432,7 @@ create_persconffile_dir(char **pf_dir_path_return)
        int ret;
 
        pf_dir_path = get_persconffile_dir();
-       if (stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
+       if (eth_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
 #ifdef _WIN32
                /*
                 * Does the parent directory of that directory
@@ -462,20 +450,20 @@ create_persconffile_dir(char **pf_dir_path_return)
                pf_dir_parent_path_len = strlen(pf_dir_parent_path);
                if (pf_dir_parent_path_len > 0
                    && pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
-                   && stat(pf_dir_parent_path, &s_buf) != 0) {
+                   && eth_stat(pf_dir_parent_path, &s_buf) != 0) {
                        /*
                         * No, it doesn't exist - make it first.
                         */
-                       ret = mkdir(pf_dir_parent_path);
+                       ret = eth_mkdir(pf_dir_parent_path, 0755);
                        if (ret == -1) {
                                *pf_dir_path_return = pf_dir_parent_path;
                                return -1;
                        }
                }
                g_free(pf_dir_path_copy);
-               ret = mkdir(pf_dir_path);
+               ret = eth_mkdir(pf_dir_path, 0755);
 #else
-               ret = mkdir(pf_dir_path, 0755);
+               ret = eth_mkdir(pf_dir_path, 0755);
 #endif
        } else {
                /*
@@ -573,24 +561,20 @@ get_persconffile_path(const char *filename, gboolean for_writing
        char *old_path;
 #endif
 
-       path = (gchar *) g_malloc(strlen(get_persconffile_dir()) +
-           strlen(filename) + 2);
-       sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
+       path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
            filename);
 #ifdef _WIN32
        if (!for_writing) {
-               if (stat(path, &s_buf) != 0 && errno == ENOENT) {
+               if (eth_stat(path, &s_buf) != 0 && errno == ENOENT) {
                        /*
                         * OK, it's not in the personal configuration file
                         * directory; is it in the ".ethereal" subdirectory
                         * of their home directory?
                         */
-                       old_path = (gchar *) g_malloc(strlen(get_home_dir()) +
-                           strlen(".ethereal") + strlen(filename) + 3);
-                       sprintf(old_path,
+                       old_path = g_strdup_printf(
                            "%s" G_DIR_SEPARATOR_S ".ethereal" G_DIR_SEPARATOR_S "%s",
                            get_home_dir(), filename);
-                       if (stat(old_path, &s_buf) == 0) {
+                       if (eth_stat(old_path, &s_buf) == 0) {
                                /*
                                 * OK, it exists; return it instead.
                                 */
@@ -611,21 +595,16 @@ get_persconffile_path(const char *filename, gboolean for_writing
 char *
 get_datafile_path(const char *filename)
 {
-       char *path;
 
-       path = (gchar *) g_malloc(strlen(get_datafile_dir()) +
-           strlen(filename) + 2);
-       sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
+       return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
            filename);
-
-       return path;
 }
 
 /* Delete a file */
 gboolean
 deletefile(const char *path)
 {
-       return unlink(path) == 0;
+       return eth_unlink(path) == 0;
 }
 
 /*
@@ -634,23 +613,18 @@ deletefile(const char *path)
  */
 char *get_tempfile_path(const char *filename)
 {
-       char *path;
 
-       path = (gchar *) g_malloc(strlen(g_get_tmp_dir()) +
-           strlen(filename) + 2);
-       sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
-
-       return path;
+       return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
 }
 
 /*
  * Return an error message for UNIX-style errno indications on open or
  * create operations.
  */
-char *
+const char *
 file_open_error_message(int err, gboolean for_writing)
 {
-       char *errmsg;
+       const char *errmsg;
        static char errmsg_errno[1024+1];
 
        switch (err) {
@@ -684,7 +658,7 @@ file_open_error_message(int err, gboolean for_writing)
 #endif
 
        default:
-               snprintf(errmsg_errno, sizeof(errmsg_errno),
+               g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                                "The file \"%%s\" could not be %s: %s.",
                                for_writing ? "created" : "opened",
                                strerror(err));
@@ -698,10 +672,10 @@ file_open_error_message(int err, gboolean for_writing)
  * Return an error message for UNIX-style errno indications on write
  * operations.
  */
-char *
+const char *
 file_write_error_message(int err)
 {
-       char *errmsg;
+       const char *errmsg;
        static char errmsg_errno[1024+1];
 
        switch (err) {
@@ -717,7 +691,7 @@ file_write_error_message(int err)
 #endif
 
        default:
-               snprintf(errmsg_errno, sizeof(errmsg_errno),
+               g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                    "An error occurred while writing to the file \"%%s\": %s.",
                    strerror(err));
                errmsg = errmsg_errno;
@@ -740,7 +714,7 @@ file_exists(const char *fname)
    * so this is working, but maybe not quite the way expected. ULFL
    */
    file_stat.st_ino = 1;   /* this will make things work if an error occured */
-   stat(fname, &file_stat);
+   eth_stat(fname, &file_stat);
    if (file_stat.st_ino == 0) {
        return TRUE;
    } else {
@@ -778,9 +752,6 @@ files_identical(const char *fname1, const char *fname2)
     }
 #else
   struct stat   infile, outfile;
-  save_callback_args_t callback_args;
-
-  cf_callback_invoke(cf_cb_file_safe_started, (gpointer) fname);
 
   /*
    * Check that the from file is not the same as to file
@@ -796,8 +767,8 @@ files_identical(const char *fname1, const char *fname2)
    */
    infile.st_ino = 1;   /* These prevent us from getting equality         */
    outfile.st_ino = 2;  /* If one or other of the files is not accessible */
-   stat(cf->filename, &infile);
-   stat(fname, &outfile);
+   eth_stat(fname1, &infile);
+   eth_stat(fname2, &outfile);
    if (infile.st_ino == outfile.st_ino) {
        return TRUE;
    } else {