preparing for release of 2.2.3a
[Samba.git] / source / lib / util_str.c
blobf5f9cc1fe44f8c0b793bb2c84b5f61068fda495d
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 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 ****************************************************************************/
37 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
39 char *s;
40 BOOL quoted;
41 size_t len=1;
43 if (!ptr) ptr = &last_ptr;
44 if (!ptr) return(False);
46 s = *ptr;
48 /* default to simple separators */
49 if (!sep) sep = " \t\n\r";
51 /* find the first non sep char */
52 while(*s && strchr(sep,*s)) s++;
54 /* nothing left? */
55 if (! *s) return(False);
57 /* copy over the token */
58 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++)
60 if (*s == '\"') {
61 quoted = !quoted;
62 } else {
63 len++;
64 *buff++ = *s;
68 *ptr = (*s) ? s+1 : s;
69 *buff = 0;
70 last_ptr = *ptr;
72 return(True);
75 /****************************************************************************
76 Convert list of tokens to array; dependent on above routine.
77 Uses last_ptr from above - bit of a hack.
78 ****************************************************************************/
79 char **toktocliplist(int *ctok, char *sep)
81 char *s=last_ptr;
82 int ictok=0;
83 char **ret, **iret;
85 if (!sep) sep = " \t\n\r";
87 while(*s && strchr(sep,*s)) s++;
89 /* nothing left? */
90 if (!*s) return(NULL);
92 do {
93 ictok++;
94 while(*s && (!strchr(sep,*s))) s++;
95 while(*s && strchr(sep,*s)) *s++=0;
96 } while(*s);
98 *ctok=ictok;
99 s=last_ptr;
101 if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
103 while(ictok--) {
104 *iret++=s;
105 while(*s++);
106 while(!*s) s++;
109 return ret;
113 /*******************************************************************
114 case insensitive string compararison
115 ********************************************************************/
116 int StrCaseCmp(const char *s, const char *t)
118 /* compare until we run out of string, either t or s, or find a difference */
119 /* We *must* use toupper rather than tolower here due to the
120 asynchronous upper to lower mapping.
122 #if !defined(KANJI_WIN95_COMPATIBILITY)
124 * For completeness we should put in equivalent code for code pages
125 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
126 * doubt anyone wants Samba to behave differently from Win95 and WinNT
127 * here. They both treat full width ascii characters as case senstive
128 * filenames (ie. they don't do the work we do here).
129 * JRA.
132 if(lp_client_code_page() == KANJI_CODEPAGE)
134 /* Win95 treats full width ascii characters as case sensitive. */
135 int diff;
136 for (;;)
138 if (!*s || !*t)
139 return toupper (*s) - toupper (*t);
140 else if (is_sj_alph (*s) && is_sj_alph (*t))
142 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
143 if (diff)
144 return diff;
145 s += 2;
146 t += 2;
148 else if (is_shift_jis (*s) && is_shift_jis (*t))
150 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
151 if (diff)
152 return diff;
153 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
154 if (diff)
155 return diff;
156 s += 2;
157 t += 2;
159 else if (is_shift_jis (*s))
160 return 1;
161 else if (is_shift_jis (*t))
162 return -1;
163 else
165 diff = toupper (*s) - toupper (*t);
166 if (diff)
167 return diff;
168 s++;
169 t++;
173 else
174 #endif /* KANJI_WIN95_COMPATIBILITY */
176 while (*s && *t && toupper(*s) == toupper(*t))
178 s++;
179 t++;
182 return(toupper(*s) - toupper(*t));
186 /*******************************************************************
187 case insensitive string compararison, length limited
188 ********************************************************************/
189 int StrnCaseCmp(const char *s, const char *t, size_t n)
191 /* compare until we run out of string, either t or s, or chars */
192 /* We *must* use toupper rather than tolower here due to the
193 asynchronous upper to lower mapping.
195 #if !defined(KANJI_WIN95_COMPATIBILITY)
197 * For completeness we should put in equivalent code for code pages
198 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
199 * doubt anyone wants Samba to behave differently from Win95 and WinNT
200 * here. They both treat full width ascii characters as case senstive
201 * filenames (ie. they don't do the work we do here).
202 * JRA.
205 if(lp_client_code_page() == KANJI_CODEPAGE)
207 /* Win95 treats full width ascii characters as case sensitive. */
208 int diff;
209 for (;n > 0;)
211 if (!*s || !*t)
212 return toupper (*s) - toupper (*t);
213 else if (is_sj_alph (*s) && is_sj_alph (*t))
215 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
216 if (diff)
217 return diff;
218 s += 2;
219 t += 2;
220 n -= 2;
222 else if (is_shift_jis (*s) && is_shift_jis (*t))
224 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
225 if (diff)
226 return diff;
227 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
228 if (diff)
229 return diff;
230 s += 2;
231 t += 2;
232 n -= 2;
234 else if (is_shift_jis (*s))
235 return 1;
236 else if (is_shift_jis (*t))
237 return -1;
238 else
240 diff = toupper (*s) - toupper (*t);
241 if (diff)
242 return diff;
243 s++;
244 t++;
245 n--;
248 return 0;
250 else
251 #endif /* KANJI_WIN95_COMPATIBILITY */
253 while (n && *s && *t && toupper(*s) == toupper(*t))
255 s++;
256 t++;
257 n--;
260 /* not run out of chars - strings are different lengths */
261 if (n)
262 return(toupper(*s) - toupper(*t));
264 /* identical up to where we run out of chars,
265 and strings are same length */
266 return(0);
270 /*******************************************************************
271 compare 2 strings
272 ********************************************************************/
273 BOOL strequal(const char *s1, const char *s2)
275 if (s1 == s2) return(True);
276 if (!s1 || !s2) return(False);
278 return(StrCaseCmp(s1,s2)==0);
281 /*******************************************************************
282 compare 2 strings up to and including the nth char.
283 ******************************************************************/
284 BOOL strnequal(const char *s1,const char *s2,size_t n)
286 if (s1 == s2) return(True);
287 if (!s1 || !s2 || !n) return(False);
289 return(StrnCaseCmp(s1,s2,n)==0);
292 /*******************************************************************
293 compare 2 strings (case sensitive)
294 ********************************************************************/
295 BOOL strcsequal(const char *s1,const char *s2)
297 if (s1 == s2) return(True);
298 if (!s1 || !s2) return(False);
300 return(strcmp(s1,s2)==0);
303 /***************************************************************************
304 Do a case-insensitive, whitespace-ignoring string compare.
305 ***************************************************************************/
306 int strwicmp(char *psz1, char *psz2)
308 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
309 /* appropriate value. */
310 if (psz1 == psz2)
311 return (0);
312 else if (psz1 == NULL)
313 return (-1);
314 else if (psz2 == NULL)
315 return (1);
317 /* sync the strings on first non-whitespace */
318 while (1)
320 while (isspace((int)*psz1))
321 psz1++;
322 while (isspace((int)*psz2))
323 psz2++;
324 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
325 || *psz2 == '\0')
326 break;
327 psz1++;
328 psz2++;
330 return (*psz1 - *psz2);
334 /*******************************************************************
335 convert a string to lower case
336 ********************************************************************/
337 void strlower(char *s)
339 while (*s)
341 #if !defined(KANJI_WIN95_COMPATIBILITY)
343 * For completeness we should put in equivalent code for code pages
344 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
345 * doubt anyone wants Samba to behave differently from Win95 and WinNT
346 * here. They both treat full width ascii characters as case senstive
347 * filenames (ie. they don't do the work we do here).
348 * JRA.
351 if(lp_client_code_page() == KANJI_CODEPAGE)
353 /* Win95 treats full width ascii characters as case sensitive. */
354 if (is_shift_jis (*s))
356 if (is_sj_upper (s[0], s[1]))
357 s[1] = sj_tolower2 (s[1]);
358 s += 2;
360 else if (is_kana (*s))
362 s++;
364 else
366 if (isupper(*s))
367 *s = tolower(*s);
368 s++;
371 else
372 #endif /* KANJI_WIN95_COMPATIBILITY */
374 size_t skip = get_character_len( *s );
375 if( skip != 0 )
376 s += skip;
377 else
379 if (isupper(*s))
380 *s = tolower(*s);
381 s++;
387 /*******************************************************************
388 convert a string to upper case
389 ********************************************************************/
390 void strupper(char *s)
392 while (*s)
394 #if !defined(KANJI_WIN95_COMPATIBILITY)
396 * For completeness we should put in equivalent code for code pages
397 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
398 * doubt anyone wants Samba to behave differently from Win95 and WinNT
399 * here. They both treat full width ascii characters as case senstive
400 * filenames (ie. they don't do the work we do here).
401 * JRA.
404 if(lp_client_code_page() == KANJI_CODEPAGE)
406 /* Win95 treats full width ascii characters as case sensitive. */
407 if (is_shift_jis (*s))
409 if (is_sj_lower (s[0], s[1]))
410 s[1] = sj_toupper2 (s[1]);
411 s += 2;
413 else if (is_kana (*s))
415 s++;
417 else
419 if (islower(*s))
420 *s = toupper(*s);
421 s++;
424 else
425 #endif /* KANJI_WIN95_COMPATIBILITY */
427 size_t skip = get_character_len( *s );
428 if( skip != 0 )
429 s += skip;
430 else
432 if (islower(*s))
433 *s = toupper(*s);
434 s++;
440 /*******************************************************************
441 convert a string to "normal" form
442 ********************************************************************/
443 void strnorm(char *s)
445 extern int case_default;
446 if (case_default == CASE_UPPER)
447 strupper(s);
448 else
449 strlower(s);
452 /*******************************************************************
453 check if a string is in "normal" case
454 ********************************************************************/
455 BOOL strisnormal(char *s)
457 extern int case_default;
458 if (case_default == CASE_UPPER)
459 return(!strhaslower(s));
461 return(!strhasupper(s));
465 /****************************************************************************
466 string replace
467 ****************************************************************************/
468 void string_replace(char *s,char oldc,char newc)
470 size_t skip;
473 * sbcs optimization.
475 if(!global_is_multibyte_codepage) {
476 while (*s) {
477 if (oldc == *s)
478 *s = newc;
479 s++;
481 } else {
482 while (*s)
484 skip = get_character_len( *s );
485 if( skip != 0 )
486 s += skip;
487 else
489 if (oldc == *s)
490 *s = newc;
491 s++;
498 /*******************************************************************
499 skip past some strings in a buffer
500 ********************************************************************/
501 char *skip_string(char *buf,size_t n)
503 while (n--)
504 buf += strlen(buf) + 1;
505 return(buf);
508 /*******************************************************************
509 Count the number of characters in a string. Normally this will
510 be the same as the number of bytes in a string for single byte strings,
511 but will be different for multibyte.
512 16.oct.98, jdblair@cobaltnet.com.
513 ********************************************************************/
515 size_t str_charnum(const char *s)
517 size_t len = 0;
520 * sbcs optimization.
522 if(!global_is_multibyte_codepage) {
523 return strlen(s);
524 } else {
525 while (*s != '\0') {
526 int skip = get_character_len(*s);
527 s += (skip ? skip : 1);
528 len++;
531 return len;
534 /*******************************************************************
535 trim the specified elements off the front and back of a string
536 ********************************************************************/
538 BOOL trim_string(char *s,const char *front,const char *back)
540 BOOL ret = False;
541 size_t s_len;
542 size_t front_len;
543 size_t back_len;
544 char *sP;
546 /* Ignore null or empty strings. */
548 if ( !s || (s[0] == '\0'))
549 return False;
551 sP = s;
552 s_len = strlen( s ) + 1;
553 front_len = (front) ? strlen( front ) + 1 : 0;
554 back_len = (back) ? strlen( back ) + 1 : 0;
557 * remove "front" string from given "s", if it matches front part,
558 * repeatedly.
560 if ( front && front_len > 1 ) {
561 while (( s_len >= front_len )&&
562 ( memcmp( sP, front, front_len - 1 )) == 0 ) {
563 ret = True;
564 sP += ( front_len - 1 );
565 s_len -= ( front_len - 1 );
570 * we'll memmove sP to s later, after we're done with
571 * back part removal, for minimizing copy.
576 * We split out the multibyte code page
577 * case here for speed purposes. Under a
578 * multibyte code page we need to walk the
579 * string forwards only and multiple times.
580 * Thanks to John Blair for finding this
581 * one. JRA.
584 * This JRA's comment is partly correct, but partly wrong.
585 * You can always check from "end" part, and if it did not match,
586 * it means there is no possibility of finding one.
587 * If you found matching point, mark them, then look from front
588 * if marking point suits multi-byte string rule.
589 * Kenichi Okuyama.
592 if ( back && back_len > 1 && s_len >= back_len) {
593 char *bP = sP + s_len - back_len;
594 long b_len = s_len;
596 while (( b_len >= back_len )&&
597 ( memcmp( bP, back, back_len - 1 ) == 0 )) {
598 bP -= ( back_len - 1 );
599 b_len -= ( back_len - 1 );
603 * You're here, means you ether have found match multiple times,
604 * or you found none. If you've found match, then bP should be
605 * moving.
607 if ( bP != sP + s_len - back_len ) {
608 bP += ( back_len - 1 ); /* slide bP to first matching point. */
610 if( !global_is_multibyte_codepage ) {
611 /* simply terminate */
612 (*bP) = '\0';
613 s_len = b_len;
614 ret = True;
615 } else {
616 /* trace string from start. */
617 char *cP = sP;
618 while ( cP < sP + s_len - back_len ) {
619 size_t skip;
620 skip = skip_multibyte_char( *cP );
621 cP += ( skip ? skip : 1 );
622 if ( cP == bP ) {
623 /* you found the match */
624 (*bP) = '\0';
625 ret = True;
626 s_len = b_len;
627 break;
629 while (( cP > bP )&&( bP < sP + s_len - back_len )) {
630 bP += ( back_len - 1 );
631 b_len += ( back_len - 1 );
638 /* if front found matching point */
639 if ( sP != s ) {
640 /* slide string to buffer top */
641 memmove( s, sP, s_len );
643 return ret;
647 /****************************************************************************
648 does a string have any uppercase chars in it?
649 ****************************************************************************/
650 BOOL strhasupper(const char *s)
652 while (*s)
654 #if !defined(KANJI_WIN95_COMPATIBILITY)
656 * For completeness we should put in equivalent code for code pages
657 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
658 * doubt anyone wants Samba to behave differently from Win95 and WinNT
659 * here. They both treat full width ascii characters as case senstive
660 * filenames (ie. they don't do the work we do here).
661 * JRA.
664 if(lp_client_code_page() == KANJI_CODEPAGE)
666 /* Win95 treats full width ascii characters as case sensitive. */
667 if (is_shift_jis (*s))
668 s += 2;
669 else if (is_kana (*s))
670 s++;
671 else
673 if (isupper(*s))
674 return(True);
675 s++;
678 else
679 #endif /* KANJI_WIN95_COMPATIBILITY */
681 size_t skip = get_character_len( *s );
682 if( skip != 0 )
683 s += skip;
684 else {
685 if (isupper(*s))
686 return(True);
687 s++;
691 return(False);
694 /****************************************************************************
695 does a string have any lowercase chars in it?
696 ****************************************************************************/
697 BOOL strhaslower(const char *s)
699 while (*s)
701 #if !defined(KANJI_WIN95_COMPATIBILITY)
703 * For completeness we should put in equivalent code for code pages
704 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
705 * doubt anyone wants Samba to behave differently from Win95 and WinNT
706 * here. They both treat full width ascii characters as case senstive
707 * filenames (ie. they don't do the work we do here).
708 * JRA.
711 if(lp_client_code_page() == KANJI_CODEPAGE)
713 /* Win95 treats full width ascii characters as case sensitive. */
714 if (is_shift_jis (*s))
716 if (is_sj_upper (s[0], s[1]))
717 return(True);
718 if (is_sj_lower (s[0], s[1]))
719 return (True);
720 s += 2;
722 else if (is_kana (*s))
724 s++;
726 else
728 if (islower(*s))
729 return(True);
730 s++;
733 else
734 #endif /* KANJI_WIN95_COMPATIBILITY */
736 size_t skip = get_character_len( *s );
737 if( skip != 0 )
738 s += skip;
739 else {
740 if (islower(*s))
741 return(True);
742 s++;
746 return(False);
749 /****************************************************************************
750 find the number of chars in a string
751 ****************************************************************************/
752 size_t count_chars(const char *s,char c)
754 size_t count=0;
756 #if !defined(KANJI_WIN95_COMPATIBILITY)
758 * For completeness we should put in equivalent code for code pages
759 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
760 * doubt anyone wants Samba to behave differently from Win95 and WinNT
761 * here. They both treat full width ascii characters as case senstive
762 * filenames (ie. they don't do the work we do here).
763 * JRA.
766 if(lp_client_code_page() == KANJI_CODEPAGE)
768 /* Win95 treats full width ascii characters as case sensitive. */
769 while (*s)
771 if (is_shift_jis (*s))
772 s += 2;
773 else
775 if (*s == c)
776 count++;
777 s++;
781 else
782 #endif /* KANJI_WIN95_COMPATIBILITY */
784 while (*s)
786 size_t skip = get_character_len( *s );
787 if( skip != 0 )
788 s += skip;
789 else {
790 if (*s == c)
791 count++;
792 s++;
796 return(count);
799 /*******************************************************************
800 Return True if a string consists only of one particular character.
801 ********************************************************************/
803 BOOL str_is_all(const char *s,char c)
805 if(s == NULL)
806 return False;
807 if(!*s)
808 return False;
810 #if !defined(KANJI_WIN95_COMPATIBILITY)
812 * For completeness we should put in equivalent code for code pages
813 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
814 * doubt anyone wants Samba to behave differently from Win95 and WinNT
815 * here. They both treat full width ascii characters as case senstive
816 * filenames (ie. they don't do the work we do here).
817 * JRA.
820 if(lp_client_code_page() == KANJI_CODEPAGE)
822 /* Win95 treats full width ascii characters as case sensitive. */
823 while (*s)
825 if (is_shift_jis (*s))
826 s += 2;
827 else
829 if (*s != c)
830 return False;
831 s++;
835 else
836 #endif /* KANJI_WIN95_COMPATIBILITY */
838 while (*s)
840 size_t skip = get_character_len( *s );
841 if( skip != 0 )
842 s += skip;
843 else {
844 if (*s != c)
845 return False;
846 s++;
850 return True;
853 /*******************************************************************
854 safe string copy into a known length string. maxlength does not
855 include the terminating zero.
856 ********************************************************************/
858 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
860 size_t len;
862 if (!dest) {
863 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
864 return NULL;
867 if (!src) {
868 *dest = 0;
869 return dest;
872 len = strlen(src);
874 if (len > maxlength) {
875 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
876 (int)(len-maxlength), src));
877 len = maxlength;
880 memcpy(dest, src, len);
881 dest[len] = 0;
882 return dest;
885 /*******************************************************************
886 safe string cat into a string. maxlength does not
887 include the terminating zero.
888 ********************************************************************/
890 char *safe_strcat(char *dest, const char *src, size_t maxlength)
892 size_t src_len, dest_len;
894 if (!dest) {
895 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
896 return NULL;
899 if (!src) {
900 return dest;
903 src_len = strlen(src);
904 dest_len = strlen(dest);
906 if (src_len + dest_len > maxlength) {
907 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
908 (int)(src_len + dest_len - maxlength), src));
909 src_len = maxlength - dest_len;
912 memcpy(&dest[dest_len], src, src_len);
913 dest[dest_len + src_len] = 0;
914 return dest;
917 /*******************************************************************
918 Paranoid strcpy into a buffer of given length (includes terminating
919 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
920 and replaces with '_'. Deliberately does *NOT* check for multibyte
921 characters. Don't change it !
922 ********************************************************************/
924 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
926 size_t len, i;
927 size_t buflen;
928 smb_ucs2_t *str_ucs, *other_ucs;
930 if (!dest) {
931 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
932 return NULL;
935 if (!src) {
936 *dest = 0;
937 return dest;
940 /* Get UCS2 version of src string*/
942 buflen=2*strlen(src)+2;
943 if (buflen >= (2*maxlength))
944 buflen = 2*(maxlength - 1);
946 str_ucs = (smb_ucs2_t*)malloc(buflen);
947 if(!str_ucs) {
948 *dest=0;
949 return dest;
951 unix_to_unicode(str_ucs, src, buflen);
952 len = strlen_w(str_ucs);
954 if (!other_safe_chars)
955 other_safe_chars = "";
957 /* Get UCS2 version of other_safe_chars string*/
958 buflen=2*strlen(other_safe_chars)+2;
959 other_ucs = (smb_ucs2_t*)malloc(buflen);
960 if(!other_ucs) {
961 *dest=0;
962 SAFE_FREE(str_ucs);
963 return dest;
965 unix_to_unicode(other_ucs, other_safe_chars, buflen);
967 for(i = 0; i < len; i++) {
968 if(isupper_w(str_ucs[i]) || islower_w(str_ucs[i]) || isdigit_w(str_ucs[i]) || strchr_w(other_ucs, str_ucs[i]))
970 else
971 str_ucs[i] = (smb_ucs2_t)'_'; /*This will work*/
974 unicode_to_unix(dest, str_ucs, maxlength);
976 SAFE_FREE(other_ucs);
977 SAFE_FREE(str_ucs);
979 return dest;
982 /****************************************************************************
983 Like strncpy but always null terminates. Make sure there is room!
984 The variable n should always be one less than the available size.
985 ****************************************************************************/
987 char *StrnCpy(char *dest,const char *src,size_t n)
989 char *d = dest;
990 if (!dest) return(NULL);
991 if (!src) {
992 *dest = 0;
993 return(dest);
995 while (n-- && (*d++ = *src++)) ;
996 *d = 0;
997 return(dest);
1000 /****************************************************************************
1001 like strncpy but copies up to the character marker. always null terminates.
1002 returns a pointer to the character marker in the source string (src).
1003 ****************************************************************************/
1004 char *strncpyn(char *dest, const char *src,size_t n, char c)
1006 char *p;
1007 size_t str_len;
1009 p = strchr(src, c);
1010 if (p == NULL)
1012 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
1013 return NULL;
1016 str_len = PTR_DIFF(p, src);
1017 strncpy(dest, src, MIN(n, str_len));
1018 dest[str_len] = '\0';
1020 return p;
1024 /*************************************************************
1025 Routine to get hex characters and turn them into a 16 byte array.
1026 the array can be variable length, and any non-hex-numeric
1027 characters are skipped. "0xnn" or "0Xnn" is specially catered
1028 for.
1030 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1032 **************************************************************/
1033 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1035 size_t i;
1036 size_t num_chars = 0;
1037 unsigned char lonybble, hinybble;
1038 char *hexchars = "0123456789ABCDEF";
1039 char *p1 = NULL, *p2 = NULL;
1041 for (i = 0; i < len && strhex[i] != 0; i++)
1043 if (strnequal(hexchars, "0x", 2))
1045 i++; /* skip two chars */
1046 continue;
1049 if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
1051 break;
1054 i++; /* next hex digit */
1056 if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
1058 break;
1061 /* get the two nybbles */
1062 hinybble = PTR_DIFF(p1, hexchars);
1063 lonybble = PTR_DIFF(p2, hexchars);
1065 p[num_chars] = (hinybble << 4) | lonybble;
1066 num_chars++;
1068 p1 = NULL;
1069 p2 = NULL;
1071 return num_chars;
1074 /****************************************************************************
1075 check if a string is part of a list
1076 ****************************************************************************/
1077 BOOL in_list(char *s,char *list,BOOL casesensitive)
1079 pstring tok;
1080 char *p=list;
1082 if (!list) return(False);
1084 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
1085 if (casesensitive) {
1086 if (strcmp(tok,s) == 0)
1087 return(True);
1088 } else {
1089 if (StrCaseCmp(tok,s) == 0)
1090 return(True);
1093 return(False);
1096 /* this is used to prevent lots of mallocs of size 1 */
1097 static char *null_string = NULL;
1099 /****************************************************************************
1100 set a string value, allocing the space for the string
1101 ****************************************************************************/
1102 static BOOL string_init(char **dest,const char *src)
1104 size_t l;
1105 if (!src)
1106 src = "";
1108 l = strlen(src);
1110 if (l == 0)
1112 if (!null_string) {
1113 if((null_string = (char *)malloc(1)) == NULL) {
1114 DEBUG(0,("string_init: malloc fail for null_string.\n"));
1115 return False;
1117 *null_string = 0;
1119 *dest = null_string;
1121 else
1123 (*dest) = (char *)malloc(l+1);
1124 if ((*dest) == NULL) {
1125 DEBUG(0,("Out of memory in string_init\n"));
1126 return False;
1129 pstrcpy(*dest,src);
1131 return(True);
1134 /****************************************************************************
1135 free a string value
1136 ****************************************************************************/
1137 void string_free(char **s)
1139 if (!s || !(*s)) return;
1140 if (*s == null_string)
1141 *s = NULL;
1142 SAFE_FREE(*s);
1145 /****************************************************************************
1146 set a string value, allocing the space for the string, and deallocating any
1147 existing space
1148 ****************************************************************************/
1149 BOOL string_set(char **dest,const char *src)
1151 string_free(dest);
1153 return(string_init(dest,src));
1157 /****************************************************************************
1158 substitute a string for a pattern in another string. Make sure there is
1159 enough room!
1161 This routine looks for pattern in s and replaces it with
1162 insert. It may do multiple replacements.
1164 any of " ; ' $ or ` in the insert string are replaced with _
1165 if len==0 then no length check is performed
1166 ****************************************************************************/
1167 void string_sub(char *s,const char *pattern,const char *insert, size_t len)
1169 char *p;
1170 ssize_t ls,lp,li, i;
1172 if (!insert || !pattern || !s) return;
1174 ls = (ssize_t)strlen(s);
1175 lp = (ssize_t)strlen(pattern);
1176 li = (ssize_t)strlen(insert);
1178 if (!*pattern) return;
1180 while (lp <= ls && (p = strstr(s,pattern))) {
1181 if (len && (ls + (li-lp) >= len)) {
1182 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
1183 (int)(ls + (li-lp) - len),
1184 pattern, (int)len));
1185 break;
1187 if (li != lp) {
1188 memmove(p+li,p+lp,strlen(p+lp)+1);
1190 for (i=0;i<li;i++) {
1191 switch (insert[i]) {
1192 case '`':
1193 case '"':
1194 case '\'':
1195 case ';':
1196 case '$':
1197 case '%':
1198 case '\r':
1199 case '\n':
1200 p[i] = '_';
1201 break;
1202 default:
1203 p[i] = insert[i];
1206 s = p + li;
1207 ls += (li-lp);
1211 void fstring_sub(char *s,const char *pattern,const char *insert)
1213 string_sub(s, pattern, insert, sizeof(fstring));
1216 void pstring_sub(char *s,const char *pattern,const char *insert)
1218 string_sub(s, pattern, insert, sizeof(pstring));
1221 /****************************************************************************
1222 similar to string_sub() but allows for any character to be substituted.
1223 Use with caution!
1224 if len==0 then no length check is performed
1225 ****************************************************************************/
1226 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1228 char *p;
1229 ssize_t ls,lp,li;
1231 if (!insert || !pattern || !s) return;
1233 ls = (ssize_t)strlen(s);
1234 lp = (ssize_t)strlen(pattern);
1235 li = (ssize_t)strlen(insert);
1237 if (!*pattern) return;
1239 while (lp <= ls && (p = strstr(s,pattern))) {
1240 if (len && (ls + (li-lp) >= len)) {
1241 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
1242 (int)(ls + (li-lp) - len),
1243 pattern, (int)len));
1244 break;
1246 if (li != lp) {
1247 memmove(p+li,p+lp,strlen(p+lp)+1);
1249 memcpy(p, insert, li);
1250 s = p + li;
1251 ls += (li-lp);
1255 /****************************************************************************
1256 splits out the front and back at a separator.
1257 ****************************************************************************/
1258 void split_at_last_component(char *path, char *front, char sep, char *back)
1260 char *p = strrchr(path, sep);
1262 if (p != NULL)
1264 *p = 0;
1266 if (front != NULL)
1268 pstrcpy(front, path);
1270 if (p != NULL)
1272 if (back != NULL)
1274 pstrcpy(back, p+1);
1276 *p = '\\';
1278 else
1280 if (back != NULL)
1282 back[0] = 0;
1288 /****************************************************************************
1289 write an octal as a string
1290 ****************************************************************************/
1291 char *octal_string(int i)
1293 static char ret[64];
1294 if (i == -1) {
1295 return "-1";
1297 slprintf(ret, sizeof(ret)-1, "0%o", i);
1298 return ret;
1302 /****************************************************************************
1303 truncate a string at a specified length
1304 ****************************************************************************/
1305 char *string_truncate(char *s, int length)
1307 if (s && strlen(s) > length) {
1308 s[length] = 0;
1310 return s;