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 /*****************************************************************************
116 * strcasecmp: compare two strings ignoring case
117 *****************************************************************************/
118 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
119 int vlc_strcasecmp( const char *s1, const char *s2 )
122 if( !s1 || !s2 ) return -1;
129 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
133 if( !*s1 && !*s2 ) return 0;
134 else return (*s1 ? 1 : -1);
138 /*****************************************************************************
139 * strncasecmp: compare n chars from two strings ignoring case
140 *****************************************************************************/
141 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
142 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
145 if( !s1 || !s2 ) return -1;
147 while( n > 0 && *s1 && *s2 )
152 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
156 if( !n || (!*s1 && !*s2) ) return 0;
157 else return (*s1 ? 1 : -1);
161 /******************************************************************************
162 * strcasestr: find a substring (little) in another substring (big)
163 * Case sensitive. Return NULL if not found, return big if little == null
164 *****************************************************************************/
165 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
166 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
168 char *p_pos = (char *)psz_big;
170 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
174 if( toupper( *p_pos ) == toupper( *psz_little ) )
176 char * psz_cur1 = p_pos + 1;
177 char * psz_cur2 = (char *)psz_little + 1;
178 while( *psz_cur1 && *psz_cur2 &&
179 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
184 if( !*psz_cur2 ) return p_pos;
192 /*****************************************************************************
194 *****************************************************************************/
195 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
196 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
198 /* Guess we need no more than 100 bytes. */
200 char *p = malloc( i_size );
211 /* Try to print in the allocated space. */
212 n = vsnprintf( p, i_size, fmt, ap );
214 /* If that worked, return the string. */
215 if (n > -1 && n < i_size)
220 /* Else try again with more space. */
221 if (n > -1) /* glibc 2.1 */
223 i_size = n+1; /* precisely what is needed */
227 i_size *= 2; /* twice the old size */
229 if( (p = realloc( p, i_size ) ) == NULL)
238 /*****************************************************************************
240 *****************************************************************************/
241 #if !defined(HAVE_ASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
242 int vlc_asprintf( char **strp, const char *fmt, ... )
247 va_start( args, fmt );
248 i_ret = vasprintf( strp, fmt, args );
255 /*****************************************************************************
256 * atof: convert a string to a double.
257 *****************************************************************************/
258 #if !defined( HAVE_ATOF )
259 double vlc_atof( const char *nptr )
262 wchar_t *psz_tmp = NULL;
263 int i_len = strlen( nptr ) + 1;
265 psz_tmp = malloc( i_len * sizeof(wchar_t) );
268 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
269 f_result = wcstod( psz_tmp, NULL );
276 /*****************************************************************************
277 * strtoll: convert a string to a 64 bits int.
278 *****************************************************************************/
279 #if !defined( HAVE_STRTOLL )
280 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
283 int sign = 1, newbase = base ? base : 10;
285 while( isspace(*nptr) ) nptr++;
293 /* Try to detect base */
306 if( base && newbase != base )
308 if( endptr ) *endptr = (char *)nptr;
315 while( *nptr >= '0' && *nptr <= '9' )
318 i_value += ( *nptr++ - '0' );
320 if( endptr ) *endptr = (char *)nptr;
324 while( (*nptr >= '0' && *nptr <= '9') ||
325 (*nptr >= 'a' && *nptr <= 'f') ||
326 (*nptr >= 'A' && *nptr <= 'F') )
329 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
330 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
331 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
336 if( endptr ) *endptr = (char *)nptr;
340 i_value = strtol( nptr, endptr, newbase );
344 return i_value * sign;
348 /*****************************************************************************
349 * atoll: convert a string to a 64 bits int.
350 *****************************************************************************/
351 #if !defined( HAVE_ATOLL )
352 int64_t vlc_atoll( const char *nptr )
354 return strtoll( nptr, (char **)NULL, 10 );
358 /*****************************************************************************
359 * lldiv: returns quotient and remainder
360 *****************************************************************************/
361 #if defined(SYS_BEOS) \
362 || (defined (__FreeBSD__) && (__FreeBSD__ < 5))
363 lldiv_t vlc_lldiv( long long numer, long long denom )
366 d.quot = numer / denom;
367 d.rem = numer % denom;
374 * Copy a string to a sized buffer. The result is always nul-terminated
375 * (contrary to strncpy()).
377 * @param dest destination buffer
378 * @param src string to be copied
379 * @param len maximum number of characters to be copied plus one for the
382 * @return strlen(src)
385 extern size_t vlc_strlcpy (char *tgt, const char *src, size_t bufsize)
389 for (length = 1; (length < bufsize) && *src; length++)
402 /*****************************************************************************
403 * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
404 * when called with an empty argument or just '\'
405 *****************************************************************************/
406 #if defined(WIN32) && !defined(UNDER_CE)
409 typedef struct vlc_DIR
413 struct _wdirent dd_dir;
414 vlc_bool_t b_insert_back;
417 void *vlc_wopendir( const wchar_t *wpath )
419 vlc_DIR *p_dir = NULL;
420 _WDIR *p_real_dir = NULL;
422 if ( wpath == NULL || wpath[0] == '\0'
423 || (wcscmp (wpath, L"\\") == 0) )
425 /* Special mode to list drive letters */
426 p_dir = malloc( sizeof(vlc_DIR) );
429 p_dir->p_real_dir = NULL;
430 p_dir->i_drives = GetLogicalDrives();
431 return (void *)p_dir;
434 p_real_dir = _wopendir( wpath );
435 if ( p_real_dir == NULL )
438 p_dir = malloc( sizeof(vlc_DIR) );
441 _wclosedir( p_real_dir );
444 p_dir->p_real_dir = p_real_dir;
446 assert (wpath[0]); // wpath[1] is defined
447 p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\");
448 return (void *)p_dir;
451 struct _wdirent *vlc_wreaddir( void *_p_dir )
453 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
457 if ( p_dir->p_real_dir != NULL )
459 if ( p_dir->b_insert_back )
461 /* Adds "..", gruik! */
462 p_dir->dd_dir.d_ino = 0;
463 p_dir->dd_dir.d_reclen = 0;
464 p_dir->dd_dir.d_namlen = 2;
465 wcscpy( p_dir->dd_dir.d_name, L".." );
466 p_dir->b_insert_back = VLC_FALSE;
467 return &p_dir->dd_dir;
470 return _wreaddir( p_dir->p_real_dir );
473 /* Drive letters mode */
474 i_drives = p_dir->i_drives;
476 return NULL; /* end */
478 for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
479 if ( i_drives & 1 ) break;
482 return NULL; /* this should not happen */
484 swprintf( p_dir->dd_dir.d_name, L"%c:\\", 'A' + i );
485 p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name);
486 p_dir->i_drives &= ~(1UL << i);
487 return &p_dir->dd_dir;
490 int vlc_wclosedir( void *_p_dir )
492 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
495 if ( p_dir->p_real_dir != NULL )
496 i_ret = _wclosedir( p_dir->p_real_dir );
502 void vlc_rewinddir( void *_p_dir )
504 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
506 if ( p_dir->p_real_dir != NULL )
507 _wrewinddir( p_dir->p_real_dir );
510 void vlc_seekdir( void *_p_dir, long loc)
512 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
514 if ( p_dir->p_real_dir != NULL )
515 _wseekdir( p_dir->p_real_dir, loc );
518 long vlc_telldir( void *_p_dir )
520 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
522 if ( p_dir->p_real_dir != NULL )
523 return _wtelldir( p_dir->p_real_dir );
528 /*****************************************************************************
529 * scandir: scan a directory alpha-sorted
530 *****************************************************************************/
531 #if !defined( HAVE_SCANDIR )
532 /* FIXME: I suspect this is dead code -> utf8_scandir */
538 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
540 return strcoll( (*a)->d_name, (*b)->d_name );
543 int vlc_scandir( const char *name, struct dirent ***namelist,
544 int (*filter) ( const struct dirent * ),
545 int (*compar) ( const struct dirent **,
546 const struct dirent ** ) )
549 struct dirent * p_content;
550 struct dirent ** pp_list;
553 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
557 while( ( p_content = readdir( p_dir ) ) )
559 if( filter && !filter( p_content ) )
563 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
564 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
565 pp_list[ret] = malloc( size );
568 memcpy( pp_list[ret], p_content, size );
573 /* Continuing is useless when no more memory can be allocted,
574 * so better return what we have found.
585 qsort( pp_list, ret, sizeof( struct dirent * ),
586 (int (*)(const void *, const void *)) compar );
594 #if defined (WIN32) || !defined (HAVE_SHARED_LIBVLC)
595 /*****************************************************************************
596 * dgettext: gettext for plugins.
597 *****************************************************************************/
598 char *vlc_dgettext( const char *package, const char *msgid )
600 #if defined( ENABLE_NLS ) \
601 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
602 return dgettext( package, msgid );
604 return (char *)msgid;
609 /*****************************************************************************
610 * count_utf8_string: returns the number of characters in the string.
611 *****************************************************************************/
612 static int count_utf8_string( const char *psz_string )
614 int i = 0, i_count = 0;
615 while( psz_string[ i ] != 0 )
617 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
623 /*****************************************************************************
624 * wraptext: inserts \n at convenient places to wrap the text.
625 * Returns the modified string in a new buffer.
626 *****************************************************************************/
627 char *vlc_wraptext( const char *psz_text, int i_line )
630 char *psz_line, *psz_new_text;
632 psz_line = psz_new_text = strdup( psz_text );
634 i_len = count_utf8_string( psz_text );
636 while( i_len > i_line )
638 /* Look if there is a newline somewhere. */
639 char *psz_parser = psz_line;
641 while( i_count <= i_line && *psz_parser != '\n' )
643 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
647 if( *psz_parser == '\n' )
649 i_len -= (i_count + 1);
650 psz_line = psz_parser + 1;
654 /* Find the furthest space. */
655 while( psz_parser > psz_line && *psz_parser != ' ' )
657 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
661 if( *psz_parser == ' ' )
664 i_len -= (i_count + 1);
665 psz_line = psz_parser + 1;
669 /* Wrapping has failed. Find the first space or newline */
670 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
672 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
676 if( i_count < i_len ) *psz_parser = '\n';
677 i_len -= (i_count + 1);
678 psz_line = psz_parser + 1;
684 /*****************************************************************************
686 *****************************************************************************/
687 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
689 #if defined(HAVE_ICONV)
690 return iconv_open( tocode, fromcode );
696 size_t vlc_iconv( vlc_iconv_t cd, const char **inbuf, size_t *inbytesleft,
697 char **outbuf, size_t *outbytesleft )
699 #if defined(HAVE_ICONV)
700 return iconv( cd, (ICONV_CONST char **)inbuf, inbytesleft,
701 outbuf, outbytesleft );
705 if (inbytesleft == NULL || outbytesleft == NULL)
710 i_bytes = __MIN(*inbytesleft, *outbytesleft);
711 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
712 memcpy( *outbuf, *inbuf, i_bytes );
715 inbytesleft -= i_bytes;
716 outbytesleft -= i_bytes;
721 int vlc_iconv_close( vlc_iconv_t cd )
723 #if defined(HAVE_ICONV)
724 return iconv_close( cd );
730 /*****************************************************************************
732 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
733 *****************************************************************************/
734 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
735 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
737 vlc_bool_t b_exact = 1;
747 i_gcd = GCD( i_nom, i_den );
751 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
753 if( i_nom > i_max || i_den > i_max )
755 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
760 uint64_t i_x = i_nom / i_den;
761 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
762 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
764 if( i_a2n > i_max || i_a2d > i_max ) break;
768 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
769 i_a1_num = i_a2n; i_a1_den = i_a2d;
770 if( i_nom == 0 ) break;
771 i_x = i_nom; i_nom = i_den; i_den = i_x;
783 /*************************************************************************
784 * vlc_parse_cmdline: Command line parsing into elements.
786 * The command line is composed of space/tab separated arguments.
787 * Quotes can be used as argument delimiters and a backslash can be used to
789 *************************************************************************/
790 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
799 (*ppsz_parser)++; (*s)++;
802 else if( **s == '"' || **s == '\'' )
804 /* Preceeded by a number of '\' which we erase. */
805 *ppsz_parser -= i_bcount / 2;
808 /* '\\' followed by a '"' or '\'' */
811 (*ppsz_parser)++; (*s)++;
823 /* Different quoting */
826 (*ppsz_parser)++; (*s)++;
827 find_end_quote( s, ppsz_parser, i_quote );
829 (*ppsz_parser)++; (*s)++;
836 /* A regular character */
838 (*ppsz_parser)++; (*s)++;
844 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
848 char *s, *psz_parser, *psz_arg, *psz_orig;
851 if( !psz_cmdline ) return 0;
852 psz_orig = strdup( psz_cmdline );
853 psz_arg = psz_parser = s = psz_orig;
857 if( *s == '\t' || *s == ' ' )
859 /* We have a complete argument */
861 TAB_APPEND( argc, argv, strdup(psz_arg) );
863 /* Skip trailing spaces/tabs */
864 do{ s++; } while( *s == '\t' || *s == ' ' );
867 psz_arg = psz_parser = s;
870 else if( *s == '\\' )
872 *psz_parser++ = *s++;
875 else if( *s == '"' || *s == '\'' )
877 if( ( i_bcount & 1 ) == 0 )
879 /* Preceeded by an even number of '\', this is half that
880 * number of '\', plus a quote which we erase. */
882 psz_parser -= i_bcount / 2;
884 find_end_quote( &s, &psz_parser, i_quote );
889 /* Preceeded by an odd number of '\', this is half that
890 * number of '\' followed by a '"' */
891 psz_parser = psz_parser - i_bcount/2 - 1;
899 /* A regular character */
900 *psz_parser++ = *s++;
905 /* Take care of the last arg */
909 TAB_APPEND( argc, argv, strdup(psz_arg) );
912 if( i_args ) *i_args = argc;
917 /*************************************************************************
918 * vlc_execve: Execute an external program with a given environment,
919 * wait until it finishes and return its standard output
920 *************************************************************************/
921 int __vlc_execve( vlc_object_t *p_object, int i_argc, char *const *ppsz_argv,
922 char *const *ppsz_env, const char *psz_cwd,
923 const char *p_in, size_t i_in,
924 char **pp_data, size_t *pi_data )
926 (void)i_argc; // <-- hmph
928 # define BUFSIZE 1024
929 int fds[2], i_status;
931 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds))
935 if ((fds[0] > 2) && (fds[1] > 2))
941 msg_Err (p_object, "unable to fork (%s)", strerror (errno));
948 * Like it or not, close can fail (and not only with EBADF)
950 if ((close (0) == 0) && (close (1) == 0) && (close (2) == 0)
951 && (dup (fds[1]) == 0) && (dup (fds[1]) == 1)
952 && (open ("/dev/null", O_RDONLY) == 2)
953 && ((psz_cwd == NULL) || (chdir (psz_cwd) == 0)))
954 execve (ppsz_argv[0], ppsz_argv, ppsz_env);
967 shutdown (fds[0], SHUT_WR);
969 while (!p_object->b_die)
971 struct pollfd ufd[1];
972 memset (ufd, 0, sizeof (ufd));
974 ufd[0].events = POLLIN;
977 ufd[0].events |= POLLOUT;
979 if (poll (ufd, 1, 10) <= 0)
982 if (ufd[0].revents & ~POLLOUT)
984 char *ptr = realloc (*pp_data, *pi_data + BUFSIZE + 1);
986 break; /* safely abort */
990 ssize_t val = read (fds[0], ptr + *pi_data, BUFSIZE);
995 shutdown (fds[0], SHUT_RD);
1003 if (ufd[0].revents & POLLOUT)
1005 ssize_t val = write (fds[0], p_in, i_in);
1011 shutdown (fds[0], SHUT_WR);
1023 while (waitpid (pid, &i_status, 0) == -1);
1025 if (WIFEXITED (i_status))
1027 i_status = WEXITSTATUS (i_status);
1029 msg_Warn (p_object, "child %s (PID %d) exited with error code %d",
1030 ppsz_argv[0], (int)pid, i_status);
1033 if (WIFSIGNALED (i_status)) // <-- this should be redumdant a check
1035 i_status = WTERMSIG (i_status);
1036 msg_Warn (p_object, "child %s (PID %d) exited on signal %d (%s)",
1037 ppsz_argv[0], (int)pid, i_status, strsignal (i_status));
1040 #elif defined( WIN32 ) && !defined( UNDER_CE )
1041 SECURITY_ATTRIBUTES saAttr;
1042 PROCESS_INFORMATION piProcInfo;
1043 STARTUPINFO siStartInfo;
1044 BOOL bFuncRetn = FALSE;
1045 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr;
1047 char *psz_cmd = NULL, *p_env = NULL, *p = NULL;
1051 /* Set the bInheritHandle flag so pipe handles are inherited. */
1052 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1053 saAttr.bInheritHandle = TRUE;
1054 saAttr.lpSecurityDescriptor = NULL;
1056 /* Create a pipe for the child process's STDOUT. */
1057 if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
1059 msg_Err( p_object, "stdout pipe creation failed" );
1063 /* Ensure the read handle to the pipe for STDOUT is not inherited. */
1064 SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 );
1066 /* Create a pipe for the child process's STDIN. */
1067 if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) )
1069 msg_Err( p_object, "stdin pipe creation failed" );
1073 /* Ensure the write handle to the pipe for STDIN is not inherited. */
1074 SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 );
1076 /* Set up members of the PROCESS_INFORMATION structure. */
1077 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
1079 /* Set up members of the STARTUPINFO structure. */
1080 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
1081 siStartInfo.cb = sizeof(STARTUPINFO);
1082 siStartInfo.hStdError = hChildStdoutWr;
1083 siStartInfo.hStdOutput = hChildStdoutWr;
1084 siStartInfo.hStdInput = hChildStdinRd;
1085 siStartInfo.wShowWindow = SW_HIDE;
1086 siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
1088 /* Set up the command line. */
1089 psz_cmd = malloc(32768);
1094 ppsz_parser = &ppsz_argv[0];
1095 while ( ppsz_parser[0] != NULL && i_size > 0 )
1097 /* Protect the last argument with quotes ; the other arguments
1098 * are supposed to be already protected because they have been
1099 * passed as a command-line option. */
1100 if ( ppsz_parser[1] == NULL )
1102 strncat( psz_cmd, "\"", i_size );
1105 strncat( psz_cmd, *ppsz_parser, i_size );
1106 i_size -= strlen( *ppsz_parser );
1107 if ( ppsz_parser[1] == NULL )
1109 strncat( psz_cmd, "\"", i_size );
1112 strncat( psz_cmd, " ", i_size );
1117 /* Set up the environment. */
1118 p = p_env = malloc(32768);
1126 ppsz_parser = &ppsz_env[0];
1127 while ( *ppsz_parser != NULL && i_size > 0 )
1129 memcpy( p, *ppsz_parser,
1130 __MIN((int)(strlen(*ppsz_parser) + 1), i_size) );
1131 p += strlen(*ppsz_parser) + 1;
1132 i_size -= strlen(*ppsz_parser) + 1;
1137 /* Create the child process. */
1138 bFuncRetn = CreateProcess( NULL,
1139 psz_cmd, // command line
1140 NULL, // process security attributes
1141 NULL, // primary thread security attributes
1142 TRUE, // handles are inherited
1143 0, // creation flags
1146 &siStartInfo, // STARTUPINFO pointer
1147 &piProcInfo ); // receives PROCESS_INFORMATION
1152 if ( bFuncRetn == 0 )
1154 msg_Err( p_object, "child creation failed" );
1158 /* Read from a file and write its contents to a pipe. */
1159 while ( i_in > 0 && !p_object->b_die )
1162 if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) )
1168 /* Close the pipe handle so the child process stops reading. */
1169 CloseHandle(hChildStdinWr);
1171 /* Close the write end of the pipe before reading from the
1172 * read end of the pipe. */
1173 CloseHandle(hChildStdoutWr);
1175 /* Read output from the child process. */
1180 *pp_data = malloc( 1025 ); /* +1 for \0 */
1182 while ( !p_object->b_die )
1185 if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read,
1190 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1193 while ( !p_object->b_die
1194 && !GetExitCodeProcess( piProcInfo.hProcess, &i_status )
1195 && i_status != STILL_ACTIVE )
1198 CloseHandle(piProcInfo.hProcess);
1199 CloseHandle(piProcInfo.hThread);
1203 "child %s returned with error code %ld",
1204 ppsz_argv[0], i_status );
1207 msg_Err( p_object, "vlc_execve called but no implementation is available" );
1212 if (*pp_data == NULL)
1215 (*pp_data)[*pi_data] = '\0';