Commit 2b617e77 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Inline strdup, strndup, lldiv and getenv

Also fix an overflow in strndup().
parent c4e0f186
......@@ -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 ) );
......
......@@ -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
......
......@@ -74,48 +74,6 @@
# define strcoll strcmp
#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:
*****************************************************************************/
......@@ -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()).
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment