Changeset 2b617e77c1f838066916c4a46de7c0e99b38105c
- Timestamp:
- 24/05/08 10:55:05
(5 months ago)
- Author:
- Rémi Denis-Courmont <rem@videolan.org>
- git-committer:
- Rémi Denis-Courmont <rem@videolan.org> 1211619305 +0300
- git-parent:
[c4e0f18676bfbc37fe1504a719b51204e92de664]
- git-author:
- Rémi Denis-Courmont <rem@videolan.org> 1211619305 +0300
- Message:
Inline strdup, strndup, lldiv and getenv
Also fix an overflow in strndup().
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| rbb29acd |
r2b617e7 |
|
| 720 | 720 | |
|---|
| 721 | 721 | /* Stuff defined in src/extras/libc.c */ |
|---|
| 722 | | VLC_EXPORT( char *, vlc_strdup, ( const char *s ) ); |
|---|
| 723 | 722 | VLC_EXPORT( int, vlc_vasprintf, (char **, const char *, va_list ) ); |
|---|
| 724 | 723 | VLC_EXPORT( int, vlc_asprintf, (char **, const char *, ... ) ATTRIBUTE_FORMAT( 2, 3 ) ); |
|---|
| 725 | | VLC_EXPORT( char *, vlc_strndup, ( const char *s, size_t n ) ); |
|---|
| 726 | 724 | VLC_EXPORT( size_t, vlc_strlcpy, ( char *, const char *, size_t ) ); |
|---|
| 727 | 725 | VLC_EXPORT( double, vlc_atof, ( const char *nptr ) ); |
|---|
| … | … | |
| 730 | 728 | VLC_EXPORT( size_t, vlc_strnlen, ( const char *, size_t ) ); |
|---|
| 731 | 729 | |
|---|
| 732 | | #if defined(SYS_BEOS) \ |
|---|
| 733 | | || (defined (__FreeBSD__) && (__FreeBSD__ < 5)) |
|---|
| 734 | | typedef struct { |
|---|
| 735 | | long long quot; /* Quotient. */ |
|---|
| 736 | | long long rem; /* Remainder. */ |
|---|
| 737 | | } lldiv_t; |
|---|
| 738 | | #endif |
|---|
| 739 | | VLC_EXPORT( lldiv_t, vlc_lldiv, ( long long numer, long long denom ) ); |
|---|
| 740 | | |
|---|
| 741 | 730 | struct dirent; |
|---|
| 742 | 731 | VLC_EXPORT( int, vlc_scandir, ( const char *name, struct dirent ***namelist, int (*filter) ( const struct dirent * ), int (*compar) ( const struct dirent **, const struct dirent ** ) ) ); |
|---|
| 743 | 732 | VLC_EXPORT( int, vlc_alphasort, ( const struct dirent **a, const struct dirent **b ) ); |
|---|
| 744 | | |
|---|
| 745 | | VLC_EXPORT( char *, vlc_getenv, ( const char *name ) ); |
|---|
| 746 | 733 | |
|---|
| 747 | 734 | VLC_EXPORT( int, vlc_strcasecmp, ( const char *s1, const char *s2 ) ); |
|---|
| r3f68eb0 |
r2b617e7 |
|
| 27 | 27 | # define LIBVLC_FIXUPS_H 1 |
|---|
| 28 | 28 | |
|---|
| | 29 | # include <string.h> |
|---|
| | 30 | # include <stdlib.h> |
|---|
| | 31 | |
|---|
| 29 | 32 | #ifndef HAVE_STRDUP |
|---|
| 30 | | # define strdup vlc_strdup |
|---|
| | 33 | static inline char *strdup (const char *str) |
|---|
| | 34 | { |
|---|
| | 35 | size_t len = strlen (str) + 1; |
|---|
| | 36 | char *res = malloc (len); |
|---|
| | 37 | if (res) memcpy (res, str, len); |
|---|
| | 38 | return res; |
|---|
| | 39 | } |
|---|
| 31 | 40 | #endif |
|---|
| 32 | 41 | |
|---|
| … | … | |
| 40 | 49 | |
|---|
| 41 | 50 | #ifndef HAVE_STRNDUP |
|---|
| 42 | | # define strndup vlc_strndup |
|---|
| | 51 | static inline char *strndup (const char *str, size_t max) |
|---|
| | 52 | { |
|---|
| | 53 | const char *end = memchr (str, '\0', max); |
|---|
| | 54 | size_t len = end ? (size_t)(end - str) : max; |
|---|
| | 55 | char *res = malloc (len + 1); |
|---|
| | 56 | if (res) |
|---|
| | 57 | { |
|---|
| | 58 | memcpy (res, str, len); |
|---|
| | 59 | res[len] = '\0'; |
|---|
| | 60 | } |
|---|
| | 61 | return res; |
|---|
| | 62 | } |
|---|
| 43 | 63 | #endif |
|---|
| 44 | 64 | |
|---|
| … | … | |
| 70 | 90 | |
|---|
| 71 | 91 | #ifndef HAVE_LLDIV |
|---|
| 72 | | # define lldiv vlc_lldiv |
|---|
| | 92 | typedef struct { |
|---|
| | 93 | long long quot; /* Quotient. */ |
|---|
| | 94 | long long rem; /* Remainder. */ |
|---|
| | 95 | } lldiv_t; |
|---|
| | 96 | |
|---|
| | 97 | static inline lldiv_t lldiv (long long numer, long long denom) |
|---|
| | 98 | { |
|---|
| | 99 | lldiv_t d = { .quot = numer / denom, .rem = numer % denom }; |
|---|
| | 100 | return d; |
|---|
| | 101 | } |
|---|
| 73 | 102 | #endif |
|---|
| 74 | 103 | |
|---|
| … | … | |
| 79 | 108 | |
|---|
| 80 | 109 | #ifndef HAVE_GETENV |
|---|
| 81 | | # define getenv vlc_getenv |
|---|
| | 110 | static inline getenv (const char *name) |
|---|
| | 111 | { |
|---|
| | 112 | (void)name; |
|---|
| | 113 | return NULL; |
|---|
| | 114 | } |
|---|
| 82 | 115 | #endif |
|---|
| 83 | 116 | |
|---|
| rff02bf9 |
r2b617e7 |
|
| 76 | 76 | |
|---|
| 77 | 77 | /***************************************************************************** |
|---|
| 78 | | * getenv: just in case, but it should never be called |
|---|
| 79 | | *****************************************************************************/ |
|---|
| 80 | | #if !defined( HAVE_GETENV ) |
|---|
| 81 | | char *vlc_getenv( const char *name ) |
|---|
| 82 | | { |
|---|
| 83 | | return NULL; |
|---|
| 84 | | } |
|---|
| 85 | | #endif |
|---|
| 86 | | |
|---|
| 87 | | /***************************************************************************** |
|---|
| 88 | | * strdup: returns a malloc'd copy of a string |
|---|
| 89 | | *****************************************************************************/ |
|---|
| 90 | | #if !defined( HAVE_STRDUP ) |
|---|
| 91 | | char *vlc_strdup( const char *string ) |
|---|
| 92 | | { |
|---|
| 93 | | return strndup( string, strlen( string ) ); |
|---|
| 94 | | } |
|---|
| 95 | | #endif |
|---|
| 96 | | |
|---|
| 97 | | /***************************************************************************** |
|---|
| 98 | | * strndup: returns a malloc'd copy of at most n bytes of string |
|---|
| 99 | | * Does anyone know whether or not it will be present in Jaguar? |
|---|
| 100 | | *****************************************************************************/ |
|---|
| 101 | | #if !defined( HAVE_STRNDUP ) |
|---|
| 102 | | char *vlc_strndup( const char *string, size_t n ) |
|---|
| 103 | | { |
|---|
| 104 | | char *psz; |
|---|
| 105 | | size_t len = strlen( string ); |
|---|
| 106 | | |
|---|
| 107 | | len = __MIN( len, n ); |
|---|
| 108 | | psz = (char*)malloc( len + 1 ); |
|---|
| 109 | | if( psz != NULL ) |
|---|
| 110 | | { |
|---|
| 111 | | memcpy( (void*)psz, (const void*)string, len ); |
|---|
| 112 | | psz[ len ] = 0; |
|---|
| 113 | | } |
|---|
| 114 | | |
|---|
| 115 | | return psz; |
|---|
| 116 | | } |
|---|
| 117 | | #endif |
|---|
| 118 | | |
|---|
| 119 | | /***************************************************************************** |
|---|
| 120 | 78 | * strnlen: |
|---|
| 121 | 79 | *****************************************************************************/ |
|---|
| … | … | |
| 371 | 329 | #endif |
|---|
| 372 | 330 | |
|---|
| 373 | | /***************************************************************************** |
|---|
| 374 | | * lldiv: returns quotient and remainder |
|---|
| 375 | | *****************************************************************************/ |
|---|
| 376 | | #if !defined( HAVE_LLDIV ) |
|---|
| 377 | | lldiv_t vlc_lldiv( long long numer, long long denom ) |
|---|
| 378 | | { |
|---|
| 379 | | lldiv_t d; |
|---|
| 380 | | d.quot = numer / denom; |
|---|
| 381 | | d.rem = numer % denom; |
|---|
| 382 | | return d; |
|---|
| 383 | | } |
|---|
| 384 | | #endif |
|---|
| 385 | | |
|---|
| 386 | | |
|---|
| 387 | 331 | /** |
|---|
| 388 | 332 | * Copy a string to a sized buffer. The result is always nul-terminated |
|---|
| re4c0e92 |
r2b617e7 |
|
| 402 | 402 | vlc_gai_strerror |
|---|
| 403 | 403 | vlc_getaddrinfo |
|---|
| 404 | | vlc_getenv |
|---|
| 405 | 404 | vlc_getnameinfo |
|---|
| 406 | 405 | vlc_gettext |
|---|
| … | … | |
| 411 | 410 | __vlc_list_find |
|---|
| 412 | 411 | vlc_list_release |
|---|
| 413 | | vlc_lldiv |
|---|
| 414 | 412 | vlc_memcpy |
|---|
| 415 | 413 | vlc_memset |
|---|
| … | … | |
| 445 | 443 | vlc_strcasecmp |
|---|
| 446 | 444 | vlc_strcasestr |
|---|
| 447 | | vlc_strdup |
|---|
| 448 | 445 | vlc_strlcpy |
|---|
| 449 | 446 | vlc_strncasecmp |
|---|
| 450 | | vlc_strndup |
|---|
| 451 | 447 | vlc_strnlen |
|---|
| 452 | 448 | vlc_strtoll |
|---|