VLC_API char *vlc_getcwd( void ) VLC_USED;
#if defined( _WIN32 )
+typedef struct vlc_DIR
+{
+ _WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
+ char *entry;
+ union
+ {
+ DWORD drives;
+ bool insert_dot_dot;
+ } u;
+} vlc_DIR;
+
static inline int vlc_closedir( DIR *dir )
{
- _WDIR *wdir = *(_WDIR **)dir;
- free( dir );
- return wdir ? _wclosedir( wdir ) : 0;
+ vlc_DIR *vdir = (vlc_DIR *)dir;
+ _WDIR *wdir = vdir->wdir;
+
+ free( vdir->entry );
+ free( vdir );
+ return (wdir != NULL) ? _wclosedir( wdir ) : 0;
}
# undef closedir
# define closedir vlc_closedir
/* find the requested file in the directory */
for( ; ; ) {
- char *psz_filename;
+ const char *psz_filename = vlc_readdir( p_dir );
- if( ! (psz_filename = vlc_readdir( p_dir ) ) )
+ if( psz_filename == NULL )
break;
if( !strncmp( p_scan->parameter.sat_info.psz_name, psz_filename, 20 ) )
{
if( asprintf( &p_scan->parameter.sat_info.psz_path, "%s" DIR_SEP "%s", psz_dir, psz_filename ) == -1 )
p_scan->parameter.sat_info.psz_path = NULL;
-
- free( psz_filename );
break;
}
- free( psz_filename );
}
closedir( p_dir );
if (p_src_dir != NULL)
{
- char *psz_file;
+ const char *psz_file;
while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
{
if (strlen(psz_file) > 4)
if (!s_filename.compare(p_demux->psz_file))
#endif
{
- free (psz_file);
continue; // don't reuse the original opened file
}
}
}
}
- free (psz_file);
}
closedir( p_src_dir );
}
DirsDestroy(sys);
- char *entry;
+ const char *entry;
while ((entry = vlc_readdir(current_dir))) {
if (!sys->show_hidden_files && *entry == '.' && strcmp(entry, ".."))
- goto next;
+ continue;
struct dir_entry_t *dir_entry = malloc(sizeof *dir_entry);
- if (!dir_entry)
- goto next;
+ if (unlikely(dir_entry == NULL))
+ continue;
dir_entry->file = IsFile(sys->current_dir, entry);
- dir_entry->path = entry;
+ dir_entry->path = xstrdup(entry);
INSERT_ELEM(sys->dir_entries, sys->n_dir_entries,
sys->n_dir_entries, dir_entry);
continue;
-
-next:
- free(entry);
}
qsort(sys->dir_entries, sys->n_dir_entries,
// Path separator
const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
- DIR *pCurrDir;
- char *pszDirContent;
+ const char *pszDirContent;
// Open the dir
- pCurrDir = vlc_opendir( rootDir.c_str() );
+ DIR *pCurrDir = vlc_opendir( rootDir.c_str() );
if( pCurrDir == NULL )
{
// Can we find the file in this subdirectory?
if( findFile( newURI, rFileName, themeFilePath ) )
{
- free( pszDirContent );
closedir( pCurrDir );
return true;
}
if( rFileName == string( pszDirContent ) )
{
themeFilePath = newURI;
- free( pszDirContent );
closedir( pCurrDir );
return true;
}
}
}
-
- free( pszDirContent );
}
closedir( pCurrDir );
void ThemeRepository::parseDirectory( const string &rDir_locale )
{
- DIR *pDir;
- char *pszDirContent;
+ const char *pszDirContent;
// Path separator
const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
// Open the dir
// FIXME: parseDirectory should be invoked with UTF-8 input instead!!
string rDir = sFromLocale( rDir_locale );
- pDir = vlc_opendir( rDir.c_str() );
+ DIR *pDir = vlc_opendir( rDir.c_str() );
if( pDir == NULL )
{
msg_Dbg( getIntf(), "found skin %s", path.c_str() );
}
-
- free( pszDirContent );
}
closedir( pDir );
lua_newtable( L );
for( ;; )
{
- char *psz_filename = vlc_readdir( p_dir );
+ const char *psz_filename = vlc_readdir( p_dir );
if( !psz_filename ) break;
i++;
lua_pushstring( L, psz_filename );
lua_rawseti( L, -2, i );
- free( psz_filename );
}
closedir( p_dir );
return 1;
msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir );
- char *psz_name;
+ const char *psz_name;
while( (psz_name = vlc_readdir( dir )) && i_sub_count < MAX_SUBTITLE_FILES )
{
if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) )
- {
- free( psz_name );
continue;
- }
char tmp_fname_noext[strlen( psz_name ) + 1];
char tmp_fname_trim[strlen( psz_name ) + 1];
sprintf( psz_path, "%s"DIR_SEP"%s", psz_dir, psz_name );
if( !strcmp( psz_path, psz_fname ) )
- {
- free( psz_name );
continue;
- }
if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) && result )
{
psz_path, i_prio );
}
}
- free( psz_name );
}
closedir( dir );
}
/* Skip ".", ".." */
if (!strcmp (file, ".") || !strcmp (file, ".."))
- goto skip;
+ continue;
/* Compute path relative to plug-in base directory */
if (reldir != NULL)
else
relpath = strdup (file);
if (unlikely(relpath == NULL))
- goto skip;
+ continue;
/* Compute absolute path */
if (asprintf (&abspath, "%s"DIR_SEP"%s", bank->base, relpath) == -1)
skip:
free (relpath);
free (abspath);
- free (file);
}
closedir (dh);
}
}
bool b_found = false;
- char *psz_filename;
+ const char *psz_filename;
while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
{
if( !strncmp( psz_filename, "art", 3 ) )
b_found = true;
}
- free( psz_filename );
}
/* */
* @param dir directory handle as returned by vlc_opendir()
* (must not be used by another thread concurrently)
*
- * @return a UTF-8 string of the directory entry. Use free() to release it.
+ * @return a UTF-8 string of the directory entry. The string is valid until
+ * the next call to vlc_readdir() or closedir() on the handle.
* If there are no more entries in the directory, NULL is returned.
* If an error occurs, errno is set and NULL is returned.
*/
char *vlc_readdir( DIR *dir )
{
struct dirent *ent = readdir (dir);
- return (ent != NULL) ? strdup (ent->d_name) : NULL;
+ return (ent != NULL) ? ent->d_name : NULL;
}
/**
for (unsigned size = 0;;)
{
errno = 0;
- char *entry = vlc_readdir (dir);
+ const char *entry = vlc_readdir (dir);
if (entry == NULL)
{
if (errno)
}
if (!select (entry))
- {
- free (entry);
continue;
- }
if (num >= size)
{
char **newtab = realloc (tab, sizeof (*tab) * (size));
if (unlikely(newtab == NULL))
- {
- free (entry);
goto error;
- }
tab = newtab;
}
- tab[num++] = entry;
+ tab[num] = strdup(entry);
+ if (likely(tab[num] != NULL))
+ num++;
}
if (compar != NULL)
/* Under Windows, these wrappers return the list of drive letters
* when called with an empty argument or just '\'. */
-typedef struct vlc_DIR
-{
- _WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
- union
- {
- DWORD drives;
- bool insert_dot_dot;
- } u;
-} vlc_DIR;
-
-
DIR *vlc_opendir (const char *dirname)
{
wchar_t *wpath = widen_path (dirname);
return NULL;
}
p_dir->wdir = wdir;
+ p_dir->entry = NULL;
return (void *)p_dir;
}
{
vlc_DIR *p_dir = (vlc_DIR *)dir;
+ free(p_dir->entry);
+
#if !VLC_WINSTORE_APP
/* Drive letters mode */
if (p_dir->wdir == NULL)
p_dir->u.drives &= ~(1UL << i);
assert (i < 26);
- char *ret;
- if (asprintf (&ret, "%c:\\", 'A' + i) == -1)
- return NULL;
- return ret;
+ if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
+ p_dir->entry = NULL;
}
+ else
#endif
-
if (p_dir->u.insert_dot_dot)
{
/* Adds "..", gruik! */
p_dir->u.insert_dot_dot = false;
- return strdup ("..");
+ p_dir->entry = strdup ("..");
}
-
- struct _wdirent *ent = _wreaddir (p_dir->wdir);
- if (ent == NULL)
- return NULL;
- return FromWide (ent->d_name);
+ else
+ {
+ struct _wdirent *ent = _wreaddir (p_dir->wdir);
+ p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
+ }
+ return p_dir->entry;
}
int vlc_stat (const char *filename, struct stat *buf)