| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#ifdef HAVE_CONFIG_H |
|---|
| 29 |
# include "config.h" |
|---|
| 30 |
#endif |
|---|
| 31 |
|
|---|
| 32 |
#include <vlc_common.h> |
|---|
| 33 |
|
|---|
| 34 |
#include <ctype.h> |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
#undef iconv_t |
|---|
| 38 |
#undef iconv_open |
|---|
| 39 |
#undef iconv |
|---|
| 40 |
#undef iconv_close |
|---|
| 41 |
|
|---|
| 42 |
#if defined(HAVE_ICONV) |
|---|
| 43 |
# include <iconv.h> |
|---|
| 44 |
#endif |
|---|
| 45 |
|
|---|
| 46 |
#ifdef HAVE_DIRENT_H |
|---|
| 47 |
# include <dirent.h> |
|---|
| 48 |
#endif |
|---|
| 49 |
|
|---|
| 50 |
#ifdef HAVE_SIGNAL_H |
|---|
| 51 |
# include <signal.h> |
|---|
| 52 |
#endif |
|---|
| 53 |
|
|---|
| 54 |
#ifdef HAVE_FORK |
|---|
| 55 |
# include <sys/time.h> |
|---|
| 56 |
# include <unistd.h> |
|---|
| 57 |
# include <errno.h> |
|---|
| 58 |
# include <sys/wait.h> |
|---|
| 59 |
# include <fcntl.h> |
|---|
| 60 |
# include <sys/socket.h> |
|---|
| 61 |
# include <sys/poll.h> |
|---|
| 62 |
#endif |
|---|
| 63 |
|
|---|
| 64 |
#if defined(WIN32) || defined(UNDER_CE) |
|---|
| 65 |
# undef _wopendir |
|---|
| 66 |
# undef _wreaddir |
|---|
| 67 |
# undef _wclosedir |
|---|
| 68 |
# undef rewinddir |
|---|
| 69 |
# define WIN32_LEAN_AND_MEAN |
|---|
| 70 |
# include <windows.h> |
|---|
| 71 |
#endif |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
char * vlc_strcasestr( const char *psz_big, const char *psz_little ) |
|---|
| 78 |
{ |
|---|
| 79 |
#if defined (HAVE_STRCASESTR) || defined (HAVE_STRISTR) |
|---|
| 80 |
return strcasestr (psz_big, psz_little); |
|---|
| 81 |
#else |
|---|
| 82 |
char *p_pos = (char *)psz_big; |
|---|
| 83 |
|
|---|
| 84 |
if( !psz_big || !psz_little || !*psz_little ) return p_pos; |
|---|
| 85 |
|
|---|
| 86 |
while( *p_pos ) |
|---|
| 87 |
{ |
|---|
| 88 |
if( toupper( *p_pos ) == toupper( *psz_little ) ) |
|---|
| 89 |
{ |
|---|
| 90 |
char * psz_cur1 = p_pos + 1; |
|---|
| 91 |
char * psz_cur2 = (char *)psz_little + 1; |
|---|
| 92 |
while( *psz_cur1 && *psz_cur2 && |
|---|
| 93 |
toupper( *psz_cur1 ) == toupper( *psz_cur2 ) ) |
|---|
| 94 |
{ |
|---|
| 95 |
psz_cur1++; |
|---|
| 96 |
psz_cur2++; |
|---|
| 97 |
} |
|---|
| 98 |
if( !*psz_cur2 ) return p_pos; |
|---|
| 99 |
} |
|---|
| 100 |
p_pos++; |
|---|
| 101 |
} |
|---|
| 102 |
return NULL; |
|---|
| 103 |
#endif |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
long long vlc_strtoll( const char *nptr, char **endptr, int base ) |
|---|
| 110 |
{ |
|---|
| 111 |
#if defined( HAVE_STRTOLL ) |
|---|
| 112 |
return strtoll( nptr, endptr, base ); |
|---|
| 113 |
#else |
|---|
| 114 |
long long i_value = 0; |
|---|
| 115 |
int sign = 1, newbase = base ? base : 10; |
|---|
| 116 |
|
|---|
| 117 |
while( isspace(*nptr) ) nptr++; |
|---|
| 118 |
|
|---|
| 119 |
if( *nptr == '-' ) |
|---|
| 120 |
{ |
|---|
| 121 |
sign = -1; |
|---|
| 122 |
nptr++; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
if( *nptr == '0' ) |
|---|
| 127 |
{ |
|---|
| 128 |
newbase = 8; |
|---|
| 129 |
nptr++; |
|---|
| 130 |
|
|---|
| 131 |
if( *nptr == 'x' ) |
|---|
| 132 |
{ |
|---|
| 133 |
newbase = 16; |
|---|
| 134 |
nptr++; |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
if( base && newbase != base ) |
|---|
| 139 |
{ |
|---|
| 140 |
if( endptr ) *endptr = (char *)nptr; |
|---|
| 141 |
return i_value; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
switch( newbase ) |
|---|
| 145 |
{ |
|---|
| 146 |
case 10: |
|---|
| 147 |
while( *nptr >= '0' && *nptr <= '9' ) |
|---|
| 148 |
{ |
|---|
| 149 |
i_value *= 10; |
|---|
| 150 |
i_value += ( *nptr++ - '0' ); |
|---|
| 151 |
} |
|---|
| 152 |
if( endptr ) *endptr = (char *)nptr; |
|---|
| 153 |
break; |
|---|
| 154 |
|
|---|
| 155 |
case 16: |
|---|
| 156 |
while( (*nptr >= '0' && *nptr <= '9') || |
|---|
| 157 |
(*nptr >= 'a' && *nptr <= 'f') || |
|---|
| 158 |
(*nptr >= 'A' && *nptr <= 'F') ) |
|---|
| 159 |
{ |
|---|
| 160 |
int i_valc = 0; |
|---|
| 161 |
if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0'; |
|---|
| 162 |
else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10; |
|---|
| 163 |
else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10; |
|---|
| 164 |
i_value *= 16; |
|---|
| 165 |
i_value += i_valc; |
|---|
| 166 |
nptr++; |
|---|
| 167 |
} |
|---|
| 168 |
if( endptr ) *endptr = (char *)nptr; |
|---|
| 169 |
break; |
|---|
| 170 |
|
|---|
| 171 |
default: |
|---|
| 172 |
i_value = strtol( nptr, endptr, newbase ); |
|---|
| 173 |
break; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
return i_value * sign; |
|---|
| 177 |
#endif |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
extern size_t vlc_strlcpy (char *tgt, const char *src, size_t bufsize) |
|---|
| 192 |
{ |
|---|
| 193 |
#ifdef HAVE_STRLCPY |
|---|
| 194 |
return strlcpy (tgt, src, bufsize); |
|---|
| 195 |
#else |
|---|
| 196 |
size_t length; |
|---|
| 197 |
|
|---|
| 198 |
for (length = 1; (length < bufsize) && *src; length++) |
|---|
| 199 |
*tgt++ = *src++; |
|---|
| 200 |
|
|---|
| 201 |
if (bufsize) |
|---|
| 202 |
*tgt = '\0'; |
|---|
| 203 |
|
|---|
| 204 |
while (*src++) |
|---|
| 205 |
length++; |
|---|
| 206 |
|
|---|
| 207 |
return length - 1; |
|---|
| 208 |
#endif |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
char *vlc_strsep( char **ppsz_string, const char *psz_delimiters ) |
|---|
| 216 |
{ |
|---|
| 217 |
char *psz_string = *ppsz_string; |
|---|
| 218 |
if( !psz_string ) |
|---|
| 219 |
return NULL; |
|---|
| 220 |
|
|---|
| 221 |
char *p = strpbrk( psz_string, psz_delimiters ); |
|---|
| 222 |
if( !p ) |
|---|
| 223 |
{ |
|---|
| 224 |
*ppsz_string = NULL; |
|---|
| 225 |
return psz_string; |
|---|
| 226 |
} |
|---|
| 227 |
*p++ = '\0'; |
|---|
| 228 |
|
|---|
| 229 |
*ppsz_string = p; |
|---|
| 230 |
return psz_string; |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
#if defined(WIN32) && !defined(UNDER_CE) |
|---|
| 238 |
# include <assert.h> |
|---|
| 239 |
|
|---|
| 240 |
typedef struct vlc_DIR |
|---|
| 241 |
{ |
|---|
| 242 |
_WDIR *p_real_dir; |
|---|
| 243 |
int i_drives; |
|---|
| 244 |
struct _wdirent dd_dir; |
|---|
| 245 |
bool b_insert_back; |
|---|
| 246 |
} vlc_DIR; |
|---|
| 247 |
|
|---|
| 248 |
void *vlc_wopendir( const wchar_t *wpath ) |
|---|
| 249 |
{ |
|---|
| 250 |
vlc_DIR *p_dir = NULL; |
|---|
| 251 |
_WDIR *p_real_dir = NULL; |
|---|
| 252 |
|
|---|
| 253 |
if ( wpath == NULL || wpath[0] == '\0' |
|---|
| 254 |
|| (wcscmp (wpath, L"\\") == 0) ) |
|---|
| 255 |
{ |
|---|
| 256 |
|
|---|
| 257 |
p_dir = malloc( sizeof(vlc_DIR) ); |
|---|
| 258 |
if( !p_dir ) |
|---|
| 259 |
return NULL; |
|---|
| 260 |
p_dir->p_real_dir = NULL; |
|---|
| 261 |
p_dir->i_drives = GetLogicalDrives(); |
|---|
| 262 |
return (void *)p_dir; |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
p_real_dir = _wopendir( wpath ); |
|---|
| 266 |
if ( p_real_dir == NULL ) |
|---|
| 267 |
return NULL; |
|---|
| 268 |
|
|---|
| 269 |
p_dir = malloc( sizeof(vlc_DIR) ); |
|---|
| 270 |
if( !p_dir ) |
|---|
| 271 |
{ |
|---|
| 272 |
_wclosedir( p_real_dir ); |
|---|
| 273 |
return NULL; |
|---|
| 274 |
} |
|---|
| 275 |
p_dir->p_real_dir = p_real_dir; |
|---|
| 276 |
|
|---|
| 277 |
assert (wpath[0]); |
|---|
| 278 |
p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\"); |
|---|
| 279 |
return (void *)p_dir; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
struct _wdirent *vlc_wreaddir( void *_p_dir ) |
|---|
| 283 |
{ |
|---|
| 284 |
vlc_DIR *p_dir = (vlc_DIR *)_p_dir; |
|---|
| 285 |
unsigned int i; |
|---|
| 286 |
DWORD i_drives; |
|---|
| 287 |
|
|---|
| 288 |
if ( p_dir->p_real_dir != NULL ) |
|---|
| 289 |
{ |
|---|
| 290 |
if ( p_dir->b_insert_back ) |
|---|
| 291 |
{ |
|---|
| 292 |
|
|---|
| 293 |
p_dir->dd_dir.d_ino = 0; |
|---|
| 294 |
p_dir->dd_dir.d_reclen = 0; |
|---|
| 295 |
p_dir->dd_dir.d_namlen = 2; |
|---|
| 296 |
wcscpy( p_dir->dd_dir.d_name, L".." ); |
|---|
| 297 |
p_dir->b_insert_back = false; |
|---|
| 298 |
return &p_dir->dd_dir; |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
return _wreaddir( p_dir->p_real_dir ); |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
i_drives = p_dir->i_drives; |
|---|
| 306 |
if ( !i_drives ) |
|---|
| 307 |
return NULL; |
|---|
| 308 |
|
|---|
| 309 |
for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 ) |
|---|
| 310 |
if ( i_drives & 1 ) break; |
|---|
| 311 |
|
|---|
| 312 |
if ( i >= 26 ) |
|---|
| 313 |
return NULL; |
|---|
| 314 |
|
|---|
| 315 |
swprintf( p_dir->dd_dir.d_name, L"%c:\\", 'A' + i ); |
|---|
| 316 |
p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name); |
|---|
| 317 |
p_dir->i_drives &= ~(1UL << i); |
|---|
| 318 |
return &p_dir->dd_dir; |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
void vlc_rewinddir( void *_p_dir ) |
|---|
| 322 |
{ |
|---|
| 323 |
vlc_DIR *p_dir = (vlc_DIR *)_p_dir; |
|---|
| 324 |
|
|---|
| 325 |
if ( p_dir->p_real_dir != NULL ) |
|---|
| 326 |
_wrewinddir( p_dir->p_real_dir ); |
|---|
| 327 |
} |
|---|
| 328 |
#endif |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
int vlc_wclosedir( void *_p_dir ) |
|---|
| 332 |
{ |
|---|
| 333 |
#if defined(WIN32) && !defined(UNDER_CE) |
|---|
| 334 |
vlc_DIR *p_dir = (vlc_DIR *)_p_dir; |
|---|
| 335 |
int i_ret = 0; |
|---|
| 336 |
|
|---|
| 337 |
if ( p_dir->p_real_dir != NULL ) |
|---|
| 338 |
i_ret = _wclosedir( p_dir->p_real_dir ); |
|---|
| 339 |
|
|---|
| 340 |
free( p_dir ); |
|---|
| 341 |
return i_ret; |
|---|
| 342 |
#else |
|---|
| 343 |
return closedir( _p_dir ); |
|---|
| 344 |
#endif |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
char *vlc_gettext( const char *msgid ) |
|---|
| 351 |
{ |
|---|
| 352 |
#ifdef ENABLE_NLS |
|---|
| 353 |
return dgettext( PACKAGE_NAME, msgid ); |
|---|
| 354 |
#else |
|---|
| 355 |
return (char *)msgid; |
|---|
| 356 |
#endif |
|---|
| 357 |
} |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
static int count_utf8_string( const char *psz_string ) |
|---|
| 363 |
{ |
|---|
| 364 |
int i = 0, i_count = 0; |
|---|
| 365 |
while( psz_string[ i ] != 0 ) |
|---|
| 366 |
{ |
|---|
| 367 |
if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++; |
|---|
| 368 |
i++; |
|---|
| 369 |
} |
|---|
| 370 |
return i_count; |
|---|
| 371 |
} |
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
char *vlc_wraptext( const char *psz_text, int i_line ) |
|---|
| 378 |
{ |
|---|
| 379 |
int i_len; |
|---|
| 380 |
char *psz_line, *psz_new_text; |
|---|
| 381 |
|
|---|
| 382 |
psz_line = psz_new_text = strdup( psz_text ); |
|---|
| 383 |
|
|---|
| 384 |
i_len = count_utf8_string( psz_text ); |
|---|
| 385 |
|
|---|
| 386 |
while( i_len > i_line ) |
|---|
| 387 |
{ |
|---|
| 388 |
|
|---|
| 389 |
char *psz_parser = psz_line; |
|---|
| 390 |
int i_count = 0; |
|---|
| 391 |
while( i_count <= i_line && *psz_parser != '\n' ) |
|---|
| 392 |
{ |
|---|
| 393 |
while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++; |
|---|
| 394 |
psz_parser++; |
|---|
| 395 |
i_count++; |
|---|
| 396 |
} |
|---|
| 397 |
if( *psz_parser == '\n' ) |
|---|
| 398 |
{ |
|---|
| 399 |
i_len -= (i_count + 1); |
|---|
| 400 |
psz_line = psz_parser + 1; |
|---|
| 401 |
continue; |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
while( psz_parser > psz_line && *psz_parser != ' ' ) |
|---|
| 406 |
{ |
|---|
| 407 |
while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--; |
|---|
| 408 |
psz_parser--; |
|---|
| 409 |
i_count--; |
|---|
| 410 |
} |
|---|
| 411 |
if( *psz_parser == ' ' ) |
|---|
| 412 |
{ |
|---|
| 413 |
*psz_parser = '\n'; |
|---|
| 414 |
i_len -= (i_count + 1); |
|---|
| 415 |
psz_line = psz_parser + 1; |
|---|
| 416 |
continue; |
|---|
| 417 |
} |
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' ) |
|---|
| 421 |
{ |
|---|
| 422 |
while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++; |
|---|
| 423 |
psz_parser++; |
|---|
| 424 |
i_count++; |
|---|
| 425 |
} |
|---|
| 426 |
if( i_count < i_len ) *psz_parser = '\n'; |
|---|
| 427 |
i_len -= (i_count + 1); |
|---|
| 428 |
psz_line = psz_parser + 1; |
|---|
| 429 |
} |
|---|
| 430 |
|
|---|
| 431 |
return psz_new_text; |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode ) |
|---|
| 438 |
{ |
|---|
| 439 |
#if defined(HAVE_ICONV) |
|---|
| 440 |
return iconv_open( tocode, fromcode ); |
|---|
| 441 |
#else |
|---|
| 442 |
return (vlc_iconv_t)(-1); |
|---|
| 443 |
#endif |
|---|
| 444 |
} |
|---|
| 445 |
|
|---|
| 446 |
size_t vlc_iconv( vlc_iconv_t cd, const char **inbuf, size_t *inbytesleft, |
|---|
| 447 |
char **outbuf, size_t *outbytesleft ) |
|---|
| 448 |
{ |
|---|
| 449 |
#if defined(HAVE_ICONV) |
|---|
| 450 |
return iconv( cd, (ICONV_CONST char **)inbuf, inbytesleft, |
|---|
| 451 |
outbuf, outbytesleft ); |
|---|
| 452 |
#else |
|---|
| 453 |
abort (); |
|---|
| 454 |
#endif |
|---|
| 455 |
} |
|---|
| 456 |
|
|---|
| 457 |
int vlc_iconv_close( vlc_iconv_t cd ) |
|---|
| 458 |
{ |
|---|
| 459 |
#if defined(HAVE_ICONV) |
|---|
| 460 |
return iconv_close( cd ); |
|---|
| 461 |
#else |
|---|
| 462 |
abort (); |
|---|
| 463 |
#endif |
|---|
| 464 |
} |
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
bool vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den, |
|---|
| 471 |
uint64_t i_nom, uint64_t i_den, uint64_t i_max ) |
|---|
| 472 |
{ |
|---|
| 473 |
bool b_exact = 1; |
|---|
| 474 |
uint64_t i_gcd; |
|---|
| 475 |
|
|---|
| 476 |
if( i_den == 0 ) |
|---|
| 477 |
{ |
|---|
| 478 |
*pi_dst_nom = 0; |
|---|
| 479 |
*pi_dst_den = 1; |
|---|
| 480 |
return 1; |
|---|
| 481 |
} |
|---|
| 482 |
|
|---|
| 483 |
i_gcd = GCD( i_nom, i_den ); |
|---|
| 484 |
i_nom /= i_gcd; |
|---|
| 485 |
i_den /= i_gcd; |
|---|
| 486 |
|
|---|
| 487 |
if( i_max == 0 ) i_max = INT64_C(0xFFFFFFFF); |
|---|
| 488 |
|
|---|
| 489 |
if( i_nom > i_max || i_den > i_max ) |
|---|
| 490 |
{ |
|---|
| 491 |
uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0; |
|---|
| 492 |
b_exact = 0; |
|---|
| 493 |
|
|---|
| 494 |
for( ; ; ) |
|---|
| 495 |
{ |
|---|
| 496 |
uint64_t i_x = i_nom / i_den; |
|---|
| 497 |
uint64_t i_a2n = i_x * i_a1_num + i_a0_num; |
|---|
| 498 |
uint64_t i_a2d = i_x * i_a1_den + i_a0_den; |
|---|
| 499 |
|
|---|
| 500 |
if( i_a2n > i_max || i_a2d > i_max ) break; |
|---|
| 501 |
|
|---|
| 502 |
i_nom %= i_den; |
|---|
| 503 |
|
|---|
| 504 |
i_a0_num = i_a1_num; i_a0_den = i_a1_den; |
|---|
| 505 |
i_a1_num = i_a2n; i_a1_den = i_a2d; |
|---|
| 506 |
if( i_nom == 0 ) break; |
|---|
| 507 |
i_x = i_nom; i_nom = i_den; i_den = i_x; |
|---|
| 508 |
} |
|---|
| 509 |
i_nom = i_a1_num; |
|---|
| 510 |
i_den = i_a1_den; |
|---|
| 511 |
} |
|---|
| 512 |
|
|---|
| 513 |
*pi_dst_nom = i_nom; |
|---|
| 514 |
*pi_dst_den = i_den; |
|---|
| 515 |
|
|---|
| 516 |
return b_exact; |
|---|
| 517 |
} |
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
int __vlc_execve( vlc_object_t *p_object, int i_argc, char *const *ppsz_argv, |
|---|
| 524 |
char *const *ppsz_env, const char *psz_cwd, |
|---|
| 525 |
const char *p_in, size_t i_in, |
|---|
| 526 |
char **pp_data, size_t *pi_data ) |
|---|
| 527 |
{ |
|---|
| 528 |
(void)i_argc; |
|---|
| 529 |
#ifdef HAVE_FORK |
|---|
| 530 |
# define BUFSIZE 1024 |
|---|
| 531 |
int fds[2], i_status; |
|---|
| 532 |
|
|---|
| 533 |
if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds)) |
|---|
| 534 |
return -1; |
|---|
| 535 |
|
|---|
| 536 |
pid_t pid = -1; |
|---|
| 537 |
if ((fds[0] > 2) && (fds[1] > 2)) |
|---|
| 538 |
pid = fork (); |
|---|
| 539 |
|
|---|
| 540 |
switch (pid) |
|---|
| 541 |
{ |
|---|
| 542 |
case -1: |
|---|
| 543 |
msg_Err (p_object, "unable to fork (%m)"); |
|---|
| 544 |
close (fds[0]); |
|---|
| 545 |
close (fds[1]); |
|---|
| 546 |
return -1; |
|---|
| 547 |
|
|---|
| 548 |
case 0: |
|---|
| 549 |
{ |
|---|
| 550 |
sigset_t set; |
|---|
| 551 |
sigemptyset (&set); |
|---|
| 552 |
pthread_sigmask (SIG_SETMASK, &set, NULL); |
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
|
|---|
| 557 |
if ((close (0) == 0) && (close (1) == 0) && (close (2) == 0) |
|---|
| 558 |
&& (dup (fds[1]) == 0) && (dup (fds[1]) == 1) |
|---|
| 559 |
&& (open ("/dev/null", O_RDONLY) == 2) |
|---|
| 560 |
&& ((psz_cwd == NULL) || (chdir (psz_cwd) == 0))) |
|---|
| 561 |
execve (ppsz_argv[0], ppsz_argv, ppsz_env); |
|---|
| 562 |
|
|---|
| 563 |
exit (EXIT_FAILURE); |
|---|
| 564 |
} |
|---|
| 565 |
} |
|---|
| 566 |
|
|---|
| 567 |
close (fds[1]); |
|---|
| 568 |
|
|---|
| 569 |
*pi_data = 0; |
|---|
| 570 |
if (*pp_data) |
|---|
| 571 |
free (*pp_data); |
|---|
| 572 |
*pp_data = NULL; |
|---|
| 573 |
|
|---|
| 574 |
if (i_in == 0) |
|---|
| 575 |
shutdown (fds[0], SHUT_WR); |
|---|
| 576 |
|
|---|
| 577 |
while (!p_object->b_die) |
|---|
| 578 |
{ |
|---|
| 579 |
struct pollfd ufd[1]; |
|---|
| 580 |
memset (ufd, 0, sizeof (ufd)); |
|---|
| 581 |
ufd[0].fd = fds[0]; |
|---|
| 582 |
ufd[0].events = POLLIN; |
|---|
| 583 |
|
|---|
| 584 |
if (i_in > 0) |
|---|
| 585 |
ufd[0].events |= POLLOUT; |
|---|
| 586 |
|
|---|
| 587 |
if (poll (ufd, 1, 10) <= 0) |
|---|
| 588 |
continue; |
|---|
| 589 |
|
|---|
| 590 |
if (ufd[0].revents & ~POLLOUT) |
|---|
| 591 |
{ |
|---|
| 592 |
char *ptr = realloc (*pp_data, *pi_data + BUFSIZE + 1); |
|---|
| 593 |
if (ptr == NULL) |
|---|
| 594 |
break; |
|---|
| 595 |
|
|---|
| 596 |
*pp_data = ptr; |
|---|
| 597 |
|
|---|
| 598 |
ssize_t val = read (fds[0], ptr + *pi_data, BUFSIZE); |
|---|
| 599 |
switch (val) |
|---|
| 600 |
{ |
|---|
| 601 |
case -1: |
|---|
| 602 |
case 0: |
|---|
| 603 |
shutdown (fds[0], SHUT_RD); |
|---|
| 604 |
break; |
|---|
| 605 |
|
|---|
| 606 |
default: |
|---|
| 607 |
*pi_data += val; |
|---|
| 608 |
} |
|---|
| 609 |
} |
|---|
| 610 |
|
|---|
| 611 |
if (ufd[0].revents & POLLOUT) |
|---|
| 612 |
{ |
|---|
| 613 |
ssize_t val = write (fds[0], p_in, i_in); |
|---|
| 614 |
switch (val) |
|---|
| 615 |
{ |
|---|
| 616 |
case -1: |
|---|
| 617 |
case 0: |
|---|
| 618 |
i_in = 0; |
|---|
| 619 |
shutdown (fds[0], SHUT_WR); |
|---|
| 620 |
break; |
|---|
| 621 |
|
|---|
| 622 |
default: |
|---|
| 623 |
i_in -= val; |
|---|
| 624 |
p_in += val; |
|---|
| 625 |
} |
|---|
| 626 |
} |
|---|
| 627 |
} |
|---|
| 628 |
|
|---|
| 629 |
close (fds[0]); |
|---|
| 630 |
|
|---|
| 631 |
while (waitpid (pid, &i_status, 0) == -1); |
|---|
| 632 |
|
|---|
| 633 |
if (WIFEXITED (i_status)) |
|---|
| 634 |
{ |
|---|
| 635 |
i_status = WEXITSTATUS (i_status); |
|---|
| 636 |
if (i_status) |
|---|
| 637 |
msg_Warn (p_object, "child %s (PID %d) exited with error code %d", |
|---|
| 638 |
ppsz_argv[0], (int)pid, i_status); |
|---|
| 639 |
} |
|---|
| 640 |
else |
|---|
| 641 |
if (WIFSIGNALED (i_status)) |
|---|
| 642 |
{ |
|---|
| 643 |
i_status = WTERMSIG (i_status); |
|---|
| 644 |
msg_Warn (p_object, "child %s (PID %d) exited on signal %d (%s)", |
|---|
| 645 |
ppsz_argv[0], (int)pid, i_status, strsignal (i_status)); |
|---|
| 646 |
} |
|---|
| 647 |
|
|---|
| 648 |
#elif defined( WIN32 ) && !defined( UNDER_CE ) |
|---|
| 649 |
SECURITY_ATTRIBUTES saAttr; |
|---|
| 650 |
PROCESS_INFORMATION piProcInfo; |
|---|
| 651 |
STARTUPINFO siStartInfo; |
|---|
| 652 |
BOOL bFuncRetn = FALSE; |
|---|
| 653 |
HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr; |
|---|
| 654 |
DWORD i_status; |
|---|
| 655 |
char *psz_cmd = NULL, *p_env = NULL, *p = NULL; |
|---|
| 656 |
char **ppsz_parser; |
|---|
| 657 |
int i_size; |
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
|---|
| 661 |
saAttr.bInheritHandle = TRUE; |
|---|
| 662 |
saAttr.lpSecurityDescriptor = NULL; |
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) ) |
|---|
| 666 |
{ |
|---|
| 667 |
msg_Err( p_object, "stdout pipe creation failed" ); |
|---|
| 668 |
return -1; |
|---|
| 669 |
} |
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 ); |
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) ) |
|---|
| 676 |
{ |
|---|
| 677 |
msg_Err( p_object, "stdin pipe creation failed" ); |
|---|
| 678 |
return -1; |
|---|
| 679 |
} |
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 ); |
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) ); |
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); |
|---|
| 689 |
siStartInfo.cb = sizeof(STARTUPINFO); |
|---|
| 690 |
siStartInfo.hStdError = hChildStdoutWr; |
|---|
| 691 |
siStartInfo.hStdOutput = hChildStdoutWr; |
|---|
| 692 |
siStartInfo.hStdInput = hChildStdinRd; |
|---|
| 693 |
siStartInfo.wShowWindow = SW_HIDE; |
|---|
| 694 |
siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 |
psz_cmd = malloc(32768); |
|---|
| 698 |
if( !psz_cmd ) |
|---|
| 699 |
return -1; |
|---|
| 700 |
psz_cmd[0] = '\0'; |
|---|
| 701 |
i_size = 32768; |
|---|
| 702 |
ppsz_parser = &ppsz_argv[0]; |
|---|
| 703 |
while ( ppsz_parser[0] != NULL && i_size > 0 ) |
|---|
| 704 |
{ |
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
if ( ppsz_parser[1] == NULL ) |
|---|
| 709 |
{ |
|---|
| 710 |
strncat( psz_cmd, "\"", i_size ); |
|---|
| 711 |
i_size--; |
|---|
| 712 |
} |
|---|
| 713 |
strncat( psz_cmd, *ppsz_parser, i_size ); |
|---|
| 714 |
i_size -= strlen( *ppsz_parser ); |
|---|
| 715 |
if ( ppsz_parser[1] == NULL ) |
|---|
| 716 |
{ |
|---|
| 717 |
strncat( psz_cmd, "\"", i_size ); |
|---|
| 718 |
i_size--; |
|---|
| 719 |
} |
|---|
| 720 |
strncat( psz_cmd, " ", i_size ); |
|---|
| 721 |
i_size--; |
|---|
| 722 |
ppsz_parser++; |
|---|
| 723 |
} |
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
p = p_env = malloc(32768); |
|---|
| 727 |
if( !p ) |
|---|
| 728 |
{ |
|---|
| 729 |
free( psz_cmd ); |
|---|
| 730 |
return -1; |
|---|
| 731 |
} |
|---|
| 732 |
|
|---|
| 733 |
i_size = 32768; |
|---|
| 734 |
ppsz_parser = &ppsz_env[0]; |
|---|
| 735 |
while ( *ppsz_parser != NULL && i_size > 0 ) |
|---|
| 736 |
{ |
|---|
| 737 |
memcpy( p, *ppsz_parser, |
|---|
| 738 |
__MIN((int)(strlen(*ppsz_parser) + 1), i_size) ); |
|---|
| 739 |
p += strlen(*ppsz_parser) + 1; |
|---|
| 740 |
i_size -= strlen(*ppsz_parser) + 1; |
|---|
| 741 |
ppsz_parser++; |
|---|
| 742 |
} |
|---|
| 743 |
*p = '\0'; |
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
bFuncRetn = CreateProcess( NULL, |
|---|
| 747 |
psz_cmd, |
|---|
| 748 |
NULL, |
|---|
| 749 |
NULL, |
|---|
| 750 |
TRUE, |
|---|
| 751 |
0, |
|---|
| 752 |
p_env, |
|---|
| 753 |
psz_cwd, |
|---|
| 754 |
&siStartInfo, |
|---|
| 755 |
&piProcInfo ); |
|---|
| 756 |
|
|---|
| 757 |
free( psz_cmd ); |
|---|
| 758 |
free( p_env ); |
|---|
| 759 |
|
|---|
| 760 |
if ( bFuncRetn == 0 ) |
|---|
| 761 |
{ |
|---|
| 762 |
msg_Err( p_object, "child creation failed" ); |
|---|
| 763 |
return -1; |
|---|
| 764 |
} |
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
while ( i_in > 0 && !p_object->b_die ) |
|---|
| 768 |
{ |
|---|
| 769 |
DWORD i_written; |
|---|
| 770 |
if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) ) |
|---|
| 771 |
break; |
|---|
| 772 |
i_in -= i_written; |
|---|
| 773 |
p_in += i_written; |
|---|
| 774 |
} |
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
CloseHandle(hChildStdinWr); |
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
CloseHandle(hChildStdoutWr); |
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
*pi_data = 0; |
|---|
| 785 |
if( *pp_data ) |
|---|
| 786 |
free( pp_data ); |
|---|
| 787 |
*pp_data = NULL; |
|---|
| 788 |
*pp_data = malloc( 1025 ); |
|---|
| 789 |
|
|---|
| 790 |
while ( !p_object->b_die ) |
|---|
| 791 |
{ |
|---|
| 792 |
DWORD i_read; |
|---|
| 793 |
if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read, |
|---|
| 794 |
NULL ) |
|---|
| 795 |
|| i_read == 0 ) |
|---|
| 796 |
break; |
|---|
| 797 |
*pi_data += i_read; |
|---|
| 798 |
*pp_data = realloc( *pp_data, *pi_data + 1025 ); |
|---|
| 799 |
} |
|---|
| 800 |
|
|---|
| 801 |
while ( !p_object->b_die |
|---|
| 802 |
&& !GetExitCodeProcess( piProcInfo.hProcess, &i_status ) |
|---|
| 803 |
&& i_status != STILL_ACTIVE ) |
|---|
| 804 |
msleep( 10000 ); |
|---|
| 805 |
|
|---|
| 806 |
CloseHandle(piProcInfo.hProcess); |
|---|
| 807 |
CloseHandle(piProcInfo.hThread); |
|---|
| 808 |
|
|---|
| 809 |
if ( i_status ) |
|---|
| 810 |
msg_Warn( p_object, |
|---|
| 811 |
"child %s returned with error code %ld", |
|---|
| 812 |
ppsz_argv[0], i_status ); |
|---|
| 813 |
|
|---|
| 814 |
#else |
|---|
| 815 |
msg_Err( p_object, "vlc_execve called but no implementation is available" ); |
|---|
| 816 |
return -1; |
|---|
| 817 |
|
|---|
| 818 |
#endif |
|---|
| 819 |
|
|---|
| 820 |
if (*pp_data == NULL) |
|---|
| 821 |
return -1; |
|---|
| 822 |
|
|---|
| 823 |
(*pp_data)[*pi_data] = '\0'; |
|---|
| 824 |
return 0; |
|---|
| 825 |
} |
|---|