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 *****************************************************************************/
38 #if defined(HAVE_ICONV)
47 # include <sys/time.h>
50 # include <sys/wait.h>
52 # include <sys/socket.h>
53 # include <sys/poll.h>
56 #if defined(WIN32) || defined(UNDER_CE)
63 # define WIN32_LEAN_AND_MEAN
68 # define strcoll strcmp
71 /*****************************************************************************
72 * getenv: just in case, but it should never be called
73 *****************************************************************************/
74 #if !defined( HAVE_GETENV )
75 char *vlc_getenv( const char *name )
81 /*****************************************************************************
82 * strdup: returns a malloc'd copy of a string
83 *****************************************************************************/
84 #if !defined( HAVE_STRDUP )
85 char *vlc_strdup( const char *string )
87 return strndup( string, strlen( string ) );
91 /*****************************************************************************
92 * strndup: returns a malloc'd copy of at most n bytes of string
93 * Does anyone know whether or not it will be present in Jaguar?
94 *****************************************************************************/
95 #if !defined( HAVE_STRNDUP )
96 char *vlc_strndup( const char *string, size_t n )
99 size_t len = strlen( string );
101 len = __MIN( len, n );
102 psz = (char*)malloc( len + 1 );
105 memcpy( (void*)psz, (const void*)string, len );
113 /*****************************************************************************
115 *****************************************************************************/
116 #if !defined( HAVE_STRNLEN )
117 size_t vlc_strnlen( const char *psz, size_t n )
119 const char *psz_end = memchr( psz, 0, n );
120 return psz_end ? (size_t)( psz_end - psz ) : n;
124 /*****************************************************************************
125 * strcasecmp: compare two strings ignoring case
126 *****************************************************************************/
127 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
128 int vlc_strcasecmp( const char *s1, const char *s2 )
131 if( !s1 || !s2 ) return -1;
138 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
142 if( !*s1 && !*s2 ) return 0;
143 else return (*s1 ? 1 : -1);
147 /*****************************************************************************
148 * strncasecmp: compare n chars from two strings ignoring case
149 *****************************************************************************/
150 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
151 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
154 if( !s1 || !s2 ) return -1;
156 while( n > 0 && *s1 && *s2 )
161 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
165 if( !n || (!*s1 && !*s2) ) return 0;
166 else return (*s1 ? 1 : -1);
170 /******************************************************************************
171 * strcasestr: find a substring (little) in another substring (big)
172 * Case sensitive. Return NULL if not found, return big if little == null
173 *****************************************************************************/
174 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
175 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
177 char *p_pos = (char *)psz_big;
179 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
183 if( toupper( *p_pos ) == toupper( *psz_little ) )
185 char * psz_cur1 = p_pos + 1;
186 char * psz_cur2 = (char *)psz_little + 1;
187 while( *psz_cur1 && *psz_cur2 &&
188 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
193 if( !*psz_cur2 ) return p_pos;
201 /*****************************************************************************
203 *****************************************************************************/
204 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
205 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
207 /* Guess we need no more than 100 bytes. */
209 char *p = malloc( i_size );
220 /* Try to print in the allocated space. */
221 n = vsnprintf( p, i_size, fmt, ap );
223 /* If that worked, return the string. */
224 if (n > -1 && n < i_size)
229 /* Else try again with more space. */
230 if (n > -1) /* glibc 2.1 */
232 i_size = n+1; /* precisely what is needed */
236 i_size *= 2; /* twice the old size */
238 if( (p = realloc( p, i_size ) ) == NULL)
247 /*****************************************************************************
249 *****************************************************************************/
250 #if !defined(HAVE_ASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
251 int vlc_asprintf( char **strp, const char *fmt, ... )
256 va_start( args, fmt );
257 i_ret = vasprintf( strp, fmt, args );
264 /*****************************************************************************
265 * atof: convert a string to a double.
266 *****************************************************************************/
267 #if !defined( HAVE_ATOF )
268 double vlc_atof( const char *nptr )
271 wchar_t *psz_tmp = NULL;
272 int i_len = strlen( nptr ) + 1;
274 psz_tmp = malloc( i_len * sizeof(wchar_t) );
277 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
278 f_result = wcstod( psz_tmp, NULL );
285 /*****************************************************************************
286 * strtoll: convert a string to a 64 bits int.
287 *****************************************************************************/
288 #if !defined( HAVE_STRTOLL )
289 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
292 int sign = 1, newbase = base ? base : 10;
294 while( isspace(*nptr) ) nptr++;
302 /* Try to detect base */
315 if( base && newbase != base )
317 if( endptr ) *endptr = (char *)nptr;
324 while( *nptr >= '0' && *nptr <= '9' )
327 i_value += ( *nptr++ - '0' );
329 if( endptr ) *endptr = (char *)nptr;
333 while( (*nptr >= '0' && *nptr <= '9') ||
334 (*nptr >= 'a' && *nptr <= 'f') ||
335 (*nptr >= 'A' && *nptr <= 'F') )
338 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
339 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
340 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
345 if( endptr ) *endptr = (char *)nptr;
349 i_value = strtol( nptr, endptr, newbase );
353 return i_value * sign;
357 /*****************************************************************************
358 * atoll: convert a string to a 64 bits int.
359 *****************************************************************************/
360 #if !defined( HAVE_ATOLL )
361 int64_t vlc_atoll( const char *nptr )
363 return strtoll( nptr, (char **)NULL, 10 );
367 /*****************************************************************************
368 * lldiv: returns quotient and remainder
369 *****************************************************************************/
370 #if defined(SYS_BEOS) \
371 || (defined (__FreeBSD__) && (__FreeBSD__ < 5))
372 lldiv_t vlc_lldiv( long long numer, long long denom )
375 d.quot = numer / denom;
376 d.rem = numer % denom;
383 * Copy a string to a sized buffer. The result is always nul-terminated
384 * (contrary to strncpy()).
386 * @param dest destination buffer
387 * @param src string to be copied
388 * @param len maximum number of characters to be copied plus one for the
391 * @return strlen(src)
394 extern size_t vlc_strlcpy (char *tgt, const char *src, size_t bufsize)
398 for (length = 1; (length < bufsize) && *src; length++)
411 /*****************************************************************************
412 * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
413 * when called with an empty argument or just '\'
414 *****************************************************************************/
415 #if defined(WIN32) && !defined(UNDER_CE)
418 typedef struct vlc_DIR
422 struct _wdirent dd_dir;
423 vlc_bool_t b_insert_back;
426 void *vlc_wopendir( const wchar_t *wpath )
428 vlc_DIR *p_dir = NULL;
429 _WDIR *p_real_dir = NULL;
431 if ( wpath == NULL || wpath[0] == '\0'
432 || (wcscmp (wpath, L"\\") == 0) )
434 /* Special mode to list drive letters */
435 p_dir = malloc( sizeof(vlc_DIR) );
438 p_dir->p_real_dir = NULL;
439 p_dir->i_drives = GetLogicalDrives();
440 return (void *)p_dir;
443 p_real_dir = _wopendir( wpath );
444 if ( p_real_dir == NULL )
447 p_dir = malloc( sizeof(vlc_DIR) );
450 _wclosedir( p_real_dir );
453 p_dir->p_real_dir = p_real_dir;
455 assert (wpath[0]); // wpath[1] is defined
456 p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\");
457 return (void *)p_dir;
460 struct _wdirent *vlc_wreaddir( void *_p_dir )
462 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
466 if ( p_dir->p_real_dir != NULL )
468 if ( p_dir->b_insert_back )
470 /* Adds "..", gruik! */
471 p_dir->dd_dir.d_ino = 0;
472 p_dir->dd_dir.d_reclen = 0;
473 p_dir->dd_dir.d_namlen = 2;
474 wcscpy( p_dir->dd_dir.d_name, L".." );
475 p_dir->b_insert_back = VLC_FALSE;
476 return &p_dir->dd_dir;
479 return _wreaddir( p_dir->p_real_dir );
482 /* Drive letters mode */
483 i_drives = p_dir->i_drives;
485 return NULL; /* end */
487 for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
488 if ( i_drives & 1 ) break;
491 return NULL; /* this should not happen */
493 swprintf( p_dir->dd_dir.d_name, L"%c:\\", 'A' + i );
494 p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name);
495 p_dir->i_drives &= ~(1UL << i);
496 return &p_dir->dd_dir;
499 int vlc_wclosedir( void *_p_dir )
501 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
504 if ( p_dir->p_real_dir != NULL )
505 i_ret = _wclosedir( p_dir->p_real_dir );
511 void vlc_rewinddir( void *_p_dir )
513 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
515 if ( p_dir->p_real_dir != NULL )
516 _wrewinddir( p_dir->p_real_dir );
519 void vlc_seekdir( void *_p_dir, long loc)
521 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
523 if ( p_dir->p_real_dir != NULL )
524 _wseekdir( p_dir->p_real_dir, loc );
527 long vlc_telldir( void *_p_dir )
529 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
531 if ( p_dir->p_real_dir != NULL )
532 return _wtelldir( p_dir->p_real_dir );
537 /*****************************************************************************
538 * scandir: scan a directory alpha-sorted
539 *****************************************************************************/
540 #if !defined( HAVE_SCANDIR )
541 /* FIXME: I suspect this is dead code -> utf8_scandir */
547 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
549 return strcoll( (*a)->d_name, (*b)->d_name );
552 int vlc_scandir( const char *name, struct dirent ***namelist,
553 int (*filter) ( const struct dirent * ),
554 int (*compar) ( const struct dirent **,
555 const struct dirent ** ) )
558 struct dirent * p_content;
559 struct dirent ** pp_list;
562 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
566 while( ( p_content = readdir( p_dir ) ) )
568 if( filter && !filter( p_content ) )
572 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
573 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
574 pp_list[ret] = malloc( size );
577 memcpy( pp_list[ret], p_content, size );
582 /* Continuing is useless when no more memory can be allocted,
583 * so better return what we have found.
594 qsort( pp_list, ret, sizeof( struct dirent * ),
595 (int (*)(const void *, const void *)) compar );
604 /*****************************************************************************
605 * dgettext: gettext for plugins.
606 *****************************************************************************/
607 char *vlc_dgettext( const char *package, const char *msgid )
609 #if defined( ENABLE_NLS ) \
610 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
611 return dgettext( package, msgid );
613 return (char *)msgid;
618 /*****************************************************************************
619 * count_utf8_string: returns the number of characters in the string.
620 *****************************************************************************/
621 static int count_utf8_string( const char *psz_string )
623 int i = 0, i_count = 0;
624 while( psz_string[ i ] != 0 )
626 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
632 /*****************************************************************************
633 * wraptext: inserts \n at convenient places to wrap the text.
634 * Returns the modified string in a new buffer.
635 *****************************************************************************/
636 char *vlc_wraptext( const char *psz_text, int i_line )
639 char *psz_line, *psz_new_text;
641 psz_line = psz_new_text = strdup( psz_text );
643 i_len = count_utf8_string( psz_text );
645 while( i_len > i_line )
647 /* Look if there is a newline somewhere. */
648 char *psz_parser = psz_line;
650 while( i_count <= i_line && *psz_parser != '\n' )
652 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
656 if( *psz_parser == '\n' )
658 i_len -= (i_count + 1);
659 psz_line = psz_parser + 1;
663 /* Find the furthest space. */
664 while( psz_parser > psz_line && *psz_parser != ' ' )
666 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
670 if( *psz_parser == ' ' )
673 i_len -= (i_count + 1);
674 psz_line = psz_parser + 1;
678 /* Wrapping has failed. Find the first space or newline */
679 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
681 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
685 if( i_count < i_len ) *psz_parser = '\n';
686 i_len -= (i_count + 1);
687 psz_line = psz_parser + 1;
693 /*****************************************************************************
695 *****************************************************************************/
696 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
698 #if defined(HAVE_ICONV)
699 return iconv_open( tocode, fromcode );
705 size_t vlc_iconv( vlc_iconv_t cd, const char **inbuf, size_t *inbytesleft,
706 char **outbuf, size_t *outbytesleft )
708 #if defined(HAVE_ICONV)
709 return iconv( cd, (ICONV_CONST char **)inbuf, inbytesleft,
710 outbuf, outbytesleft );
714 if (inbytesleft == NULL || outbytesleft == NULL)
719 i_bytes = __MIN(*inbytesleft, *outbytesleft);
720 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
721 memcpy( *outbuf, *inbuf, i_bytes );
724 inbytesleft -= i_bytes;
725 outbytesleft -= i_bytes;
730 int vlc_iconv_close( vlc_iconv_t cd )
732 #if defined(HAVE_ICONV)
733 return iconv_close( cd );
739 /*****************************************************************************
741 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
742 *****************************************************************************/
743 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
744 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
746 vlc_bool_t b_exact = 1;
756 i_gcd = GCD( i_nom, i_den );
760 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
762 if( i_nom > i_max || i_den > i_max )
764 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
769 uint64_t i_x = i_nom / i_den;
770 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
771 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
773 if( i_a2n > i_max || i_a2d > i_max ) break;
777 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
778 i_a1_num = i_a2n; i_a1_den = i_a2d;
779 if( i_nom == 0 ) break;
780 i_x = i_nom; i_nom = i_den; i_den = i_x;
792 /*************************************************************************
793 * vlc_parse_cmdline: Command line parsing into elements.
795 * The command line is composed of space/tab separated arguments.
796 * Quotes can be used as argument delimiters and a backslash can be used to
798 *************************************************************************/
799 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
808 (*ppsz_parser)++; (*s)++;
811 else if( **s == '"' || **s == '\'' )
813 /* Preceeded by a number of '\' which we erase. */
814 *ppsz_parser -= i_bcount / 2;
817 /* '\\' followed by a '"' or '\'' */
820 (*ppsz_parser)++; (*s)++;
832 /* Different quoting */
835 (*ppsz_parser)++; (*s)++;
836 find_end_quote( s, ppsz_parser, i_quote );
838 (*ppsz_parser)++; (*s)++;
845 /* A regular character */
847 (*ppsz_parser)++; (*s)++;
853 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
857 char *s, *psz_parser, *psz_arg, *psz_orig;
860 if( !psz_cmdline ) return 0;
861 psz_orig = strdup( psz_cmdline );
862 psz_arg = psz_parser = s = psz_orig;
866 if( *s == '\t' || *s == ' ' )
868 /* We have a complete argument */
870 TAB_APPEND( argc, argv, strdup(psz_arg) );
872 /* Skip trailing spaces/tabs */
873 do{ s++; } while( *s == '\t' || *s == ' ' );
876 psz_arg = psz_parser = s;
879 else if( *s == '\\' )
881 *psz_parser++ = *s++;
884 else if( *s == '"' || *s == '\'' )
886 if( ( i_bcount & 1 ) == 0 )
888 /* Preceeded by an even number of '\', this is half that
889 * number of '\', plus a quote which we erase. */
891 psz_parser -= i_bcount / 2;
893 find_end_quote( &s, &psz_parser, i_quote );
898 /* Preceeded by an odd number of '\', this is half that
899 * number of '\' followed by a '"' */
900 psz_parser = psz_parser - i_bcount/2 - 1;
908 /* A regular character */
909 *psz_parser++ = *s++;
914 /* Take care of the last arg */
918 TAB_APPEND( argc, argv, strdup(psz_arg) );
921 if( i_args ) *i_args = argc;
926 /*************************************************************************
927 * vlc_execve: Execute an external program with a given environment,
928 * wait until it finishes and return its standard output
929 *************************************************************************/
930 int __vlc_execve( vlc_object_t *p_object, int i_argc, char *const *ppsz_argv,
931 char *const *ppsz_env, const char *psz_cwd,
932 const char *p_in, size_t i_in,
933 char **pp_data, size_t *pi_data )
935 (void)i_argc; // <-- hmph
937 # define BUFSIZE 1024
938 int fds[2], i_status;
940 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds))
944 if ((fds[0] > 2) && (fds[1] > 2))
950 msg_Err (p_object, "unable to fork (%s)", strerror (errno));
957 * Like it or not, close can fail (and not only with EBADF)
959 if ((close (0) == 0) && (close (1) == 0) && (close (2) == 0)
960 && (dup (fds[1]) == 0) && (dup (fds[1]) == 1)
961 && (open ("/dev/null", O_RDONLY) == 2)
962 && ((psz_cwd == NULL) || (chdir (psz_cwd) == 0)))
963 execve (ppsz_argv[0], ppsz_argv, ppsz_env);
976 shutdown (fds[0], SHUT_WR);
978 while (!p_object->b_die)
980 struct pollfd ufd[1];
981 memset (ufd, 0, sizeof (ufd));
983 ufd[0].events = POLLIN;
986 ufd[0].events |= POLLOUT;
988 if (poll (ufd, 1, 10) <= 0)
991 if (ufd[0].revents & ~POLLOUT)
993 char *ptr = realloc (*pp_data, *pi_data + BUFSIZE + 1);
995 break; /* safely abort */
999 ssize_t val = read (fds[0], ptr + *pi_data, BUFSIZE);
1004 shutdown (fds[0], SHUT_RD);
1012 if (ufd[0].revents & POLLOUT)
1014 ssize_t val = write (fds[0], p_in, i_in);
1020 shutdown (fds[0], SHUT_WR);
1032 while (waitpid (pid, &i_status, 0) == -1);
1034 if (WIFEXITED (i_status))
1036 i_status = WEXITSTATUS (i_status);
1038 msg_Warn (p_object, "child %s (PID %d) exited with error code %d",
1039 ppsz_argv[0], (int)pid, i_status);
1042 if (WIFSIGNALED (i_status)) // <-- this should be redumdant a check
1044 i_status = WTERMSIG (i_status);
1045 msg_Warn (p_object, "child %s (PID %d) exited on signal %d (%s)",
1046 ppsz_argv[0], (int)pid, i_status, strsignal (i_status));
1049 #elif defined( WIN32 ) && !defined( UNDER_CE )
1050 SECURITY_ATTRIBUTES saAttr;
1051 PROCESS_INFORMATION piProcInfo;
1052 STARTUPINFO siStartInfo;
1053 BOOL bFuncRetn = FALSE;
1054 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr;
1056 char *psz_cmd = NULL, *p_env = NULL, *p = NULL;
1060 /* Set the bInheritHandle flag so pipe handles are inherited. */
1061 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1062 saAttr.bInheritHandle = TRUE;
1063 saAttr.lpSecurityDescriptor = NULL;
1065 /* Create a pipe for the child process's STDOUT. */
1066 if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
1068 msg_Err( p_object, "stdout pipe creation failed" );
1072 /* Ensure the read handle to the pipe for STDOUT is not inherited. */
1073 SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 );
1075 /* Create a pipe for the child process's STDIN. */
1076 if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) )
1078 msg_Err( p_object, "stdin pipe creation failed" );
1082 /* Ensure the write handle to the pipe for STDIN is not inherited. */
1083 SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 );
1085 /* Set up members of the PROCESS_INFORMATION structure. */
1086 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
1088 /* Set up members of the STARTUPINFO structure. */
1089 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
1090 siStartInfo.cb = sizeof(STARTUPINFO);
1091 siStartInfo.hStdError = hChildStdoutWr;
1092 siStartInfo.hStdOutput = hChildStdoutWr;
1093 siStartInfo.hStdInput = hChildStdinRd;
1094 siStartInfo.wShowWindow = SW_HIDE;
1095 siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1097 /* Set up the command line. */
1098 psz_cmd = malloc(32768);
1103 ppsz_parser = &ppsz_argv[0];
1104 while ( ppsz_parser[0] != NULL && i_size > 0 )
1106 /* Protect the last argument with quotes ; the other arguments
1107 * are supposed to be already protected because they have been
1108 * passed as a command-line option. */
1109 if ( ppsz_parser[1] == NULL )
1111 strncat( psz_cmd, "\"", i_size );
1114 strncat( psz_cmd, *ppsz_parser, i_size );
1115 i_size -= strlen( *ppsz_parser );
1116 if ( ppsz_parser[1] == NULL )
1118 strncat( psz_cmd, "\"", i_size );
1121 strncat( psz_cmd, " ", i_size );
1126 /* Set up the environment. */
1127 p = p_env = malloc(32768);
1135 ppsz_parser = &ppsz_env[0];
1136 while ( *ppsz_parser != NULL && i_size > 0 )
1138 memcpy( p, *ppsz_parser,
1139 __MIN((int)(strlen(*ppsz_parser) + 1), i_size) );
1140 p += strlen(*ppsz_parser) + 1;
1141 i_size -= strlen(*ppsz_parser) + 1;
1146 /* Create the child process. */
1147 bFuncRetn = CreateProcess( NULL,
1148 psz_cmd, // command line
1149 NULL, // process security attributes
1150 NULL, // primary thread security attributes
1151 TRUE, // handles are inherited
1152 0, // creation flags
1155 &siStartInfo, // STARTUPINFO pointer
1156 &piProcInfo ); // receives PROCESS_INFORMATION
1161 if ( bFuncRetn == 0 )
1163 msg_Err( p_object, "child creation failed" );
1167 /* Read from a file and write its contents to a pipe. */
1168 while ( i_in > 0 && !p_object->b_die )
1171 if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) )
1177 /* Close the pipe handle so the child process stops reading. */
1178 CloseHandle(hChildStdinWr);
1180 /* Close the write end of the pipe before reading from the
1181 * read end of the pipe. */
1182 CloseHandle(hChildStdoutWr);
1184 /* Read output from the child process. */
1189 *pp_data = malloc( 1025 ); /* +1 for \0 */
1191 while ( !p_object->b_die )
1194 if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read,
1199 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1202 while ( !p_object->b_die
1203 && !GetExitCodeProcess( piProcInfo.hProcess, &i_status )
1204 && i_status != STILL_ACTIVE )
1207 CloseHandle(piProcInfo.hProcess);
1208 CloseHandle(piProcInfo.hThread);
1212 "child %s returned with error code %ld",
1213 ppsz_argv[0], i_status );
1216 msg_Err( p_object, "vlc_execve called but no implementation is available" );
1221 if (*pp_data == NULL)
1224 (*pp_data)[*pi_data] = '\0';