removed -lresolv for --enable-ldapsam is present; should be included by -lsasl (neede...
[Samba.git] / source / lib / util_str.c
blob3698130abd54a906b9910c2871fa1d90c9f77d53
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 /* Convert a string to upper case, but don't modify it */
442 char *strupper_static(const char *s)
444 static pstring str;
446 pstrcpy(str, s);
447 strupper(str);
449 return str;
452 /*******************************************************************
453 convert a string to "normal" form
454 ********************************************************************/
455 void strnorm(char *s)
457 extern int case_default;
458 if (case_default == CASE_UPPER)
459 strupper(s);
460 else
461 strlower(s);
464 /*******************************************************************
465 check if a string is in "normal" case
466 ********************************************************************/
467 BOOL strisnormal(char *s)
469 extern int case_default;
470 if (case_default == CASE_UPPER)
471 return(!strhaslower(s));
473 return(!strhasupper(s));
477 /****************************************************************************
478 string replace
479 ****************************************************************************/
480 void string_replace(char *s,char oldc,char newc)
482 size_t skip;
485 * sbcs optimization.
487 if(!global_is_multibyte_codepage) {
488 while (*s) {
489 if (oldc == *s)
490 *s = newc;
491 s++;
493 } else {
494 while (*s)
496 skip = get_character_len( *s );
497 if( skip != 0 )
498 s += skip;
499 else
501 if (oldc == *s)
502 *s = newc;
503 s++;
510 /*******************************************************************
511 skip past some strings in a buffer
512 ********************************************************************/
513 char *skip_string(char *buf,size_t n)
515 while (n--)
516 buf += strlen(buf) + 1;
517 return(buf);
520 /*******************************************************************
521 Count the number of characters in a string. Normally this will
522 be the same as the number of bytes in a string for single byte strings,
523 but will be different for multibyte.
524 16.oct.98, jdblair@cobaltnet.com.
525 ********************************************************************/
527 size_t str_charnum(const char *s)
529 size_t len = 0;
532 * sbcs optimization.
534 if(!global_is_multibyte_codepage) {
535 return strlen(s);
536 } else {
537 while (*s != '\0') {
538 int skip = get_character_len(*s);
539 s += (skip ? skip : 1);
540 len++;
543 return len;
546 /*******************************************************************
547 trim the specified elements off the front and back of a string
548 ********************************************************************/
550 BOOL trim_string(char *s,const char *front,const char *back)
552 BOOL ret = False;
553 size_t s_len;
554 size_t front_len;
555 size_t back_len;
556 char *sP;
558 /* Ignore null or empty strings. */
560 if ( !s || (s[0] == '\0'))
561 return False;
563 sP = s;
564 s_len = strlen( s ) + 1;
565 front_len = (front) ? strlen( front ) + 1 : 0;
566 back_len = (back) ? strlen( back ) + 1 : 0;
569 * remove "front" string from given "s", if it matches front part,
570 * repeatedly.
572 if ( front && front_len > 1 ) {
573 while (( s_len >= front_len )&&
574 ( memcmp( sP, front, front_len - 1 )) == 0 ) {
575 ret = True;
576 sP += ( front_len - 1 );
577 s_len -= ( front_len - 1 );
582 * we'll memmove sP to s later, after we're done with
583 * back part removal, for minimizing copy.
588 * We split out the multibyte code page
589 * case here for speed purposes. Under a
590 * multibyte code page we need to walk the
591 * string forwards only and multiple times.
592 * Thanks to John Blair for finding this
593 * one. JRA.
596 * This JRA's comment is partly correct, but partly wrong.
597 * You can always check from "end" part, and if it did not match,
598 * it means there is no possibility of finding one.
599 * If you found matching point, mark them, then look from front
600 * if marking point suits multi-byte string rule.
601 * Kenichi Okuyama.
604 if ( back && back_len > 1 && s_len >= back_len) {
605 char *bP = sP + s_len - back_len;
606 long b_len = s_len;
608 while (( b_len >= back_len )&&
609 ( memcmp( bP, back, back_len - 1 ) == 0 )) {
610 bP -= ( back_len - 1 );
611 b_len -= ( back_len - 1 );
615 * You're here, means you ether have found match multiple times,
616 * or you found none. If you've found match, then bP should be
617 * moving.
619 if ( bP != sP + s_len - back_len ) {
620 bP += ( back_len - 1 ); /* slide bP to first matching point. */
622 if( !global_is_multibyte_codepage ) {
623 /* simply terminate */
624 (*bP) = '\0';
625 s_len = b_len;
626 ret = True;
627 } else {
628 /* trace string from start. */
629 char *cP = sP;
630 while ( cP < sP + s_len - back_len ) {
631 size_t skip;
632 skip = skip_multibyte_char( *cP );
633 cP += ( skip ? skip : 1 );
634 if ( cP == bP ) {
635 /* you found the match */
636 (*bP) = '\0';
637 ret = True;
638 s_len = b_len;
639 break;
641 while (( cP > bP )&&( bP < sP + s_len - back_len )) {
642 bP += ( back_len - 1 );
643 b_len += ( back_len - 1 );
650 /* if front found matching point */
651 if ( sP != s ) {
652 /* slide string to buffer top */
653 memmove( s, sP, s_len );
655 return ret;
659 /****************************************************************************
660 does a string have any uppercase chars in it?
661 ****************************************************************************/
662 BOOL strhasupper(const char *s)
664 while (*s)
666 #if !defined(KANJI_WIN95_COMPATIBILITY)
668 * For completeness we should put in equivalent code for code pages
669 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
670 * doubt anyone wants Samba to behave differently from Win95 and WinNT
671 * here. They both treat full width ascii characters as case senstive
672 * filenames (ie. they don't do the work we do here).
673 * JRA.
676 if(lp_client_code_page() == KANJI_CODEPAGE)
678 /* Win95 treats full width ascii characters as case sensitive. */
679 if (is_shift_jis (*s))
680 s += 2;
681 else if (is_kana (*s))
682 s++;
683 else
685 if (isupper(*s))
686 return(True);
687 s++;
690 else
691 #endif /* KANJI_WIN95_COMPATIBILITY */
693 size_t skip = get_character_len( *s );
694 if( skip != 0 )
695 s += skip;
696 else {
697 if (isupper(*s))
698 return(True);
699 s++;
703 return(False);
706 /****************************************************************************
707 does a string have any lowercase chars in it?
708 ****************************************************************************/
709 BOOL strhaslower(const char *s)
711 while (*s)
713 #if !defined(KANJI_WIN95_COMPATIBILITY)
715 * For completeness we should put in equivalent code for code pages
716 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
717 * doubt anyone wants Samba to behave differently from Win95 and WinNT
718 * here. They both treat full width ascii characters as case senstive
719 * filenames (ie. they don't do the work we do here).
720 * JRA.
723 if(lp_client_code_page() == KANJI_CODEPAGE)
725 /* Win95 treats full width ascii characters as case sensitive. */
726 if (is_shift_jis (*s))
728 if (is_sj_upper (s[0], s[1]))
729 return(True);
730 if (is_sj_lower (s[0], s[1]))
731 return (True);
732 s += 2;
734 else if (is_kana (*s))
736 s++;
738 else
740 if (islower(*s))
741 return(True);
742 s++;
745 else
746 #endif /* KANJI_WIN95_COMPATIBILITY */
748 size_t skip = get_character_len( *s );
749 if( skip != 0 )
750 s += skip;
751 else {
752 if (islower(*s))
753 return(True);
754 s++;
758 return(False);
761 /****************************************************************************
762 find the number of chars in a string
763 ****************************************************************************/
764 size_t count_chars(const char *s,char c)
766 size_t count=0;
768 #if !defined(KANJI_WIN95_COMPATIBILITY)
770 * For completeness we should put in equivalent code for code pages
771 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
772 * doubt anyone wants Samba to behave differently from Win95 and WinNT
773 * here. They both treat full width ascii characters as case senstive
774 * filenames (ie. they don't do the work we do here).
775 * JRA.
778 if(lp_client_code_page() == KANJI_CODEPAGE)
780 /* Win95 treats full width ascii characters as case sensitive. */
781 while (*s)
783 if (is_shift_jis (*s))
784 s += 2;
785 else
787 if (*s == c)
788 count++;
789 s++;
793 else
794 #endif /* KANJI_WIN95_COMPATIBILITY */
796 while (*s)
798 size_t skip = get_character_len( *s );
799 if( skip != 0 )
800 s += skip;
801 else {
802 if (*s == c)
803 count++;
804 s++;
808 return(count);
811 /*******************************************************************
812 Return True if a string consists only of one particular character.
813 ********************************************************************/
815 BOOL str_is_all(const char *s,char c)
817 if(s == NULL)
818 return False;
819 if(!*s)
820 return False;
822 #if !defined(KANJI_WIN95_COMPATIBILITY)
824 * For completeness we should put in equivalent code for code pages
825 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
826 * doubt anyone wants Samba to behave differently from Win95 and WinNT
827 * here. They both treat full width ascii characters as case senstive
828 * filenames (ie. they don't do the work we do here).
829 * JRA.
832 if(lp_client_code_page() == KANJI_CODEPAGE)
834 /* Win95 treats full width ascii characters as case sensitive. */
835 while (*s)
837 if (is_shift_jis (*s))
838 s += 2;
839 else
841 if (*s != c)
842 return False;
843 s++;
847 else
848 #endif /* KANJI_WIN95_COMPATIBILITY */
850 while (*s)
852 size_t skip = get_character_len( *s );
853 if( skip != 0 )
854 s += skip;
855 else {
856 if (*s != c)
857 return False;
858 s++;
862 return True;
865 /*******************************************************************
866 safe string copy into a known length string. maxlength does not
867 include the terminating zero.
868 ********************************************************************/
870 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
872 size_t len;
874 if (!dest) {
875 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
876 return NULL;
879 if (!src) {
880 *dest = 0;
881 return dest;
884 len = strlen(src);
886 if (len > maxlength) {
887 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
888 (int)(len-maxlength), src));
889 len = maxlength;
892 memcpy(dest, src, len);
893 dest[len] = 0;
894 return dest;
897 /*******************************************************************
898 safe string cat into a string. maxlength does not
899 include the terminating zero.
900 ********************************************************************/
902 char *safe_strcat(char *dest, const char *src, size_t maxlength)
904 size_t src_len, dest_len;
906 if (!dest) {
907 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
908 return NULL;
911 if (!src)
912 return dest;
914 src_len = strlen(src);
915 dest_len = strlen(dest);
917 if (src_len + dest_len > maxlength) {
918 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
919 (int)(src_len + dest_len - maxlength), src));
920 if (dest_len >= maxlength)
921 return dest;
922 src_len = maxlength - dest_len;
925 memcpy(&dest[dest_len], src, src_len);
926 dest[dest_len + src_len] = 0;
927 return dest;
930 /*******************************************************************
931 Paranoid strcpy into a buffer of given length (includes terminating
932 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
933 and replaces with '_'. Deliberately does *NOT* check for multibyte
934 characters. Don't change it !
935 ********************************************************************/
937 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
939 size_t len, i;
940 size_t buflen;
941 smb_ucs2_t *str_ucs, *other_ucs;
943 if (!dest) {
944 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
945 return NULL;
948 if (!src) {
949 *dest = 0;
950 return dest;
953 /* Get UCS2 version of src string*/
955 buflen=2*strlen(src)+2;
956 if (buflen >= (2*maxlength))
957 buflen = 2*(maxlength - 1);
959 str_ucs = (smb_ucs2_t*)malloc(buflen);
960 if(!str_ucs) {
961 *dest=0;
962 return dest;
964 unix_to_unicode(str_ucs, src, buflen);
965 len = strlen_w(str_ucs);
967 if (!other_safe_chars)
968 other_safe_chars = "";
970 /* Get UCS2 version of other_safe_chars string*/
971 buflen=2*strlen(other_safe_chars)+2;
972 other_ucs = (smb_ucs2_t*)malloc(buflen);
973 if(!other_ucs) {
974 *dest=0;
975 SAFE_FREE(str_ucs);
976 return dest;
978 unix_to_unicode(other_ucs, other_safe_chars, buflen);
980 for(i = 0; i < len; i++) {
981 if(isupper_w(str_ucs[i]) || islower_w(str_ucs[i]) || isdigit_w(str_ucs[i]) || strchr_w(other_ucs, str_ucs[i]))
983 else
984 str_ucs[i] = (smb_ucs2_t)'_'; /*This will work*/
987 unicode_to_unix(dest, str_ucs, maxlength);
989 SAFE_FREE(other_ucs);
990 SAFE_FREE(str_ucs);
992 return dest;
995 /****************************************************************************
996 Like strncpy but always null terminates. Make sure there is room!
997 The variable n should always be one less than the available size.
998 ****************************************************************************/
1000 char *StrnCpy(char *dest,const char *src,size_t n)
1002 char *d = dest;
1003 if (!dest) return(NULL);
1004 if (!src) {
1005 *dest = 0;
1006 return(dest);
1008 while (n-- && (*d++ = *src++)) ;
1009 *d = 0;
1010 return(dest);
1013 /****************************************************************************
1014 like strncpy but copies up to the character marker. always null terminates.
1015 returns a pointer to the character marker in the source string (src).
1016 ****************************************************************************/
1017 char *strncpyn(char *dest, const char *src,size_t n, char c)
1019 char *p;
1020 size_t str_len;
1022 p = strchr(src, c);
1023 if (p == NULL)
1025 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
1026 return NULL;
1029 str_len = PTR_DIFF(p, src);
1030 strncpy(dest, src, MIN(n, str_len));
1031 dest[str_len] = '\0';
1033 return p;
1037 /*************************************************************
1038 Routine to get hex characters and turn them into a 16 byte array.
1039 the array can be variable length, and any non-hex-numeric
1040 characters are skipped. "0xnn" or "0Xnn" is specially catered
1041 for.
1043 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1045 **************************************************************/
1046 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1048 size_t i;
1049 size_t num_chars = 0;
1050 unsigned char lonybble, hinybble;
1051 char *hexchars = "0123456789ABCDEF";
1052 char *p1 = NULL, *p2 = NULL;
1054 for (i = 0; i < len && strhex[i] != 0; i++)
1056 if (strnequal(hexchars, "0x", 2))
1058 i++; /* skip two chars */
1059 continue;
1062 if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
1064 break;
1067 i++; /* next hex digit */
1069 if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
1071 break;
1074 /* get the two nybbles */
1075 hinybble = PTR_DIFF(p1, hexchars);
1076 lonybble = PTR_DIFF(p2, hexchars);
1078 p[num_chars] = (hinybble << 4) | lonybble;
1079 num_chars++;
1081 p1 = NULL;
1082 p2 = NULL;
1084 return num_chars;
1087 /****************************************************************************
1088 check if a string is part of a list
1089 ****************************************************************************/
1090 BOOL in_list(char *s,char *list,BOOL casesensitive)
1092 pstring tok;
1093 char *p=list;
1095 if (!list) return(False);
1097 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
1098 if (casesensitive) {
1099 if (strcmp(tok,s) == 0)
1100 return(True);
1101 } else {
1102 if (StrCaseCmp(tok,s) == 0)
1103 return(True);
1106 return(False);
1109 /* this is used to prevent lots of mallocs of size 1 */
1110 static char *null_string = NULL;
1112 /****************************************************************************
1113 set a string value, allocing the space for the string
1114 ****************************************************************************/
1115 static BOOL string_init(char **dest,const char *src)
1117 size_t l;
1118 if (!src)
1119 src = "";
1121 l = strlen(src);
1123 if (l == 0)
1125 if (!null_string) {
1126 if((null_string = (char *)malloc(1)) == NULL) {
1127 DEBUG(0,("string_init: malloc fail for null_string.\n"));
1128 return False;
1130 *null_string = 0;
1132 *dest = null_string;
1134 else
1136 (*dest) = (char *)malloc(l+1);
1137 if ((*dest) == NULL) {
1138 DEBUG(0,("Out of memory in string_init\n"));
1139 return False;
1142 pstrcpy(*dest,src);
1144 return(True);
1147 /****************************************************************************
1148 free a string value
1149 ****************************************************************************/
1150 void string_free(char **s)
1152 if (!s || !(*s)) return;
1153 if (*s == null_string)
1154 *s = NULL;
1155 SAFE_FREE(*s);
1158 /****************************************************************************
1159 set a string value, allocing the space for the string, and deallocating any
1160 existing space
1161 ****************************************************************************/
1162 BOOL string_set(char **dest,const char *src)
1164 string_free(dest);
1166 return(string_init(dest,src));
1170 /****************************************************************************
1171 substitute a string for a pattern in another string. Make sure there is
1172 enough room!
1174 This routine looks for pattern in s and replaces it with
1175 insert. It may do multiple replacements.
1177 any of " ; ' $ or ` in the insert string are replaced with _
1178 if len==0 then no expansion is permitted.
1179 ****************************************************************************/
1180 void string_sub(char *s,const char *pattern,const char *insert, size_t len)
1182 char *p;
1183 ssize_t ls,lp,li, i;
1185 if (!insert || !pattern || !s) return;
1187 ls = (ssize_t)strlen(s);
1188 lp = (ssize_t)strlen(pattern);
1189 li = (ssize_t)strlen(insert);
1191 if (!*pattern) return;
1193 if (len == 0)
1194 len = ls + 1; /* len is number of *bytes* */
1196 while (lp <= ls && (p = strstr(s,pattern))) {
1197 if (ls + (li-lp) >= len) {
1198 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
1199 (int)(ls + (li-lp) - len),
1200 pattern, (int)len));
1201 break;
1203 if (li != lp) {
1204 memmove(p+li,p+lp,strlen(p+lp)+1);
1206 for (i=0;i<li;i++) {
1207 switch (insert[i]) {
1208 case '`':
1209 case '"':
1210 case '\'':
1211 case ';':
1212 case '$':
1213 case '%':
1214 case '\r':
1215 case '\n':
1216 p[i] = '_';
1217 break;
1218 default:
1219 p[i] = insert[i];
1222 s = p + li;
1223 ls += (li-lp);
1227 void fstring_sub(char *s,const char *pattern,const char *insert)
1229 string_sub(s, pattern, insert, sizeof(fstring));
1232 void pstring_sub(char *s,const char *pattern,const char *insert)
1234 string_sub(s, pattern, insert, sizeof(pstring));
1237 /****************************************************************************
1238 similar to string_sub() but allows for any character to be substituted.
1239 Use with caution!
1240 if len==0 then no expansion is permitted.
1241 ****************************************************************************/
1242 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1244 char *p;
1245 ssize_t ls,lp,li;
1247 if (!insert || !pattern || !s) return;
1249 ls = (ssize_t)strlen(s);
1250 lp = (ssize_t)strlen(pattern);
1251 li = (ssize_t)strlen(insert);
1253 if (!*pattern) return;
1255 if (len == 0)
1256 len = ls + 1; /* len is number of *bytes* */
1258 while (lp <= ls && (p = strstr(s,pattern))) {
1259 if (ls + (li-lp) >= len) {
1260 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
1261 (int)(ls + (li-lp) - len),
1262 pattern, (int)len));
1263 break;
1265 if (li != lp) {
1266 memmove(p+li,p+lp,strlen(p+lp)+1);
1268 memcpy(p, insert, li);
1269 s = p + li;
1270 ls += (li-lp);
1274 /****************************************************************************
1275 splits out the front and back at a separator.
1276 ****************************************************************************/
1277 void split_at_last_component(char *path, char *front, char sep, char *back)
1279 char *p = strrchr(path, sep);
1281 if (p != NULL)
1283 *p = 0;
1285 if (front != NULL)
1287 pstrcpy(front, path);
1289 if (p != NULL)
1291 if (back != NULL)
1293 pstrcpy(back, p+1);
1295 *p = '\\';
1297 else
1299 if (back != NULL)
1301 back[0] = 0;
1307 /****************************************************************************
1308 write an octal as a string
1309 ****************************************************************************/
1310 char *octal_string(int i)
1312 static char ret[64];
1313 if (i == -1) {
1314 return "-1";
1316 slprintf(ret, sizeof(ret)-1, "0%o", i);
1317 return ret;
1321 /****************************************************************************
1322 truncate a string at a specified length
1323 ****************************************************************************/
1324 char *string_truncate(char *s, int length)
1326 if (s && strlen(s) > length) {
1327 s[length] = 0;
1329 return s;
1333 return a RFC2254 binary string representation of a buffer
1334 used in LDAP filters
1335 caller must free
1337 char *binary_string(char *buf, int len)
1339 char *s;
1340 int i, j;
1341 const char *hex = "0123456789ABCDEF";
1342 s = malloc(len * 3 + 1);
1343 if (!s) return NULL;
1344 for (j=i=0;i<len;i++) {
1345 s[j] = '\\';
1346 s[j+1] = hex[((unsigned char)buf[i]) >> 4];
1347 s[j+2] = hex[((unsigned char)buf[i]) & 0xF];
1348 j += 3;
1350 s[j] = 0;
1351 return s;
1354 #ifndef HAVE_STRNLEN
1355 /*******************************************************************
1356 Some platforms don't have strnlen
1357 ********************************************************************/
1359 size_t strnlen(const char *s, size_t n)
1361 int i;
1362 for (i=0; s[i] && i<n; i++)
1363 /* noop */ ;
1364 return i;
1366 #endif
1368 #ifndef HAVE_STRNDUP
1369 /*******************************************************************
1370 Some platforms don't have strndup.
1371 ********************************************************************/
1373 char *strndup(const char *s, size_t n)
1375 char *ret;
1377 n = strnlen(s, n);
1378 ret = malloc(n+1);
1379 if (!ret)
1380 return NULL;
1381 memcpy(ret, s, n);
1382 ret[n] = 0;
1384 return ret;
1386 #endif