1 /*****************************************************************************
2 * libc.c: Extra libc function for some systems.
3 *****************************************************************************
4 * Copyright (C) 2002-2006 the VideoLAN team
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 * Rémi Denis-Courmont <rem à videolan.org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
28 #include <string.h> /* strdup() */
39 #if defined(HAVE_ICONV)
48 # include <sys/time.h>
51 # include <sys/wait.h>
54 #if defined(WIN32) || defined(UNDER_CE)
61 # define WIN32_LEAN_AND_MEAN
66 # define strcoll strcmp
69 /*****************************************************************************
70 * getenv: just in case, but it should never be called
71 *****************************************************************************/
72 #if !defined( HAVE_GETENV )
73 char *vlc_getenv( const char *name )
79 /*****************************************************************************
80 * strdup: returns a malloc'd copy of a string
81 *****************************************************************************/
82 #if !defined( HAVE_STRDUP )
83 char *vlc_strdup( const char *string )
85 return strndup( string, strlen( string ) );
89 /*****************************************************************************
90 * strndup: returns a malloc'd copy of at most n bytes of string
91 * Does anyone know whether or not it will be present in Jaguar?
92 *****************************************************************************/
93 #if !defined( HAVE_STRNDUP )
94 char *vlc_strndup( const char *string, size_t n )
97 size_t len = strlen( string );
99 len = __MIN( len, n );
100 psz = (char*)malloc( len + 1 );
103 memcpy( (void*)psz, (const void*)string, len );
111 /*****************************************************************************
112 * strcasecmp: compare two strings ignoring case
113 *****************************************************************************/
114 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
115 int vlc_strcasecmp( const char *s1, const char *s2 )
118 if( !s1 || !s2 ) return -1;
125 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
129 if( !*s1 && !*s2 ) return 0;
130 else return (*s1 ? 1 : -1);
134 /*****************************************************************************
135 * strncasecmp: compare n chars from two strings ignoring case
136 *****************************************************************************/
137 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
138 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
141 if( !s1 || !s2 ) return -1;
143 while( n > 0 && *s1 && *s2 )
148 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
152 if( !n || (!*s1 && !*s2) ) return 0;
153 else return (*s1 ? 1 : -1);
157 /******************************************************************************
158 * strcasestr: find a substring (little) in another substring (big)
159 * Case sensitive. Return NULL if not found, return big if little == null
160 *****************************************************************************/
161 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
162 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
164 char *p_pos = (char *)psz_big;
166 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
170 if( toupper( *p_pos ) == toupper( *psz_little ) )
172 char * psz_cur1 = p_pos + 1;
173 char * psz_cur2 = (char *)psz_little + 1;
174 while( *psz_cur1 && *psz_cur2 &&
175 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
180 if( !*psz_cur2 ) return p_pos;
188 /*****************************************************************************
190 *****************************************************************************/
191 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
192 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
194 /* Guess we need no more than 100 bytes. */
196 char *p = malloc( i_size );
207 /* Try to print in the allocated space. */
208 n = vsnprintf( p, i_size, fmt, ap );
210 /* If that worked, return the string. */
211 if (n > -1 && n < i_size)
216 /* Else try again with more space. */
217 if (n > -1) /* glibc 2.1 */
219 i_size = n+1; /* precisely what is needed */
223 i_size *= 2; /* twice the old size */
225 if( (p = realloc( p, i_size ) ) == NULL)
234 /*****************************************************************************
236 *****************************************************************************/
237 #if !defined(HAVE_ASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
238 int vlc_asprintf( char **strp, const char *fmt, ... )
243 va_start( args, fmt );
244 i_ret = vasprintf( strp, fmt, args );
251 /*****************************************************************************
252 * atof: convert a string to a double.
253 *****************************************************************************/
254 #if !defined( HAVE_ATOF )
255 double vlc_atof( const char *nptr )
258 wchar_t *psz_tmp = NULL;
259 int i_len = strlen( nptr ) + 1;
261 psz_tmp = malloc( i_len * sizeof(wchar_t) );
264 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
265 f_result = wcstod( psz_tmp, NULL );
272 /*****************************************************************************
273 * strtoll: convert a string to a 64 bits int.
274 *****************************************************************************/
275 #if !defined( HAVE_STRTOLL )
276 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
279 int sign = 1, newbase = base ? base : 10;
281 while( isspace(*nptr) ) nptr++;
289 /* Try to detect base */
302 if( base && newbase != base )
304 if( endptr ) *endptr = (char *)nptr;
311 while( *nptr >= '0' && *nptr <= '9' )
314 i_value += ( *nptr++ - '0' );
316 if( endptr ) *endptr = (char *)nptr;
320 while( (*nptr >= '0' && *nptr <= '9') ||
321 (*nptr >= 'a' && *nptr <= 'f') ||
322 (*nptr >= 'A' && *nptr <= 'F') )
325 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
326 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
327 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
332 if( endptr ) *endptr = (char *)nptr;
336 i_value = strtol( nptr, endptr, newbase );
340 return i_value * sign;
344 /*****************************************************************************
345 * atoll: convert a string to a 64 bits int.
346 *****************************************************************************/
347 #if !defined( HAVE_ATOLL )
348 int64_t vlc_atoll( const char *nptr )
350 return strtoll( nptr, (char **)NULL, 10 );
354 /*****************************************************************************
355 * lldiv: returns quotient and remainder
356 *****************************************************************************/
357 #if defined(SYS_BEOS) \
358 || (defined (__FreeBSD__) && (__FreeBSD__ < 5))
359 lldiv_t vlc_lldiv( long long numer, long long denom )
362 d.quot = numer / denom;
363 d.rem = numer % denom;
370 * Copy a string to a sized buffer. The result is always nul-terminated
371 * (contrary to strncpy()).
373 * @param dest destination buffer
374 * @param src string to be copied
375 * @param len maximum number of characters to be copied plus one for the
378 * @return strlen(src)
381 extern size_t vlc_strlcpy (char *tgt, const char *src, size_t bufsize)
385 for (length = 1; (length < bufsize) && *src; length++)
398 /*****************************************************************************
399 * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
400 * when called with an empty argument or just '\'
401 *****************************************************************************/
402 #if defined(WIN32) && !defined(UNDER_CE)
405 typedef struct vlc_DIR
409 struct _wdirent dd_dir;
410 vlc_bool_t b_insert_back;
413 void *vlc_wopendir( const wchar_t *wpath )
415 vlc_DIR *p_dir = NULL;
416 _WDIR *p_real_dir = NULL;
418 if ( wpath == NULL || wpath[0] == '\0'
419 || (wcscmp (wpath, L"\\") == 0) )
421 /* Special mode to list drive letters */
422 p_dir = malloc( sizeof(vlc_DIR) );
425 p_dir->p_real_dir = NULL;
426 p_dir->i_drives = GetLogicalDrives();
427 return (void *)p_dir;
430 p_real_dir = _wopendir( wpath );
431 if ( p_real_dir == NULL )
434 p_dir = malloc( sizeof(vlc_DIR) );
437 _wclosedir( p_real_dir );
440 p_dir->p_real_dir = p_real_dir;
442 assert (wpath[0]); // wpath[1] is defined
443 p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\");
444 return (void *)p_dir;
447 struct _wdirent *vlc_wreaddir( void *_p_dir )
449 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
453 if ( p_dir->p_real_dir != NULL )
455 if ( p_dir->b_insert_back )
457 /* Adds "..", gruik! */
458 p_dir->dd_dir.d_ino = 0;
459 p_dir->dd_dir.d_reclen = 0;
460 p_dir->dd_dir.d_namlen = 2;
461 wcscpy( p_dir->dd_dir.d_name, L".." );
462 p_dir->b_insert_back = VLC_FALSE;
463 return &p_dir->dd_dir;
466 return _wreaddir( p_dir->p_real_dir );
469 /* Drive letters mode */
470 i_drives = p_dir->i_drives;
472 return NULL; /* end */
474 for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
475 if ( i_drives & 1 ) break;
478 return NULL; /* this should not happen */
480 swprintf( p_dir->dd_dir.d_name, L"%c:\\", 'A' + i );
481 p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name);
482 p_dir->i_drives &= ~(1UL << i);
483 return &p_dir->dd_dir;
486 int vlc_wclosedir( void *_p_dir )
488 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
491 if ( p_dir->p_real_dir != NULL )
492 i_ret = _wclosedir( p_dir->p_real_dir );
498 void vlc_rewinddir( void *_p_dir )
500 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
502 if ( p_dir->p_real_dir != NULL )
503 _wrewinddir( p_dir->p_real_dir );
506 void vlc_seekdir( void *_p_dir, long loc)
508 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
510 if ( p_dir->p_real_dir != NULL )
511 _wseekdir( p_dir->p_real_dir, loc );
514 long vlc_telldir( void *_p_dir )
516 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
518 if ( p_dir->p_real_dir != NULL )
519 return _wtelldir( p_dir->p_real_dir );
524 /*****************************************************************************
525 * scandir: scan a directory alpha-sorted
526 *****************************************************************************/
527 #if !defined( HAVE_SCANDIR )
528 /* FIXME: I suspect this is dead code -> utf8_scandir */
534 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
536 return strcoll( (*a)->d_name, (*b)->d_name );
539 int vlc_scandir( const char *name, struct dirent ***namelist,
540 int (*filter) ( const struct dirent * ),
541 int (*compar) ( const struct dirent **,
542 const struct dirent ** ) )
545 struct dirent * p_content;
546 struct dirent ** pp_list;
549 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
553 while( ( p_content = readdir( p_dir ) ) )
555 if( filter && !filter( p_content ) )
559 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
560 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
561 pp_list[ret] = malloc( size );
564 memcpy( pp_list[ret], p_content, size );
569 /* Continuing is useless when no more memory can be allocted,
570 * so better return what we have found.
581 qsort( pp_list, ret, sizeof( struct dirent * ),
582 (int (*)(const void *, const void *)) compar );
590 #if defined (WIN32) || !defined (HAVE_SHARED_LIBVLC)
591 /*****************************************************************************
592 * dgettext: gettext for plugins.
593 *****************************************************************************/
594 char *vlc_dgettext( const char *package, const char *msgid )
596 #if defined( ENABLE_NLS ) \
597 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
598 return dgettext( package, msgid );
600 return (char *)msgid;
605 /*****************************************************************************
606 * count_utf8_string: returns the number of characters in the string.
607 *****************************************************************************/
608 static int count_utf8_string( const char *psz_string )
610 int i = 0, i_count = 0;
611 while( psz_string[ i ] != 0 )
613 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
619 /*****************************************************************************
620 * wraptext: inserts \n at convenient places to wrap the text.
621 * Returns the modified string in a new buffer.
622 *****************************************************************************/
623 char *vlc_wraptext( const char *psz_text, int i_line )
626 char *psz_line, *psz_new_text;
628 psz_line = psz_new_text = strdup( psz_text );
630 i_len = count_utf8_string( psz_text );
632 while( i_len > i_line )
634 /* Look if there is a newline somewhere. */
635 char *psz_parser = psz_line;
637 while( i_count <= i_line && *psz_parser != '\n' )
639 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
643 if( *psz_parser == '\n' )
645 i_len -= (i_count + 1);
646 psz_line = psz_parser + 1;
650 /* Find the furthest space. */
651 while( psz_parser > psz_line && *psz_parser != ' ' )
653 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
657 if( *psz_parser == ' ' )
660 i_len -= (i_count + 1);
661 psz_line = psz_parser + 1;
665 /* Wrapping has failed. Find the first space or newline */
666 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
668 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
672 if( i_count < i_len ) *psz_parser = '\n';
673 i_len -= (i_count + 1);
674 psz_line = psz_parser + 1;
680 /*****************************************************************************
682 *****************************************************************************/
683 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
685 #if defined(HAVE_ICONV)
686 return iconv_open( tocode, fromcode );
692 size_t vlc_iconv( vlc_iconv_t cd, const char **inbuf, size_t *inbytesleft,
693 char **outbuf, size_t *outbytesleft )
695 #if defined(HAVE_ICONV)
696 return iconv( cd, (ICONV_CONST char **)inbuf, inbytesleft,
697 outbuf, outbytesleft );
701 if (inbytesleft == NULL || outbytesleft == NULL)
706 i_bytes = __MIN(*inbytesleft, *outbytesleft);
707 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
708 memcpy( *outbuf, *inbuf, i_bytes );
711 inbytesleft -= i_bytes;
712 outbytesleft -= i_bytes;
717 int vlc_iconv_close( vlc_iconv_t cd )
719 #if defined(HAVE_ICONV)
720 return iconv_close( cd );
726 /*****************************************************************************
728 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
729 *****************************************************************************/
730 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
731 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
733 vlc_bool_t b_exact = 1;
743 i_gcd = GCD( i_nom, i_den );
747 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
749 if( i_nom > i_max || i_den > i_max )
751 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
756 uint64_t i_x = i_nom / i_den;
757 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
758 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
760 if( i_a2n > i_max || i_a2d > i_max ) break;
764 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
765 i_a1_num = i_a2n; i_a1_den = i_a2d;
766 if( i_nom == 0 ) break;
767 i_x = i_nom; i_nom = i_den; i_den = i_x;
779 /*************************************************************************
780 * vlc_parse_cmdline: Command line parsing into elements.
782 * The command line is composed of space/tab separated arguments.
783 * Quotes can be used as argument delimiters and a backslash can be used to
785 *************************************************************************/
786 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
795 (*ppsz_parser)++; (*s)++;
798 else if( **s == '"' || **s == '\'' )
800 /* Preceeded by a number of '\' which we erase. */
801 *ppsz_parser -= i_bcount / 2;
804 /* '\\' followed by a '"' or '\'' */
807 (*ppsz_parser)++; (*s)++;
819 /* Different quoting */
822 (*ppsz_parser)++; (*s)++;
823 find_end_quote( s, ppsz_parser, i_quote );
825 (*ppsz_parser)++; (*s)++;
832 /* A regular character */
834 (*ppsz_parser)++; (*s)++;
840 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
844 char *s, *psz_parser, *psz_arg, *psz_orig;
847 if( !psz_cmdline ) return 0;
848 psz_orig = strdup( psz_cmdline );
849 psz_arg = psz_parser = s = psz_orig;
853 if( *s == '\t' || *s == ' ' )
855 /* We have a complete argument */
857 TAB_APPEND( argc, argv, strdup(psz_arg) );
859 /* Skip trailing spaces/tabs */
860 do{ s++; } while( *s == '\t' || *s == ' ' );
863 psz_arg = psz_parser = s;
866 else if( *s == '\\' )
868 *psz_parser++ = *s++;
871 else if( *s == '"' || *s == '\'' )
873 if( ( i_bcount & 1 ) == 0 )
875 /* Preceeded by an even number of '\', this is half that
876 * number of '\', plus a quote which we erase. */
878 psz_parser -= i_bcount / 2;
880 find_end_quote( &s, &psz_parser, i_quote );
885 /* Preceeded by an odd number of '\', this is half that
886 * number of '\' followed by a '"' */
887 psz_parser = psz_parser - i_bcount/2 - 1;
895 /* A regular character */
896 *psz_parser++ = *s++;
901 /* Take care of the last arg */
905 TAB_APPEND( argc, argv, strdup(psz_arg) );
908 if( i_args ) *i_args = argc;
913 /*************************************************************************
914 * vlc_execve: Execute an external program with a given environment,
915 * wait until it finishes and return its standard output
916 *************************************************************************/
917 int __vlc_execve( vlc_object_t *p_object, int i_argc, char **ppsz_argv,
918 char **ppsz_env, char *psz_cwd, char *p_in, int i_in,
919 char **pp_data, int *pi_data )
929 if ( (i_child_pid = fork()) == -1 )
931 msg_Err( p_object, "unable to fork (%s)", strerror(errno) );
935 if ( i_child_pid == 0 )
947 if ( psz_cwd != NULL )
949 execve( ppsz_argv[0], ppsz_argv, ppsz_env );
956 close( pi_stdin[0] );
962 *pp_data = malloc( 1025 ); /* +1 for \0 */
966 while ( !p_object->b_die )
969 fd_set readfds, writefds;
973 FD_ZERO( &writefds );
974 FD_SET( pi_stdout[0], &readfds );
976 FD_SET( pi_stdin[0], &writefds );
981 i_ret = select( pi_stdin[0] > pi_stdout[0] ? pi_stdin[0] + 1 :
982 pi_stdout[0] + 1, &readfds, &writefds, NULL, &tv );
985 if ( FD_ISSET( pi_stdout[0], &readfds ) )
987 ssize_t i_read = read( pi_stdout[0], &(*pp_data)[*pi_data],
992 *pp_data = realloc( *pp_data, *pi_data + 1025 );
995 if ( FD_ISSET( pi_stdin[0], &writefds ) )
997 ssize_t i_write = write( pi_stdin[0], p_in, __MIN(i_in, 1024) );
1005 close( pi_stdin[0] );
1009 if ( waitpid( i_child_pid, &i_status, WNOHANG ) == i_child_pid )
1011 if ( WIFEXITED( i_status ) )
1013 if ( WEXITSTATUS( i_status ) )
1016 "child %s returned with error code %d",
1017 ppsz_argv[0], WEXITSTATUS( i_status ) );
1022 if ( WIFSIGNALED( i_status ) )
1025 "child %s quit on signal %d", ppsz_argv[0],
1026 WTERMSIG( i_status ) );
1030 close( pi_stdin[0] );
1031 close( pi_stdout[0] );
1035 if ( i_ret < 0 && errno != EINTR )
1037 msg_Warn( p_object, "select failed (%s)", strerror(errno) );
1041 #elif defined( WIN32 ) && !defined( UNDER_CE )
1042 SECURITY_ATTRIBUTES saAttr;
1043 PROCESS_INFORMATION piProcInfo;
1044 STARTUPINFO siStartInfo;
1045 BOOL bFuncRetn = FALSE;
1046 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr;
1048 char *psz_cmd = NULL, *p_env = NULL, *p = NULL;
1052 /* Set the bInheritHandle flag so pipe handles are inherited. */
1053 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1054 saAttr.bInheritHandle = TRUE;
1055 saAttr.lpSecurityDescriptor = NULL;
1057 /* Create a pipe for the child process's STDOUT. */
1058 if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
1060 msg_Err( p_object, "stdout pipe creation failed" );
1064 /* Ensure the read handle to the pipe for STDOUT is not inherited. */
1065 SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 );
1067 /* Create a pipe for the child process's STDIN. */
1068 if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) )
1070 msg_Err( p_object, "stdin pipe creation failed" );
1074 /* Ensure the write handle to the pipe for STDIN is not inherited. */
1075 SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 );
1077 /* Set up members of the PROCESS_INFORMATION structure. */
1078 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
1080 /* Set up members of the STARTUPINFO structure. */
1081 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
1082 siStartInfo.cb = sizeof(STARTUPINFO);
1083 siStartInfo.hStdError = hChildStdoutWr;
1084 siStartInfo.hStdOutput = hChildStdoutWr;
1085 siStartInfo.hStdInput = hChildStdinRd;
1086 siStartInfo.wShowWindow = SW_HIDE;
1087 siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1089 /* Set up the command line. */
1090 psz_cmd = malloc(32768);
1095 ppsz_parser = &ppsz_argv[0];
1096 while ( ppsz_parser[0] != NULL && i_size > 0 )
1098 /* Protect the last argument with quotes ; the other arguments
1099 * are supposed to be already protected because they have been
1100 * passed as a command-line option. */
1101 if ( ppsz_parser[1] == NULL )
1103 strncat( psz_cmd, "\"", i_size );
1106 strncat( psz_cmd, *ppsz_parser, i_size );
1107 i_size -= strlen( *ppsz_parser );
1108 if ( ppsz_parser[1] == NULL )
1110 strncat( psz_cmd, "\"", i_size );
1113 strncat( psz_cmd, " ", i_size );
1118 /* Set up the environment. */
1119 p = p_env = malloc(32768);
1127 ppsz_parser = &ppsz_env[0];
1128 while ( *ppsz_parser != NULL && i_size > 0 )
1130 memcpy( p, *ppsz_parser,
1131 __MIN((int)(strlen(*ppsz_parser) + 1), i_size) );
1132 p += strlen(*ppsz_parser) + 1;
1133 i_size -= strlen(*ppsz_parser) + 1;
1138 /* Create the child process. */
1139 bFuncRetn = CreateProcess( NULL,
1140 psz_cmd, // command line
1141 NULL, // process security attributes
1142 NULL, // primary thread security attributes
1143 TRUE, // handles are inherited
1144 0, // creation flags
1147 &siStartInfo, // STARTUPINFO pointer
1148 &piProcInfo ); // receives PROCESS_INFORMATION
1153 if ( bFuncRetn == 0 )
1155 msg_Err( p_object, "child creation failed" );
1159 /* Read from a file and write its contents to a pipe. */
1160 while ( i_in > 0 && !p_object->b_die )
1163 if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) )
1169 /* Close the pipe handle so the child process stops reading. */
1170 CloseHandle(hChildStdinWr);
1172 /* Close the write end of the pipe before reading from the
1173 * read end of the pipe. */
1174 CloseHandle(hChildStdoutWr);
1176 /* Read output from the child process. */
1181 *pp_data = malloc( 1025 ); /* +1 for \0 */
1183 while ( !p_object->b_die )
1186 if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read,
1191 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1194 while ( !p_object->b_die
1195 && !GetExitCodeProcess( piProcInfo.hProcess, &i_status )
1196 && i_status != STILL_ACTIVE )
1199 CloseHandle(piProcInfo.hProcess);
1200 CloseHandle(piProcInfo.hThread);
1204 "child %s returned with error code %ld",
1205 ppsz_argv[0], i_status );
1208 msg_Err( p_object, "vlc_execve called but no implementation is available" );
1213 (*pp_data)[*pi_data] = '\0';