00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <string.h>
00028 #include <stdlib.h>
00029 #include <ctype.h>
00030
00031 #include <vlc/vlc.h>
00032
00033 #undef iconv_t
00034 #undef iconv_open
00035 #undef iconv
00036 #undef iconv_close
00037
00038 #if defined(HAVE_ICONV)
00039 # include <iconv.h>
00040 #endif
00041
00042 #ifdef HAVE_DIRENT_H
00043 # include <dirent.h>
00044 #endif
00045
00046 #ifdef HAVE_FORK
00047 # include <sys/time.h>
00048 # include <unistd.h>
00049 # include <errno.h>
00050 # include <sys/wait.h>
00051 #endif
00052
00053 #if defined(WIN32) || defined(UNDER_CE)
00054 # define WIN32_LEAN_AND_MEAN
00055 # include <windows.h>
00056 #endif
00057
00058 #ifdef UNDER_CE
00059 # define strcoll strcmp
00060 #endif
00061
00062
00063
00064
00065 #if !defined( HAVE_GETENV )
00066 char *vlc_getenv( const char *name )
00067 {
00068 return NULL;
00069 }
00070 #endif
00071
00072
00073
00074
00075 #if !defined( HAVE_STRDUP )
00076 char *vlc_strdup( const char *string )
00077 {
00078 return strndup( string, strlen( string ) );
00079 }
00080 #endif
00081
00082
00083
00084
00085
00086 #if !defined( HAVE_STRNDUP )
00087 char *vlc_strndup( const char *string, size_t n )
00088 {
00089 char *psz;
00090 size_t len = strlen( string );
00091
00092 len = __MIN( len, n );
00093 psz = (char*)malloc( len + 1 );
00094
00095 if( psz != NULL )
00096 {
00097 memcpy( (void*)psz, (const void*)string, len );
00098 psz[ len ] = 0;
00099 }
00100
00101 return psz;
00102 }
00103 #endif
00104
00105
00106
00107
00108 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
00109 int vlc_strcasecmp( const char *s1, const char *s2 )
00110 {
00111 int c1, c2;
00112 if( !s1 || !s2 ) return -1;
00113
00114 while( *s1 && *s2 )
00115 {
00116 c1 = tolower(*s1);
00117 c2 = tolower(*s2);
00118
00119 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
00120 s1++; s2++;
00121 }
00122
00123 if( !*s1 && !*s2 ) return 0;
00124 else return (*s1 ? 1 : -1);
00125 }
00126 #endif
00127
00128
00129
00130
00131 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
00132 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
00133 {
00134 int c1, c2;
00135 if( !s1 || !s2 ) return -1;
00136
00137 while( n > 0 && *s1 && *s2 )
00138 {
00139 c1 = tolower(*s1);
00140 c2 = tolower(*s2);
00141
00142 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
00143 s1++; s2++; n--;
00144 }
00145
00146 if( !n || (!*s1 && !*s2) ) return 0;
00147 else return (*s1 ? 1 : -1);
00148 }
00149 #endif
00150
00151
00152
00153
00154
00155 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
00156 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
00157 {
00158 char *p_pos = (char *)psz_big;
00159
00160 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
00161
00162 while( *p_pos )
00163 {
00164 if( toupper( *p_pos ) == toupper( *psz_little ) )
00165 {
00166 char * psz_cur1 = p_pos + 1;
00167 char * psz_cur2 = (char *)psz_little + 1;
00168 while( *psz_cur1 && *psz_cur2 &&
00169 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
00170 {
00171 psz_cur1++;
00172 psz_cur2++;
00173 }
00174 if( !*psz_cur2 ) return p_pos;
00175 }
00176 p_pos++;
00177 }
00178 return NULL;
00179 }
00180 #endif
00181
00182
00183
00184
00185 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
00186 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
00187 {
00188
00189 int i_size = 100;
00190 char *p = malloc( i_size );
00191 int n;
00192
00193 if( p == NULL )
00194 {
00195 *strp = NULL;
00196 return -1;
00197 }
00198
00199 for( ;; )
00200 {
00201
00202 n = vsnprintf( p, i_size, fmt, ap );
00203
00204
00205 if (n > -1 && n < i_size)
00206 {
00207 *strp = p;
00208 return strlen( p );
00209 }
00210
00211 if (n > -1)
00212 {
00213 i_size = n+1;
00214 }
00215 else
00216 {
00217 i_size *= 2;
00218 }
00219 if( (p = realloc( p, i_size ) ) == NULL)
00220 {
00221 *strp = NULL;
00222 return -1;
00223 }
00224 }
00225 }
00226 #endif
00227
00228
00229
00230
00231 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
00232 int vlc_asprintf( char **strp, const char *fmt, ... )
00233 {
00234 va_list args;
00235 int i_ret;
00236
00237 va_start( args, fmt );
00238 i_ret = vasprintf( strp, fmt, args );
00239 va_end( args );
00240
00241 return i_ret;
00242 }
00243 #endif
00244
00245
00246
00247
00248 #if !defined( HAVE_ATOF )
00249 double vlc_atof( const char *nptr )
00250 {
00251 double f_result;
00252 wchar_t *psz_tmp;
00253 int i_len = strlen( nptr ) + 1;
00254
00255 psz_tmp = malloc( i_len * sizeof(wchar_t) );
00256 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
00257 f_result = wcstod( psz_tmp, NULL );
00258 free( psz_tmp );
00259
00260 return f_result;
00261 }
00262 #endif
00263
00264
00265
00266
00267 #if !defined( HAVE_STRTOLL )
00268 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
00269 {
00270 int64_t i_value = 0;
00271 int sign = 1, newbase = base ? base : 10;
00272
00273 while( isspace(*nptr) ) nptr++;
00274
00275 if( *nptr == '-' )
00276 {
00277 sign = -1;
00278 nptr++;
00279 }
00280
00281
00282 if( *nptr == '0' )
00283 {
00284 newbase = 8;
00285 nptr++;
00286
00287 if( *nptr == 'x' )
00288 {
00289 newbase = 16;
00290 nptr++;
00291 }
00292 }
00293
00294 if( base && newbase != base )
00295 {
00296 if( endptr ) *endptr = (char *)nptr;
00297 return i_value;
00298 }
00299
00300 switch( newbase )
00301 {
00302 case 10:
00303 while( *nptr >= '0' && *nptr <= '9' )
00304 {
00305 i_value *= 10;
00306 i_value += ( *nptr++ - '0' );
00307 }
00308 if( endptr ) *endptr = (char *)nptr;
00309 break;
00310
00311 case 16:
00312 while( (*nptr >= '0' && *nptr <= '9') ||
00313 (*nptr >= 'a' && *nptr <= 'f') ||
00314 (*nptr >= 'A' && *nptr <= 'F') )
00315 {
00316 int i_valc = 0;
00317 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
00318 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
00319 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
00320 i_value *= 16;
00321 i_value += i_valc;
00322 nptr++;
00323 }
00324 if( endptr ) *endptr = (char *)nptr;
00325 break;
00326
00327 default:
00328 i_value = strtol( nptr, endptr, newbase );
00329 break;
00330 }
00331
00332 return i_value * sign;
00333 }
00334 #endif
00335
00336
00337
00338
00339 #if !defined( HAVE_ATOLL )
00340 int64_t vlc_atoll( const char *nptr )
00341 {
00342 return strtoll( nptr, (char **)NULL, 10 );
00343 }
00344 #endif
00345
00346
00347
00348
00349
00350 #if defined(WIN32) && !defined(UNDER_CE)
00351 typedef struct vlc_DIR
00352 {
00353 DIR *p_real_dir;
00354 int i_drives;
00355 struct dirent dd_dir;
00356 vlc_bool_t b_insert_back;
00357 } vlc_DIR;
00358
00359 void *vlc_opendir_wrapper( const char *psz_path )
00360 {
00361 vlc_DIR *p_dir;
00362 DIR *p_real_dir;
00363
00364 if ( psz_path == NULL || psz_path[0] == '\0'
00365 || (psz_path[0] == '\\' && psz_path[1] == '\0') )
00366 {
00367
00368 p_dir = malloc( sizeof(vlc_DIR) );
00369 p_dir->p_real_dir = NULL;
00370 p_dir->i_drives = GetLogicalDrives();
00371 return (void *)p_dir;
00372 }
00373
00374 p_real_dir = opendir( psz_path );
00375 if ( p_real_dir == NULL )
00376 return NULL;
00377
00378 p_dir = malloc( sizeof(vlc_DIR) );
00379 p_dir->p_real_dir = p_real_dir;
00380 p_dir->b_insert_back = ( psz_path[1] == ':' && psz_path[2] == '\\'
00381 && psz_path[3] =='\0' );
00382 return (void *)p_dir;
00383 }
00384
00385 struct dirent *vlc_readdir_wrapper( void *_p_dir )
00386 {
00387 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
00388 unsigned int i;
00389 DWORD i_drives;
00390
00391 if ( p_dir->p_real_dir != NULL )
00392 {
00393 if ( p_dir->b_insert_back )
00394 {
00395 p_dir->dd_dir.d_ino = 0;
00396 p_dir->dd_dir.d_reclen = 0;
00397 p_dir->dd_dir.d_namlen = 2;
00398 strcpy( p_dir->dd_dir.d_name, ".." );
00399 p_dir->b_insert_back = VLC_FALSE;
00400 return &p_dir->dd_dir;
00401 }
00402
00403 return readdir( p_dir->p_real_dir );
00404 }
00405
00406
00407 i_drives = p_dir->i_drives;
00408 if ( !i_drives )
00409 return NULL;
00410
00411 for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
00412 if ( i_drives & 1 ) break;
00413
00414 if ( i >= 26 )
00415 return NULL;
00416
00417 sprintf( p_dir->dd_dir.d_name, "%c:\\", 'A' + i );
00418 p_dir->dd_dir.d_namlen = strlen(p_dir->dd_dir.d_name);
00419 p_dir->i_drives &= ~(1UL << i);
00420 return &p_dir->dd_dir;
00421 }
00422
00423 int vlc_closedir_wrapper( void *_p_dir )
00424 {
00425 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
00426
00427 if ( p_dir->p_real_dir != NULL )
00428 {
00429 int i_ret = closedir( p_dir->p_real_dir );
00430 free( p_dir );
00431 return i_ret;
00432 }
00433
00434 free( p_dir );
00435 return 0;
00436 }
00437 #else
00438 void *vlc_opendir_wrapper( const char *psz_path )
00439 {
00440 return (void *)opendir( psz_path );
00441 }
00442 struct dirent *vlc_readdir_wrapper( void *_p_dir )
00443 {
00444 return readdir( (DIR *)_p_dir );
00445 }
00446 int vlc_closedir_wrapper( void *_p_dir )
00447 {
00448 return closedir( (DIR *)_p_dir );
00449 }
00450 #endif
00451
00452
00453
00454
00455 #if !defined( HAVE_SCANDIR )
00456 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
00457 {
00458 return strcoll( (*a)->d_name, (*b)->d_name );
00459 }
00460
00461 int vlc_scandir( const char *name, struct dirent ***namelist,
00462 int (*filter) ( const struct dirent * ),
00463 int (*compar) ( const struct dirent **,
00464 const struct dirent ** ) )
00465 {
00466 DIR * p_dir;
00467 struct dirent * p_content;
00468 struct dirent ** pp_list;
00469 int ret, size;
00470
00471 if( !namelist || !( p_dir = vlc_opendir_wrapper( name ) ) ) return -1;
00472
00473 ret = 0;
00474 pp_list = NULL;
00475 while( ( p_content = vlc_readdir_wrapper( p_dir ) ) )
00476 {
00477 if( filter && !filter( p_content ) )
00478 {
00479 continue;
00480 }
00481 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
00482 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
00483 pp_list[ret] = malloc( size );
00484 memcpy( pp_list[ret], p_content, size );
00485 ret++;
00486 }
00487
00488 vlc_closedir_wrapper( p_dir );
00489
00490 if( compar )
00491 {
00492 qsort( pp_list, ret, sizeof( struct dirent * ),
00493 (int (*)(const void *, const void *)) compar );
00494 }
00495
00496 *namelist = pp_list;
00497 return ret;
00498 }
00499 #endif
00500
00501
00502
00503
00504 char *vlc_dgettext( const char *package, const char *msgid )
00505 {
00506 #if defined( ENABLE_NLS ) \
00507 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
00508 return dgettext( package, msgid );
00509 #else
00510 return (char *)msgid;
00511 #endif
00512 }
00513
00514
00515
00516
00517 static int count_utf8_string( const char *psz_string )
00518 {
00519 int i = 0, i_count = 0;
00520 while( psz_string[ i ] != 0 )
00521 {
00522 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
00523 i++;
00524 }
00525 return i_count;
00526 }
00527
00528
00529
00530
00531
00532 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
00533 {
00534 int i_len;
00535 char *psz_line, *psz_new_text;
00536
00537 psz_line = psz_new_text = strdup( psz_text );
00538
00539 if( b_utf8 )
00540 i_len = count_utf8_string( psz_text );
00541 else
00542 i_len = strlen( psz_text );
00543
00544 while( i_len > i_line )
00545 {
00546
00547 char *psz_parser = psz_line;
00548 int i_count = 0;
00549 while( i_count <= i_line && *psz_parser != '\n' )
00550 {
00551 if( b_utf8 )
00552 {
00553 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
00554 }
00555 psz_parser++;
00556 i_count++;
00557 }
00558 if( *psz_parser == '\n' )
00559 {
00560 i_len -= (i_count + 1);
00561 psz_line = psz_parser + 1;
00562 continue;
00563 }
00564
00565
00566 while( psz_parser > psz_line && *psz_parser != ' ' )
00567 {
00568 if( b_utf8 )
00569 {
00570 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
00571 }
00572 psz_parser--;
00573 i_count--;
00574 }
00575 if( *psz_parser == ' ' )
00576 {
00577 *psz_parser = '\n';
00578 i_len -= (i_count + 1);
00579 psz_line = psz_parser + 1;
00580 continue;
00581 }
00582
00583
00584 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
00585 {
00586 if( b_utf8 )
00587 {
00588 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
00589 }
00590 psz_parser++;
00591 i_count++;
00592 }
00593 if( i_count < i_len ) *psz_parser = '\n';
00594 i_len -= (i_count + 1);
00595 psz_line = psz_parser + 1;
00596 }
00597
00598 return psz_new_text;
00599 }
00600
00601
00602
00603
00604 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
00605 {
00606 #if defined(HAVE_ICONV)
00607 return iconv_open( tocode, fromcode );
00608 #else
00609 return NULL;
00610 #endif
00611 }
00612
00613 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
00614 char **outbuf, size_t *outbytesleft )
00615 {
00616 #if defined(HAVE_ICONV)
00617 return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
00618 #else
00619 int i_bytes;
00620
00621 if (inbytesleft == NULL || outbytesleft == NULL)
00622 {
00623 return 0;
00624 }
00625
00626 i_bytes = __MIN(*inbytesleft, *outbytesleft);
00627 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
00628 memcpy( *outbuf, *inbuf, i_bytes );
00629 inbuf += i_bytes;
00630 outbuf += i_bytes;
00631 inbytesleft -= i_bytes;
00632 outbytesleft -= i_bytes;
00633 return i_bytes;
00634 #endif
00635 }
00636
00637 int vlc_iconv_close( vlc_iconv_t cd )
00638 {
00639 #if defined(HAVE_ICONV)
00640 return iconv_close( cd );
00641 #else
00642 return 0;
00643 #endif
00644 }
00645
00646
00647
00648
00649
00650 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
00651 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
00652 {
00653 vlc_bool_t b_exact = 1;
00654 uint64_t i_gcd;
00655
00656 if( i_den == 0 )
00657 {
00658 *pi_dst_nom = 0;
00659 *pi_dst_den = 1;
00660 return 1;
00661 }
00662
00663 i_gcd = GCD( i_nom, i_den );
00664 i_nom /= i_gcd;
00665 i_den /= i_gcd;
00666
00667 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
00668
00669 if( i_nom > i_max || i_den > i_max )
00670 {
00671 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
00672 b_exact = 0;
00673
00674 for( ; ; )
00675 {
00676 uint64_t i_x = i_nom / i_den;
00677 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
00678 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
00679
00680 if( i_a2n > i_max || i_a2d > i_max ) break;
00681
00682 i_nom %= i_den;
00683
00684 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
00685 i_a1_num = i_a2n; i_a1_den = i_a2d;
00686 if( i_nom == 0 ) break;
00687 i_x = i_nom; i_nom = i_den; i_den = i_x;
00688 }
00689 i_nom = i_a1_num;
00690 i_den = i_a1_den;
00691 }
00692
00693 *pi_dst_nom = i_nom;
00694 *pi_dst_den = i_den;
00695
00696 return b_exact;
00697 }
00698
00699
00700
00701
00702
00703
00704
00705
00706 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
00707 {
00708 int i_bcount = 0;
00709
00710 while( **s )
00711 {
00712 if( **s == '\\' )
00713 {
00714 **ppsz_parser = **s;
00715 (*ppsz_parser)++; (*s)++;
00716 i_bcount++;
00717 }
00718 else if( **s == '"' || **s == '\'' )
00719 {
00720
00721 *ppsz_parser -= i_bcount / 2;
00722 if( i_bcount & 1 )
00723 {
00724
00725 *ppsz_parser -= 1;
00726 **ppsz_parser = **s;
00727 (*ppsz_parser)++; (*s)++;
00728 i_bcount = 0;
00729 continue;
00730 }
00731
00732 if( **s == i_quote )
00733 {
00734
00735 return;
00736 }
00737 else
00738 {
00739
00740 int i_quote = **s;
00741 **ppsz_parser = **s;
00742 (*ppsz_parser)++; (*s)++;
00743 find_end_quote( s, ppsz_parser, i_quote );
00744 **ppsz_parser = **s;
00745 (*ppsz_parser)++; (*s)++;
00746 }
00747
00748 i_bcount = 0;
00749 }
00750 else
00751 {
00752
00753 **ppsz_parser = **s;
00754 (*ppsz_parser)++; (*s)++;
00755 i_bcount = 0;
00756 }
00757 }
00758 }
00759
00760 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
00761 {
00762 int argc = 0;
00763 char **argv = 0;
00764 char *s, *psz_parser, *psz_arg, *psz_orig;
00765 int i_bcount = 0;
00766
00767 if( !psz_cmdline ) return 0;
00768 psz_orig = strdup( psz_cmdline );
00769 psz_arg = psz_parser = s = psz_orig;
00770
00771 while( *s )
00772 {
00773 if( *s == '\t' || *s == ' ' )
00774 {
00775
00776 *psz_parser = 0;
00777 TAB_APPEND( argc, argv, strdup(psz_arg) );
00778
00779
00780 do{ s++; } while( *s == '\t' || *s == ' ' );
00781
00782
00783 psz_arg = psz_parser = s;
00784 i_bcount = 0;
00785 }
00786 else if( *s == '\\' )
00787 {
00788 *psz_parser++ = *s++;
00789 i_bcount++;
00790 }
00791 else if( *s == '"' || *s == '\'' )
00792 {
00793 if( ( i_bcount & 1 ) == 0 )
00794 {
00795
00796
00797 int i_quote = *s;
00798 psz_parser -= i_bcount / 2;
00799 s++;
00800 find_end_quote( &s, &psz_parser, i_quote );
00801 s++;
00802 }
00803 else
00804 {
00805
00806
00807 psz_parser = psz_parser - i_bcount/2 - 1;
00808 *psz_parser++ = '"';
00809 s++;
00810 }
00811 i_bcount = 0;
00812 }
00813 else
00814 {
00815
00816 *psz_parser++ = *s++;
00817 i_bcount = 0;
00818 }
00819 }
00820
00821
00822 if( *psz_arg )
00823 {
00824 *psz_parser = '\0';
00825 TAB_APPEND( argc, argv, strdup(psz_arg) );
00826 }
00827
00828 if( i_args ) *i_args = argc;
00829 free( psz_orig );
00830 return argv;
00831 }
00832
00833
00834
00835
00836
00837 int __vlc_execve( vlc_object_t *p_object, int i_argc, char **ppsz_argv,
00838 char **ppsz_env, char *psz_cwd, char *p_in, int i_in,
00839 char **pp_data, int *pi_data )
00840 {
00841 #ifdef HAVE_FORK
00842 int pi_stdin[2];
00843 int pi_stdout[2];
00844 pid_t i_child_pid;
00845
00846 pipe( pi_stdin );
00847 pipe( pi_stdout );
00848
00849 if ( (i_child_pid = fork()) == -1 )
00850 {
00851 msg_Err( p_object, "unable to fork (%s)", strerror(errno) );
00852 return -1;
00853 }
00854
00855 if ( i_child_pid == 0 )
00856 {
00857 close(0);
00858 dup(pi_stdin[1]);
00859 close(pi_stdin[0]);
00860
00861 close(1);
00862 dup(pi_stdout[1]);
00863 close(pi_stdout[0]);
00864
00865 close(2);
00866
00867 if ( psz_cwd != NULL )
00868 chdir( psz_cwd );
00869 execve( ppsz_argv[0], ppsz_argv, ppsz_env );
00870 exit(1);
00871 }
00872
00873 close(pi_stdin[1]);
00874 close(pi_stdout[1]);
00875 if ( !i_in )
00876 close( pi_stdin[0] );
00877
00878 *pi_data = 0;
00879 *pp_data = malloc( 1025 );
00880
00881 while ( !p_object->b_die )
00882 {
00883 int i_ret, i_status;
00884 fd_set readfds, writefds;
00885 struct timeval tv;
00886
00887 FD_ZERO( &readfds );
00888 FD_ZERO( &writefds );
00889 FD_SET( pi_stdout[0], &readfds );
00890 if ( i_in )
00891 FD_SET( pi_stdin[0], &writefds );
00892
00893 tv.tv_sec = 0;
00894 tv.tv_usec = 10000;
00895
00896 i_ret = select( pi_stdin[0] > pi_stdout[0] ? pi_stdin[0] + 1 :
00897 pi_stdout[0] + 1, &readfds, &writefds, NULL, &tv );
00898 if ( i_ret > 0 )
00899 {
00900 if ( FD_ISSET( pi_stdout[0], &readfds ) )
00901 {
00902 ssize_t i_read = read( pi_stdout[0], &(*pp_data)[*pi_data],
00903 1024 );
00904 if ( i_read > 0 )
00905 {
00906 *pi_data += i_read;
00907 *pp_data = realloc( *pp_data, *pi_data + 1025 );
00908 }
00909 }
00910 if ( FD_ISSET( pi_stdin[0], &writefds ) )
00911 {
00912 ssize_t i_write = write( pi_stdin[0], p_in, __MIN(i_in, 1024) );
00913
00914 if ( i_write > 0 )
00915 {
00916 p_in += i_write;
00917 i_in -= i_write;
00918 }
00919 if ( !i_in )
00920 close( pi_stdin[0] );
00921 }
00922 }
00923
00924 if ( waitpid( i_child_pid, &i_status, WNOHANG ) == i_child_pid )
00925 {
00926 if ( WIFEXITED( i_status ) )
00927 {
00928 if ( WEXITSTATUS( i_status ) )
00929 {
00930 msg_Warn( p_object,
00931 "child %s returned with error code %d",
00932 ppsz_argv[0], WEXITSTATUS( i_status ) );
00933 }
00934 }
00935 else
00936 {
00937 if ( WIFSIGNALED( i_status ) )
00938 {
00939 msg_Warn( p_object,
00940 "child %s quit on signal %d", ppsz_argv[0],
00941 WTERMSIG( i_status ) );
00942 }
00943 }
00944 if ( i_in )
00945 close( pi_stdin[0] );
00946 close( pi_stdout[0] );
00947 break;
00948 }
00949
00950 if ( i_ret < 0 && errno != EINTR )
00951 {
00952 msg_Warn( p_object, "select failed (%s)", strerror(errno) );
00953 }
00954 }
00955
00956 #elif defined( WIN32 ) && !defined( UNDER_CE )
00957 SECURITY_ATTRIBUTES saAttr;
00958 PROCESS_INFORMATION piProcInfo;
00959 STARTUPINFO siStartInfo;
00960 BOOL bFuncRetn = FALSE;
00961 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr;
00962 DWORD i_status;
00963 char *psz_cmd, *p_env, *p;
00964 char **ppsz_parser;
00965 int i_size;
00966
00967
00968 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
00969 saAttr.bInheritHandle = TRUE;
00970 saAttr.lpSecurityDescriptor = NULL;
00971
00972
00973 if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
00974 {
00975 msg_Err( p_object, "stdout pipe creation failed" );
00976 return -1;
00977 }
00978
00979
00980 SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 );
00981
00982
00983 if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) )
00984 {
00985 msg_Err( p_object, "stdin pipe creation failed" );
00986 return -1;
00987 }
00988
00989
00990 SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 );
00991
00992
00993 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
00994
00995
00996 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
00997 siStartInfo.cb = sizeof(STARTUPINFO);
00998 siStartInfo.hStdError = hChildStdoutWr;
00999 siStartInfo.hStdOutput = hChildStdoutWr;
01000 siStartInfo.hStdInput = hChildStdinRd;
01001 siStartInfo.wShowWindow = SW_HIDE;
01002 siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
01003
01004
01005 psz_cmd = malloc(32768);
01006 psz_cmd[0] = '\0';
01007 i_size = 32768;
01008 ppsz_parser = &ppsz_argv[0];
01009 while ( ppsz_parser[0] != NULL && i_size > 0 )
01010 {
01011
01012
01013
01014 if ( ppsz_parser[1] == NULL )
01015 {
01016 strncat( psz_cmd, "\"", i_size );
01017 i_size--;
01018 }
01019 strncat( psz_cmd, *ppsz_parser, i_size );
01020 i_size -= strlen( *ppsz_parser );
01021 if ( ppsz_parser[1] == NULL )
01022 {
01023 strncat( psz_cmd, "\"", i_size );
01024 i_size--;
01025 }
01026 strncat( psz_cmd, " ", i_size );
01027 i_size--;
01028 ppsz_parser++;
01029 }
01030
01031
01032 p = p_env = malloc(32768);
01033 i_size = 32768;
01034 ppsz_parser = &ppsz_env[0];
01035 while ( *ppsz_parser != NULL && i_size > 0 )
01036 {
01037 memcpy( p, *ppsz_parser,
01038 __MIN((int)(strlen(*ppsz_parser) + 1), i_size) );
01039 p += strlen(*ppsz_parser) + 1;
01040 i_size -= strlen(*ppsz_parser) + 1;
01041 ppsz_parser++;
01042 }
01043 *p = '\0';
01044
01045
01046 bFuncRetn = CreateProcess( NULL,
01047 psz_cmd,
01048 NULL,
01049 NULL,
01050 TRUE,
01051 0,
01052 p_env,
01053 psz_cwd,
01054 &siStartInfo,
01055 &piProcInfo );
01056
01057 free( psz_cmd );
01058 free( p_env );
01059
01060 if ( bFuncRetn == 0 )
01061 {
01062 msg_Err( p_object, "child creation failed" );
01063 return -1;
01064 }
01065
01066
01067 while ( i_in > 0 && !p_object->b_die )
01068 {
01069 DWORD i_written;
01070 if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) )
01071 break;
01072 i_in -= i_written;
01073 p_in += i_written;
01074 }
01075
01076
01077 CloseHandle(hChildStdinWr);
01078
01079
01080
01081 CloseHandle(hChildStdoutWr);
01082
01083
01084 *pi_data = 0;
01085 *pp_data = malloc( 1025 );
01086
01087 while ( !p_object->b_die )
01088 {
01089 DWORD i_read;
01090 if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read,
01091 NULL )
01092 || i_read == 0 )
01093 break;
01094 *pi_data += i_read;
01095 *pp_data = realloc( *pp_data, *pi_data + 1025 );
01096 }
01097
01098 while ( !p_object->b_die
01099 && !GetExitCodeProcess( piProcInfo.hProcess, &i_status )
01100 && i_status != STILL_ACTIVE )
01101 msleep( 10000 );
01102
01103 CloseHandle(piProcInfo.hProcess);
01104 CloseHandle(piProcInfo.hThread);
01105
01106 if ( i_status )
01107 msg_Warn( p_object,
01108 "child %s returned with error code %ld",
01109 ppsz_argv[0], i_status );
01110
01111 #else
01112 msg_Err( p_object, "vlc_execve called but no implementation is available" );
01113 return -1;
01114
01115 #endif
01116
01117 (*pp_data)[*pi_data] = '\0';
01118
01119 return 0;
01120 }