Use GDir instead of dirent

This commit is contained in:
TingPing 2014-06-04 13:56:33 -04:00
parent 9c981cfc6b
commit 3cd8556c54
2 changed files with 33 additions and 32 deletions

View File

@ -52,14 +52,13 @@
*/ */
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef WIN32 #ifdef WIN32
#include <direct.h> #include <direct.h>
#include <glib/gstdio.h>
#include "../../src/dirent/dirent-win32.h"
#else #else
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
@ -459,26 +458,31 @@ Util_BuildEOLList(char *word[])
static void static void
Util_Autoload_from (const char *dir_name) Util_Autoload_from (const char *dir_name)
{ {
#ifndef PATH_MAX gchar *oldcwd;
#define PATH_MAX 1024 /* Hurd doesn't define it */ const char *entry_name;
#endif GDir *dir;
char oldcwd[PATH_MAX];
struct dirent *ent; oldcwd = g_get_current_dir ();
DIR *dir; if (oldcwd == NULL)
if (getcwd(oldcwd, PATH_MAX) == NULL)
return; return;
if (chdir(dir_name) != 0) if (g_chdir(dir_name) != 0)
{
g_free (oldcwd);
return; return;
dir = opendir(".");
if (dir == NULL)
return;
while ((ent = readdir(dir))) {
int len = strlen(ent->d_name);
if (len > 3 && strcmp(".py", ent->d_name+len-3) == 0)
Command_PyLoad(ent->d_name);
} }
closedir(dir); dir = g_dir_open (".", 0, NULL);
chdir(oldcwd); if (dir == NULL)
{
g_free (oldcwd);
return;
}
while ((entry_name = g_dir_read_name (dir)))
{
if (g_str_has_suffix (entry_name, ".py"))
Command_PyLoad((char*)entry_name);
}
g_dir_close (dir);
g_chdir (oldcwd);
} }
static void static void
@ -486,7 +490,7 @@ Util_Autoload()
{ {
const char *xdir; const char *xdir;
char *sub_dir; char *sub_dir;
/* we need local filesystem encoding for chdir, opendir etc */ /* we need local filesystem encoding for g_chdir, g_dir_open etc */
xdir = hexchat_get_info(ph, "configdir"); xdir = hexchat_get_info(ph, "configdir");

View File

@ -34,13 +34,11 @@
#include <process.h> #include <process.h>
#include <io.h> #include <io.h>
#include <VersionHelpers.h> #include <VersionHelpers.h>
#include "../dirent/dirent-win32.h"
#else #else
#include <unistd.h> #include <unistd.h>
#include <pwd.h> #include <pwd.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <dirent.h>
#endif #endif
#include "../../config.h" #include "../../config.h"
@ -934,27 +932,26 @@ break_while:
void void
for_files (char *dirname, char *mask, void callback (char *file)) for_files (char *dirname, char *mask, void callback (char *file))
{ {
DIR *dir; GDir *dir;
struct dirent *ent; const gchar *entry_name;
char *buf; char *buf;
dir = opendir (dirname); dir = g_dir_open (dirname, 0, NULL);
if (dir) if (dir)
{ {
while ((ent = readdir (dir))) while ((entry_name = g_dir_read_name (dir)))
{ {
if (strcmp (ent->d_name, ".") && strcmp (ent->d_name, "..")) if (strcmp (entry_name, ".") && strcmp (entry_name, ".."))
{ {
if (match (mask, ent->d_name)) if (match (mask, entry_name))
{ {
buf = malloc (strlen (dirname) + strlen (ent->d_name) + 2); buf = g_build_filename (dirname, entry_name, NULL);
sprintf (buf, "%s" G_DIR_SEPARATOR_S "%s", dirname, ent->d_name);
callback (buf); callback (buf);
free (buf); g_free (buf);
} }
} }
} }
closedir (dir); g_dir_close (dir);
} }
} }