[vlc-devel] commit: Inline strdup, strndup, lldiv and getenv ( Rémi Denis-Courmont )

git version control git at videolan.org
Sat May 24 10:53:33 CEST 2008


vlc | branch: master | Rémi Denis-Courmont <rem at videolan.org> | Sat May 24 11:55:05 2008 +0300| [2b617e77c1f838066916c4a46de7c0e99b38105c]

Inline strdup, strndup, lldiv and getenv

Also fix an overflow in strndup().

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=2b617e77c1f838066916c4a46de7c0e99b38105c
---

 include/vlc_common.h |   13 -----------
 include/vlc_fixups.h |   41 +++++++++++++++++++++++++++++++++---
 src/extras/libc.c    |   56 --------------------------------------------------
 src/libvlccore.sym   |    4 ---
 4 files changed, 37 insertions(+), 77 deletions(-)

diff --git a/include/vlc_common.h b/include/vlc_common.h
index 0ba6de4..5206dbe 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -719,31 +719,18 @@ static inline void _SetQWBE( uint8_t *p, uint64_t i_qw )
 #define VLC_UNUSED(x) (void)(x)
 
 /* Stuff defined in src/extras/libc.c */
-VLC_EXPORT( char *, vlc_strdup, ( const char *s ) );
 VLC_EXPORT( int, vlc_vasprintf, (char **, const char *, va_list ) );
 VLC_EXPORT( int, vlc_asprintf, (char **, const char *, ... ) ATTRIBUTE_FORMAT( 2, 3 ) );
-VLC_EXPORT( char *, vlc_strndup, ( const char *s, size_t n ) );
 VLC_EXPORT( size_t, vlc_strlcpy, ( char *, const char *, size_t ) );
 VLC_EXPORT( double, vlc_atof, ( const char *nptr ) );
 VLC_EXPORT( int64_t, vlc_atoll, ( const char *nptr ) );
 VLC_EXPORT( int64_t, vlc_strtoll, ( const char *nptr, char **endptr, int base ) );
 VLC_EXPORT( size_t, vlc_strnlen, ( const char *, size_t ) );
 
-#if defined(SYS_BEOS) \
- || (defined (__FreeBSD__) && (__FreeBSD__ < 5))
-    typedef struct {
-        long long quot; /* Quotient. */
-        long long rem;  /* Remainder. */
-    } lldiv_t;
-#endif
-VLC_EXPORT( lldiv_t, vlc_lldiv, ( long long numer, long long denom ) );
-
 struct dirent;
 VLC_EXPORT( int, vlc_scandir, ( const char *name, struct dirent ***namelist, int (*filter) ( const struct dirent * ), int (*compar) ( const struct dirent **, const struct dirent ** ) ) );
 VLC_EXPORT( int, vlc_alphasort, ( const struct dirent **a, const struct dirent **b ) );
 
-VLC_EXPORT( char *, vlc_getenv, ( const char *name ) );
-
 VLC_EXPORT( int, vlc_strcasecmp, ( const char *s1, const char *s2 ) );
 VLC_EXPORT( int, vlc_strncasecmp, ( const char *s1, const char *s2, size_t n ) );
 VLC_EXPORT( char *, vlc_strcasestr, ( const char *s1, const char *s2 ) );
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index 15d9d63..8045ed5 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -26,8 +26,17 @@
 #ifndef LIBVLC_FIXUPS_H
 # define LIBVLC_FIXUPS_H 1
 
+# include <string.h>
+# include <stdlib.h>
+
 #ifndef HAVE_STRDUP
-# define strdup vlc_strdup
+static inline char *strdup (const char *str)
+{
+    size_t len = strlen (str) + 1;
+    char *res = malloc (len);
+    if (res) memcpy (res, str, len);
+    return res;
+}
 #endif
 
 #ifndef HAVE_VASPRINTF
@@ -39,7 +48,18 @@
 #endif
 
 #ifndef HAVE_STRNDUP
