5935533ceebe7ad3347ffc93231b6155231780fb
[samba.git] / source / lib / util_str.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26 static char *last_ptr=NULL;
27
28 void set_first_token(char *ptr)
29 {
30         last_ptr = ptr;
31 }
32
33 /****************************************************************************
34   Get the next token from a string, return False if none found
35   handles double-quotes. 
36 Based on a routine by GJC@VILLAGE.COM. 
37 Extensively modified by Andrew.Tridgell@anu.edu.au
38 ****************************************************************************/
39 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
40 {
41   char *s;
42   BOOL quoted;
43   size_t len=1;
44
45   if (!ptr) ptr = &last_ptr;
46   if (!ptr) return(False);
47
48   s = *ptr;
49
50   /* default to simple separators */
51   if (!sep) sep = " \t\n\r";
52
53   /* find the first non sep char */
54   while(*s && strchr(sep,*s)) s++;
55
56   /* nothing left? */
57   if (! *s) return(False);
58
59   /* copy over the token */
60   for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++)
61     {
62             if (*s == '\"') {
63                     quoted = !quoted;
64             } else {
65                     len++;
66                     *buff++ = *s;
67             }
68     }
69
70   *ptr = (*s) ? s+1 : s;  
71   *buff = 0;
72   last_ptr = *ptr;
73
74   return(True);
75 }
76
77 /****************************************************************************
78 Convert list of tokens to array; dependent on above routine.
79 Uses last_ptr from above - bit of a hack.
80 ****************************************************************************/
81 char **toktocliplist(int *ctok, char *sep)
82 {
83   char *s=last_ptr;
84   int ictok=0;
85   char **ret, **iret;
86
87   if (!sep) sep = " \t\n\r";
88
89   while(*s && strchr(sep,*s)) s++;
90
91   /* nothing left? */
92   if (!*s) return(NULL);
93
94   do {
95     ictok++;
96     while(*s && (!strchr(sep,*s))) s++;
97     while(*s && strchr(sep,*s)) *s++=0;
98   } while(*s);
99
100   *ctok=ictok;
101   s=last_ptr;
102
103   if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
104   
105   while(ictok--) {    
106     *iret++=s;
107     while(*s++);
108     while(!*s) s++;
109   }
110
111   return ret;
112 }
113
114
115 /*******************************************************************
116   case insensitive string compararison
117 ********************************************************************/
118 int StrCaseCmp(const char *s, const char *t)
119 {
120   /* compare until we run out of string, either t or s, or find a difference */
121   /* We *must* use toupper rather than tolower here due to the
122      asynchronous upper to lower mapping.
123    */
124 #if !defined(KANJI_WIN95_COMPATIBILITY)
125   /*
126    * For completeness we should put in equivalent code for code pages
127    * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
128    * doubt anyone wants Samba to behave differently from Win95 and WinNT
129    * here. They both treat full width ascii characters as case senstive
130    * filenames (ie. they don't do the work we do here).
131    * JRA.
132    */
133
134   if(lp_client_code_page() == KANJI_CODEPAGE)
135   {
136     /* Win95 treats full width ascii characters as case sensitive. */
137     int diff;
138     for (;;)
139     {
140       if (!*s || !*t)
141             return toupper (*s) - toupper (*t);
142       else if (is_sj_alph (*s) && is_sj_alph (*t))
143       {
144         diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
145         if (diff)
146           return diff;
147         s += 2;
148         t += 2;
149       }
150       else if (is_shift_jis (*s) && is_shift_jis (*t))
151       {
152         diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
153         if (diff)
154           return diff;
155         diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
156         if (diff)
157           return diff;
158         s += 2;
159         t += 2;
160       }
161       else if (is_shift_jis (*s))
162         return 1;
163       else if (is_shift_jis (*t))
164         return -1;
165       else 
166       {
167         diff = toupper (*s) - toupper (*t);
168         if (diff)
169           return diff;
170         s++;
171         t++;
172       }
173     }
174   }
175   else
176 #endif /* KANJI_WIN95_COMPATIBILITY */
177   {
178     while (*s && *t && toupper(*s) == toupper(*t))
179     {
180       s++;
181       t++;
182     }
183
184     return(toupper(*s) - toupper(*t));
185   }
186 }
187
188 /*******************************************************************
189   case insensitive string compararison, length limited
190 ********************************************************************/
191 int StrnCaseCmp(const char *s, const char *t, size_t n)
192 {
193   /* compare until we run out of string, either t or s, or chars */
194   /* We *must* use toupper rather than tolower here due to the
195      asynchronous upper to lower mapping.
196    */
197 #if !defined(KANJI_WIN95_COMPATIBILITY)
198   /*
199    * For completeness we should put in equivalent code for code pages
200    * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
201    * doubt anyone wants Samba to behave differently from Win95 and WinNT
202    * here. They both treat full width ascii characters as case senstive
203    * filenames (ie. they don't do the work we do here).
204    * JRA. 
205    */
206
207   if(lp_client_code_page() == KANJI_CODEPAGE)
208   {
209     /* Win95 treats full width ascii characters as case sensitive. */
210     int diff;
211     for (;n > 0;)
212     {
213       if (!*s || !*t)
214         return toupper (*s) - toupper (*t);
215       else if (is_sj_alph (*s) && is_sj_alph (*t))
216       {
217         diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
218         if (diff)
219           return diff;
220         s += 2;
221         t += 2;
222         n -= 2;
223       }
224       else if (is_shift_jis (*s) && is_shift_jis (*t))
225       {
226         diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
227         if (diff)
228           return diff;
229         diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
230         if (diff)
231           return diff;
232         s += 2;
233         t += 2;
234         n -= 2;
235       }
236       else if (is_shift_jis (*s))
237         return 1;
238       else if (is_shift_jis (*t))
239         return -1;
240       else 
241       {
242         diff = toupper (*s) - toupper (*t);
243         if (diff)
244           return diff;
245         s++;
246         t++;
247         n--;
248       }
249     }
250     return 0;
251   }
252   else
253 #endif /* KANJI_WIN95_COMPATIBILITY */
254   {
255     while (n && *s && *t && toupper(*s) == toupper(*t))
256     {
257       s++;
258       t++;
259       n--;
260     }
261
262     /* not run out of chars - strings are different lengths */
263     if (n) 
264       return(toupper(*s) - toupper(*t));
265
266     /* identical up to where we run out of chars, 
267        and strings are same length */
268     return(0);
269   }
270 }
271
272 /*******************************************************************
273   compare 2 strings 
274 ********************************************************************/
275 BOOL strequal(const char *s1, const char *s2)
276 {
277   if (s1 == s2) return(True);
278   if (!s1 || !s2) return(False);
279   
280   return(StrCaseCmp(s1,s2)==0);
281 }
282
283 /*******************************************************************
284   compare 2 strings up to and including the nth char.
285   ******************************************************************/
286 BOOL strnequal(const char *s1,const char *s2,size_t n)
287 {
288   if (s1 == s2) return(True);
289   if (!s1 || !s2 || !n) return(False);
290   
291   return(StrnCaseCmp(s1,s2,n)==0);
292 }
293
294 /*******************************************************************
295   compare 2 strings (case sensitive)
296 ********************************************************************/
297 BOOL strcsequal(const char *s1,const char *s2)
298 {
299   if (s1 == s2) return(True);
300   if (!s1 || !s2) return(False);
301   
302   return(strcmp(s1,s2)==0);
303 }
304
305
306 /*******************************************************************
307   convert a string to lower case
308 ********************************************************************/
309 void strlower(char *s)
310 {
311   while (*s)
312   {
313 #if !defined(KANJI_WIN95_COMPATIBILITY)
314   /*
315    * For completeness we should put in equivalent code for code pages
316    * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
317    * doubt anyone wants Samba to behave differently from Win95 and WinNT
318    * here. They both treat full width ascii characters as case senstive
319    * filenames (ie. they don't do the work we do here).
320    * JRA. 
321    */
322
323     if(lp_client_code_page() == KANJI_CODEPAGE)
324     {
325       /* Win95 treats full width ascii characters as case sensitive. */
326       if (is_shift_jis (*s))
327       {
328         if (is_sj_upper (s[0], s[1]))
329           s[1] = sj_tolower2 (s[1]);
330         s += 2;
331       }
332       else if (is_kana (*s))
333       {
334         s++;
335       }
336       else
337       {
338         if (isupper(*s))
339           *s = tolower(*s);
340         s++;
341       }
342     }
343     else
344 #endif /* KANJI_WIN95_COMPATIBILITY */
345     {
346       size_t skip = skip_multibyte_char( *s );
347       if( skip != 0 )
348         s += skip;
349       else
350       {
351         if (isupper(*s))
352           *s = tolower(*s);
353         s++;
354       }
355     }
356   }
357 }
358
359 /*******************************************************************
360   convert a string to upper case
361 ********************************************************************/
362 void strupper(char *s)
363 {
364   while (*s)
365   {
366 #if !defined(KANJI_WIN95_COMPATIBILITY)
367   /*
368    * For completeness we should put in equivalent code for code pages
369    * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
370    * doubt anyone wants Samba to behave differently from Win95 and WinNT
371    * here. They both treat full width ascii characters as case senstive
372    * filenames (ie. they don't do the work we do here).
373    * JRA. 
374    */
375
376     if(lp_client_code_page() == KANJI_CODEPAGE)
377     {
378       /* Win95 treats full width ascii characters as case sensitive. */
379       if (is_shift_jis (*s))
380       {
381         if (is_sj_lower (s[0], s[1]))
382           s[1] = sj_toupper2 (s[1]);
383         s += 2;
384       }
385       else if (is_kana (*s))
386       {
387         s++;
388       }
389       else
390       {
391         if (islower(*s))
392           *s = toupper(*s);
393         s++;
394       }
395     }
396     else
397 #endif /* KANJI_WIN95_COMPATIBILITY */
398     {
399       size_t skip = skip_multibyte_char( *s );
400       if( skip != 0 )
401         s += skip;
402       else
403       {
404         if (islower(*s))
405           *s = toupper(*s);
406         s++;
407       }
408     }
409   }
410 }
411
412 /*******************************************************************
413   convert a string to "normal" form
414 ********************************************************************/
415 void strnorm(char *s)
416 {
417   extern int case_default;
418   if (case_default == CASE_UPPER)
419     strupper(s);
420   else
421     strlower(s);
422 }
423
424 /*******************************************************************
425 check if a string is in "normal" case
426 ********************************************************************/
427 BOOL strisnormal(char *s)
428 {
429   extern int case_default;
430   if (case_default == CASE_UPPER)
431     return(!strhaslower(s));
432
433   return(!strhasupper(s));
434 }
435
436
437 /****************************************************************************
438   string replace
439 ****************************************************************************/
440 void string_replace(char *s,char oldc,char newc)
441 {
442   size_t skip;
443   while (*s)
444   {
445     skip = skip_multibyte_char( *s );
446     if( skip != 0 )
447       s += skip;
448     else
449     {
450       if (oldc == *s)
451         *s = newc;
452       s++;
453     }
454   }
455 }
456
457
458 /*******************************************************************
459 skip past some strings in a buffer
460 ********************************************************************/
461 char *skip_string(const char *buf,size_t n)
462 {
463   while (n--)
464     buf += strlen(buf) + 1;
465   return((char *)buf);
466 }
467
468 /*******************************************************************
469  Count the number of characters in a string. Normally this will
470  be the same as the number of bytes in a string for single byte strings,
471  but will be different for multibyte.
472  16.oct.98, jdblair@cobaltnet.com.
473 ********************************************************************/
474
475 size_t str_charnum(const char *s)
476 {
477   size_t len = 0;
478   
479   while (*s != '\0') {
480     int skip = skip_multibyte_char(*s);
481     s += (skip ? skip : 1);
482     len++;
483   }
484   return len;
485 }
486
487 /*******************************************************************
488 trim the specified elements off the front and back of a string
489 ********************************************************************/
490
491 BOOL trim_string(char *s,const char *front,const char *back)
492 {
493   BOOL ret = False;
494   size_t front_len = (front && *front) ? strlen(front) : 0;
495   size_t back_len = (back && *back) ? strlen(back) : 0;
496   size_t s_len;
497
498   while (front_len && strncmp(s, front, front_len) == 0)
499   {
500     char *p = s;
501     ret = True;
502     while (1)
503     {
504       if (!(*p = p[front_len]))
505         break;
506       p++;
507     }
508   }
509
510   /*
511    * We split out the multibyte code page
512    * case here for speed purposes. Under a
513    * multibyte code page we need to walk the
514    * string forwards only and multiple times.
515    * Thanks to John Blair for finding this
516    * one. JRA.
517    */
518
519   if(back_len)
520   {
521     if(!is_multibyte_codepage())
522     {
523       s_len = strlen(s);
524       while ((s_len >= back_len) && 
525              (strncmp(s + s_len - back_len, back, back_len)==0))  
526       {
527         ret = True;
528         s[s_len - back_len] = '\0';
529         s_len = strlen(s);
530       }
531     }
532     else
533     {
534
535       /*
536        * Multibyte code page case.
537        * Keep going through the string, trying
538        * to match the 'back' string with the end
539        * of the string. If we get a match, truncate
540        * 'back' off the end of the string and
541        * go through the string again from the
542        * start. Keep doing this until we have
543        * gone through the string with no match
544        * at the string end.
545        */
546
547       size_t mb_back_len = str_charnum(back);
548       size_t mb_s_len = str_charnum(s);
549
550       while(mb_s_len >= mb_back_len)
551       {
552         size_t charcount = 0;
553         char *mbp = s;
554
555         while(charcount < (mb_s_len - mb_back_len))
556         {
557           size_t skip = skip_multibyte_char(*mbp);
558           mbp += (skip ? skip : 1);
559           charcount++;
560         }
561
562         /*
563          * mbp now points at mb_back_len multibyte
564          * characters from the end of s.
565          */
566
567         if(strcmp(mbp, back) == 0)
568         {
569           ret = True;
570           *mbp = '\0';
571           mb_s_len = str_charnum(s);
572           mbp = s;
573         }
574         else
575           break;
576       } /* end while mb_s_len... */
577     } /* end else .. */
578   } /* end if back_len .. */
579
580   return(ret);
581 }
582
583
584 /****************************************************************************
585 does a string have any uppercase chars in it?
586 ****************************************************************************/
587 BOOL strhasupper(const char *s)
588 {
589   while (*s) 
590   {
591 #if !defined(KANJI_WIN95_COMPATIBILITY)
592   /*
593    * For completeness we should put in equivalent code for code pages
594    * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
595    * doubt anyone wants Samba to behave differently from Win95 and WinNT
596    * here. They both treat full width ascii characters as case senstive
597    * filenames (ie. they don't do the work we do here).
598    * JRA. 
599    */
600
601     if(lp_client_code_page() == KANJI_CODEPAGE)
602     {
603       /* Win95 treats full width ascii characters as case sensitive. */
604       if (is_shift_jis (*s))
605         s += 2;
606       else if (is_kana (*s))
607         s++;
608       else
609       {
610         if (isupper(*s))
611           return(True);
612         s++;
613       }
614     }
615     else
616 #endif /* KANJI_WIN95_COMPATIBILITY */
617     {
618       size_t skip = skip_multibyte_char( *s );
619       if( skip != 0 )
620         s += skip;
621       else {
622         if (isupper(*s))
623           return(True);
624         s++;
625       }
626     }
627   }
628   return(False);
629 }
630
631 /****************************************************************************
632 does a string have any lowercase chars in it?
633 ****************************************************************************/
634 BOOL strhaslower(const char *s)
635 {
636   while (*s) 
637   {
638 #if !defined(KANJI_WIN95_COMPATIBILITY)
639   /*
640    * For completeness we should put in equivalent code for code pages
641    * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
642    * doubt anyone wants Samba to behave differently from Win95 and WinNT
643    * here. They both treat full width ascii characters as case senstive
644    * filenames (ie. they don't do the work we do here).
645    * JRA. 
646    */
647
648     if(lp_client_code_page() == KANJI_CODEPAGE)
649     {
650       /* Win95 treats full width ascii characters as case sensitive. */
651       if (is_shift_jis (*s))
652       {
653         if (is_sj_upper (s[0], s[1]))
654           return(True);
655         if (is_sj_lower (s[0], s[1]))
656           return (True);
657         s += 2;
658       }
659       else if (is_kana (*s))
660       {
661         s++;
662       }
663       else
664       {
665         if (islower(*s))
666           return(True);
667         s++;
668       }
669     }
670     else
671 #endif /* KANJI_WIN95_COMPATIBILITY */
672     {
673       size_t skip = skip_multibyte_char( *s );
674       if( skip != 0 )
675         s += skip;
676       else {
677         if (islower(*s))
678           return(True);
679         s++;
680       }
681     }
682   }
683   return(False);
684 }
685
686 /****************************************************************************
687 find the number of chars in a string
688 ****************************************************************************/
689 size_t count_chars(const char *s,char c)
690 {
691   size_t count=0;
692
693 #if !defined(KANJI_WIN95_COMPATIBILITY)
694   /*
695    * For completeness we should put in equivalent code for code pages
696    * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
697    * doubt anyone wants Samba to behave differently from Win95 and WinNT
698    * here. They both treat full width ascii characters as case senstive
699    * filenames (ie. they don't do the work we do here).
700    * JRA. 
701    */
702
703   if(lp_client_code_page() == KANJI_CODEPAGE)
704   {
705     /* Win95 treats full width ascii characters as case sensitive. */
706     while (*s) 
707     {
708       if (is_shift_jis (*s))
709         s += 2;
710       else 
711       {
712         if (*s == c)
713           count++;
714         s++;
715       }
716     }
717   }
718   else
719 #endif /* KANJI_WIN95_COMPATIBILITY */
720   {
721     while (*s) 
722     {
723       size_t skip = skip_multibyte_char( *s );
724       if( skip != 0 )
725         s += skip;
726       else {
727         if (*s == c)
728           count++;
729         s++;
730       }
731     }
732   }
733   return(count);
734 }
735
736
737
738 /*******************************************************************
739 safe string copy into a known length string. maxlength does not
740 include the terminating zero.
741 ********************************************************************/
742 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
743 {
744     size_t len;
745
746     if (!dest) {
747         DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
748         return NULL;
749     }
750
751     if (!src) {
752         *dest = 0;
753         return dest;
754     }  
755
756     len = strlen(src);
757
758     if (len > maxlength) {
759             DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
760                      len-maxlength, src));
761             len = maxlength;
762     }
763       
764     memcpy(dest, src, len);
765     dest[len] = 0;
766     return dest;
767 }  
768
769 /*******************************************************************
770 safe string cat into a string. maxlength does not
771 include the terminating zero.
772 ********************************************************************/
773 char *safe_strcat(char *dest, const char *src, size_t maxlength)
774 {
775     size_t src_len, dest_len;
776
777     if (!dest) {
778         DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
779         return NULL;
780     }
781
782     if (!src) {
783         return dest;
784     }  
785
786     src_len = strlen(src);
787     dest_len = strlen(dest);
788
789     if (src_len + dest_len > maxlength) {
790             DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
791                      src_len + dest_len - maxlength, src));
792             src_len = maxlength - dest_len;
793     }
794       
795     memcpy(&dest[dest_len], src, src_len);
796     dest[dest_len + src_len] = 0;
797     return dest;
798 }
799
800 /****************************************************************************
801 this is a safer strcpy(), meant to prevent core dumps when nasty things happen
802 ****************************************************************************/
803 char *StrCpy(char *dest,const char *src)
804 {
805   char *d = dest;
806
807   /* I don't want to get lazy with these ... */
808   SMB_ASSERT(dest && src);
809
810   if (!dest) return(NULL);
811   if (!src) {
812     *dest = 0;
813     return(dest);
814   }
815   while ((*d++ = *src++)) ;
816   return(dest);
817 }
818
819 /****************************************************************************
820 like strncpy but always null terminates. Make sure there is room!
821 ****************************************************************************/
822 char *StrnCpy(char *dest,const char *src,size_t n)
823 {
824   char *d = dest;
825   if (!dest) return(NULL);
826   if (!src) {
827     *dest = 0;
828     return(dest);
829   }
830   while (n-- && (*d++ = *src++)) ;
831   *d = 0;
832   return(dest);
833 }
834
835
836 /****************************************************************************
837 like strncpy but copies up to the character marker.  always null terminates.
838 returns a pointer to the character marker in the source string (src).
839 ****************************************************************************/
840 char *strncpyn(char *dest, const char *src,size_t n, char c)
841 {
842         char *p;
843         size_t str_len;
844
845         p = strchr(src, c);
846         if (p == NULL)
847         {
848                 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
849                 return NULL;
850         }
851
852         str_len = PTR_DIFF(p, src);
853         strncpy(dest, src, MIN(n, str_len));
854         dest[str_len] = '\0';
855
856         return p;
857 }
858
859
860 /*************************************************************
861  Routine to get hex characters and turn them into a 16 byte array.
862  the array can be variable length, and any non-hex-numeric
863  characters are skipped.  "0xnn" or "0Xnn" is specially catered
864  for.
865
866  valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
867
868 **************************************************************/
869 size_t strhex_to_str(char *p, size_t len, const char *strhex)
870 {
871         size_t i;
872         size_t num_chars = 0;
873         unsigned char   lonybble, hinybble;
874         char           *hexchars = "0123456789ABCDEF";
875         char           *p1 = NULL, *p2 = NULL;
876
877         for (i = 0; i < len && strhex[i] != 0; i++)
878         {
879                 if (strnequal(hexchars, "0x", 2))
880                 {
881                         i++; /* skip two chars */
882                         continue;
883                 }
884
885                 while (!(p1 = strchr(hexchars, toupper(strhex[i]))))
886                 {
887                         continue;
888                 }
889
890                 i++; /* next hex digit */
891
892                 while (!(p2 = strchr(hexchars, toupper(strhex[i]))))
893                 {
894                         continue;
895                 }
896
897                 /* get the two nybbles */
898                 hinybble = PTR_DIFF(p1, hexchars);
899                 lonybble = PTR_DIFF(p2, hexchars);
900
901                 p[num_chars] = (hinybble << 4) | lonybble;
902                 num_chars++;
903
904                 p1 = NULL;
905                 p2 = NULL;
906         }
907         return num_chars;
908 }
909
910 /****************************************************************************
911 check if a string is part of a list
912 ****************************************************************************/
913 BOOL in_list(char *s,char *list,BOOL casesensitive)
914 {
915   pstring tok;
916   char *p=list;
917
918   if (!list) return(False);
919
920   while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
921     if (casesensitive) {
922       if (strcmp(tok,s) == 0)
923         return(True);
924     } else {
925       if (StrCaseCmp(tok,s) == 0)
926         return(True);
927     }
928   }
929   return(False);
930 }
931
932 /* this is used to prevent lots of mallocs of size 1 */
933 static char *null_string = NULL;
934
935 /****************************************************************************
936 set a string value, allocing the space for the string
937 ****************************************************************************/
938 BOOL string_init(char **dest,const char *src)
939 {
940   size_t l;
941   if (!src)     
942     src = "";
943
944   l = strlen(src);
945
946   if (l == 0)
947     {
948       if (!null_string) {
949         if((null_string = (char *)malloc(1)) == NULL) {
950           DEBUG(0,("string_init: malloc fail for null_string.\n"));
951           return False;
952         }
953         *null_string = 0;
954       }
955       *dest = null_string;
956     }
957   else
958     {
959       (*dest) = (char *)malloc(l+1);
960       if ((*dest) == NULL) {
961               DEBUG(0,("Out of memory in string_init\n"));
962               return False;
963       }
964
965       pstrcpy(*dest,src);
966     }
967   return(True);
968 }
969
970 /****************************************************************************
971 free a string value
972 ****************************************************************************/
973 void string_free(char **s)
974 {
975   if (!s || !(*s)) return;
976   if (*s == null_string)
977     *s = NULL;
978   if (*s) free(*s);
979   *s = NULL;
980 }
981
982 /****************************************************************************
983 set a string value, allocing the space for the string, and deallocating any 
984 existing space
985 ****************************************************************************/
986 BOOL string_set(char **dest,const char *src)
987 {
988   string_free(dest);
989
990   return(string_init(dest,src));
991 }
992
993
994 /****************************************************************************
995 substitute a string for a pattern in another string. Make sure there is 
996 enough room!
997
998 This routine looks for pattern in s and replaces it with 
999 insert. It may do multiple replacements.
1000
1001 any of " ; ' or ` in the insert string are replaced with _
1002 ****************************************************************************/
1003 void string_sub(char *s,const char *pattern,const char *insert)
1004 {
1005         char *p;
1006         size_t ls,lp,li, i;
1007
1008         if (!insert || !pattern || !s) return;
1009
1010         ls = strlen(s);
1011         lp = strlen(pattern);
1012         li = strlen(insert);
1013
1014         if (!*pattern) return;
1015         
1016         while (lp <= ls && (p = strstr(s,pattern))) {
1017                 memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
1018                 for (i=0;i<li;i++) {
1019                         switch (insert[i]) {
1020                         case '`':
1021                         case '"':
1022                         case '\'':
1023                         case ';':
1024                                 p[i] = '_';
1025                                 break;
1026                         default:
1027                                 p[i] = insert[i];
1028                         }
1029                 }
1030                 s = p + li;
1031                 ls += (li-lp);
1032         }
1033 }
1034
1035
1036 /****************************************************************************
1037 similar to string_sub() but allows for any character to be substituted. 
1038 Use with caution!
1039 ****************************************************************************/
1040 void all_string_sub(char *s,const char *pattern,const char *insert)
1041 {
1042         char *p;
1043         size_t ls,lp,li, i;
1044
1045         if (!insert || !pattern || !s) return;
1046
1047         ls = strlen(s);
1048         lp = strlen(pattern);
1049         li = strlen(insert);
1050
1051         if (!*pattern) return;
1052         
1053         while (lp <= ls && (p = strstr(s,pattern))) {
1054                 memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
1055                 memcpy(p, insert, li);
1056                 s = p + li;
1057                 ls += (li-lp);
1058         }
1059 }
1060
1061 /****************************************************************************
1062  splits out the front and back at a separator.
1063 ****************************************************************************/
1064 void split_at_last_component(char *path, char *front, char sep, char *back)
1065 {
1066         char *p = strrchr(path, sep);
1067
1068         if (p != NULL)
1069         {
1070                 *p = 0;
1071         }
1072         if (front != NULL)
1073         {
1074                 pstrcpy(front, path);
1075         }
1076         if (p != NULL)
1077         {
1078                 if (back != NULL)
1079                 {
1080                         pstrcpy(back, p+1);
1081                 }
1082                 *p = '\\';
1083         }
1084         else
1085         {
1086                 if (back != NULL)
1087                 {
1088                         back[0] = 0;
1089                 }
1090         }
1091 }