#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
{
struct stat statb;
- if (stat(path, &statb) < 0)
+ if (eth_stat(path, &statb) < 0)
return errno;
if (S_ISDIR(statb.st_mode))
{
struct stat statb;
- if (stat(path, &statb) < 0)
+ if (eth_stat(path, &statb) < 0)
return errno;
if (S_ISFIFO(statb.st_mode))
char *appdatadir;
char *userprofiledir;
#else
- char *homedir;
+ const char *homedir;
struct passwd *pwd;
#endif
static char *pf_dir = NULL;
/*
* 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
*/
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
} 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;
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
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 {
/*
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.
*/
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;
}
/*
*/
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) {
#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));
* 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) {
#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;
* 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 {
}
#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
*/
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 {