fixes for unpakcaged files and removeing *.old (from Eirik Thorsnes)
[Samba.git] / source / lib / util_str.c
blobbe1588d1e4bc6376d74519f155ca7fcfe855105e
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
22 #include "includes.h"
24 static const char *last_ptr=NULL;
26 void set_first_token(char *ptr)
28 last_ptr = ptr;
31 /****************************************************************************
32 Get the next token from a string, return False if none found
33 handles double-quotes.
34 Based on a routine by GJC@VILLAGE.COM.
35 Extensively modified by Andrew.Tridgell@anu.edu.au
36 ****************************************************************************/
38 BOOL next_token(const char **ptr,char *buff,const char *sep, size_t bufsize)
40 const char *s;
41 BOOL quoted;
42 size_t len=1;
44 if (!ptr)
45 ptr = &last_ptr;
46 if (!ptr)
47 return(False);
49 s = *ptr;
51 /* default to simple separators */
52 if (!sep)
53 sep = " \t\n\r";
55 /* find the first non sep char */
56 while(*s && strchr(sep,*s))
57 s++;
59 /* nothing left? */
60 if (! *s)
61 return(False);
63 /* copy over the token */
64 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) {
65 if (*s == '\"') {
66 quoted = !quoted;
67 } else {
68 len++;
69 *buff++ = *s;
73 *ptr = (*s) ? s+1 : s;
74 *buff = 0;
75 last_ptr = *ptr;
77 return(True);
80 /****************************************************************************
81 Convert list of tokens to array; dependent on above routine.
82 Uses last_ptr from above - bit of a hack.
83 ****************************************************************************/
84 char **toktocliplist(int *ctok, const char *sep)
86 char *s= (char *)last_ptr;
87 int ictok=0;
88 char **ret, **iret;
90 if (!sep) sep = " \t\n\r";
92 while(*s && strchr(sep,*s)) s++;
94 /* nothing left? */
95 if (!*s) return(NULL);
97 do {
98 ictok++;
99 while(*s && (!strchr(sep,*s))) s++;
100 while(*s && strchr(sep,*s)) *s++=0;
101 } while(*s);
103 *ctok=ictok;
104 s=last_ptr;
106 if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
108 while(ictok--) {
109 *iret++=s;
110 while(*s++);
111 while(!*s) s++;
114 return ret;
118 /*******************************************************************
119 case insensitive string compararison
120 ********************************************************************/
121 int StrCaseCmp(const char *s, const char *t)
123 /* compare until we run out of string, either t or s, or find a difference */
124 /* We *must* use toupper rather than tolower here due to the
125 asynchronous upper to lower mapping.
127 #if !defined(KANJI_WIN95_COMPATIBILITY)
129 * For completeness we should put in equivalent code for code pages
130 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
131 * doubt anyone wants Samba to behave differently from Win95 and WinNT
132 * here. They both treat full width ascii characters as case senstive
133 * filenames (ie. they don't do the work we do here).
134 * JRA.
137 if(lp_client_code_page() == KANJI_CODEPAGE)
139 /* Win95 treats full width ascii characters as case sensitive. */
140 int diff;
141 for (;;)
143 if (!*s || !*t)
144 return toupper (*s) - toupper (*t);
145 else if (is_sj_alph (*s) && is_sj_alph (*t))
147 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
148 if (diff)
149 return diff;
150 s += 2;
151 t += 2;
153 else if (is_shift_jis (*s) && is_shift_jis (*t))
155 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
156 if (diff)
157 return diff;
158 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
159 if (diff)
160 return diff;
161 s += 2;
162 t += 2;
164 else if (is_shift_jis (*s))
165 return 1;
166 else if (is_shift_jis (*t))
167 return -1;
168 else
170 diff = toupper (*s) - toupper (*t);
171 if (diff)
172 return diff;
173 s++;
174 t++;
178 else
179 #endif /* KANJI_WIN95_COMPATIBILITY */
181 while (*s && *t && toupper(*s) == toupper(*t))
183 s++;
184 t++;
187 return(toupper(*s) - toupper(*t));
191 /*******************************************************************
192 case insensitive string compararison, length limited
193 ********************************************************************/
194 int StrnCaseCmp(const char *s, const char *t, size_t n)
196 /* compare until we run out of string, either t or s, or chars */
197 /* We *must* use toupper rather than tolower here due to the
198 asynchronous upper to lower mapping.
200 #if !defined(KANJI_WIN95_COMPATIBILITY)
202 * For completeness we should put in equivalent code for code pages
203 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
204 * doubt anyone wants Samba to behave differently from Win95 and WinNT
205 * here. They both treat full width ascii characters as case senstive
206 * filenames (ie. they don't do the work we do here).
207 * JRA.
210 if(lp_client_code_page() == KANJI_CODEPAGE)
212 /* Win95 treats full width ascii characters as case sensitive. */
213 int diff;
214 for (;n > 0;)
216 if (!*s || !*t)
217 return toupper (*s) - toupper (*t);
218 else if (is_sj_alph (*s) && is_sj_alph (*t))
220 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
221 if (diff)
222 return diff;
223 s += 2;
224 t += 2;
225 n -= 2;
227 else if (is_shift_jis (*s) && is_shift_jis (*t))
229 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
230 if (diff)
231 return diff;
232 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
233 if (diff)
234 return diff;
235 s += 2;
236 t += 2;
237 n -= 2;
239 else if (is_shift_jis (*s))
240 return 1;
241 else if (is_shift_jis (*t))
242 return -1;
243 else
245 diff = toupper (*s) - toupper (*t);
246 if (diff)
247 return diff;
248 s++;
249 t++;
250 n--;
253 return 0;
255 else
256 #endif /* KANJI_WIN95_COMPATIBILITY */
258 while (n && *s && *t && toupper(*s) == toupper(*t))
260 s++;
261 t++;
262 n--;
265 /* not run out of chars - strings are different lengths */
266 if (n)
267 return(toupper(*s) - toupper(*t));
269 /* identical up to where we run out of chars,
270 and strings are same length */
271 return(0);
275 /*******************************************************************
276 compare 2 strings - DOS codepage.
277 ********************************************************************/
278 BOOL strequal(const char *s1, const char *s2)
280 if (s1 == s2) return(True);
281 if (!s1 || !s2) return(False);
283 return(StrCaseCmp(s1,s2)==0);
286 /*******************************************************************
287 compare 2 strings - UNIX codepage.
288 ********************************************************************/
289 BOOL strequal_unix(const char *s1, const char *s2)
291 pstring dos_s1, dos_s2;
292 if (s1 == s2) return(True);
293 if (!s1 || !s2) return(False);
295 pstrcpy(dos_s1, unix_to_dos_static(s1));
296 pstrcpy(dos_s2, unix_to_dos_static(s2));
297 return(StrCaseCmp(dos_s1,dos_s2)==0);
300 /*******************************************************************
301 compare 2 strings up to and including the nth char.
302 ******************************************************************/
303 BOOL strnequal(const char *s1,const char *s2,size_t n)
305 if (s1 == s2) return(True);
306 if (!s1 || !s2 || !n) return(False);
308 return(StrnCaseCmp(s1,s2,n)==0);
311 /*******************************************************************
312 compare 2 strings (case sensitive)
313 ********************************************************************/
314 BOOL strcsequal(const char *s1,const char *s2)
316 if (s1 == s2) return(True);
317 if (!s1 || !s2) return(False);
319 return(strcmp(s1,s2)==0);
322 /***************************************************************************
323 Do a case-insensitive, whitespace-ignoring string compare.
324 ***************************************************************************/
325 int strwicmp(const char *psz1, const char *psz2)
327 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
328 /* appropriate value. */
329 if (psz1 == psz2)
330 return (0);
331 else if (psz1 == NULL)
332 return (-1);
333 else if (psz2 == NULL)
334 return (1);
336 /* sync the strings on first non-whitespace */
337 while (1)
339 while (isspace((int)*psz1))
340 psz1++;
341 while (isspace((int)*psz2))
342 psz2++;
343 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
344 || *psz2 == '\0')
345 break;
346 psz1++;
347 psz2++;
349 return (*psz1 - *psz2);
353 /*******************************************************************
354 convert a string to lower case
355 ********************************************************************/
356 void strlower(char *s)
358 while (*s)
360 #if !defined(KANJI_WIN95_COMPATIBILITY)
362 * For completeness we should put in equivalent code for code pages
363 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
364 * doubt anyone wants Samba to behave differently from Win95 and WinNT
365 * here. They both treat full width ascii characters as case senstive
366 * filenames (ie. they don't do the work we do here).
367 * JRA.
370 if(lp_client_code_page() == KANJI_CODEPAGE)
372 /* Win95 treats full width ascii characters as case sensitive. */
373 if (is_shift_jis (*s))
375 if (is_sj_upper (s[0], s[1]))
376 s[1] = sj_tolower2 (s[1]);
377 s += 2;
379 else if (is_kana (*s))
381 s++;
383 else
385 if (isupper(*s))
386 *s = tolower(*s);
387 s++;
390 else
391 #endif /* KANJI_WIN95_COMPATIBILITY */
393 size_t skip = get_character_len( *s );
394 if( skip != 0 )
395 s += skip;
396 else
398 if (isupper(*s))
399 *s = tolower(*s);
400 s++;
406 /*******************************************************************
407 convert a string to upper case
408 ********************************************************************/
409 void strupper(char *s)
411 while (*s)
413 #if !defined(KANJI_WIN95_COMPATIBILITY)
415 * For completeness we should put in equivalent code for code pages
416 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
417 * doubt anyone wants Samba to behave differently from Win95 and WinNT
418 * here. They both treat full width ascii characters as case senstive
419 * filenames (ie. they don't do the work we do here).
420 * JRA.
423 if(lp_client_code_page() == KANJI_CODEPAGE)
425 /* Win95 treats full width ascii characters as case sensitive. */
426 if (is_shift_jis (*s))
428 if (is_sj_lower (s[0], s[1]))
429 s[1] = sj_toupper2 (s[1]);
430 s += 2;
432 else if (is_kana (*s))
434 s++;
436 else
438 if (islower(*s))
439 *s = toupper(*s);
440 s++;
443 else
444 #endif /* KANJI_WIN95_COMPATIBILITY */
446 size_t skip = get_character_len( *s );
447 if( skip != 0 )
448 s += skip;
449 else
451 if (islower(*s))
452 *s = toupper(*s);
453 s++;
459 /* Convert a string to upper case, but don't modify it */
461 char *strupper_static(const char *s)
463 static pstring str;
465 pstrcpy(str, s);
466 strupper(str);
468 return str;
471 /*******************************************************************
472 convert a string to "normal" form
473 ********************************************************************/
474 void strnorm(char *s)
476 extern int case_default;
477 if (case_default == CASE_UPPER)
478 strupper(s);
479 else
480 strlower(s);
483 /*******************************************************************
484 check if a string is in "normal" case
485 ********************************************************************/
486 BOOL strisnormal(char *s)
488 extern int case_default;
489 if (case_default == CASE_UPPER)
490 return(!strhaslower(s));
492 return(!strhasupper(s));
496 /****************************************************************************
497 string replace
498 ****************************************************************************/
499 void string_replace(char *s,char oldc,char newc)
501 size_t skip;
504 * sbcs optimization.
506 if(!global_is_multibyte_codepage) {
507 while (*s) {
508 if (oldc == *s)
509 *s = newc;
510 s++;
512 } else {
513 while (*s)
515 skip = get_character_len( *s );
516 if( skip != 0 )
517 s += skip;
518 else
520 if (oldc == *s)
521 *s = newc;
522 s++;
529 /*******************************************************************
530 skip past some strings in a buffer
531 ********************************************************************/
532 char *skip_string(char *buf,size_t n)
534 while (n--)
535 buf += strlen(buf) + 1;
536 return(buf);
539 /*******************************************************************
540 Count the number of characters in a string. Normally this will
541 be the same as the number of bytes in a string for single byte strings,
542 but will be different for multibyte.
543 16.oct.98, jdblair@cobaltnet.com.
544 ********************************************************************/
546 size_t str_charnum(const char *s)
548 size_t len = 0;
551 * sbcs optimization.
553 if(!global_is_multibyte_codepage) {
554 return strlen(s);
555 } else {
556 while (*s != '\0') {
557 int skip = get_character_len(*s);
558 s += (skip ? skip : 1);
559 len++;
562 return len;
565 /*******************************************************************
566 trim the specified elements off the front and back of a string
567 ********************************************************************/
569 BOOL trim_string(char *s,const char *front,const char *back)
571 BOOL ret = False;
572 size_t s_len;
573 size_t front_len;
574 size_t back_len;
575 char *sP;
577 /* Ignore null or empty strings. */
579 if ( !s || (s[0] == '\0'))
580 return False;
582 sP = s;
583 s_len = strlen( s ) + 1;
584 front_len = (front) ? strlen( front ) + 1 : 0;
585 back_len = (back) ? strlen( back ) + 1 : 0;
588 * remove "front" string from given "s", if it matches front part,
589 * repeatedly.
591 if ( front && front_len > 1 ) {
592 while (( s_len >= front_len )&&
593 ( memcmp( sP, front, front_len - 1 )) == 0 ) {
594 ret = True;
595 sP += ( front_len - 1 );
596 s_len -= ( front_len - 1 );
601 * we'll memmove sP to s later, after we're done with
602 * back part removal, for minimizing copy.
607 * We split out the multibyte code page
608 * case here for speed purposes. Under a
609 * multibyte code page we need to walk the
610 * string forwards only and multiple times.
611 * Thanks to John Blair for finding this
612 * one. JRA.
615 * This JRA's comment is partly correct, but partly wrong.
616 * You can always check from "end" part, and if it did not match,
617 * it means there is no possibility of finding one.
618 * If you found matching point, mark them, then look from front
619 * if marking point suits multi-byte string rule.
620 * Kenichi Okuyama.
623 if ( back && back_len > 1 && s_len >= back_len) {
624 char *bP = sP + s_len - back_len;
625 long b_len = s_len;
627 while (( b_len >= back_len )&&
628 ( memcmp( bP, back, back_len - 1 ) == 0 )) {
629 bP -= ( back_len - 1 );
630 b_len -= ( back_len - 1 );
634 * You're here, means you ether have found match multiple times,
635 * or you found none. If you've found match, then bP should be
636 * moving.
638 if ( bP != sP + s_len - back_len ) {
639 bP += ( back_len - 1 ); /* slide bP to first matching point. */
641 if( !global_is_multibyte_codepage ) {
642 /* simply terminate */
643 (*bP) = '\0';
644 s_len = b_len;
645 ret = True;
646 } else {
647 /* trace string from start. */
648 char *cP = sP;
649 while ( cP < sP + s_len - back_len ) {
650 size_t skip;
651 skip = skip_multibyte_char( *cP );
652 cP += ( skip ? skip : 1 );
653 if ( cP == bP ) {
654 /* you found the match */
655 (*bP) = '\0';
656 ret = True;
657 s_len = b_len;
658 break;
660 while (( cP > bP )&&( bP < sP + s_len - back_len )) {
661 bP += ( back_len - 1 );
662 b_len += ( back_len - 1 );
669 /* if front found matching point */
670 if ( sP != s ) {
671 /* slide string to buffer top */
672 memmove( s, sP, s_len );
674 return ret;
678 /****************************************************************************
679 does a string have any uppercase chars in it?
680 ****************************************************************************/
681 BOOL strhasupper(const char *s)
683 while (*s)
685 #if !defined(KANJI_WIN95_COMPATIBILITY)
687 * For completeness we should put in equivalent code for code pages
688 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
689 * doubt anyone wants Samba to behave differently from Win95 and WinNT
690 * here. They both treat full width ascii characters as case senstive
691 * filenames (ie. they don't do the work we do here).
692 * JRA.
695 if(lp_client_code_page() == KANJI_CODEPAGE)
697 /* Win95 treats full width ascii characters as case sensitive. */
698 if (is_shift_jis (*s))
699 s += 2;
700 else if (is_kana (*s))
701 s++;
702 else
704 if (isupper(*s))
705 return(True);
706 s++;
709 else
710 #endif /* KANJI_WIN95_COMPATIBILITY */
712 size_t skip = get_character_len( *s );
713 if( skip != 0 )
714 s += skip;
715 else {
716 if (isupper(*s))
717 return(True);
718 s++;
722 return(False);
725 /****************************************************************************
726 does a string have any lowercase chars in it?
727 ****************************************************************************/
728 BOOL strhaslower(const char *s)
730 while (*s)
732 #if !defined(KANJI_WIN95_COMPATIBILITY)
734 * For completeness we should put in equivalent code for code pages
735 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
736 * doubt anyone wants Samba to behave differently from Win95 and WinNT
737 * here. They both treat full width ascii characters as case senstive
738 * filenames (ie. they don't do the work we do here).
739 * JRA.
742 if(lp_client_code_page() == KANJI_CODEPAGE)
744 /* Win95 treats full width ascii characters as case sensitive. */
745 if (is_shift_jis (*s))
747 if (is_sj_upper (s[0], s[1]))
748 return(True);
749 if (is_sj_lower (s[0], s[1]))
750 return (True);
751 s += 2;
753 else if (is_kana (*s))
755 s++;
757 else
759 if (islower(*s))
760 return(True);
761 s++;
764 else
765 #endif /* KANJI_WIN95_COMPATIBILITY */
767 size_t skip = get_character_len( *s );
768 if( skip != 0 )
769 s += skip;
770 else {
771 if (islower(*s))
772 return(True);
773 s++;
777 return(False);
780 /****************************************************************************
781 find the number of chars in a string
782 ****************************************************************************/
783 size_t count_chars(const char *s,char c)
785 size_t count=0;
787 #if !defined(KANJI_WIN95_COMPATIBILITY)
789 * For completeness we should put in equivalent code for code pages
790 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
791 * doubt anyone wants Samba to behave differently from Win95 and WinNT
792 * here. They both treat full width ascii characters as case senstive
793 * filenames (ie. they don't do the work we do here).
794 * JRA.
797 if(lp_client_code_page() == KANJI_CODEPAGE)
799 /* Win95 treats full width ascii characters as case sensitive. */
800 while (*s)
802 if (is_shift_jis (*s))
803 s += 2;
804 else
806 if (*s == c)
807 count++;
808 s++;
812 else
813 #endif /* KANJI_WIN95_COMPATIBILITY */
815 while (*s)
817 size_t skip = get_character_len( *s );
818 if( skip != 0 )
819 s += skip;
820 else {
821 if (*s == c)
822 count++;
823 s++;
827 return(count);
830 /*******************************************************************
831 Return True if a string consists only of one particular character.
832 ********************************************************************/
834 BOOL str_is_all(const char *s,char c)
836 if(s == NULL)
837 return False;
838 if(!*s)
839 return False;
841 #if !defined(KANJI_WIN95_COMPATIBILITY)
843 * For completeness we should put in equivalent code for code pages
844 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
845 * doubt anyone wants Samba to behave differently from Win95 and WinNT
846 * here. They both treat full width ascii characters as case senstive
847 * filenames (ie. they don't do the work we do here).
848 * JRA.
851 if(lp_client_code_page() == KANJI_CODEPAGE)
853 /* Win95 treats full width ascii characters as case sensitive. */
854 while (*s)
856 if (is_shift_jis (*s))
857 s += 2;
858 else
860 if (*s != c)
861 return False;
862 s++;
866 else
867 #endif /* KANJI_WIN95_COMPATIBILITY */
869 while (*s)
871 size_t skip = get_character_len( *s );
872 if( skip != 0 )
873 s += skip;
874 else {
875 if (*s != c)
876 return False;
877 s++;
881 return True;
884 /*******************************************************************
885 safe string copy into a known length string. maxlength does not
886 include the terminating zero.
887 ********************************************************************/
889 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
891 size_t len;
893 if (!dest) {
894 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
895 return NULL;
898 if (!src) {
899 *dest = 0;
900 return dest;
903 len = strlen(src);
905 if (len > maxlength) {
906 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
907 (int)(len-maxlength), src));
908 len = maxlength;
911 memcpy(dest, src, len);
912 dest[len] = 0;
913 return dest;
916 /*******************************************************************
917 safe string cat into a string. maxlength does not
918 include the terminating zero.
919 ********************************************************************/
921 char *safe_strcat(char *dest, const char *src, size_t maxlength)
923 size_t src_len, dest_len;
925 if (!dest) {
926 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
927 return NULL;
930 if (!src)
931 return dest;
933 src_len = strlen(src);
934 dest_len = strlen(dest);
936 if (src_len + dest_len > maxlength) {
937 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
938 (int)(src_len + dest_len - maxlength), src));
939 if (maxlength > dest_len) {
940 memcpy(&dest[dest_len], src, maxlength - dest_len);
942 dest[maxlength] = 0;
943 return NULL;
946 memcpy(&dest[dest_len], src, src_len);
947 dest[dest_len + src_len] = 0;
948 return dest;
951 /*******************************************************************
952 Paranoid strcpy into a buffer of given length (includes terminating
953 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
954 and replaces with '_'. Deliberately does *NOT* check for multibyte
955 characters. Don't change it !
956 ********************************************************************/
958 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
960 size_t len, i;
961 size_t buflen;
962 smb_ucs2_t *str_ucs, *other_ucs;
964 if (!dest) {
965 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
966 return NULL;
969 if (!src) {
970 *dest = 0;
971 return dest;
974 /* Get UCS2 version of src string*/
976 buflen=2*strlen(src)+2;
977 if (buflen >= (2*maxlength))
978 buflen = 2*(maxlength - 1);
980 str_ucs = (smb_ucs2_t*)malloc(buflen);
981 if(!str_ucs) {
982 *dest=0;
983 return dest;
985 unix_to_unicode(str_ucs, src, buflen);
986 len = strlen_w(str_ucs);
988 if (!other_safe_chars)
989 other_safe_chars = "";
991 /* Get UCS2 version of other_safe_chars string*/
992 buflen=2*strlen(other_safe_chars)+2;
993 other_ucs = (smb_ucs2_t*)malloc(buflen);
994 if(!other_ucs) {
995 *dest=0;
996 SAFE_FREE(str_ucs);
997 return dest;
999 unix_to_unicode(other_ucs, other_safe_chars, buflen);
1001 for(i = 0; i < len; i++) {
1002 if(isupper_w(str_ucs[i]) || islower_w(str_ucs[i]) || isdigit_w(str_ucs[i]) || strchr_w(other_ucs, str_ucs[i]))
1004 else
1005 str_ucs[i] = (smb_ucs2_t)'_'; /*This will work*/
1008 unicode_to_unix(dest, str_ucs, maxlength);
1010 SAFE_FREE(other_ucs);
1011 SAFE_FREE(str_ucs);
1013 return dest;
1016 /****************************************************************************
1017 Like strncpy but always null terminates. Make sure there is room!
1018 The variable n should always be one less than the available size.
1019 ****************************************************************************/
1021 char *StrnCpy(char *dest,const char *src,size_t n)
1023 char *d = dest;
1024 if (!dest) return(NULL);
1025 if (!src) {
1026 *dest = 0;
1027 return(dest);
1029 while (n-- && (*d++ = *src++)) ;
1030 *d = 0;
1031 return(dest);
1034 /****************************************************************************
1035 like strncpy but copies up to the character marker. always null terminates.
1036 returns a pointer to the character marker in the source string (src).
1037 ****************************************************************************/
1038 char *strncpyn(char *dest, const char *src,size_t n, char c)
1040 char *p;
1041 size_t str_len;
1043 p = strchr(src, c);
1044 if (p == NULL)
1046 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
1047 return NULL;
1050 str_len = PTR_DIFF(p, src);
1051 strncpy(dest, src, MIN(n, str_len));
1052 dest[str_len] = '\0';
1054 return p;
1058 /*************************************************************
1059 Routine to get hex characters and turn them into a 16 byte array.
1060 the array can be variable length, and any non-hex-numeric
1061 characters are skipped. "0xnn" or "0Xnn" is specially catered
1062 for.
1064 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1066 **************************************************************/
1067 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1069 size_t i;
1070 size_t num_chars = 0;
1071 unsigned char lonybble, hinybble;
1072 const char *hexchars = "0123456789ABCDEF";
1073 char *p1 = NULL, *p2 = NULL;
1075 for (i = 0; i < len && strhex[i] != 0; i++)
1077 if (strnequal(hexchars, "0x", 2))
1079 i++; /* skip two chars */
1080 continue;
1083 if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
1085 break;
1088 i++; /* next hex digit */
1090 if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
1092 break;
1095 /* get the two nybbles */
1096 hinybble = PTR_DIFF(p1, hexchars);
1097 lonybble = PTR_DIFF(p2, hexchars);
1099 p[num_chars] = (hinybble << 4) | lonybble;
1100 num_chars++;
1102 p1 = NULL;
1103 p2 = NULL;
1105 return num_chars;
1108 /****************************************************************************
1109 check if a string is part of a list
1110 ****************************************************************************/
1111 BOOL in_list(char *s,char *list,BOOL casesensitive)
1113 pstring tok;
1114 const char *p=list;
1116 if (!list) return(False);
1118 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
1119 if (casesensitive) {
1120 if (strcmp(tok,s) == 0)
1121 return(True);
1122 } else {
1123 if (StrCaseCmp(tok,s) == 0)
1124 return(True);
1127 return(False);
1130 /* this is used to prevent lots of mallocs of size 1 */
1131 static char *null_string = NULL;
1133 /****************************************************************************
1134 set a string value, allocing the space for the string
1135 ****************************************************************************/
1136 static BOOL string_init(char **dest,const char *src)
1138 size_t l;
1139 if (!src)
1140 src = "";
1142 l = strlen(src);
1144 if (l == 0)
1146 if (!null_string) {
1147 if((null_string = (char *)malloc(1)) == NULL) {
1148 DEBUG(0,("string_init: malloc fail for null_string.\n"));
1149 return False;
1151 *null_string = 0;
1153 *dest = null_string;
1155 else
1157 (*dest) = (char *)malloc(l+1);
1158 if ((*dest) == NULL) {
1159 DEBUG(0,("Out of memory in string_init\n"));
1160 return False;
1163 pstrcpy(*dest,src);
1165 return(True);
1168 /****************************************************************************
1169 free a string value
1170 ****************************************************************************/
1171 void string_free(char **s)
1173 if (!s || !(*s)) return;
1174 if (*s == null_string)
1175 *s = NULL;
1176 SAFE_FREE(*s);
1179 /****************************************************************************
1180 set a string value, allocing the space for the string, and deallocating any
1181 existing space
1182 ****************************************************************************/
1183 BOOL string_set(char **dest,const char *src)
1185 string_free(dest);
1187 return(string_init(dest,src));
1191 /****************************************************************************
1192 substitute a string for a pattern in another string. Make sure there is
1193 enough room!
1195 This routine looks for pattern in s and replaces it with
1196 insert. It may do multiple replacements.
1198 any of " ; ' $ or ` in the insert string are replaced with _
1199 if len==0 then no expansion is permitted.
1200 ****************************************************************************/
1201 void string_sub(char *s,const char *pattern,const char *insert, size_t len)
1203 char *p;
1204 ssize_t ls,lp,li, i;
1206 if (!insert || !pattern || !s) return;
1208 ls = (ssize_t)strlen(s);
1209 lp = (ssize_t)strlen(pattern);
1210 li = (ssize_t)strlen(insert);
1212 if (!*pattern) return;
1214 if (len == 0)
1215 len = ls + 1; /* len is number of *bytes* */
1217 while (lp <= ls && (p = strstr(s,pattern))) {
1218 if (ls + (li-lp) >= len) {
1219 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
1220 (int)(ls + (li-lp) - len),
1221 pattern, (int)len));
1222 break;
1224 if (li != lp) {
1225 memmove(p+li,p+lp,strlen(p+lp)+1);
1227 for (i=0;i<li;i++) {
1228 switch (insert[i]) {
1229 case '`':
1230 case '"':
1231 case '\'':
1232 case ';':
1233 case '$':
1234 case '%':
1235 case '\r':
1236 case '\n':
1237 p[i] = '_';
1238 break;
1239 default:
1240 p[i] = insert[i];
1243 s = p + li;
1244 ls += (li-lp);
1248 void fstring_sub(char *s,const char *pattern,const char *insert)
1250 string_sub(s, pattern, insert, sizeof(fstring));
1253 void pstring_sub(char *s,const char *pattern,const char *insert)
1255 string_sub(s, pattern, insert, sizeof(pstring));
1258 /****************************************************************************
1259 similar to string_sub() but allows for any character to be substituted.
1260 Use with caution!
1261 if len==0 then no expansion is permitted.
1262 ****************************************************************************/
1263 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1265 char *p;
1266 ssize_t ls,lp,li;
1268 if (!insert || !pattern || !s) return;
1270 ls = (ssize_t)strlen(s);
1271 lp = (ssize_t)strlen(pattern);
1272 li = (ssize_t)strlen(insert);
1274 if (!*pattern) return;
1276 if (len == 0)
1277 len = ls + 1; /* len is number of *bytes* */
1279 while (lp <= ls && (p = strstr(s,pattern))) {
1280 if (ls + (li-lp) >= len) {
1281 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
1282 (int)(ls + (li-lp) - len),
1283 pattern, (int)len));
1284 break;
1286 if (li != lp) {
1287 memmove(p+li,p+lp,strlen(p+lp)+1);
1289 memcpy(p, insert, li);
1290 s = p + li;
1291 ls += (li-lp);
1295 /****************************************************************************
1296 splits out the front and back at a separator.
1297 ****************************************************************************/
1298 void split_at_last_component(char *path, char *front, char sep, char *back)
1300 char *p = strrchr(path, sep);
1302 if (p != NULL)
1304 *p = 0;
1306 if (front != NULL)
1308 pstrcpy(front, path);
1310 if (p != NULL)
1312 if (back != NULL)
1314 pstrcpy(back, p+1);
1316 *p = '\\';
1318 else
1320 if (back != NULL)
1322 back[0] = 0;
1328 /****************************************************************************
1329 write an octal as a string
1330 ****************************************************************************/
1331 const char *octal_string(int i)
1333 static char ret[64];
1334 if (i == -1) {
1335 return "-1";
1337 slprintf(ret, sizeof(ret)-1, "0%o", i);
1338 return ret;
1342 /****************************************************************************
1343 truncate a string at a specified length
1344 ****************************************************************************/
1345 char *string_truncate(char *s, int length)
1347 if (s && strlen(s) > length) {
1348 s[length] = 0;
1350 return s;
1354 return a RFC2254 binary string representation of a buffer
1355 used in LDAP filters
1356 caller must free
1358 char *binary_string(char *buf, int len)
1360 char *s;
1361 int i, j;
1362 const char *hex = "0123456789ABCDEF";
1363 s = malloc(len * 3 + 1);
1364 if (!s) return NULL;
1365 for (j=i=0;i<len;i++) {
1366 s[j] = '\\';
1367 s[j+1] = hex[((unsigned char)buf[i]) >> 4];
1368 s[j+2] = hex[((unsigned char)buf[i]) & 0xF];
1369 j += 3;
1371 s[j] = 0;
1372 return s;
1375 #ifndef HAVE_STRNLEN
1376 /*******************************************************************
1377 Some platforms don't have strnlen
1378 ********************************************************************/
1380 size_t strnlen(const char *s, size_t n)
1382 int i;
1383 for (i=0; s[i] && i<n; i++)
1384 /* noop */ ;
1385 return i;
1387 #endif
1389 #ifndef HAVE_STRNDUP
1390 /*******************************************************************
1391 Some platforms don't have strndup.
1392 ********************************************************************/
1394 char *strndup(const char *s, size_t n)
1396 char *ret;
1398 n = strnlen(s, n);
1399 ret = malloc(n+1);
1400 if (!ret)
1401 return NULL;
1402 memcpy(ret, s, n);
1403 ret[n] = 0;
1405 return ret;
1407 #endif