]> git.sesse.net Git - vlc/blob - src/extras/libc.c
* src/extras/libc.c: Implemented a wrapper around fork() and execve()
[vlc] / src / extras / libc.c
1 /*****************************************************************************
2  * libc.c: Extra libc function for some systems.
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *          Christophe Massiot <massiot@via.ecp.fr>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27 #include <string.h>                                              /* strdup() */
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include <vlc/vlc.h>
32
33 #undef iconv_t
34 #undef iconv_open
35 #undef iconv
36 #undef iconv_close
37
38 #if defined(HAVE_ICONV)
39 #   include <iconv.h>
40 #endif
41
42 #ifdef HAVE_DIRENT_H
43 #   include <dirent.h>
44 #endif
45
46 #ifdef HAVE_FORK
47 #   include <sys/time.h>
48 #   include <unistd.h>
49 #   include <errno.h>
50 #   include <sys/wait.h>
51 #endif
52
53 /*****************************************************************************
54  * getenv: just in case, but it should never be called
55  *****************************************************************************/
56 #if !defined( HAVE_GETENV )
57 char *vlc_getenv( const char *name )
58 {
59     return NULL;
60 }
61 #endif
62
63 /*****************************************************************************
64  * strdup: returns a malloc'd copy of a string
65  *****************************************************************************/
66 #if !defined( HAVE_STRDUP )
67 char *vlc_strdup( const char *string )
68 {
69     return strndup( string, strlen( string ) );
70 }
71 #endif
72
73 /*****************************************************************************
74  * strndup: returns a malloc'd copy of at most n bytes of string
75  * Does anyone know whether or not it will be present in Jaguar?
76  *****************************************************************************/
77 #if !defined( HAVE_STRNDUP )
78 char *vlc_strndup( const char *string, size_t n )
79 {
80     char *psz;
81     size_t len = strlen( string );
82
83     len = __MIN( len, n );
84     psz = (char*)malloc( len + 1 );
85
86     if( psz != NULL )
87     {
88         memcpy( (void*)psz, (const void*)string, len );
89         psz[ len ] = 0;
90     }
91
92     return psz;
93 }
94 #endif
95
96 /*****************************************************************************
97  * strcasecmp: compare two strings ignoring case
98  *****************************************************************************/
99 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
100 int vlc_strcasecmp( const char *s1, const char *s2 )
101 {
102     int c1, c2;
103     if( !s1 || !s2 ) return  -1;
104
105     while( *s1 && *s2 )
106     {
107         c1 = tolower(*s1);
108         c2 = tolower(*s2);
109
110         if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
111         s1++; s2++;
112     }
113
114     if( !*s1 && !*s2 ) return 0;
115     else return (*s1 ? 1 : -1);
116 }
117 #endif
118
119 /*****************************************************************************
120  * strncasecmp: compare n chars from two strings ignoring case
121  *****************************************************************************/
122 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
123 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
124 {
125     int c1, c2;
126     if( !s1 || !s2 ) return  -1;
127
128     while( n > 0 && *s1 && *s2 )
129     {
130         c1 = tolower(*s1);
131         c2 = tolower(*s2);
132
133         if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
134         s1++; s2++; n--;
135     }
136
137     if( !n || (!*s1 && !*s2) ) return 0;
138     else return (*s1 ? 1 : -1);
139 }
140 #endif
141
142 /******************************************************************************
143  * strcasestr: find a substring (little) in another substring (big)
144  * Case sensitive. Return NULL if not found, return big if little == null
145  *****************************************************************************/
146 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
147 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
148 {
149     char *p_pos = (char *)psz_big;
150
151     if( !psz_big || !psz_little || !*psz_little ) return p_pos;
152  
153     while( *p_pos ) 
154     {
155         if( toupper( *p_pos ) == toupper( *psz_little ) )
156         {
157             char * psz_cur1 = p_pos + 1;
158             char * psz_cur2 = (char *)psz_little + 1;
159             while( *psz_cur1 && *psz_cur2 &&
160                    toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
161             {
162                 psz_cur1++;
163                 psz_cur2++;
164             }
165             if( !*psz_cur2 ) return p_pos;
166         }
167         p_pos++;
168     }
169     return NULL;
170 }
171 #endif
172
173 /*****************************************************************************
174  * vasprintf:
175  *****************************************************************************/
176 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
177 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
178 {
179     /* Guess we need no more than 100 bytes. */
180     int     i_size = 100;
181     char    *p = malloc( i_size );
182     int     n;
183
184     if( p == NULL )
185     {
186         *strp = NULL;
187         return -1;
188     }
189
190     for( ;; )
191     {
192         /* Try to print in the allocated space. */
193         n = vsnprintf( p, i_size, fmt, ap );
194
195         /* If that worked, return the string. */
196         if (n > -1 && n < i_size)
197         {
198             *strp = p;
199             return strlen( p );
200         }
201         /* Else try again with more space. */
202         if (n > -1)    /* glibc 2.1 */
203         {
204            i_size = n+1; /* precisely what is needed */
205         }
206         else           /* glibc 2.0 */
207         {
208            i_size *= 2;  /* twice the old size */
209         }
210         if( (p = realloc( p, i_size ) ) == NULL)
211         {
212             *strp = NULL;
213             return -1;
214         }
215     }
216 }
217 #endif
218
219 /*****************************************************************************
220  * asprintf:
221  *****************************************************************************/
222 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
223 int vlc_asprintf( char **strp, const char *fmt, ... )
224 {
225     va_list args;
226     int i_ret;
227
228     va_start( args, fmt );
229     i_ret = vasprintf( strp, fmt, args );
230     va_end( args );
231
232     return i_ret;
233 }
234 #endif
235
236 /*****************************************************************************
237  * atof: convert a string to a double.
238  *****************************************************************************/
239 #if !defined( HAVE_ATOF )
240 double vlc_atof( const char *nptr )
241 {
242     double f_result;
243     wchar_t *psz_tmp;
244     int i_len = strlen( nptr ) + 1;
245
246     psz_tmp = malloc( i_len * sizeof(wchar_t) );
247     MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
248     f_result = wcstod( psz_tmp, NULL );
249     free( psz_tmp );
250
251     return f_result;
252 }
253 #endif
254
255 /*****************************************************************************
256  * strtoll: convert a string to a 64 bits int.
257  *****************************************************************************/
258 #if !defined( HAVE_STRTOLL )
259 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
260 {
261     int64_t i_value = 0;
262     int sign = 1, newbase = base ? base : 10;
263
264     while( isspace(*nptr) ) nptr++;
265
266     if( *nptr == '-' )
267     {
268         sign = -1;
269         nptr++;
270     }
271
272     /* Try to detect base */
273     if( *nptr == '0' )
274     {
275         newbase = 8;
276         nptr++;
277
278         if( *nptr == 'x' )
279         {
280             newbase = 16;
281             nptr++;
282         }
283     }
284
285     if( base && newbase != base )
286     {
287         if( endptr ) *endptr = (char *)nptr;
288         return i_value;
289     }
290
291     switch( newbase )
292     {
293         case 10:
294             while( *nptr >= '0' && *nptr <= '9' )
295             {
296                 i_value *= 10;
297                 i_value += ( *nptr++ - '0' );
298             }
299             if( endptr ) *endptr = (char *)nptr;
300             break;
301
302         case 16:
303             while( (*nptr >= '0' && *nptr <= '9') ||
304                    (*nptr >= 'a' && *nptr <= 'f') ||
305                    (*nptr >= 'A' && *nptr <= 'F') )
306             {
307                 int i_valc = 0;
308                 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
309                 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
310                 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
311                 i_value *= 16;
312                 i_value += i_valc;
313                 nptr++;
314             }
315             if( endptr ) *endptr = (char *)nptr;
316             break;
317
318         default:
319             i_value = strtol( nptr, endptr, newbase );
320             break;
321     }
322
323     return i_value * sign;
324 }
325 #endif
326
327 /*****************************************************************************
328  * atoll: convert a string to a 64 bits int.
329  *****************************************************************************/
330 #if !defined( HAVE_ATOLL )
331 int64_t vlc_atoll( const char *nptr )
332 {
333     return strtoll( nptr, (char **)NULL, 10 );
334 }
335 #endif
336
337 /*****************************************************************************
338  * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
339  * when called with an empty argument or just '\'
340  *****************************************************************************/
341 #if defined(WIN32) || defined(UNDER_CE)
342
343 #define WIN32_LEAN_AND_MEAN
344 #include <windows.h> /* for GetLogicalDrives */
345
346 typedef struct vlc_DIR
347 {
348     DIR *p_real_dir;
349     int i_drives;
350     struct dirent dd_dir;
351     vlc_bool_t b_insert_back;
352 } vlc_DIR;
353
354 void *vlc_opendir_wrapper( const char *psz_path )
355 {
356     vlc_DIR *p_dir;
357     DIR *p_real_dir;
358
359     if ( psz_path == NULL || psz_path[0] == '\0'
360           || (psz_path[0] == '\\' && psz_path[1] == '\0') )
361     {
362         /* Special mode to list drive letters */
363         p_dir = malloc( sizeof(vlc_DIR) );
364         p_dir->p_real_dir = NULL;
365         p_dir->i_drives = GetLogicalDrives();
366         return (void *)p_dir;
367     }
368
369     p_real_dir = opendir( psz_path );
370     if ( p_real_dir == NULL )
371         return NULL;
372
373     p_dir = malloc( sizeof(vlc_DIR) );
374     p_dir->p_real_dir = p_real_dir;
375     p_dir->b_insert_back = ( psz_path[1] == ':' && psz_path[2] == '\\'
376                               && psz_path[3] =='\0' );
377     return (void *)p_dir;
378 }
379
380 struct dirent *vlc_readdir_wrapper( void *_p_dir )
381 {
382     vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
383     unsigned int i;
384     DWORD i_drives;
385
386     if ( p_dir->p_real_dir != NULL )
387     {
388         if ( p_dir->b_insert_back )
389         {
390             p_dir->dd_dir.d_ino = 0;
391             p_dir->dd_dir.d_reclen = 0;
392             p_dir->dd_dir.d_namlen = 2;
393             strcpy( p_dir->dd_dir.d_name, ".." );
394             p_dir->b_insert_back = VLC_FALSE;
395             return &p_dir->dd_dir;
396         }
397
398         return readdir( p_dir->p_real_dir );
399     }
400
401     /* Drive letters mode */
402     i_drives = p_dir->i_drives;
403     if ( !i_drives )
404         return NULL; /* end */
405
406     for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
407         if ( i_drives & 1 ) break;
408
409     if ( i >= 26 )
410         return NULL; /* this should not happen */
411
412     sprintf( p_dir->dd_dir.d_name, "%c:\\", 'A' + i );
413     p_dir->dd_dir.d_namlen = strlen(p_dir->dd_dir.d_name);
414     p_dir->i_drives &= ~(1UL << i);
415     return &p_dir->dd_dir;
416 }
417
418 int vlc_closedir_wrapper( void *_p_dir )
419 {
420     vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
421
422     if ( p_dir->p_real_dir != NULL )
423     {
424         int i_ret = closedir( p_dir->p_real_dir );
425         free( p_dir );
426         return i_ret;
427     }
428
429     free( p_dir );
430     return 0;
431 }
432 #else
433 void *vlc_opendir_wrapper( const char *psz_path )
434 {
435     return (void *)opendir( psz_path );
436 }
437 struct dirent *vlc_readdir_wrapper( void *_p_dir )
438 {
439     return readdir( (DIR *)_p_dir );
440 }
441 int vlc_closedir_wrapper( void *_p_dir )
442 {
443     return closedir( (DIR *)_p_dir );
444 }
445 #endif
446
447 /*****************************************************************************
448  * scandir: scan a directory alpha-sorted
449  *****************************************************************************/
450 #if !defined( HAVE_SCANDIR )
451 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
452 {
453     return strcoll( (*a)->d_name, (*b)->d_name );
454 }
455
456 int vlc_scandir( const char *name, struct dirent ***namelist,
457                     int (*filter) ( const struct dirent * ),
458                     int (*compar) ( const struct dirent **,
459                                     const struct dirent ** ) )
460 {
461     DIR            * p_dir;
462     struct dirent  * p_content;
463     struct dirent ** pp_list;
464     int              ret, size;
465
466     if( !namelist || !( p_dir = vlc_opendir_wrapper( name ) ) ) return -1;
467
468     ret     = 0;
469     pp_list = NULL;
470     while( ( p_content = vlc_readdir_wrapper( p_dir ) ) )
471     {
472         if( filter && !filter( p_content ) )
473         {
474             continue;
475         }
476         pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
477         size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
478         pp_list[ret] = malloc( size );
479         memcpy( pp_list[ret], p_content, size );
480         ret++;
481     }
482
483     vlc_closedir_wrapper( p_dir );
484
485     if( compar )
486     {
487         qsort( pp_list, ret, sizeof( struct dirent * ),
488                (int (*)(const void *, const void *)) compar );
489     }
490
491     *namelist = pp_list;
492     return ret;
493 }
494 #endif
495
496 /*****************************************************************************
497  * dgettext: gettext for plugins.
498  *****************************************************************************/
499 char *vlc_dgettext( const char *package, const char *msgid )
500 {
501 #if defined( ENABLE_NLS ) \
502      && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
503     return dgettext( package, msgid );
504 #else
505     return (char *)msgid;
506 #endif
507 }
508
509 /*****************************************************************************
510  * count_utf8_string: returns the number of characters in the string.
511  *****************************************************************************/
512 static int count_utf8_string( const char *psz_string )
513 {
514     int i = 0, i_count = 0;
515     while( psz_string[ i ] != 0 )
516     {
517         if( ((unsigned char *)psz_string)[ i ] <  0x80UL ) i_count++;
518         i++;
519     }
520     return i_count;
521 }
522
523 /*****************************************************************************
524  * wraptext: inserts \n at convenient places to wrap the text.
525  *           Returns the modified string in a new buffer.
526  *****************************************************************************/
527 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
528 {
529     int i_len;
530     char *psz_line, *psz_new_text;
531
532     psz_line = psz_new_text = strdup( psz_text );
533
534     if( b_utf8 )
535         i_len = count_utf8_string( psz_text );
536     else
537         i_len = strlen( psz_text );
538
539     while( i_len > i_line )
540     {
541         /* Look if there is a newline somewhere. */
542         char *psz_parser = psz_line;
543         int i_count = 0;
544         while( i_count <= i_line && *psz_parser != '\n' )
545         {
546             if( b_utf8 )
547             {
548                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
549             }
550             psz_parser++;
551             i_count++;
552         }
553         if( *psz_parser == '\n' )
554         {
555             i_len -= (i_count + 1);
556             psz_line = psz_parser + 1;
557             continue;
558         }
559
560         /* Find the furthest space. */
561         while( psz_parser > psz_line && *psz_parser != ' ' )
562         {
563             if( b_utf8 )
564             {
565                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
566             }
567             psz_parser--;
568             i_count--;
569         }
570         if( *psz_parser == ' ' )
571         {
572             *psz_parser = '\n';
573             i_len -= (i_count + 1);
574             psz_line = psz_parser + 1;
575             continue;
576         }
577
578         /* Wrapping has failed. Find the first space or newline */
579         while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
580         {
581             if( b_utf8 )
582             {
583                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
584             }
585             psz_parser++;
586             i_count++;
587         }
588         if( i_count < i_len ) *psz_parser = '\n';
589         i_len -= (i_count + 1);
590         psz_line = psz_parser + 1;
591     }
592
593     return psz_new_text;
594 }
595
596 /*****************************************************************************
597  * iconv wrapper
598  *****************************************************************************/
599 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
600 {
601 #if defined(HAVE_ICONV)
602     return iconv_open( tocode, fromcode );
603 #else
604     return NULL;
605 #endif
606 }
607
608 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
609                   char **outbuf, size_t *outbytesleft )
610 {
611 #if defined(HAVE_ICONV)
612     return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
613 #else
614     int i_bytes = __MIN(*inbytesleft, *outbytesleft);
615     if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
616     memcpy( *outbuf, *inbuf, i_bytes );
617     inbuf += i_bytes;
618     outbuf += i_bytes;
619     inbytesleft -= i_bytes;
620     outbytesleft -= i_bytes;
621     return i_bytes;
622 #endif
623 }
624
625 int vlc_iconv_close( vlc_iconv_t cd )
626 {
627 #if defined(HAVE_ICONV)
628     return iconv_close( cd );
629 #else
630     return 0;
631 #endif
632 }
633
634 /*****************************************************************************
635  * reduce a fraction
636  *   (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
637  *****************************************************************************/
638 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
639                         uint64_t i_nom, uint64_t i_den, uint64_t i_max )
640 {
641     vlc_bool_t b_exact = 1;
642     uint64_t i_gcd;
643
644     if( i_den == 0 )
645     {
646         *pi_dst_nom = 0;
647         *pi_dst_den = 1;
648         return 1;
649     }
650
651     i_gcd = GCD( i_nom, i_den );
652     i_nom /= i_gcd;
653     i_den /= i_gcd;
654
655     if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
656
657     if( i_nom > i_max || i_den > i_max )
658     {
659         uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
660         b_exact = 0;
661
662         for( ; ; )
663         {
664             uint64_t i_x = i_nom / i_den;
665             uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
666             uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
667
668             if( i_a2n > i_max || i_a2d > i_max ) break;
669
670             i_nom %= i_den;
671
672             i_a0_num = i_a1_num; i_a0_den = i_a1_den;
673             i_a1_num = i_a2n; i_a1_den = i_a2d;
674             if( i_nom == 0 ) break;
675             i_x = i_nom; i_nom = i_den; i_den = i_x;
676         }
677         i_nom = i_a1_num;
678         i_den = i_a1_den;
679     }
680
681     *pi_dst_nom = i_nom;
682     *pi_dst_den = i_den;
683
684     return b_exact;
685 }
686
687 /*************************************************************************
688  * vlc_parse_cmdline: Command line parsing into elements.
689  *
690  * The command line is composed of space/tab separated arguments.
691  * Quotes can be used as argument delimiters and a backslash can be used to
692  * escape a quote.
693  *************************************************************************/
694 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
695 {
696     int i_bcount = 0;
697
698     while( **s )
699     {
700         if( **s == '\\' )
701         {
702             **ppsz_parser = **s;
703             (*ppsz_parser)++; (*s)++;
704             i_bcount++;
705         }
706         else if( **s == '"' || **s == '\'' )
707         {
708             /* Preceeded by a number of '\' which we erase. */
709             *ppsz_parser -= i_bcount / 2;
710             if( i_bcount & 1 )
711             {
712                 /* '\\' followed by a '"' or '\'' */
713                 *ppsz_parser -= 1;
714                 **ppsz_parser = **s;
715                 (*ppsz_parser)++; (*s)++;
716                 i_bcount = 0;
717                 continue;
718             }
719
720             if( **s == i_quote )
721             {
722                 /* End */
723                 return;
724             }
725             else
726             {
727                 /* Different quoting */
728                 int i_quote = **s;
729                 **ppsz_parser = **s;
730                 (*ppsz_parser)++; (*s)++;
731                 find_end_quote( s, ppsz_parser, i_quote );
732                 **ppsz_parser = **s;
733                 (*ppsz_parser)++; (*s)++;
734             }
735
736             i_bcount = 0;
737         }
738         else
739         {
740             /* A regular character */
741             **ppsz_parser = **s;
742             (*ppsz_parser)++; (*s)++;
743             i_bcount = 0;
744         }
745     }
746 }
747
748 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
749 {
750     int argc = 0;
751     char **argv = 0;
752     char *s, *psz_parser, *psz_arg, *psz_orig;
753     int i_bcount = 0;
754
755     if( !psz_cmdline ) return 0;
756     psz_orig = strdup( psz_cmdline );
757     psz_arg = psz_parser = s = psz_orig;
758
759     while( *s )
760     {
761         if( *s == '\t' || *s == ' ' )
762         {
763             /* We have a complete argument */
764             *psz_parser = 0;
765             TAB_APPEND( argc, argv, strdup(psz_arg) );
766
767             /* Skip trailing spaces/tabs */
768             do{ s++; } while( *s == '\t' || *s == ' ' );
769
770             /* New argument */
771             psz_arg = psz_parser = s;
772             i_bcount = 0;
773         }
774         else if( *s == '\\' )
775         {
776             *psz_parser++ = *s++;
777             i_bcount++;
778         }
779         else if( *s == '"' || *s == '\'' )
780         {
781             if( ( i_bcount & 1 ) == 0 )
782             {
783                 /* Preceeded by an even number of '\', this is half that
784                  * number of '\', plus a quote which we erase. */
785                 int i_quote = *s;
786                 psz_parser -= i_bcount / 2;
787                 s++;
788                 find_end_quote( &s, &psz_parser, i_quote );
789                 s++;
790             }
791             else
792             {
793                 /* Preceeded by an odd number of '\', this is half that
794                  * number of '\' followed by a '"' */
795                 psz_parser = psz_parser - i_bcount/2 - 1;
796                 *psz_parser++ = '"';
797                 s++;
798             }
799             i_bcount = 0;
800         }
801         else
802         {
803             /* A regular character */
804             *psz_parser++ = *s++;
805             i_bcount = 0;
806         }
807     }
808
809     /* Take care of the last arg */
810     if( *psz_arg )
811     {
812         *psz_parser = '\0';
813         TAB_APPEND( argc, argv, strdup(psz_arg) );
814     }
815
816     if( i_args ) *i_args = argc;
817     free( psz_orig );
818     return argv;
819 }
820
821 /*************************************************************************
822  * vlc_execve: Execute an external program with a given environment,
823  * wait until it finishes and return its standard output
824  *************************************************************************/
825 int __vlc_execve( vlc_object_t *p_object, int i_argc, char **ppsz_argv,
826                   char **ppsz_env, char *psz_cwd, char *p_in, int i_in,
827                   char **pp_data, int *pi_data )
828 {
829 #ifdef HAVE_FORK
830     int pi_stdin[2];
831     int pi_stdout[2];
832     pid_t i_child_pid;
833
834     pipe( pi_stdin );
835     pipe( pi_stdout );
836
837     if ( (i_child_pid = fork()) == -1 )
838     {
839         msg_Err( p_object, "unable to fork (%s)", strerror(errno) );
840         return -1;
841     }
842
843     if ( i_child_pid == 0 )
844     {
845         close(0);
846         dup(pi_stdin[1]);
847         close(pi_stdin[0]);
848
849         close(1);
850         dup(pi_stdout[1]);
851         close(pi_stdout[0]);
852
853         close(2);
854
855         if ( psz_cwd != NULL )
856             chdir( psz_cwd );
857         execve( ppsz_argv[0], ppsz_argv, ppsz_env );
858         exit(1);
859     }
860
861     close(pi_stdin[1]);
862     close(pi_stdout[1]);
863     if ( !i_in )
864         close( pi_stdin[0] );
865
866     *pi_data = 0;
867     *pp_data = malloc( 1025 );  /* +1 for \0 */
868
869     while ( !p_object->b_die )
870     {
871         int i_ret, i_status;
872         fd_set readfds, writefds;
873         struct timeval tv;
874
875         FD_ZERO( &readfds );
876         FD_ZERO( &writefds );
877         FD_SET( pi_stdout[0], &readfds );
878         if ( i_in )
879             FD_SET( pi_stdin[0], &writefds );
880
881         tv.tv_sec = 0;
882         tv.tv_usec = 10000;
883         
884         i_ret = select( pi_stdin[0] > pi_stdout[0] ? pi_stdin[0] + 1 :
885                         pi_stdout[0] + 1, &readfds, &writefds, NULL, &tv );
886         if ( i_ret > 0 )
887         {
888             if ( FD_ISSET( pi_stdout[0], &readfds ) )
889             {
890                 ssize_t i_read = read( pi_stdout[0], &(*pp_data)[*pi_data],
891                                        1024 );
892                 if ( i_read > 0 )
893                 {
894                     *pi_data += i_read;
895                     *pp_data = realloc( *pp_data, *pi_data + 1025 );
896                 }
897             }
898             if ( FD_ISSET( pi_stdin[0], &writefds ) )
899             {
900                 ssize_t i_write = write( pi_stdin[0], p_in, __MIN(i_in, 1024) );
901
902                 if ( i_write > 0 )
903                 {
904                     p_in += i_write;
905                     i_in -= i_write;
906                 }
907                 if ( !i_in )
908                     close( pi_stdin[0] );
909             }
910         }
911
912         if ( waitpid( i_child_pid, &i_status, WNOHANG ) == i_child_pid )
913         {
914             if ( WIFEXITED( i_status ) )
915             {
916                 if ( WEXITSTATUS( i_status ) )
917                 {
918                     msg_Warn( p_object,
919                               "child %s returned with error code %d",
920                               ppsz_argv[0], WEXITSTATUS( i_status ) );
921                 }
922             }
923             else
924             {
925                 if ( WIFSIGNALED( i_status ) )
926                 {
927                     msg_Warn( p_object,
928                               "child %s quit on signal %d", ppsz_argv[0],
929                               WTERMSIG( i_status ) );
930                 }
931             }
932             if ( i_in )
933                 close( pi_stdin[0] );
934             close( pi_stdout[0] );
935             break;
936         }
937
938         if ( i_ret < 0 && errno != EINTR )
939         {
940             msg_Warn( p_object, "select failed (%s)", strerror(errno) );
941         }
942     }
943
944 #else
945     msg_Err( p_intf, "vlc_execve called but no implementation is available" );
946     return -1;
947
948 #endif
949
950     (*pp_data)[*pi_data] = '\0';
951
952     return 0;
953 }