/* 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>
#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 == '/')
/*
* 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);
const char *
get_datafile_dir(void)
{
-#ifdef WIN32
+#ifdef _WIN32
char prog_pathname[_MAX_PATH+2];
char *dir_end;
size_t datafile_dir_len;
const char *
get_systemfile_dir(void)
{
-#ifdef WIN32
+#ifdef _WIN32
return get_datafile_dir();
#else
return "/etc";
* 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
/*
static const char *
get_persconffile_dir(void)
{
-#ifdef WIN32
+#ifdef _WIN32
char *appdatadir;
char *userprofiledir;
#else
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.
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
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
return ret;
}
-#ifdef WIN32
+#ifdef _WIN32
/*
* Returns the user's home directory on Win32.
*/
*/
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
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) {
/*
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;
+}
+