]> git.sesse.net Git - vlc/blobdiff - src/extras/libc.c
* charset.c: don't return a pointer to a buffer allocated on the stack!
[vlc] / src / extras / libc.c
index b4935d034d3daea37cf1c45101163baa662f180b..0e72751b79f2cdf6468617dfe02753965c70be91 100644 (file)
@@ -7,6 +7,7 @@
  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
  *          Samuel Hocevar <sam@zoy.org>
  *          Gildas Bazin <gbazin@videolan.org>
+ *          Derk-Jan Hartman <hartman at videolan dot org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #   include <iconv.h>
 #endif
 
+#ifdef HAVE_DIRENT_H
+#   include <dirent.h>
+#endif
+
 /*****************************************************************************
  * getenv: just in case, but it should never be called
  *****************************************************************************/
@@ -321,6 +326,55 @@ int64_t vlc_atoll( const char *nptr )
 }
 #endif
 
+/*****************************************************************************
+ * scandir: scan a directory alpha-sorted
+ *****************************************************************************/
+#if !defined( HAVE_SCANDIR )
+int vlc_alphasort( const struct dirent **a, const struct dirent **b )
+{
+    return strcoll( (*a)->d_name, (*b)->d_name );
+}
+
+int vlc_scandir( const char *name, struct dirent ***namelist,
+                    int (*filter) ( const struct dirent * ),
+                    int (*compar) ( const struct dirent **,
+                                    const struct dirent ** ) )
+{
+    DIR            * p_dir;
+    struct dirent  * p_content;
+    struct dirent ** pp_list;
+    int              ret, size;
+
+    if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
+
+    ret     = 0;
+    pp_list = NULL;
+    while( ( p_content = readdir( p_dir ) ) )
+    {
+        if( filter && !filter( p_content ) )
+        {
+            continue;
+        }
+        pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
+        size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
+        pp_list[ret] = malloc( size );
+        memcpy( pp_list[ret], p_content, size );
+        ret++;
+    }
+
+    closedir( p_dir );
+
+    if( compar )
+    {
+        qsort( pp_list, ret, sizeof( struct dirent * ),
+               (int (*)(const void *, const void *)) compar );
+    }
+
+    *namelist = pp_list;
+    return ret;
+}
+#endif
+
 /*****************************************************************************
  * dgettext: gettext for plugins.
  *****************************************************************************/
@@ -463,8 +517,8 @@ int vlc_iconv_close( vlc_iconv_t cd )
  * reduce a fraction
  *   (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
  *****************************************************************************/
-vlc_bool_t vlc_reduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
-                       uint64_t i_nom, uint64_t i_den, uint64_t i_max )
+vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
+                        uint64_t i_nom, uint64_t i_den, uint64_t i_max )
 {
     vlc_bool_t b_exact = 1;
     uint64_t i_gcd;
@@ -484,7 +538,7 @@ vlc_bool_t vlc_reduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
 
     if( i_nom > i_max || i_den > i_max )
     {
-        uint i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
+        uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
         b_exact = 0;
 
         for( ; ; )