]> git.sesse.net Git - vlc/blob - src/extras/libc.c
* src/extras/libc.c: Implemented a vlc_readdir_wrapper under Win32. This
[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 #endif
426
427 /*****************************************************************************
428  * scandir: scan a directory alpha-sorted
429  *****************************************************************************/
430 #if !defined( HAVE_SCANDIR )
431 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
432 {
433     return strcoll( (*a)->d_name, (*b)->d_name );
434 }
435
436 int vlc_scandir( const char *name, struct dirent ***namelist,
437                     int (*filter) ( const struct dirent * ),
438                     int (*compar) ( const struct dirent **,
439                                     const struct dirent ** ) )
440 {
441     DIR            * p_dir;
442     struct dirent  * p_content;
443     struct dirent ** pp_list;
444     int              ret, size;
445
446     if( !namelist || !( p_dir = vlc_opendir_wrapper( name ) ) ) return -1;
447
448     ret     = 0;
449     pp_list = NULL;
450     while( ( p_content = vlc_readdir_wrapper( p_dir ) ) )
451     {
452         if( filter && !filter( p_content ) )
453         {
454             continue;
455         }
456         pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
457         size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
458         pp_list[ret] = malloc( size );
459         memcpy( pp_list[ret], p_content, size );
460         ret++;
461     }
462
463     vlc_closedir_wrapper( p_dir );
464
465     if( compar )
466     {
467         qsort( pp_list, ret, sizeof( struct dirent * ),
468                (int (*)(const void *, const void *)) compar );
469     }
470
471     *namelist = pp_list;
472     return ret;
473 }
474 #endif
475
476 /*****************************************************************************
477  * dgettext: gettext for plugins.
478  *****************************************************************************/
479 char *vlc_dgettext( const char *package, const char *msgid )
480 {
481 #if defined( ENABLE_NLS ) \
482      && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
483     return dgettext( package, msgid );
484 #else
485     return (char *)msgid;
486 #endif
487 }
488
489 /*****************************************************************************
490  * count_utf8_string: returns the number of characters in the string.
491  *****************************************************************************/
492 static int count_utf8_string( const char *psz_string )
493 {
494     int i = 0, i_count = 0;
495     while( psz_string[ i ] != 0 )
496     {
497         if( ((unsigned char *)psz_string)[ i ] <  0x80UL ) i_count++;
498         i++;
499     }
500     return i_count;
501 }
502
503 /*****************************************************************************
504  * wraptext: inserts \n at convenient places to wrap the text.
505  *           Returns the modified string in a new buffer.
506  *****************************************************************************/
507 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
508 {
509     int i_len;
510     char *psz_line, *psz_new_text;
511
512     psz_line = psz_new_text = strdup( psz_text );
513
514     if( b_utf8 )
515         i_len = count_utf8_string( psz_text );
516     else
517         i_len = strlen( psz_text );
518
519     while( i_len > i_line )
520     {
521         /* Look if there is a newline somewhere. */
522         char *psz_parser = psz_line;
523         int i_count = 0;
524         while( i_count <= i_line && *psz_parser != '\n' )
525         {
526             if( b_utf8 )
527             {
528                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
529             }
530             psz_parser++;
531             i_count++;
532         }
533         if( *psz_parser == '\n' )
534         {
535             i_len -= (i_count + 1);
536             psz_line = psz_parser + 1;
537             continue;
538         }
539
540         /* Find the furthest space. */
541         while( psz_parser > psz_line && *psz_parser != ' ' )
542         {
543             if( b_utf8 )
544             {
545                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
546             }
547             psz_parser--;
548             i_count--;
549         }
550         if( *psz_parser == ' ' )
551         {
552             *psz_parser = '\n';
553             i_len -= (i_count + 1);
554             psz_line = psz_parser + 1;
555             continue;
556         }
557
558         /* Wrapping has failed. Find the first space or newline */
559         while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
560         {
561             if( b_utf8 )
562             {
563                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
564             }
565             psz_parser++;
566             i_count++;
567         }
568         if( i_count < i_len ) *psz_parser = '\n';
569         i_len -= (i_count + 1);
570         psz_line = psz_parser + 1;
571     }
572
573     return psz_new_text;
574 }
575
576 /*****************************************************************************
577  * iconv wrapper
578  *****************************************************************************/
579 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
580 {
581 #if defined(HAVE_ICONV)
582     return iconv_open( tocode, fromcode );
583 #else
584     return NULL;
585 #endif
586 }
587
588 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
589                   char **outbuf, size_t *outbytesleft )
590 {
591 #if defined(HAVE_ICONV)
592     return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
593 #else
594     int i_bytes = __MIN(*inbytesleft, *outbytesleft);
595     if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
596     memcpy( *outbuf, *inbuf, i_bytes );
597     inbuf += i_bytes;
598     outbuf += i_bytes;
599     inbytesleft -= i_bytes;
600     outbytesleft -= i_bytes;
601     return i_bytes;
602 #endif
603 }
604
605 int vlc_iconv_close( vlc_iconv_t cd )
606 {
607 #if defined(HAVE_ICONV)
608     return iconv_close( cd );
609 #else
610     return 0;
611 #endif
612 }
613
614 /*****************************************************************************
615  * reduce a fraction
616  *   (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
617  *****************************************************************************/
618 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
619                         uint64_t i_nom, uint64_t i_den, uint64_t i_max )
620 {
621     vlc_bool_t b_exact = 1;
622     uint64_t i_gcd;
623
624     if( i_den == 0 )
625     {
626         *pi_dst_nom = 0;
627         *pi_dst_den = 1;
628         return 1;
629     }
630
631     i_gcd = GCD( i_nom, i_den );
632     i_nom /= i_gcd;
633     i_den /= i_gcd;
634
635     if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
636
637     if( i_nom > i_max || i_den > i_max )
638     {
639         uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
640         b_exact = 0;
641
642         for( ; ; )
643         {
644             uint64_t i_x = i_nom / i_den;
645             uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
646             uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
647
648             if( i_a2n > i_max || i_a2d > i_max ) break;
649
650             i_nom %= i_den;
651
652             i_a0_num = i_a1_num; i_a0_den = i_a1_den;
653             i_a1_num = i_a2n; i_a1_den = i_a2d;
654             if( i_nom == 0 ) break;
655             i_x = i_nom; i_nom = i_den; i_den = i_x;
656         }
657         i_nom = i_a1_num;
658         i_den = i_a1_den;
659     }
660
661     *pi_dst_nom = i_nom;
662     *pi_dst_den = i_den;
663
664     return b_exact;
665 }
666
667 /*************************************************************************
668  * vlc_parse_cmdline: Command line parsing into elements.
669  *
670  * The command line is composed of space/tab separated arguments.
671  * Quotes can be used as argument delimiters and a backslash can be used to
672  * escape a quote.
673  *************************************************************************/
674 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
675 {
676     int i_bcount = 0;
677
678     while( **s )
679     {
680         if( **s == '\\' )
681         {
682             **ppsz_parser = **s;
683             (*ppsz_parser)++; (*s)++;
684             i_bcount++;
685         }
686         else if( **s == '"' || **s == '\'' )
687         {
688             /* Preceeded by a number of '\' which we erase. */
689             *ppsz_parser -= i_bcount / 2;
690             if( i_bcount & 1 )
691             {
692                 /* '\\' followed by a '"' or '\'' */
693                 *ppsz_parser -= 1;
694                 **ppsz_parser = **s;
695                 (*ppsz_parser)++; (*s)++;
696                 i_bcount = 0;
697                 continue;
698             }
699
700             if( **s == i_quote )
701             {
702                 /* End */
703                 return;
704             }
705             else
706             {
707                 /* Different quoting */
708                 int i_quote = **s;
709                 **ppsz_parser = **s;
710                 (*ppsz_parser)++; (*s)++;
711                 find_end_quote( s, ppsz_parser, i_quote );
712                 **ppsz_parser = **s;
713                 (*ppsz_parser)++; (*s)++;
714             }
715
716             i_bcount = 0;
717         }
718         else
719         {
720             /* A regular character */
721             **ppsz_parser = **s;
722             (*ppsz_parser)++; (*s)++;
723             i_bcount = 0;
724         }
725     }
726 }
727
728 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
729 {
730     int argc = 0;
731     char **argv = 0;
732     char *s, *psz_parser, *psz_arg, *psz_orig;
733     int i_bcount = 0;
734
735     if( !psz_cmdline ) return 0;
736     psz_orig = strdup( psz_cmdline );
737     psz_arg = psz_parser = s = psz_orig;
738
739     while( *s )
740     {
741         if( *s == '\t' || *s == ' ' )
742         {
743             /* We have a complete argument */
744             *psz_parser = 0;
745             TAB_APPEND( argc, argv, strdup(psz_arg) );
746
747             /* Skip trailing spaces/tabs */
748             do{ s++; } while( *s == '\t' || *s == ' ' );
749
750             /* New argument */
751             psz_arg = psz_parser = s;
752             i_bcount = 0;
753         }
754         else if( *s == '\\' )
755         {
756             *psz_parser++ = *s++;
757             i_bcount++;
758         }
759         else if( *s == '"' || *s == '\'' )
760         {
761             if( ( i_bcount & 1 ) == 0 )
762             {
763                 /* Preceeded by an even number of '\', this is half that
764                  * number of '\', plus a quote which we erase. */
765                 int i_quote = *s;
766                 psz_parser -= i_bcount / 2;
767                 s++;
768                 find_end_quote( &s, &psz_parser, i_quote );
769                 s++;
770             }
771             else
772             {
773                 /* Preceeded by an odd number of '\', this is half that
774                  * number of '\' followed by a '"' */
775                 psz_parser = psz_parser - i_bcount/2 - 1;
776                 *psz_parser++ = '"';
777                 s++;
778             }
779             i_bcount = 0;
780         }
781         else
782         {
783             /* A regular character */
784             *psz_parser++ = *s++;
785             i_bcount = 0;
786         }
787     }
788
789     /* Take care of the last arg */
790     if( *psz_arg )
791     {
792         *psz_parser = '\0';
793         TAB_APPEND( argc, argv, strdup(psz_arg) );
794     }
795
796     if( i_args ) *i_args = argc;
797     free( psz_orig );
798     return argv;
799 }