-# define strndup vlc_strndup
+static inline char *strndup (const char *str, size_t max)
+{
+    const char *end = memchr (str, '\0', max);
+    size_t len = end ? (size_t)(end - str) : max;
+    char *res = malloc (len + 1);
+    if (res)
+    {
+        memcpy (res, str, len);
+        res[len] = '\0';
+    }
+    return res;
+}
 #endif
 
 #ifndef HAVE_STRNLEN
@@ -69,7 +89,16 @@
 #endif
 
 #ifndef HAVE_LLDIV
-# define lldiv vlc_lldiv
+typedef struct {
+    long long quot; /* Quotient. */
+    long long rem;  /* Remainder. */
+} lldiv_t;
+
+static inline lldiv_t lldiv (long long numer, long long denom)
+{
+    lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
+    return d;
+}
 #endif
 
 #ifndef HAVE_SCANDIR
@@ -78,7 +107,11 @@
 #endif
 
 #ifndef HAVE_GETENV
-# define getenv vlc_getenv
+static inline getenv (const char *name)
+{
+    (void)name;
+    return NULL;
+}
 #endif
 
 #ifndef HAVE_STRCASECMP
diff --git a/src/extras/libc.c b/src/extras/libc.c
index 4e091bc..e9bb77b 100644
--- a/src/extras/libc.c
+++ b/src/extras/libc.c
@@ -75,48 +75,6 @@
 #endif
 
 /*****************************************************************************
- * getenv: just in case, but it should never be called
- *****************************************************************************/
-#if !defined( HAVE_GETENV )
-char *vlc_getenv( const char *name )
-{
-    return NULL;
-}
-#endif
-
-/*****************************************************************************
- * strdup: returns a malloc'd copy of a string
- *****************************************************************************/
-#if !defined( HAVE_STRDUP )
-char *vlc_strdup( const char *string )
-{
-    return strndup( string, strlen( string ) );
-}
-#endif
-
-/*****************************************************************************
- * strndup: returns a malloc'd copy of at most n bytes of string
- * Does anyone know whether or not it will be present in Jaguar?
- *****************************************************************************/
-#if !defined( HAVE_STRNDUP )
-char *vlc_strndup( const char *string, size_t n )
-{
-    char *psz;
-    size_t len = strlen( string );
-
-    len = __MIN( len, n );
-    psz = (char*)malloc( len + 1 );
-    if( psz != NULL )
-    {
-        memcpy( (void*)psz, (const void*)string, len );
-        psz[ len ] = 0;
-    }
-
-    return psz;
-}
-#endif
-
-/*****************************************************************************
  * strnlen:
  *****************************************************************************/
 #if !defined( HAVE_STRNLEN )
@@ -370,20 +328,6 @@ int64_t vlc_atoll( const char *nptr )
 }
 #endif
 
-/*****************************************************************************
- * lldiv: returns quotient and remainder
- *****************************************************************************/
-#if !defined( HAVE_LLDIV )
-lldiv_t vlc_lldiv( long long numer, long long denom )
-{
-    lldiv_t d;
-    d.quot = numer / denom;
-    d.rem  = numer % denom;
-    return d;
-}
-#endif
-
-
 /**
  * Copy a string to a sized buffer. The result is always nul-terminated
  * (contrary to strncpy()).
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 88a80de..ca14d71 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -401,7 +401,6 @@ vlc_fastmem_register
 vlc_freeaddrinfo
 vlc_gai_strerror
 vlc_getaddrinfo
-vlc_getenv
 vlc_getnameinfo
 vlc_gettext
 vlc_iconv
@@ -410,7 +409,6 @@ vlc_iconv_open
 __vlc_list_children
 __vlc_list_find
 vlc_list_release
-vlc_lldiv
 vlc_memcpy
 vlc_memset
 vlc_module_create
@@ -444,10 +442,8 @@ vlc_sdp_Start
 vlc_sendmsg
 vlc_strcasecmp
 vlc_strcasestr
-vlc_strdup
 vlc_strlcpy
 vlc_strncasecmp
-vlc_strndup
 vlc_strnlen
 vlc_strtoll
 vlc_submodule_create




More information about the vlc-devel mailing list