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 *****************************************************************************/
30 #include <string.h> /* strdup() */
40 #if defined(HAVE_ICONV)
49 # include <sys/time.h>
52 # include <sys/wait.h>
54 # include <sys/socket.h>
55 # include <sys/poll.h>
58 #if defined(WIN32) || defined(UNDER_CE)
65 # define WIN32_LEAN_AND_MEAN
70 # define strcoll strcmp
73 /*****************************************************************************
74 * getenv: just in case, but it should never be called
75 *****************************************************************************/
76 #if !defined( HAVE_GETENV )
77 char *vlc_getenv( const char *name )
83 /*****************************************************************************
84 * strdup: returns a malloc'd copy of a string
85 *****************************************************************************/
86 #if !defined( HAVE_STRDUP )
87 char *vlc_strdup( const char *string )
89 return strndup( string, strlen( string ) );
93 /*****************************************************************************
94 * strndup: returns a malloc'd copy of at most n bytes of string
95 * Does anyone know whether or not it will be present in Jaguar?
96 *****************************************************************************/
97 #if !defined( HAVE_STRNDUP )
98 char *vlc_strndup( const char *string, size_t n )
101 size_t len = strlen( string );
103 len = __MIN( len, n );
104 psz = (char*)malloc( len + 1 );
107 memcpy( (void*)psz, (const void*)string, len );
115 /*****************************************************************************
117 *****************************************************************************/
118 #if !defined( HAVE_STRNLEN )
119 size_t vlc_strnlen( const char *psz, size_t n )
121 const char *psz_end = memchr( psz, 0, n );
122 return psz_end ? ( psz_end - psz ) : n;
126 /*****************************************************************************
127 * strcasecmp: compare two strings ignoring case
128 *****************************************************************************/
129 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
130 int vlc_strcasecmp( const char *s1, const char *s2 )
133 if( !s1 || !s2 ) return -1;
140 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
144 if( !*s1 && !*s2 ) return 0;
145 else return (*s1 ? 1 : -1);
149 /*****************************************************************************
150 * strncasecmp: compare n chars from two strings ignoring case
151 *****************************************************************************/
152 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
153 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
156 if( !s1 || !s2 ) return -1;
158 while( n > 0 && *s1 && *s2 )
163 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
167 if( !n || (!*s1 && !*s2) ) return 0;
168 else return (*s1 ? 1 : -1);
172 /******************************************************************************
173 * strcasestr: find a substring (little) in another substring (big)
174 * Case sensitive. Return NULL if not found, return big if little == null
175 *****************************************************************************/
176 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
177 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
179 char *p_pos = (char *)psz_big;
181 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
185 if( toupper( *p_pos ) == toupper( *psz_little ) )
187 char * psz_cur1 = p_pos + 1;
188 char * psz_cur2 = (char *)psz_little + 1;
189 while( *psz_cur1 && *psz_cur2 &&
190 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
195 if( !*psz_cur2 ) return p_pos;
203 /*****************************************************************************
205 *****************************************************************************/
206 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
207 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
209 /* Guess we need no more than 100 bytes. */
211 char *p = malloc( i_size );
222 /* Try to print in the allocated space. */
223 n = vsnprintf( p, i_size, fmt, ap );
225 /* If that worked, return the string. */
226 if (n > -1 && n < i_size)
231 /* Else try again with more space. */
232 if (n > -1) /* glibc 2.1 */
234 i_size = n+1; /* precisely what is needed */
238 i_size *= 2; /* twice the old size */
240 if( (p = realloc( p, i_size ) ) == NULL)
249 /*****************************************************************************
251 *****************************************************************************/
252 #if !defined(HAVE_ASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
253 int vlc_asprintf( char **strp, const char *fmt, ... )
258 va_start( args, fmt );
259 i_ret = vasprintf( strp, fmt, args );
266 /*****************************************************************************
267 * atof: convert a string to a double.
268 *****************************************************************************/
269 #if !defined( HAVE_ATOF )
270 double vlc_atof( const char *nptr )
273 wchar_t *psz_tmp = NULL;
274 int i_len = strlen( nptr ) + 1;
276 psz_tmp = malloc( i_len * sizeof(wchar_t) );
279 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
280 f_result = wcstod( psz_tmp, NULL );
287 /*****************************************************************************
288 * strtoll: convert a string to a 64 bits int.
289 *****************************************************************************/
290 #if !defined( HAVE_STRTOLL )
291 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
294 int sign = 1, newbase = base ? base : 10;
296 while( isspace(*nptr) ) nptr++;
304 /* Try to detect base */
317 if( base && newbase != base )
319 if( endptr ) *endptr = (char *)nptr;
326 while( *nptr >= '0' && *nptr <= '9' )
329 i_value += ( *nptr++ - '0' );
331 if( endptr ) *endptr = (char *)nptr;
335 while( (*nptr >= '0' && *nptr <= '9') ||
336 (*nptr >= 'a' && *nptr <= 'f') ||
337 (*nptr >= 'A' && *nptr <= 'F') )
340 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
341 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
342 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
347 if( endptr ) *endptr = (char *)nptr;
351 i_value = strtol( nptr, endptr, newbase );
355 return i_value * sign;
359 /*****************************************************************************
360 * atoll: convert a string to a 64 bits int.
361 *****************************************************************************/
362 #if !defined( HAVE_ATOLL )
363 int64_t vlc_atoll( const char *nptr )
365 return strtoll( nptr, (char **)NULL, 10 );
369 /*****************************************************************************
370 * lldiv: returns quotient and remainder
371 *****************************************************************************/
372 #if defined(SYS_BEOS) \
373 || (defined (__FreeBSD__) && (__FreeBSD__ < 5))
374 lldiv_t vlc_lldiv( long long numer, long long denom )
377 d.quot = numer / denom;
378 d.rem = numer % denom;
385 * Copy a string to a sized buffer. The result is always nul-terminated
386 * (contrary to strncpy()).
388 * @param dest destination buffer
389 * @param src string to be copied
390 * @param len maximum number of characters to be copied plus one for the
393 * @return strlen(src)
396 extern size_t vlc_strlcpy (char *tgt, const char *src, size_t bufsize)
400 for (length = 1; (length < bufsize) && *src; length++)
413 /*****************************************************************************
414 * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
415 * when called with an empty argument or just '\'
416 *****************************************************************************/
417 #if defined(WIN32) && !defined(UNDER_CE)
420 typedef struct vlc_DIR
424 struct _wdirent dd_dir;
425 vlc_bool_t b_insert_back;
428 void *vlc_wopendir( const wchar_t *wpath )
430 vlc_DIR *p_dir = NULL;
431 _WDIR *p_real_dir = NULL;
433 if ( wpath == NULL || wpath[0] == '\0'
434 || (wcscmp (wpath, L"\\") == 0) )
436 /* Special mode to list drive letters */
437 p_dir = malloc( sizeof(vlc_DIR) );
440 p_dir->p_real_dir = NULL;
441 p_dir->i_drives = GetLogicalDrives();
442 return (void *)p_dir;
445 p_real_dir = _wopendir( wpath );
446 if ( p_real_dir == NULL )
449 p_dir = malloc( sizeof(vlc_DIR) );
452 _wclosedir( p_real_dir );
455 p_dir->p_real_dir = p_real_dir;
457 assert (wpath[0]); // wpath[1] is defined
458 p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\");
459 return (void *)p_dir;
462 struct _wdirent *vlc_wreaddir( void *_p_dir )
464 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
468 if ( p_dir->p_real_dir != NULL )
470 if ( p_dir->b_insert_back )
472 /* Adds "..", gruik! */
473 p_dir->dd_dir.d_ino = 0;
474 p_dir->dd_dir.d_reclen = 0;
475 p_dir->dd_dir.d_namlen = 2;
476 wcscpy( p_dir->dd_dir.d_name, L".." );
477 p_dir->b_insert_back = VLC_FALSE;
478 return &p_dir->dd_dir;
481 return _wreaddir( p_dir->p_real_dir );
484 /* Drive letters mode */
485 i_drives = p_dir->i_drives;
487 return NULL; /* end */
489 for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
490 if ( i_drives & 1 ) break;
493 return NULL; /* this should not happen */
495 swprintf( p_dir->dd_dir.d_name, L"%c:\\", 'A' + i );
496 p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name);
497 p_dir->i_drives &= ~(1UL << i);
498 return &p_dir->dd_dir;
501 int vlc_wclosedir( void *_p_dir )
503 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
506 if ( p_dir->p_real_dir != NULL )
507 i_ret = _wclosedir( p_dir->p_real_dir );
513 void vlc_rewinddir( void *_p_dir )
515 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
517 if ( p_dir->p_real_dir != NULL )
518 _wrewinddir( p_dir->p_real_dir );
521 void vlc_seekdir( void *_p_dir, long loc)
523 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
525 if ( p_dir->p_real_dir != NULL )
526 _wseekdir( p_dir->p_real_dir, loc );
529 long vlc_telldir( void *_p_dir )
531 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
533 if ( p_dir->p_real_dir != NULL )
534 return _wtelldir( p_dir->p_real_dir );
539 /*****************************************************************************
540 * scandir: scan a directory alpha-sorted
541 *****************************************************************************/
542 #if !defined( HAVE_SCANDIR )
543 /* FIXME: I suspect this is dead code -> utf8_scandir */
549 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
551 return strcoll( (*a)->d_name, (*b)->d_name );
554 int vlc_scandir( const char *name, struct dirent ***namelist,
555 int (*filter) ( const struct dirent * ),
556 int (*compar) ( const struct dirent **,
557 const struct dirent ** ) )
560 struct dirent * p_content;
561 struct dirent ** pp_list;
564 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
568 while( ( p_content = readdir( p_dir ) ) )
570 if( filter && !filter( p_content ) )
574 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
575 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
576 pp_list[ret] = malloc( size );
579 memcpy( pp_list[ret], p_content, size );
584 /* Continuing is useless when no more memory can be allocted,
585 * so better return what we have found.
596 qsort( pp_list, ret, sizeof( struct dirent * ),
597 (int (*)(const void *, const void *)) compar );
606 /*****************************************************************************
607 * dgettext: gettext for plugins.
608 *****************************************************************************/
609 char *vlc_dgettext( const char *package, const char *msgid )
611 #if defined( ENABLE_NLS ) \
612 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
613 return dgettext( package, msgid );
615 return (char *)msgid;
620 /*****************************************************************************
621 * count_utf8_string: returns the number of characters in the string.
622 *****************************************************************************/
623 static int count_utf8_string( const char *psz_string )
625 int i = 0, i_count = 0;
626 while( psz_string[ i ] != 0 )
628 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
634 /*****************************************************************************
635 * wraptext: inserts \n at convenient places to wrap the text.
636 * Returns the modified string in a new buffer.
637 *****************************************************************************/
638 char *vlc_wraptext( const char *psz_text, int i_line )
641 char *psz_line, *psz_new_text;
643 psz_line = psz_new_text = strdup( psz_text );
645 i_len = count_utf8_string( psz_text );
647 while( i_len > i_line )
649 /* Look if there is a newline somewhere. */
650 char *psz_parser = psz_line;
652 while( i_count <= i_line && *psz_parser != '\n' )
654 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
658 if( *psz_parser == '\n' )
660 i_len -= (i_count + 1);
661 psz_line = psz_parser + 1;
665 /* Find the furthest space. */
666 while( psz_parser > psz_line && *psz_parser != ' ' )
668 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
672 if( *psz_parser == ' ' )
675 i_len -= (i_count + 1);
676 psz_line = psz_parser + 1;
680 /* Wrapping has failed. Find the first space or newline */
681 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
683 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
687 if( i_count < i_len ) *psz_parser = '\n';
688 i_len -= (i_count + 1);
689 psz_line = psz_parser + 1;
695 /*****************************************************************************
697 *****************************************************************************/
698 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
700 #if defined(HAVE_ICONV)
701 return iconv_open( tocode, fromcode );
707 size_t vlc_iconv( vlc_iconv_t cd, const char **inbuf, size_t *inbytesleft,
708 char **outbuf, size_t *outbytesleft )
710 #if defined(HAVE_ICONV)
711 return iconv( cd, (ICONV_CONST char **)inbuf, inbytesleft,
712 outbuf, outbytesleft );
716 if (inbytesleft == NULL || outbytesleft == NULL)
721 i_bytes = __MIN(*inbytesleft, *outbytesleft);
722 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
723 memcpy( *outbuf, *inbuf, i_bytes );
726 inbytesleft -= i_bytes;
727 outbytesleft -= i_bytes;
732 int vlc_iconv_close( vlc_iconv_t cd )
734 #if defined(HAVE_ICONV)
735 return iconv_close( cd );
741 /*****************************************************************************
743 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
744 *****************************************************************************/
745 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
746 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
748 vlc_bool_t b_exact = 1;
758 i_gcd = GCD( i_nom, i_den );
762 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
764 if( i_nom > i_max || i_den > i_max )
766 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
771 uint64_t i_x = i_nom / i_den;
772 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
773 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
775 if( i_a2n > i_max || i_a2d > i_max ) break;
779 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
780 i_a1_num = i_a2n; i_a1_den = i_a2d;
781 if( i_nom == 0 ) break;
782 i_x = i_nom; i_nom = i_den; i_den = i_x;
794 /*************************************************************************
795 * vlc_parse_cmdline: Command line parsing into elements.
797 * The command line is composed of space/tab separated arguments.
798 * Quotes can be used as argument delimiters and a backslash can be used to
800 *************************************************************************/
801 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
810 (*ppsz_parser)++; (*s)++;
813 else if( **s == '"' || **s == '\'' )
815 /* Preceeded by a number of '\' which we erase. */
816 *ppsz_parser -= i_bcount / 2;
819 /* '\\' followed by a '"' or '\'' */
822 (*ppsz_parser)++; (*s)++;
834 /* Different quoting */
837 (*ppsz_parser)++; (*s)++;
838 find_end_quote( s, ppsz_parser, i_quote );
840 (*ppsz_parser)++; (*s)++;
847 /* A regular character */
849 (*ppsz_parser)++; (*s)++;
855 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
859 char *s, *psz_parser, *psz_arg, *psz_orig;
862 if( !psz_cmdline ) return 0;
863 psz_orig = strdup( psz_cmdline );
864 psz_arg = psz_parser = s = psz_orig;
868 if( *s == '\t' || *s == ' ' )
870 /* We have a complete argument */
872 TAB_APPEND( argc, argv, strdup(psz_arg) );
874 /* Skip trailing spaces/tabs */
875 do{ s++; } while( *s == '\t' || *s == ' ' );
878 psz_arg = psz_parser = s;
881 else if( *s == '\\' )
883 *psz_parser++ = *s++;
886 else if( *s == '"' || *s == '\'' )
888 if( ( i_bcount & 1 ) == 0 )
890 /* Preceeded by an even number of '\', this is half that
891 * number of '\', plus a quote which we erase. */
893 psz_parser -= i_bcount / 2;
895 find_end_quote( &s, &psz_parser, i_quote );
900 /* Preceeded by an odd number of '\', this is half that
901 * number of '\' followed by a '"' */
902 psz_parser = psz_parser - i_bcount/2 - 1;
910 /* A regular character */
911 *psz_parser++ = *s++;
916 /* Take care of the last arg */
920 TAB_APPEND( argc, argv, strdup(psz_arg) );
923 if( i_args ) *i_args = argc;
928 /*************************************************************************
929 * vlc_execve: Execute an external program with a given environment,
930 * wait until it finishes and return its standard output
931 *************************************************************************/
932 int __vlc_execve( vlc_object_t *p_object, int i_argc, char *const *ppsz_argv,
933 char *const *ppsz_env, const char *psz_cwd,
934 const char *p_in, size_t i_in,
935 char **pp_data, size_t *pi_data )
937 (void)i_argc; // <-- hmph
939 # define BUFSIZE 1024
940 int fds[2], i_status;
942 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds))
946 if ((fds[0] > 2) && (fds[1] > 2))
952 msg_Err (p_object, "unable to fork (%s)", strerror (errno));
959 * Like it or not, close can fail (and not only with EBADF)
961 if ((close (0) == 0) && (close (1) == 0) && (close (2) == 0)
962 && (dup (fds[1]) == 0) && (dup (fds[1]) == 1)
963 && (open ("/dev/null", O_RDONLY) == 2)
964 && ((psz_cwd == NULL) || (chdir (psz_cwd) == 0)))
965 execve (ppsz_argv[0], ppsz_argv, ppsz_env);
978 shutdown (fds[0], SHUT_WR);
980 while (!p_object->b_die)
982 struct pollfd ufd[1];
983 memset (ufd, 0, sizeof (ufd));
985 ufd[0].events = POLLIN;
988 ufd[0].events |= POLLOUT;
990 if (poll (ufd, 1, 10) <= 0)
993 if (ufd[0].revents & ~POLLOUT)
995 char *ptr = realloc (*pp_data, *pi_data + BUFSIZE + 1);
997 break; /* safely abort */
1001 ssize_t val = read (fds[0], ptr + *pi_data, BUFSIZE);
1006 shutdown (fds[0], SHUT_RD);
1014 if (ufd[0].revents & POLLOUT)
1016 ssize_t val = write (fds[0], p_in, i_in);
1022 shutdown (fds[0], SHUT_WR);
1034 while (waitpid (pid, &i_status, 0) == -1);
1036 if (WIFEXITED (i_status))
1038 i_status = WEXITSTATUS (i_status);
1040 msg_Warn (p_object, "child %s (PID %d) exited with error code %d",
1041 ppsz_argv[0], (int)pid, i_status);
1044 if (WIFSIGNALED (i_status)) // <-- this should be redumdant a check
1046 i_status = WTERMSIG (i_status);
1047 msg_Warn (p_object, "child %s (PID %d) exited on signal %d (%s)",
1048 ppsz_argv[0], (int)pid, i_status, strsignal (i_status));
1051 #elif defined( WIN32 ) && !defined( UNDER_CE )
1052 SECURITY_ATTRIBUTES saAttr;
1053 PROCESS_INFORMATION piProcInfo;
1054 STARTUPINFO siStartInfo;
1055 BOOL bFuncRetn = FALSE;
1056 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr;
1058 char *psz_cmd = NULL, *p_env = NULL, *p = NULL;
1062 /* Set the bInheritHandle flag so pipe handles are inherited. */
1063 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1064 saAttr.bInheritHandle = TRUE;
1065 saAttr.lpSecurityDescriptor = NULL;
1067 /* Create a pipe for the child process's STDOUT. */
1068 if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
1070 msg_Err( p_object, "stdout pipe creation failed" );
1074 /* Ensure the read handle to the pipe for STDOUT is not inherited. */
1075 SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 );
1077 /* Create a pipe for the child process's STDIN. */
1078 if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) )
1080 msg_Err( p_object, "stdin pipe creation failed" );
1084 /* Ensure the write handle to the pipe for STDIN is not inherited. */
1085 SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 );
1087 /* Set up members of the PROCESS_INFORMATION structure. */
1088 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
1090 /* Set up members of the STARTUPINFO structure. */
1091 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
1092 siStartInfo.cb = sizeof(STARTUPINFO);
1093 siStartInfo.hStdError = hChildStdoutWr;
1094 siStartInfo.hStdOutput = hChildStdoutWr;
1095 siStartInfo.hStdInput = hChildStdinRd;
1096 siStartInfo.wShowWindow = SW_HIDE;
1097 siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1099 /* Set up the command line. */
1100 psz_cmd = malloc(32768);
1105 ppsz_parser = &ppsz_argv[0];
1106 while ( ppsz_parser[0] != NULL && i_size > 0 )
1108 /* Protect the last argument with quotes ; the other arguments
1109 * are supposed to be already protected because they have been
1110 * passed as a command-line option. */
1111 if ( ppsz_parser[1] == NULL )
1113 strncat( psz_cmd, "\"", i_size );
1116 strncat( psz_cmd, *ppsz_parser, i_size );
1117 i_size -= strlen( *ppsz_parser );
1118 if ( ppsz_parser[1] == NULL )
1120 strncat( psz_cmd, "\"", i_size );
1123 strncat( psz_cmd, " ", i_size );
1128 /* Set up the environment. */
1129 p = p_env = malloc(32768);
1137 ppsz_parser = &ppsz_env[0];
1138 while ( *ppsz_parser != NULL && i_size > 0 )
1140 memcpy( p, *ppsz_parser,
1141 __MIN((int)(strlen(*ppsz_parser) + 1), i_size) );
1142 p += strlen(*ppsz_parser) + 1;
1143 i_size -= strlen(*ppsz_parser) + 1;
1148 /* Create the child process. */
1149 bFuncRetn = CreateProcess( NULL,
1150 psz_cmd, // command line
1151 NULL, // process security attributes
1152 NULL, // primary thread security attributes
1153 TRUE, // handles are inherited
1154 0, // creation flags
1157 &siStartInfo, // STARTUPINFO pointer
1158 &piProcInfo ); // receives PROCESS_INFORMATION
1163 if ( bFuncRetn == 0 )
1165 msg_Err( p_object, "child creation failed" );
1169 /* Read from a file and write its contents to a pipe. */
1170 while ( i_in > 0 && !p_object->b_die )
1173 if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) )
1179 /* Close the pipe handle so the child process stops reading. */
1180 CloseHandle(hChildStdinWr);
1182 /* Close the write end of the pipe before reading from the
1183 * read end of the pipe. */
1184 CloseHandle(hChildStdoutWr);
1186 /* Read output from the child process. */
1191 *pp_data = malloc( 1025 ); /* +1 for \0 */
1193 while ( !p_object->b_die )
1196 if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read,
1201 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1204 while ( !p_object->b_die
1205 && !GetExitCodeProcess( piProcInfo.hProcess, &i_status )
1206 && i_status != STILL_ACTIVE )
1209 CloseHandle(piProcInfo.hProcess);
1210 CloseHandle(piProcInfo.hThread);
1214 "child %s returned with error code %ld",
1215 ppsz_argv[0], i_status );
1218 msg_Err( p_object, "vlc_execve called but no implementation is available" );
1223 if (*pp_data == NULL)
1226 (*pp_data)[*pi_data] = '\0';