]> git.sesse.net Git - vlc/blob - src/extras/libc.c
* src/extras/libc.c: Compilation fix for non-Win32 platforms.
[vlc] / src / extras / libc.c
1 /*****************************************************************************
2  * libc.c: Extra libc function for some systems.
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
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  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27 #include <string.h>                                              /* strdup() */
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include <vlc/vlc.h>
32
33 #undef iconv_t
34 #undef iconv_open
35 #undef iconv
36 #undef iconv_close
37
38 #if defined(HAVE_ICONV)
39 #   include <iconv.h>
40 #endif
41
42 #ifdef HAVE_DIRENT_H
43 #   include <dirent.h>
44 #endif
45
46 /*****************************************************************************
47  * getenv: just in case, but it should never be called
48  *****************************************************************************/
49 #if !defined( HAVE_GETENV )
50 char *vlc_getenv( const char *name )
51 {
52     return NULL;
53 }
54 #endif
55
56 /*****************************************************************************
57  * strdup: returns a malloc'd copy of a string
58  *****************************************************************************/
59 #if !defined( HAVE_STRDUP )
60 char *vlc_strdup( const char *string )
61 {
62     return strndup( string, strlen( string ) );
63 }
64 #endif
65
66 /*****************************************************************************
67  * strndup: returns a malloc'd copy of at most n bytes of string
68  * Does anyone know whether or not it will be present in Jaguar?
69  *****************************************************************************/
70 #if !defined( HAVE_STRNDUP )
71 char *vlc_strndup( const char *string, size_t n )
72 {
73     char *psz;
74     size_t len = strlen( string );
75
76     len = __MIN( len, n );
77     psz = (char*)malloc( len + 1 );
78
79     if( psz != NULL )
80     {
81         memcpy( (void*)psz, (const void*)string, len );
82         psz[ len ] = 0;
83     }
84
85     return psz;
86 }
87 #endif
88
89 /*****************************************************************************
90  * strcasecmp: compare two strings ignoring case
91  *****************************************************************************/
92 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
93 int vlc_strcasecmp( const char *s1, const char *s2 )
94 {
95     int c1, c2;
96     if( !s1 || !s2 ) return  -1;
97
98     while( *s1 && *s2 )
99     {
100         c1 = tolower(*s1);
101         c2 = tolower(*s2);
102
103         if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
104         s1++; s2++;
105     }
106
107     if( !*s1 && !*s2 ) return 0;
108     else return (*s1 ? 1 : -1);
109 }
110 #endif
111
112 /*****************************************************************************
113  * strncasecmp: compare n chars from two strings ignoring case
114  *****************************************************************************/
115 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
116 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
117 {
118     int c1, c2;
119     if( !s1 || !s2 ) return  -1;
120
121     while( n > 0 && *s1 && *s2 )
122     {
123         c1 = tolower(*s1);
124         c2 = tolower(*s2);
125
126         if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
127         s1++; s2++; n--;
128     }
129
130     if( !n || (!*s1 && !*s2) ) return 0;
131     else return (*s1 ? 1 : -1);
132 }
133 #endif
134
135 /******************************************************************************
136  * strcasestr: find a substring (little) in another substring (big)
137  * Case sensitive. Return NULL if not found, return big if little == null
138  *****************************************************************************/
139 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
140 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
141 {
142     char *p_pos = (char *)psz_big;
143
144     if( !psz_big || !psz_little || !*psz_little ) return p_pos;
145  
146     while( *p_pos ) 
147     {
148         if( toupper( *p_pos ) == toupper( *psz_little ) )
149         {
150             char * psz_cur1 = p_pos + 1;
151             char * psz_cur2 = (char *)psz_little + 1;
152             while( *psz_cur1 && *psz_cur2 &&
153                    toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
154             {
155                 psz_cur1++;
156                 psz_cur2++;
157             }
158             if( !*psz_cur2 ) return p_pos;
159         }
160         p_pos++;
161     }
162     return NULL;
163 }
164 #endif
165
166 /*****************************************************************************
167  * vasprintf:
168  *****************************************************************************/
169 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
170 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
171 {
172     /* Guess we need no more than 100 bytes. */
173     int     i_size = 100;
174     char    *p = malloc( i_size );
175     int     n;
176
177     if( p == NULL )
178     {
179         *strp = NULL;
180         return -1;
181     }
182
183     for( ;; )
184     {
185         /* Try to print in the allocated space. */
186         n = vsnprintf( p, i_size, fmt, ap );
187
188         /* If that worked, return the string. */
189         if (n > -1 && n < i_size)
190         {
191             *strp = p;
192             return strlen( p );
193         }
194         /* Else try again with more space. */
195         if (n > -1)    /* glibc 2.1 */
196         {
197            i_size = n+1; /* precisely what is needed */
198         }
199         else           /* glibc 2.0 */
200         {
201            i_size *= 2;  /* twice the old size */
202         }
203         if( (p = realloc( p, i_size ) ) == NULL)
204         {
205             *strp = NULL;
206             return -1;
207         }
208     }
209 }
210 #endif
211
212 /*****************************************************************************
213  * asprintf:
214  *****************************************************************************/
215 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
216 int vlc_asprintf( char **strp, const char *fmt, ... )
217 {
218     va_list args;
219     int i_ret;
220
221     va_start( args, fmt );
222     i_ret = vasprintf( strp, fmt, args );
223     va_end( args );
224
225     return i_ret;
226 }
227 #endif
228
229 /*****************************************************************************
230  * atof: convert a string to a double.
231  *****************************************************************************/
232 #if !defined( HAVE_ATOF )
233 double vlc_atof( const char *nptr )
234 {
235     double f_result;
236     wchar_t *psz_tmp;
237     int i_len = strlen( nptr ) + 1;
238
239     psz_tmp = malloc( i_len * sizeof(wchar_t) );
240     MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
241     f_result = wcstod( psz_tmp, NULL );
242     free( psz_tmp );
243
244     return f_result;
245 }
246 #endif
247
248 /*****************************************************************************
249  * strtoll: convert a string to a 64 bits int.
250  *****************************************************************************/
251 #if !defined( HAVE_STRTOLL )
252 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
253 {
254     int64_t i_value = 0;
255     int sign = 1, newbase = base ? base : 10;
256
257     while( isspace(*nptr) ) nptr++;
258
259     if( *nptr == '-' )
260     {
261         sign = -1;
262         nptr++;
263     }
264
265     /* Try to detect base */
266     if( *nptr == '0' )
267     {
268         newbase = 8;
269         nptr++;
270
271         if( *nptr == 'x' )
272         {
273             newbase = 16;
274             nptr++;
275         }
276     }
277
278     if( base && newbase != base )
279     {
280         if( endptr ) *endptr = (char *)nptr;
281         return i_value;
282     }
283
284     switch( newbase )
285     {
286         case 10:
287             while( *nptr >= '0' && *nptr <= '9' )
288             {
289                 i_value *= 10;
290                 i_value += ( *nptr++ - '0' );
291             }
292             if( endptr ) *endptr = (char *)nptr;
293             break;
294
295         case 16:
296             while( (*nptr >= '0' && *nptr <= '9') ||
297                    (*nptr >= 'a' && *nptr <= 'f') ||
298                    (*nptr >= 'A' && *nptr <= 'F') )
299             {
300                 int i_valc = 0;
301                 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
302                 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
303                 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
304                 i_value *= 16;
305                 i_value += i_valc;
306                 nptr++;
307             }
308             if( endptr ) *endptr = (char *)nptr;
309             break;
310
311         default:
312             i_value = strtol( nptr, endptr, newbase );
313             break;
314     }
315
316     return i_value * sign;
317 }
318 #endif
319
320 /*****************************************************************************
321  * atoll: convert a string to a 64 bits int.
322  *****************************************************************************/
323 #if !defined( HAVE_ATOLL )
324 int64_t vlc_atoll( const char *nptr )
325 {
326     return strtoll( nptr, (char **)NULL, 10 );
327 }
328 #endif
329
330 /*****************************************************************************
331  * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
332  * when called with an empty argument or just '\'
333  *****************************************************************************/
334 #if defined(WIN32) || defined(UNDER_CE)
335
336 #define WIN32_LEAN_AND_MEAN
337 #include <windows.h> /* for GetLogicalDrives */
338
339 typedef struct vlc_DIR
340 {
341     DIR *p_real_dir;
342     int i_drives;
343     struct dirent dd_dir;
344     vlc_bool_t b_insert_back;
345 } vlc_DIR;
346
347 void *vlc_opendir_wrapper( const char *psz_path )
348 {
349     vlc_DIR *p_dir;
350     DIR *p_real_dir;
351
352     if ( psz_path == NULL || psz_path[0] == '\0'
353           || (psz_path[0] == '\\' && psz_path[1] == '\0') )
354     {
355         /* Special mode to list drive letters */
356         p_dir = malloc( sizeof(vlc_DIR) );
357         p_dir->p_real_dir = NULL;
358         p_dir->i_drives = GetLogicalDrives();
359         return (void *)p_dir;
360     }
361
362     p_real_dir = opendir( psz_path );
363     if ( p_real_dir == NULL )
364         return NULL;
365
366     p_dir = malloc( sizeof(vlc_DIR) );
367     p_dir->p_real_dir = p_real_dir;
368     p_dir->b_insert_back = ( psz_path[1] == ':' && psz_path[2] == '\\'
369                               && psz_path[3] =='\0' );
370     return (void *)p_dir;
371 }
372
373 struct dirent *vlc_readdir_wrapper( void *_p_dir )
374 {
375     vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
376     unsigned int i;
377     DWORD i_drives;
378
379     if ( p_dir->p_real_dir != NULL )
380     {
381         if ( p_dir->b_insert_back )
382         {
383             p_dir->dd_dir.d_ino = 0;
384             p_dir->dd_dir.d_reclen = 0;
385             p_dir->dd_dir.d_namlen = 2;
386             strcpy( p_dir->dd_dir.d_name, ".." );
387             p_dir->b_insert_back = VLC_FALSE;
388             return &p_dir->dd_dir;
389         }
390
391         return readdir( p_dir->p_real_dir );
392     }
393
394     /* Drive letters mode */
395     i_drives = p_dir->i_drives;
396     if ( !i_drives )
397         return NULL; /* end */
398
399     for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
400         if ( i_drives & 1 ) break;
401
402     if ( i >= 26 )
403         return NULL; /* this should not happen */
404
405     sprintf( p_dir->dd_dir.d_name, "%c:\\", 'A' + i );
406     p_dir->dd_dir.d_namlen = strlen(p_dir->dd_dir.d_name);
407     p_dir->i_drives &= ~(1UL << i);
408     return &p_dir->dd_dir;
409 }
410
411 int vlc_closedir_wrapper( void *_p_dir )
412 {
413     vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
414
415     if ( p_dir->p_real_dir != NULL )
416     {
417         int i_ret = closedir( p_dir->p_real_dir );
418         free( p_dir );
419         return i_ret;
420     }
421
422     free( p_dir );
423     return 0;
424 }
425 #else
426 void *vlc_opendir_wrapper( const char *psz_path )
427 {
428     return (void *)opendir( psz_path );
429 }
430 struct dirent *vlc_readdir_wrapper( void *_p_dir )
431 {
432     return readdir( (DIR *)_p_dir );
433 }
434 int vlc_closedir_wrapper( void *_p_dir )
435 {
436     return closedir( (DIR *)_p_dir );
437 }
438 #endif
439
440 /*****************************************************************************
441  * scandir: scan a directory alpha-sorted
442  *****************************************************************************/
443 #if !defined( HAVE_SCANDIR )
444 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
445 {
446     return strcoll( (*a)->d_name, (*b)->d_name );
447 }
448
449 int vlc_scandir( const char *name, struct dirent ***namelist,
450                     int (*filter) ( const struct dirent * ),
451                     int (*compar) ( const struct dirent **,
452                                     const struct dirent ** ) )
453 {
454     DIR            * p_dir;
455     struct dirent  * p_content;
456     struct dirent ** pp_list;
457     int              ret, size;
458
459     if( !namelist || !( p_dir = vlc_opendir_wrapper( name ) ) ) return -1;
460
461     ret     = 0;
462     pp_list = NULL;
463     while( ( p_content = vlc_readdir_wrapper( p_dir ) ) )
464     {
465         if( filter && !filter( p_content ) )
466         {
467             continue;
468         }
469         pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
470         size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
471         pp_list[ret] = malloc( size );
472         memcpy( pp_list[ret], p_content, size );
473         ret++;
474     }
475
476     vlc_closedir_wrapper( p_dir );
477
478     if( compar )
479     {
480         qsort( pp_list, ret, sizeof( struct dirent * ),
481                (int (*)(const void *, const void *)) compar );
482     }
483
484     *namelist = pp_list;
485     return ret;
486 }
487 #endif
488
489 /*****************************************************************************
490  * dgettext: gettext for plugins.
491  *****************************************************************************/
492 char *vlc_dgettext( const char *package, const char *msgid )
493 {
494 #if defined( ENABLE_NLS ) \
495      && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
496     return dgettext( package, msgid );
497 #else
498     return (char *)msgid;
499 #endif
500 }
501
502 /*****************************************************************************
503  * count_utf8_string: returns the number of characters in the string.
504  *****************************************************************************/
505 static int count_utf8_string( const char *psz_string )
506 {
507     int i = 0, i_count = 0;
508     while( psz_string[ i ] != 0 )
509     {
510         if( ((unsigned char *)psz_string)[ i ] <  0x80UL ) i_count++;
511         i++;
512     }
513     return i_count;
514 }
515
516 /*****************************************************************************
517  * wraptext: inserts \n at convenient places to wrap the text.
518  *           Returns the modified string in a new buffer.
519  *****************************************************************************/
520 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
521 {
522     int i_len;
523     char *psz_line, *psz_new_text;
524
525     psz_line = psz_new_text = strdup( psz_text );
526
527     if( b_utf8 )
528         i_len = count_utf8_string( psz_text );
529     else
530         i_len = strlen( psz_text );
531
532     while( i_len > i_line )
533     {
534         /* Look if there is a newline somewhere. */
535         char *psz_parser = psz_line;
536         int i_count = 0;
537         while( i_count <= i_line && *psz_parser != '\n' )
538         {
539             if( b_utf8 )
540             {
541                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
542             }
543             psz_parser++;
544             i_count++;
545         }
546         if( *psz_parser == '\n' )
547         {
548             i_len -= (i_count + 1);
549             psz_line = psz_parser + 1;
550             continue;
551         }
552
553         /* Find the furthest space. */
554         while( psz_parser > psz_line && *psz_parser != ' ' )
555         {
556             if( b_utf8 )
557             {
558                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
559             }
560             psz_parser--;
561             i_count--;
562         }
563         if( *psz_parser == ' ' )
564         {
565             *psz_parser = '\n';
566             i_len -= (i_count + 1);
567             psz_line = psz_parser + 1;
568             continue;
569         }
570
571         /* Wrapping has failed. Find the first space or newline */
572         while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
573         {
574             if( b_utf8 )
575             {
576                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
577             }
578             psz_parser++;
579             i_count++;
580         }
581         if( i_count < i_len ) *psz_parser = '\n';
582         i_len -= (i_count + 1);
583         psz_line = psz_parser + 1;
584     }
585
586     return psz_new_text;
587 }
588
589 /*****************************************************************************
590  * iconv wrapper
591  *****************************************************************************/
592 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
593 {
594 #if defined(HAVE_ICONV)
595     return iconv_open( tocode, fromcode );
596 #else
597     return NULL;
598 #endif
599 }
600
601 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
602                   char **outbuf, size_t *outbytesleft )
603 {
604 #if defined(HAVE_ICONV)
605     return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
606 #else
607     int i_bytes = __MIN(*inbytesleft, *outbytesleft);
608     if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
609     memcpy( *outbuf, *inbuf, i_bytes );
610     inbuf += i_bytes;
611     outbuf += i_bytes;
612     inbytesleft -= i_bytes;
613     outbytesleft -= i_bytes;
614     return i_bytes;
615 #endif
616 }
617
618 int vlc_iconv_close( vlc_iconv_t cd )
619 {
620 #if defined(HAVE_ICONV)
621     return iconv_close( cd );
622 #else
623     return 0;
624 #endif
625 }
626
627 /*****************************************************************************
628  * reduce a fraction
629  *   (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
630  *****************************************************************************/
631 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
632                         uint64_t i_nom, uint64_t i_den, uint64_t i_max )
633 {
634     vlc_bool_t b_exact = 1;
635     uint64_t i_gcd;
636
637     if( i_den == 0 )
638     {
639         *pi_dst_nom = 0;
640         *pi_dst_den = 1;
641         return 1;
642     }
643
644     i_gcd = GCD( i_nom, i_den );
645     i_nom /= i_gcd;
646     i_den /= i_gcd;
647
648     if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
649
650     if( i_nom > i_max || i_den > i_max )
651     {
652         uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
653         b_exact = 0;
654
655         for( ; ; )
656         {
657             uint64_t i_x = i_nom / i_den;
658             uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
659             uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
660
661             if( i_a2n > i_max || i_a2d > i_max ) break;
662
663             i_nom %= i_den;
664
665             i_a0_num = i_a1_num; i_a0_den = i_a1_den;
666             i_a1_num = i_a2n; i_a1_den = i_a2d;
667             if( i_nom == 0 ) break;
668             i_x = i_nom; i_nom = i_den; i_den = i_x;
669         }
670         i_nom = i_a1_num;
671         i_den = i_a1_den;
672     }
673
674     *pi_dst_nom = i_nom;
675     *pi_dst_den = i_den;
676
677     return b_exact;
678 }
679
680 /*************************************************************************
681  * vlc_parse_cmdline: Command line parsing into elements.
682  *
683  * The command line is composed of space/tab separated arguments.
684  * Quotes can be used as argument delimiters and a backslash can be used to
685  * escape a quote.
686  *************************************************************************/
687 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
688 {
689     int i_bcount = 0;
690
691     while( **s )
692     {
693         if( **s == '\\' )
694         {
695             **ppsz_parser = **s;
696             (*ppsz_parser)++; (*s)++;
697             i_bcount++;
698         }
699         else if( **s == '"' || **s == '\'' )
700         {
701             /* Preceeded by a number of '\' which we erase. */
702             *ppsz_parser -= i_bcount / 2;
703             if( i_bcount & 1 )
704             {
705                 /* '\\' followed by a '"' or '\'' */
706                 *ppsz_parser -= 1;
707                 **ppsz_parser = **s;
708                 (*ppsz_parser)++; (*s)++;
709                 i_bcount = 0;
710                 continue;
711             }
712
713             if( **s == i_quote )
714             {
715                 /* End */
716                 return;
717             }
718             else
719             {
720                 /* Different quoting */
721                 int i_quote = **s;
722                 **ppsz_parser = **s;
723                 (*ppsz_parser)++; (*s)++;
724                 find_end_quote( s, ppsz_parser, i_quote );
725                 **ppsz_parser = **s;
726                 (*ppsz_parser)++; (*s)++;
727             }
728
729             i_bcount = 0;
730         }
731         else
732         {
733             /* A regular character */
734             **ppsz_parser = **s;
735             (*ppsz_parser)++; (*s)++;
736             i_bcount = 0;
737         }
738     }
739 }
740
741 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
742 {
743     int argc = 0;
744     char **argv = 0;
745     char *s, *psz_parser, *psz_arg, *psz_orig;
746     int i_bcount = 0;
747
748     if( !psz_cmdline ) return 0;
749     psz_orig = strdup( psz_cmdline );
750     psz_arg = psz_parser = s = psz_orig;
751
752     while( *s )
753     {
754         if( *s == '\t' || *s == ' ' )
755         {
756             /* We have a complete argument */
757             *psz_parser = 0;
758             TAB_APPEND( argc, argv, strdup(psz_arg) );
759
760             /* Skip trailing spaces/tabs */
761             do{ s++; } while( *s == '\t' || *s == ' ' );
762
763             /* New argument */
764             psz_arg = psz_parser = s;
765             i_bcount = 0;
766         }
767         else if( *s == '\\' )
768         {
769             *psz_parser++ = *s++;
770             i_bcount++;
771         }
772         else if( *s == '"' || *s == '\'' )
773         {
774             if( ( i_bcount & 1 ) == 0 )
775             {
776                 /* Preceeded by an even number of '\', this is half that
777                  * number of '\', plus a quote which we erase. */
778                 int i_quote = *s;
779                 psz_parser -= i_bcount / 2;
780                 s++;
781                 find_end_quote( &s, &psz_parser, i_quote );
782                 s++;
783             }
784             else
785             {
786                 /* Preceeded by an odd number of '\', this is half that
787                  * number of '\' followed by a '"' */
788                 psz_parser = psz_parser - i_bcount/2 - 1;
789                 *psz_parser++ = '"';
790                 s++;
791             }
792             i_bcount = 0;
793         }
794         else
795         {
796             /* A regular character */
797             *psz_parser++ = *s++;
798             i_bcount = 0;
799         }
800     }
801
802     /* Take care of the last arg */
803     if( *psz_arg )
804     {
805         *psz_parser = '\0';
806         TAB_APPEND( argc, argv, strdup(psz_arg) );
807     }
808
809     if( i_args ) *i_args = argc;
810     free( psz_orig );
811     return argv;
812 }