from Frederic Peters: bring debian package generation .deb up to date
[obnox/wireshark/wip.git] / epan / filesystem.c
index c6d7c2b606163643091f6afbc035a7cccf7ae757..212f4a5f6c6a731a029b0cabd0a2c3b3daad37f7 100644 (file)
@@ -1,7 +1,7 @@
 /* filesystem.c
  * Filesystem utility routines
  *
- * $Id: filesystem.c,v 1.27 2003/11/03 22:32:36 guy Exp $
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -49,7 +49,7 @@
 #include <direct.h>            /* to declare "mkdir()" on Windows */
 #endif
 
-#ifndef WIN32
+#ifndef _WIN32
 #include <pwd.h>
 #endif
 
  * character in the pathname, or NULL if the pathname contains no
  * separators.
  */
-char *
-find_last_pathname_separator(char *path)
+static char *
+find_last_pathname_separator(const char *path)
 {
        char *separator;
 
-#ifdef WIN32
+#ifdef _WIN32
        char c;
 
        /*
         * We have to scan for '\' or '/'.
         * Get to the end of the string.
         */
-       separator = path + strlen(path);        /* points to ending '\0' */
+       separator = strchr(path, '\0');         /* points to ending '\0' */
        while (separator > path) {
                c = *--separator;
                if (c == '\\' || c == '/')
@@ -93,10 +93,10 @@ find_last_pathname_separator(char *path)
 /*
  * Given a pathname, return the last component.
  */
-char *
-get_basename(char *path)
+const char *
+get_basename(const char *path)
 {
-       char *filename;
+       const char *filename;
 
        g_assert(path != NULL);
        filename = find_last_pathname_separator(path);
@@ -235,7 +235,7 @@ test_for_fifo(const char *path)
 const char *
 get_datafile_dir(void)
 {
-#ifdef WIN32
+#ifdef _WIN32
        char prog_pathname[_MAX_PATH+2];
        char *dir_end;
        size_t datafile_dir_len;
@@ -316,7 +316,7 @@ get_datafile_dir(void)
 const char *
 get_systemfile_dir(void)
 {
-#ifdef WIN32
+#ifdef _WIN32
        return get_datafile_dir();
 #else
        return "/etc";
@@ -327,7 +327,7 @@ get_systemfile_dir(void)
  * Name of directory, under the user's home directory, in which
  * personal configuration files are stored.
  */
-#ifdef WIN32
+#ifdef _WIN32
 #define PF_DIR "Ethereal"
 #else
 /*
@@ -347,7 +347,7 @@ get_systemfile_dir(void)
 static const char *
 get_persconffile_dir(void)
 {
-#ifdef WIN32
+#ifdef _WIN32
        char *appdatadir;
        char *userprofiledir;
 #else
@@ -360,7 +360,7 @@ get_persconffile_dir(void)
        if (pf_dir != NULL)
                return pf_dir;
 
-#ifdef WIN32
+#ifdef _WIN32
        /*
         * Use %APPDATA% or %USERPROFILE%, so that configuration files are
         * stored in the user profile, rather than in the home directory.
@@ -436,7 +436,7 @@ int
 create_persconffile_dir(char **pf_dir_path_return)
 {
        const char *pf_dir_path;
-#ifdef WIN32
+#ifdef _WIN32
        char *pf_dir_path_copy, *pf_dir_parent_path;
        size_t pf_dir_parent_path_len;
 #endif
@@ -445,7 +445,7 @@ create_persconffile_dir(char **pf_dir_path_return)
 
        pf_dir_path = get_persconffile_dir();
        if (stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
-#ifdef WIN32
+#ifdef _WIN32
                /*
                 * Does the parent directory of that directory
                 * exist?  %APPDATA% may not exist even though
@@ -491,7 +491,7 @@ create_persconffile_dir(char **pf_dir_path_return)
        return ret;
 }
 
-#ifdef WIN32
+#ifdef _WIN32
 /*
  * Returns the user's home directory on Win32.
  */
@@ -562,13 +562,13 @@ get_home_dir(void)
  */
 char *
 get_persconffile_path(const char *filename, gboolean for_writing
-#ifndef WIN32
+#ifndef _WIN32
        _U_
 #endif
 )
 {
        char *path;
-#ifdef WIN32
+#ifdef _WIN32
        struct stat s_buf;
        char *old_path;
 #endif
@@ -577,7 +577,7 @@ get_persconffile_path(const char *filename, gboolean for_writing
            strlen(filename) + 2);
        sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
            filename);
-#ifdef WIN32
+#ifdef _WIN32
        if (!for_writing) {
                if (stat(path, &s_buf) != 0 && errno == ENOENT) {
                        /*
@@ -643,3 +643,86 @@ char *get_tempfile_path(const char *filename)
        return path;
 }
 
+/*
+ * Return an error message for UNIX-style errno indications on open or
+ * create operations.
+ */
+char *
+file_open_error_message(int err, gboolean for_writing)
+{
+       char *errmsg;
+       static char errmsg_errno[1024+1];
+
+       switch (err) {
+
+       case ENOENT:
+               if (for_writing)
+                       errmsg = "The path to the file \"%s\" doesn't exist.";
+               else
+                       errmsg = "The file \"%s\" doesn't exist.";
+               break;
+
+       case EACCES:
+               if (for_writing)
+                       errmsg = "You don't have permission to create or write to the file \"%s\".";
+               else
+                       errmsg = "You don't have permission to read the file \"%s\".";
+               break;
+
+       case EISDIR:
+               errmsg = "\"%s\" is a directory (folder), not a file.";
+               break;
+
+       case ENOSPC:
+               errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
+               break;
+
+#ifdef EDQUOT
+       case EDQUOT:
+               errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
+               break;
+#endif
+
+       default:
+               snprintf(errmsg_errno, sizeof(errmsg_errno),
+                               "The file \"%%s\" could not be %s: %s.",
+                               for_writing ? "created" : "opened",
+                               strerror(err));
+               errmsg = errmsg_errno;
+               break;
+       }
+       return errmsg;
+}
+
+/*
+ * Return an error message for UNIX-style errno indications on write
+ * operations.
+ */
+char *
+file_write_error_message(int err)
+{
+       char *errmsg;
+       static char errmsg_errno[1024+1];
+
+       switch (err) {
+
+       case ENOSPC:
+               errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
+               break;
+
+#ifdef EDQUOT
+       case EDQUOT:
+               errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
+               break;
+#endif
+
+       default:
+               snprintf(errmsg_errno, sizeof(errmsg_errno),
+                   "An error occurred while writing to the file \"%%s\": %s.",
+                   strerror(err));
+               errmsg = errmsg_errno;
+               break;
+       }
+       return errmsg;
+}
+