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 *****************************************************************************/
42 #if defined(HAVE_ICONV)
55 # include <sys/time.h>
58 # include <sys/wait.h>
60 # include <sys/socket.h>
61 # include <sys/poll.h>
64 #if defined(WIN32) || defined(UNDER_CE)
71 # define WIN32_LEAN_AND_MEAN
76 # define strcoll strcmp
79 /*****************************************************************************
80 * getenv: just in case, but it should never be called
81 *****************************************************************************/
82 #if !defined( HAVE_GETENV )
83 char *vlc_getenv( const char *name )
89 /*****************************************************************************
90 * strdup: returns a malloc'd copy of a string
91 *****************************************************************************/
92 #if !defined( HAVE_STRDUP )
93 char *vlc_strdup( const char *string )
95 return strndup( string, strlen( string ) );
99 /*****************************************************************************
100 * strndup: returns a malloc'd copy of at most n bytes of string
101 * Does anyone know whether or not it will be present in Jaguar?
102 *****************************************************************************/
103 #if !defined( HAVE_STRNDUP )
104 char *vlc_strndup( const char *string, size_t n )
107 size_t len = strlen( string );
109 len = __MIN( len, n );
110 psz = (char*)malloc( len + 1 );
113 memcpy( (void*)psz, (const void*)string, len );
121 /*****************************************************************************
123 *****************************************************************************/
124 #if !defined( HAVE_STRNLEN )
125 size_t vlc_strnlen( const char *psz, size_t n )
127 const char *psz_end = memchr( psz, 0, n );
128 return psz_end ? (size_t)( psz_end - psz ) : n;
132 /*****************************************************************************
133 * strcasecmp: compare two strings ignoring case
134 *****************************************************************************/
135 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
136 int vlc_strcasecmp( const char *s1, const char *s2 )
139 if( !s1 || !s2 ) return -1;
146 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
150 if( !*s1 && !*s2 ) return 0;
151 else return (*s1 ? 1 : -1);
155 /*****************************************************************************
156 * strncasecmp: compare n chars from two strings ignoring case
157 *****************************************************************************/
158 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
159 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
162 if( !s1 || !s2 ) return -1;
164 while( n > 0 && *s1 && *s2 )
169 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
173 if( !n || (!*s1 && !*s2) ) return 0;
174 else return (*s1 ? 1 : -1);
178 /******************************************************************************
179 * strcasestr: find a substring (little) in another substring (big)
180 * Case sensitive. Return NULL if not found, return big if little == null
181 *****************************************************************************/
182 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
183 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
185 char *p_pos = (char *)psz_big;
187 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
191 if( toupper( *p_pos ) == toupper( *psz_little ) )
193 char * psz_cur1 = p_pos + 1;
194 char * psz_cur2 = (char *)psz_little + 1;
195 while( *psz_cur1 && *psz_cur2 &&
196 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
201 if( !*psz_cur2 ) return p_pos;
209 /*****************************************************************************
211 *****************************************************************************/
212 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
213 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
215 /* Guess we need no more than 100 bytes. */
217 char *p = malloc( i_size );
228 /* Try to print in the allocated space. */
229 n = vsnprintf( p, i_size, fmt, ap );
231 /* If that worked, return the string. */
232 if (n > -1 && n < i_size)
237 /* Else try again with more space. */
238 if (n > -1) /* glibc 2.1 */
240 i_size = n+1; /* precisely what is needed */
244 i_size *= 2; /* twice the old size */
246 if( (p = realloc( p, i_size ) ) == NULL)
255 /*****************************************************************************
257 *****************************************************************************/
258 #if !defined(HAVE_ASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
259 int vlc_asprintf( char **strp, const char *fmt, ... )
264 va_start( args, fmt );
265 i_ret = vasprintf( strp, fmt, args );
272 /*****************************************************************************
273 * atof: convert a string to a double.
274 *****************************************************************************/
275 #if !defined( HAVE_ATOF )
276 double vlc_atof( const char *nptr )
279 wchar_t *psz_tmp = NULL;
280 int i_len = strlen( nptr ) + 1;
282 psz_tmp = malloc( i_len * sizeof(wchar_t) );
285 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
286 f_result = wcstod( psz_tmp, NULL );
293 /*****************************************************************************
294 * strtoll: convert a string to a 64 bits int.
295 *****************************************************************************/
296 #if !defined( HAVE_STRTOLL )
297 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
300 int sign = 1, newbase = base ? base : 10;
302 while( isspace(*nptr) ) nptr++;
310 /* Try to detect base */
323 if( base && newbase != base )
325 if( endptr ) *endptr = (char *)nptr;
332 while( *nptr >= '0' && *nptr <= '9' )
335 i_value += ( *nptr++ - '0' );
337 if( endptr ) *endptr = (char *)nptr;
341 while( (*nptr >= '0' && *nptr <= '9') ||
342 (*nptr >= 'a' && *nptr <= 'f') ||
343 (*nptr >= 'A' && *nptr <= 'F') )
346 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
347 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
348 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
353 if( endptr ) *endptr = (char *)nptr;
357 i_value = strtol( nptr, endptr, newbase );
361 return i_value * sign;
365 /*****************************************************************************
366 * atoll: convert a string to a 64 bits int.
367 *****************************************************************************/
368 #if !defined( HAVE_ATOLL )
369 int64_t vlc_atoll( const char *nptr )
371 return strtoll( nptr, (char **)NULL, 10 );
375 /*****************************************************************************
376 * lldiv: returns quotient and remainder
377 *****************************************************************************/
378 #if !defined( HAVE_LLDIV )
379 lldiv_t vlc_lldiv( long long numer, long long denom )
382 d.quot = numer / denom;
383 d.rem = numer % denom;
390 * Copy a string to a sized buffer. The result is always nul-terminated
391 * (contrary to strncpy()).
393 * @param dest destination buffer
394 * @param src string to be copied
395 * @param len maximum number of characters to be copied plus one for the
398 * @return strlen(src)
401 extern size_t vlc_strlcpy (char *tgt, const char *src, size_t bufsize)
405 for (length = 1; (length < bufsize) && *src; length++)
418 /*****************************************************************************
419 * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
420 * when called with an empty argument or just '\'
421 *****************************************************************************/
422 #if defined(WIN32) && !defined(UNDER_CE)
425 typedef struct vlc_DIR
429 struct _wdirent dd_dir;
430 vlc_bool_t b_insert_back;
433 void *vlc_wopendir( const wchar_t *wpath )
435 vlc_DIR *p_dir = NULL;
436 _WDIR *p_real_dir = NULL;
438 if ( wpath == NULL || wpath[0] == '\0'
439 || (wcscmp (wpath, L"\\") == 0) )
441 /* Special mode to list drive letters */
442 p_dir = malloc( sizeof(vlc_DIR) );
445 p_dir->p_real_dir = NULL;
446 p_dir->i_drives = GetLogicalDrives();
447 return (void *)p_dir;
450 p_real_dir = _wopendir( wpath );
451 if ( p_real_dir == NULL )
454 p_dir = malloc( sizeof(vlc_DIR) );
457 _wclosedir( p_real_dir );
460 p_dir->p_real_dir = p_real_dir;
462 assert (wpath[0]); // wpath[1] is defined
463 p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\");
464 return (void *)p_dir;
467 struct _wdirent *vlc_wreaddir( void *_p_dir )
469 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
473 if ( p_dir->p_real_dir != NULL )
475 if ( p_dir->b_insert_back )
477 /* Adds "..", gruik! */
478 p_dir->dd_dir.d_ino = 0;
479 p_dir->dd_dir.d_reclen = 0;
480 p_dir->dd_dir.d_namlen = 2;
481 wcscpy( p_dir->dd_dir.d_name, L".." );
482 p_dir->b_insert_back = VLC_FALSE;
483 return &p_dir->dd_dir;
486 return _wreaddir( p_dir->p_real_dir );
489 /* Drive letters mode */
490 i_drives = p_dir->i_drives;
492 return NULL; /* end */
494 for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
495 if ( i_drives & 1 ) break;
498 return NULL; /* this should not happen */
500 swprintf( p_dir->dd_dir.d_name, L"%c:\\", 'A' + i );
501 p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name);
502 p_dir->i_drives &= ~(1UL << i);
503 return &p_dir->dd_dir;
506 int vlc_wclosedir( void *_p_dir )
508 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
511 if ( p_dir->p_real_dir != NULL )
512 i_ret = _wclosedir( p_dir->p_real_dir );
518 void vlc_rewinddir( void *_p_dir )
520 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
522 if ( p_dir->p_real_dir != NULL )
523 _wrewinddir( p_dir->p_real_dir );
526 void vlc_seekdir( void *_p_dir, long loc)
528 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
530 if ( p_dir->p_real_dir != NULL )
531 _wseekdir( p_dir->p_real_dir, loc );
534 long vlc_telldir( void *_p_dir )
536 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
538 if ( p_dir->p_real_dir != NULL )
539 return _wtelldir( p_dir->p_real_dir );
544 /*****************************************************************************
545 * scandir: scan a directory alpha-sorted
546 *****************************************************************************/
547 #if !defined( HAVE_SCANDIR )
548 /* FIXME: I suspect this is dead code -> utf8_scandir */
554 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
556 return strcoll( (*a)->d_name, (*b)->d_name );
559 int vlc_scandir( const char *name, struct dirent ***namelist,
560 int (*filter) ( const struct dirent * ),
561 int (*compar) ( const struct dirent **,
562 const struct dirent ** ) )
565 struct dirent * p_content;
566 struct dirent ** pp_list;
569 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
573 while( ( p_content = readdir( p_dir ) ) )
575 if( filter && !filter( p_content ) )
579 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
580 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
581 pp_list[ret] = malloc( size );
584 memcpy( pp_list[ret], p_content, size );
589 /* Continuing is useless when no more memory can be allocted,
590 * so better return what we have found.
601 qsort( pp_list, ret, sizeof( struct dirent * ),
602 (int (*)(const void *, const void *)) compar );
611 /*****************************************************************************
612 * dgettext: gettext for plugins.
613 *****************************************************************************/
614 char *vlc_dgettext( const char *package, const char *msgid )
616 #if defined( ENABLE_NLS ) \
617 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
618 return dgettext( package, msgid );
620 return (char *)msgid;
625 /*****************************************************************************
626 * count_utf8_string: returns the number of characters in the string.
627 *****************************************************************************/
628 static int count_utf8_string( const char *psz_string )
630 int i = 0, i_count = 0;
631 while( psz_string[ i ] != 0 )
633 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
639 /*****************************************************************************
640 * wraptext: inserts \n at convenient places to wrap the text.
641 * Returns the modified string in a new buffer.
642 *****************************************************************************/
643 char *vlc_wraptext( const char *psz_text, int i_line )
646 char *psz_line, *psz_new_text;
648 psz_line = psz_new_text = strdup( psz_text );
650 i_len = count_utf8_string( psz_text );
652 while( i_len > i_line )
654 /* Look if there is a newline somewhere. */
655 char *psz_parser = psz_line;
657 while( i_count <= i_line && *psz_parser != '\n' )
659 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
663 if( *psz_parser == '\n' )
665 i_len -= (i_count + 1);
666 psz_line = psz_parser + 1;
670 /* Find the furthest space. */
671 while( psz_parser > psz_line && *psz_parser != ' ' )
673 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
677 if( *psz_parser == ' ' )
680 i_len -= (i_count + 1);
681 psz_line = psz_parser + 1;
685 /* Wrapping has failed. Find the first space or newline */
686 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
688 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
692 if( i_count < i_len ) *psz_parser = '\n';
693 i_len -= (i_count + 1);
694 psz_line = psz_parser + 1;
700 /*****************************************************************************
702 *****************************************************************************/
703 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
705 #if defined(HAVE_ICONV)
706 return iconv_open( tocode, fromcode );
712 size_t vlc_iconv( vlc_iconv_t cd, const char **inbuf, size_t *inbytesleft,
713 char **outbuf, size_t *outbytesleft )
715 #if defined(HAVE_ICONV)
716 return iconv( cd, (ICONV_CONST char **)inbuf, inbytesleft,
717 outbuf, outbytesleft );
721 if (inbytesleft == NULL || outbytesleft == NULL)
726 i_bytes = __MIN(*inbytesleft, *outbytesleft);
727 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
728 memcpy( *outbuf, *inbuf, i_bytes );
731 inbytesleft -= i_bytes;
732 outbytesleft -= i_bytes;
737 int vlc_iconv_close( vlc_iconv_t cd )
739 #if defined(HAVE_ICONV)
740 return iconv_close( cd );
746 /*****************************************************************************
748 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
749 *****************************************************************************/
750 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
751 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
753 vlc_bool_t b_exact = 1;
763 i_gcd = GCD( i_nom, i_den );
767 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
769 if( i_nom > i_max || i_den > i_max )
771 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
776 uint64_t i_x = i_nom / i_den;
777 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
778 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
780 if( i_a2n > i_max || i_a2d > i_max ) break;
784 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
785 i_a1_num = i_a2n; i_a1_den = i_a2d;
786 if( i_nom == 0 ) break;
787 i_x = i_nom; i_nom = i_den; i_den = i_x;
799 /*************************************************************************
800 * vlc_parse_cmdline: Command line parsing into elements.
802 * The command line is composed of space/tab separated arguments.
803 * Quotes can be used as argument delimiters and a backslash can be used to
805 *************************************************************************/
806 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
815 (*ppsz_parser)++; (*s)++;
818 else if( **s == '"' || **s == '\'' )
820 /* Preceeded by a number of '\' which we erase. */
821 *ppsz_parser -= i_bcount / 2;
824 /* '\\' followed by a '"' or '\'' */
827 (*ppsz_parser)++; (*s)++;
839 /* Different quoting */
842 (*ppsz_parser)++; (*s)++;
843 find_end_quote( s, ppsz_parser, i_quote );
845 (*ppsz_parser)++; (*s)++;
852 /* A regular character */
854 (*ppsz_parser)++; (*s)++;
860 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
864 char *s, *psz_parser, *psz_arg, *psz_orig;
867 if( !psz_cmdline ) return 0;
868 psz_orig = strdup( psz_cmdline );
869 psz_arg = psz_parser = s = psz_orig;
873 if( *s == '\t' || *s == ' ' )
875 /* We have a complete argument */
877 TAB_APPEND( argc, argv, strdup(psz_arg) );
879 /* Skip trailing spaces/tabs */
880 do{ s++; } while( *s == '\t' || *s == ' ' );
883 psz_arg = psz_parser = s;
886 else if( *s == '\\' )
888 *psz_parser++ = *s++;
891 else if( *s == '"' || *s == '\'' )
893 if( ( i_bcount & 1 ) == 0 )
895 /* Preceeded by an even number of '\', this is half that
896 * number of '\', plus a quote which we erase. */
898 psz_parser -= i_bcount / 2;
900 find_end_quote( &s, &psz_parser, i_quote );
905 /* Preceeded by an odd number of '\', this is half that
906 * number of '\' followed by a '"' */
907 psz_parser = psz_parser - i_bcount/2 - 1;
915 /* A regular character */
916 *psz_parser++ = *s++;
921 /* Take care of the last arg */
925 TAB_APPEND( argc, argv, strdup(psz_arg) );
928 if( i_args ) *i_args = argc;
933 /*************************************************************************
934 * vlc_execve: Execute an external program with a given environment,
935 * wait until it finishes and return its standard output
936 *************************************************************************/
937 int __vlc_execve( vlc_object_t *p_object, int i_argc, char *const *ppsz_argv,
938 char *const *ppsz_env, const char *psz_cwd,
939 const char *p_in, size_t i_in,
940 char **pp_data, size_t *pi_data )
942 (void)i_argc; // <-- hmph
944 # define BUFSIZE 1024
945 int fds[2], i_status;
947 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds))
951 if ((fds[0] > 2) && (fds[1] > 2))
957 msg_Err (p_object, "unable to fork (%m)");
966 pthread_sigmask (SIG_SETMASK, &set, NULL);
969 * Like it or not, close can fail (and not only with EBADF)
971 if ((close (0) == 0) && (close (1) == 0) && (close (2) == 0)
972 && (dup (fds[1]) == 0) && (dup (fds[1]) == 1)
973 && (open ("/dev/null", O_RDONLY) == 2)
974 && ((psz_cwd == NULL) || (chdir (psz_cwd) == 0)))
975 execve (ppsz_argv[0], ppsz_argv, ppsz_env);
989 shutdown (fds[0], SHUT_WR);
991 while (!p_object->b_die)
993 struct pollfd ufd[1];
994 memset (ufd, 0, sizeof (ufd));
996 ufd[0].events = POLLIN;
999 ufd[0].events |= POLLOUT;
1001 if (poll (ufd, 1, 10) <= 0)
1004 if (ufd[0].revents & ~POLLOUT)
1006 char *ptr = realloc (*pp_data, *pi_data + BUFSIZE + 1);
1008 break; /* safely abort */
1012 ssize_t val = read (fds[0], ptr + *pi_data, BUFSIZE);
1017 shutdown (fds[0], SHUT_RD);
1025 if (ufd[0].revents & POLLOUT)
1027 ssize_t val = write (fds[0], p_in, i_in);
1033 shutdown (fds[0], SHUT_WR);
1045 while (waitpid (pid, &i_status, 0) == -1);
1047 if (WIFEXITED (i_status))
1049 i_status = WEXITSTATUS (i_status);
1051 msg_Warn (p_object, "child %s (PID %d) exited with error code %d",
1052 ppsz_argv[0], (int)pid, i_status);
1055 if (WIFSIGNALED (i_status)) // <-- this should be redumdant a check
1057 i_status = WTERMSIG (i_status);
1058 msg_Warn (p_object, "child %s (PID %d) exited on signal %d (%s)",
1059 ppsz_argv[0], (int)pid, i_status, strsignal (i_status));
1062 #elif defined( WIN32 ) && !defined( UNDER_CE )
1063 SECURITY_ATTRIBUTES saAttr;
1064 PROCESS_INFORMATION piProcInfo;
1065 STARTUPINFO siStartInfo;
1066 BOOL bFuncRetn = FALSE;
1067 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr;
1069 char *psz_cmd = NULL, *p_env = NULL, *p = NULL;
1073 /* Set the bInheritHandle flag so pipe handles are inherited. */
1074 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1075 saAttr.bInheritHandle = TRUE;
1076 saAttr.lpSecurityDescriptor = NULL;
1078 /* Create a pipe for the child process's STDOUT. */
1079 if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
1081 msg_Err( p_object, "stdout pipe creation failed" );
1085 /* Ensure the read handle to the pipe for STDOUT is not inherited. */
1086 SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 );
1088 /* Create a pipe for the child process's STDIN. */
1089 if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) )
1091 msg_Err( p_object, "stdin pipe creation failed" );
1095 /* Ensure the write handle to the pipe for STDIN is not inherited. */
1096 SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 );
1098 /* Set up members of the PROCESS_INFORMATION structure. */
1099 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
1101 /* Set up members of the STARTUPINFO structure. */
1102 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
1103 siStartInfo.cb = sizeof(STARTUPINFO);
1104 siStartInfo.hStdError = hChildStdoutWr;
1105 siStartInfo.hStdOutput = hChildStdoutWr;
1106 siStartInfo.hStdInput = hChildStdinRd;
1107 siStartInfo.wShowWindow = SW_HIDE;
1108 siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1110 /* Set up the command line. */
1111 psz_cmd = malloc(32768);
1116 ppsz_parser = &ppsz_argv[0];
1117 while ( ppsz_parser[0] != NULL && i_size > 0 )
1119 /* Protect the last argument with quotes ; the other arguments
1120 * are supposed to be already protected because they have been
1121 * passed as a command-line option. */
1122 if ( ppsz_parser[1] == NULL )
1124 strncat( psz_cmd, "\"", i_size );
1127 strncat( psz_cmd, *ppsz_parser, i_size );
1128 i_size -= strlen( *ppsz_parser );
1129 if ( ppsz_parser[1] == NULL )
1131 strncat( psz_cmd, "\"", i_size );
1134 strncat( psz_cmd, " ", i_size );
1139 /* Set up the environment. */
1140 p = p_env = malloc(32768);
1148 ppsz_parser = &ppsz_env[0];
1149 while ( *ppsz_parser != NULL && i_size > 0 )
1151 memcpy( p, *ppsz_parser,
1152 __MIN((int)(strlen(*ppsz_parser) + 1), i_size) );
1153 p += strlen(*ppsz_parser) + 1;
1154 i_size -= strlen(*ppsz_parser) + 1;
1159 /* Create the child process. */
1160 bFuncRetn = CreateProcess( NULL,
1161 psz_cmd, // command line
1162 NULL, // process security attributes
1163 NULL, // primary thread security attributes
1164 TRUE, // handles are inherited
1165 0, // creation flags
1168 &siStartInfo, // STARTUPINFO pointer
1169 &piProcInfo ); // receives PROCESS_INFORMATION
1174 if ( bFuncRetn == 0 )
1176 msg_Err( p_object, "child creation failed" );
1180 /* Read from a file and write its contents to a pipe. */
1181 while ( i_in > 0 && !p_object->b_die )
1184 if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) )
1190 /* Close the pipe handle so the child process stops reading. */
1191 CloseHandle(hChildStdinWr);
1193 /* Close the write end of the pipe before reading from the
1194 * read end of the pipe. */
1195 CloseHandle(hChildStdoutWr);
1197 /* Read output from the child process. */
1202 *pp_data = malloc( 1025 ); /* +1 for \0 */
1204 while ( !p_object->b_die )
1207 if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read,
1212 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1215 while ( !p_object->b_die
1216 && !GetExitCodeProcess( piProcInfo.hProcess, &i_status )
1217 && i_status != STILL_ACTIVE )
1220 CloseHandle(piProcInfo.hProcess);
1221 CloseHandle(piProcInfo.hThread);
1225 "child %s returned with error code %ld",
1226 ppsz_argv[0], i_status );
1229 msg_Err( p_object, "vlc_execve called but no implementation is available" );
1234 if (*pp_data == NULL)
1237 (*pp_data)[*pi_data] = '\0';