add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / lib / util_str.c
blob23aa89e14b49d2d9b025fb8662a6b1c31f635aaa
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 ****************************************************************************/
38 BOOL next_token(char **ptr,char *buff,const char *sep, size_t bufsize)
40 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, char *sep)
86 char *s=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
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 up to and including the nth char.
288 ******************************************************************/
289 BOOL strnequal(const char *s1,const char *s2,size_t n)
291 if (s1 == s2) return(True);
292 if (!s1 || !s2 || !n) return(False);
294 return(StrnCaseCmp(s1,s2,n)==0);
297 /*******************************************************************
298 compare 2 strings (case sensitive)
299 ********************************************************************/
300 BOOL strcsequal(const char *s1,const char *s2)
302 if (s1 == s2) return(True);
303 if (!s1 || !s2) return(False);
305 return(strcmp(s1,s2)==0);
308 /***************************************************************************
309 Do a case-insensitive, whitespace-ignoring string compare.
310 ***************************************************************************/
311 int strwicmp(char *psz1, char *psz2)
313 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
314 /* appropriate value. */
315 if (psz1 == psz2)
316 return (0);
317 else if (psz1 == NULL)
318 return (-1);
319 else if (psz2 == NULL)
320 return (1);
322 /* sync the strings on first non-whitespace */
323 while (1)
325 while (isspace((int)*psz1))
326 psz1++;
327 while (isspace((int)*psz2))
328 psz2++;
329 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
330 || *psz2 == '\0')
331 break;
332 psz1++;
333 psz2++;
335 return (*psz1 - *psz2);
339 /*******************************************************************
340 convert a string to lower case
341 ********************************************************************/
342 void strlower(char *s)
344 while (*s)
346 #if !defined(KANJI_WIN95_COMPATIBILITY)
348 * For completeness we should put in equivalent code for code pages
349 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
350 * doubt anyone wants Samba to behave differently from Win95 and WinNT
351 * here. They both treat full width ascii characters as case senstive
352 * filenames (ie. they don't do the work we do here).
353 * JRA.
356 if(lp_client_code_page() == KANJI_CODEPAGE)
358 /* Win95 treats full width ascii characters as case sensitive. */
359 if (is_shift_jis (*s))
361 if (is_sj_upper (s[0], s[1]))
362 s[1] = sj_tolower2 (s[1]);
363 s += 2;
365 else if (is_kana (*s))
367 s++;
369 else
371 if (isupper(*s))
372 *s = tolower(*s);
373 s++;
376 else
377 #endif /* KANJI_WIN95_COMPATIBILITY */
379 size_t skip = get_character_len( *s );
380 if( skip != 0 )
381 s += skip;
382 else
384 if (isupper(*s))
385 *s = tolower(*s);
386 s++;
392 /*******************************************************************
393 convert a string to upper case
394 ********************************************************************/
395 void strupper(char *s)
397 while (*s)
399 #if !defined(KANJI_WIN95_COMPATIBILITY)
401 * For completeness we should put in equivalent code for code pages
402 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
403 * doubt anyone wants Samba to behave differently from Win95 and WinNT
404 * here. They both treat full width ascii characters as case senstive
405 * filenames (ie. they don't do the work we do here).
406 * JRA.
409 if(lp_client_code_page() == KANJI_CODEPAGE)
411 /* Win95 treats full width ascii characters as case sensitive. */
412 if (is_shift_jis (*s))
414 if (is_sj_lower (s[0], s[1]))
415 s[1] = sj_toupper2 (s[1]);
416 s += 2;
418 else if (is_kana (*s))
420 s++;
422 else
424 if (islower(*s))
425 *s = toupper(*s);
426 s++;
429 else
430 #endif /* KANJI_WIN95_COMPATIBILITY */
432 size_t skip = get_character_len( *s );
433 if( skip != 0 )
434 s += skip;
435 else
437 if (islower(*s))
438 *s = toupper(*s);
439 s++;
445 /* Convert a string to upper case, but don't modify it */
447 char *strupper_static(const char *s)
449 static pstring str;
451 pstrcpy(str, s);
452 strupper(str);
454 return str;
457 /*******************************************************************
458 convert a string to "normal" form
459 ********************************************************************/
460 void strnorm(char *s)
462 extern int case_default;
463 if (case_default == CASE_UPPER)
464 strupper(s);
465 else
466 strlower(s);
469 /*******************************************************************
470 check if a string is in "normal" case
471 ********************************************************************/
472 BOOL strisnormal(char *s)
474 extern int case_default;
475 if (case_default == CASE_UPPER)
476 return(!strhaslower(s));
478 return(!strhasupper(s));
482 /****************************************************************************
483 string replace
484 ****************************************************************************/
485 void string_replace(char *s,char oldc,char newc)
487 size_t skip;
490 * sbcs optimization.
492 if(!global_is_multibyte_codepage) {
493 while (*s) {
494 if (oldc == *s)
495 *s = newc;
496 s++;
498 } else {
499 while (*s)
501 skip = get_character_len( *s );
502 if( skip != 0 )
503 s += skip;
504 else
506 if (oldc == *s)
507 *s = newc;
508 s++;
515 /*******************************************************************
516 skip past some strings in a buffer
517 ********************************************************************/
518 char *skip_string(char *buf,size_t n)
520 while (n--)
521 buf += strlen(buf) + 1;
522 return(buf);
525 /*******************************************************************
526 Count the number of characters in a string. Normally this will
527 be the same as the number of bytes in a string for single byte strings,
528 but will be different for multibyte.
529 16.oct.98, jdblair@cobaltnet.com.
530 ********************************************************************/
532 size_t str_charnum(const char *s)
534 size_t len = 0;
537 * sbcs optimization.
539 if(!global_is_multibyte_codepage) {
540 return strlen(s);
541 } else {
542 while (*s != '\0') {
543 int skip = get_character_len(*s);
544 s += (skip ? skip : 1);
545 len++;
548 return len;
551 /*******************************************************************
552 trim the specified elements off the front and back of a string
553 ********************************************************************/
555 BOOL trim_string(char *s,const char *front,const char *back)
557 BOOL ret = False;
558 size_t s_len;
559 size_t front_len;
560 size_t back_len;
561 char *sP;
563 /* Ignore null or empty strings. */
565 if ( !s || (s[0] == '\0'))
566 return False;
568 sP = s;
569 s_len = strlen( s ) + 1;
570 front_len = (front) ? strlen( front ) + 1 : 0;
571 back_len = (back) ? strlen( back ) + 1 : 0;
574 * remove "front" string from given "s", if it matches front part,
575 * repeatedly.
577 if ( front && front_len > 1 ) {
578 while (( s_len >= front_len )&&
579 ( memcmp( sP, front, front_len - 1 )) == 0 ) {
580 ret = True;
581 sP += ( front_len - 1 );
582 s_len -= ( front_len - 1 );
587 * we'll memmove sP to s later, after we're done with
588 * back part removal, for minimizing copy.
593 * We split out the multibyte code page
594 * case here for speed purposes. Under a
595 * multibyte code page we need to walk the
596 * string forwards only and multiple times.
597 * Thanks to John Blair for finding this
598 * one. JRA.
601 * This JRA's comment is partly correct, but partly wrong.
602 * You can always check from "end" part, and if it did not match,
603 * it means there is no possibility of finding one.
604 * If you found matching point, mark them, then look from front
605 * if marking point suits multi-byte string rule.
606 * Kenichi Okuyama.
609 if ( back && back_len > 1 && s_len >= back_len) {
610 char *bP = sP + s_len - back_len;
611 long b_len = s_len;
613 while (( b_len >= back_len )&&
614 ( memcmp( bP, back, back_len - 1 ) == 0 )) {
615 bP -= ( back_len - 1 );
616 b_len -= ( back_len - 1 );
620 * You're here, means you ether have found match multiple times,
621 * or you found none. If you've found match, then bP should be
622 * moving.
624 if ( bP != sP + s_len - back_len ) {
625 bP += ( back_len - 1 ); /* slide bP to first matching point. */
627 if( !global_is_multibyte_codepage ) {
628 /* simply terminate */
629 (*bP) = '\0';
630 s_len = b_len;
631 ret = True;
632 } else {
633 /* trace string from start. */
634 char *cP = sP;
635 while ( cP < sP + s_len - back_len ) {
636 size_t skip;
637 skip = skip_multibyte_char( *cP );
638 cP += ( skip ? skip : 1 );
639 if ( cP == bP ) {
640 /* you found the match */
641 (*bP) = '\0';
642 ret = True;
643 s_len = b_len;
644 break;
646 while (( cP > bP )&&( bP < sP + s_len - back_len )) {
647 bP += ( back_len - 1 );
648 b_len += ( back_len - 1 );
655 /* if front found matching point */
656 if ( sP != s ) {
657 /* slide string to buffer top */
658 memmove( s, sP, s_len );
660 return ret;
664 /****************************************************************************
665 does a string have any uppercase chars in it?
666 ****************************************************************************/
667 BOOL strhasupper(const char *s)
669 while (*s)
671 #if !defined(KANJI_WIN95_COMPATIBILITY)
673 * For completeness we should put in equivalent code for code pages
674 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
675 * doubt anyone wants Samba to behave differently from Win95 and WinNT
676 * here. They both treat full width ascii characters as case senstive
677 * filenames (ie. they don't do the work we do here).
678 * JRA.
681 if(lp_client_code_page() == KANJI_CODEPAGE)
683 /* Win95 treats full width ascii characters as case sensitive. */
684 if (is_shift_jis (*s))
685 s += 2;
686 else if (is_kana (*s))
687 s++;
688 else
690 if (isupper(*s))
691 return(True);
692 s++;
695 else
696 #endif /* KANJI_WIN95_COMPATIBILITY */
698 size_t skip = get_character_len( *s );
699 if( skip != 0 )
700 s += skip;
701 else {
702 if (isupper(*s))
703 return(True);
704 s++;
708 return(False);
711 /****************************************************************************
712 does a string have any lowercase chars in it?
713 ****************************************************************************/
714 BOOL strhaslower(const char *s)
716 while (*s)
718 #if !defined(KANJI_WIN95_COMPATIBILITY)
720 * For completeness we should put in equivalent code for code pages
721 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
722 * doubt anyone wants Samba to behave differently from Win95 and WinNT
723 * here. They both treat full width ascii characters as case senstive
724 * filenames (ie. they don't do the work we do here).
725 * JRA.
728 if(lp_client_code_page() == KANJI_CODEPAGE)
730 /* Win95 treats full width ascii characters as case sensitive. */
731 if (is_shift_jis (*s))
733 if (is_sj_upper (s[0], s[1]))
734 return(True);
735 if (is_sj_lower (s[0], s[1]))
736 return (True);
737 s += 2;
739 else if (is_kana (*s))
741 s++;
743 else
745 if (islower(*s))
746 return(True);
747 s++;
750 else
751 #endif /* KANJI_WIN95_COMPATIBILITY */
753 size_t skip = get_character_len( *s );
754 if( skip != 0 )
755 s += skip;
756 else {
757 if (islower(*s))
758 return(True);
759 s++;
763 return(False);
766 /****************************************************************************
767 find the number of chars in a string
768 ****************************************************************************/
769 size_t count_chars(const char *s,char c)
771 size_t count=0;
773 #if !defined(KANJI_WIN95_COMPATIBILITY)
775 * For completeness we should put in equivalent code for code pages
776 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
777 * doubt anyone wants Samba to behave differently from Win95 and WinNT
778 * here. They both treat full width ascii characters as case senstive
779 * filenames (ie. they don't do the work we do here).
780 * JRA.
783 if(lp_client_code_page() == KANJI_CODEPAGE)
785 /* Win95 treats full width ascii characters as case sensitive. */
786 while (*s)
788 if (is_shift_jis (*s))
789 s += 2;
790 else
792 if (*s == c)
793 count++;
794 s++;
798 else
799 #endif /* KANJI_WIN95_COMPATIBILITY */
801 while (*s)
803 size_t skip = get_character_len( *s );
804 if( skip != 0 )
805 s += skip;
806 else {
807 if (*s == c)
808 count++;
809 s++;
813 return(count);
816 /*******************************************************************
817 Return True if a string consists only of one particular character.
818 ********************************************************************/
820 BOOL str_is_all(const char *s,char c)
822 if(s == NULL)
823 return False;
824 if(!*s)
825 return False;
827 #if !defined(KANJI_WIN95_COMPATIBILITY)
829 * For completeness we should put in equivalent code for code pages
830 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
831 * doubt anyone wants Samba to behave differently from Win95 and WinNT
832 * here. They both treat full width ascii characters as case senstive
833 * filenames (ie. they don't do the work we do here).
834 * JRA.
837 if(lp_client_code_page() == KANJI_CODEPAGE)
839 /* Win95 treats full width ascii characters as case sensitive. */
840 while (*s)
842 if (is_shift_jis (*s))
843 s += 2;
844 else
846 if (*s != c)
847 return False;
848 s++;
852 else
853 #endif /* KANJI_WIN95_COMPATIBILITY */
855 while (*s)
857 size_t skip = get_character_len( *s );
858 if( skip != 0 )
859 s += skip;
860 else {
861 if (*s != c)
862 return False;
863 s++;
867 return True;
870 /*******************************************************************
871 safe string copy into a known length string. maxlength does not
872 include the terminating zero.
873 ********************************************************************/
875 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
877 size_t len;
879 if (!dest) {
880 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
881 return NULL;
884 if (!src) {
885 *dest = 0;
886 return dest;
889 len = strlen(src);
891 if (len > maxlength) {
892 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
893 (int)(len-maxlength), src));
894 len = maxlength;
897 memcpy(dest, src, len);
898 dest[len] = 0;
899 return dest;
902 /*******************************************************************
903 safe string cat into a string. maxlength does not
904 include the terminating zero.
905 ********************************************************************/
907 char *safe_strcat(char *dest, const char *src, size_t maxlength)
909 size_t src_len, dest_len;
911 if (!dest) {
912 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
913 return NULL;
916 if (!src)
917 return dest;
919 src_len = strlen(src);
920 dest_len = strlen(dest);
922 if (src_len + dest_len > maxlength) {
923 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
924 (int)(src_len + dest_len - maxlength), src));
925 if (dest_len >= maxlength)
926 return dest;
927 src_len = maxlength - dest_len;
930 memcpy(&dest[dest_len], src, src_len);
931 dest[dest_len + src_len] = 0;
932 return dest;
935 /*******************************************************************
936 Paranoid strcpy into a buffer of given length (includes terminating
937 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
938 and replaces with '_'. Deliberately does *NOT* check for multibyte
939 characters. Don't change it !
940 ********************************************************************/
942 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
944 size_t len, i;
945 size_t buflen;
946 smb_ucs2_t *str_ucs, *other_ucs;
948 if (!dest) {
949 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
950 return NULL;
953 if (!src) {
954 *dest = 0;
955 return dest;
958 /* Get UCS2 version of src string*/
960 buflen=2*strlen(src)+2;
961 if (buflen >= (2*maxlength))
962 buflen = 2*(maxlength - 1);
964 str_ucs = (smb_ucs2_t*)malloc(buflen);
965 if(!str_ucs) {
966 *dest=0;
967 return dest;
969 unix_to_unicode(str_ucs, src, buflen);
970 len = strlen_w(str_ucs);
972 if (!other_safe_chars)
973 other_safe_chars = "";
975 /* Get UCS2 version of other_safe_chars string*/
976 buflen=2*strlen(other_safe_chars)+2;
977 other_ucs = (smb_ucs2_t*)malloc(buflen);
978 if(!other_ucs) {
979 *dest=0;
980 SAFE_FREE(str_ucs);
981 return dest;
983 unix_to_unicode(other_ucs, other_safe_chars, buflen);
985 for(i = 0; i < len; i++) {
986 if(isupper_w(str_ucs[i]) || islower_w(str_ucs[i]) || isdigit_w(str_ucs[i]) || strchr_w(other_ucs, str_ucs[i]))
988 else
989 str_ucs[i] = (smb_ucs2_t)'_'; /*This will work*/
992 unicode_to_unix(dest, str_ucs, maxlength);
994 SAFE_FREE(other_ucs);
995 SAFE_FREE(str_ucs);
997 return dest;
1000 /****************************************************************************
1001 Like strncpy but always null terminates. Make sure there is room!
1002 The variable n should always be one less than the available size.
1003 ****************************************************************************/
1005 char *StrnCpy(char *dest,const char *src,size_t n)
1007 char *d = dest;
1008 if (!dest) return(NULL);
1009 if (!src) {
1010 *dest = 0;
1011 return(dest);
1013 while (n-- && (*d++ = *src++)) ;
1014 *d = 0;
1015 return(dest);
1018 /****************************************************************************
1019 like strncpy but copies up to the character marker. always null terminates.
1020 returns a pointer to the character marker in the source string (src).
1021 ****************************************************************************/
1022 char *strncpyn(char *dest, const char *src,size_t n, char c)
1024 char *p;
1025 size_t str_len;
1027 p = strchr(src, c);
1028 if (p == NULL)
1030 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
1031 return NULL;
1034 str_len = PTR_DIFF(p, src);
1035 strncpy(dest, src, MIN(n, str_len));
1036 dest[str_len] = '\0';
1038 return p;
1042 /*************************************************************
1043 Routine to get hex characters and turn them into a 16 byte array.
1044 the array can be variable length, and any non-hex-numeric
1045 characters are skipped. "0xnn" or "0Xnn" is specially catered
1046 for.
1048 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1050 **************************************************************/
1051 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1053 size_t i;
1054 size_t num_chars = 0;
1055 unsigned char lonybble, hinybble;
1056 char *hexchars = "0123456789ABCDEF";
1057 char *p1 = NULL, *p2 = NULL;
1059 for (i = 0; i < len && strhex[i] != 0; i++)
1061 if (strnequal(hexchars, "0x", 2))
1063 i++; /* skip two chars */
1064 continue;
1067 if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
1069 break;
1072 i++; /* next hex digit */
1074 if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
1076 break;
1079 /* get the two nybbles */
1080 hinybble = PTR_DIFF(p1, hexchars);
1081 lonybble = PTR_DIFF(p2, hexchars);
1083 p[num_chars] = (hinybble << 4) | lonybble;
1084 num_chars++;
1086 p1 = NULL;
1087 p2 = NULL;
1089 return num_chars;
1092 /****************************************************************************
1093 check if a string is part of a list
1094 ****************************************************************************/
1095 BOOL in_list(char *s,char *list,BOOL casesensitive)
1097 pstring tok;
1098 char *p=list;
1100 if (!list) return(False);
1102 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
1103 if (casesensitive) {
1104 if (strcmp(tok,s) == 0)
1105 return(True);
1106 } else {
1107 if (StrCaseCmp(tok,s) == 0)
1108 return(True);
1111 return(False);
1114 /* this is used to prevent lots of mallocs of size 1 */
1115 static char *null_string = NULL;
1117 /****************************************************************************
1118 set a string value, allocing the space for the string
1119 ****************************************************************************/
1120 static BOOL string_init(char **dest,const char *src)
1122 size_t l;
1123 if (!src)
1124 src = "";
1126 l = strlen(src);
1128 if (l == 0)
1130 if (!null_string) {
1131 if((null_string = (char *)malloc(1)) == NULL) {
1132 DEBUG(0,("string_init: malloc fail for null_string.\n"));
1133 return False;
1135 *null_string = 0;
1137 *dest = null_string;
1139 else
1141 (*dest) = (char *)malloc(l+1);
1142 if ((*dest) == NULL) {
1143 DEBUG(0,("Out of memory in string_init\n"));
1144 return False;
1147 pstrcpy(*dest,src);
1149 return(True);
1152 /****************************************************************************
1153 free a string value
1154 ****************************************************************************/
1155 void string_free(char **s)
1157 if (!s || !(*s)) return;
1158 if (*s == null_string)
1159 *s = NULL;
1160 SAFE_FREE(*s);
1163 /****************************************************************************
1164 set a string value, allocing the space for the string, and deallocating any
1165 existing space
1166 ****************************************************************************/
1167 BOOL string_set(char **dest,const char *src)
1169 string_free(dest);
1171 return(string_init(dest,src));
1175 /****************************************************************************
1176 substitute a string for a pattern in another string. Make sure there is
1177 enough room!
1179 This routine looks for pattern in s and replaces it with
1180 insert. It may do multiple replacements.
1182 any of " ; ' $ or ` in the insert string are replaced with _
1183 if len==0 then no expansion is permitted.
1184 ****************************************************************************/
1185 void string_sub(char *s,const char *pattern,const char *insert, size_t len)
1187 char *p;
1188 ssize_t ls,lp,li, i;
1190 if (!insert || !pattern || !s) return;
1192 ls = (ssize_t)strlen(s);
1193 lp = (ssize_t)strlen(pattern);
1194 li = (ssize_t)strlen(insert);
1196 if (!*pattern) return;
1198 if (len == 0)
1199 len = ls + 1; /* len is number of *bytes* */
1201 while (lp <= ls && (p = strstr(s,pattern))) {
1202 if (ls + (li-lp) >= len) {
1203 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
1204 (int)(ls + (li-lp) - len),
1205 pattern, (int)len));
1206 break;
1208 if (li != lp) {
1209 memmove(p+li,p+lp,strlen(p+lp)+1);
1211 for (i=0;i<li;i++) {
1212 switch (insert[i]) {
1213 case '`':
1214 case '"':
1215 case '\'':
1216 case ';':
1217 case '$':
1218 case '%':
1219 case '\r':
1220 case '\n':
1221 p[i] = '_';
1222 break;
1223 default:
1224 p[i] = insert[i];
1227 s = p + li;
1228 ls += (li-lp);
1232 void fstring_sub(char *s,const char *pattern,const char *insert)
1234 string_sub(s, pattern, insert, sizeof(fstring));
1237 void pstring_sub(char *s,const char *pattern,const char *insert)
1239 string_sub(s, pattern, insert, sizeof(pstring));
1242 /****************************************************************************
1243 similar to string_sub() but allows for any character to be substituted.
1244 Use with caution!
1245 if len==0 then no expansion is permitted.
1246 ****************************************************************************/
1247 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1249 char *p;
1250 ssize_t ls,lp,li;
1252 if (!insert || !pattern || !s) return;
1254 ls = (ssize_t)strlen(s);
1255 lp = (ssize_t)strlen(pattern);
1256 li = (ssize_t)strlen(insert);
1258 if (!*pattern) return;
1260 if (len == 0)
1261 len = ls + 1; /* len is number of *bytes* */
1263 while (lp <= ls && (p = strstr(s,pattern))) {
1264 if (ls + (li-lp) >= len) {
1265 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
1266 (int)(ls + (li-lp) - len),
1267 pattern, (int)len));
1268 break;
1270 if (li != lp) {
1271 memmove(p+li,p+lp,strlen(p+lp)+1);
1273 memcpy(p, insert, li);
1274 s = p + li;
1275 ls += (li-lp);
1279 /****************************************************************************
1280 splits out the front and back at a separator.
1281 ****************************************************************************/
1282 void split_at_last_component(char *path, char *front, char sep, char *back)
1284 char *p = strrchr(path, sep);
1286 if (p != NULL)
1288 *p = 0;
1290 if (front != NULL)
1292 pstrcpy(front, path);
1294 if (p != NULL)
1296 if (back != NULL)
1298 pstrcpy(back, p+1);
1300 *p = '\\';
1302 else
1304 if (back != NULL)
1306 back[0] = 0;
1312 /****************************************************************************
1313 write an octal as a string
1314 ****************************************************************************/
1315 char *octal_string(int i)
1317 static char ret[64];
1318 if (i == -1) {
1319 return "-1";
1321 slprintf(ret, sizeof(ret)-1, "0%o", i);
1322 return ret;
1326 /****************************************************************************
1327 truncate a string at a specified length
1328 ****************************************************************************/
1329 char *string_truncate(char *s, int length)
1331 if (s && strlen(s) > length) {
1332 s[length] = 0;
1334 return s;
1338 return a RFC2254 binary string representation of a buffer
1339 used in LDAP filters
1340 caller must free
1342 char *binary_string(char *buf, int len)
1344 char *s;
1345 int i, j;
1346 const char *hex = "0123456789ABCDEF";
1347 s = malloc(len * 3 + 1);
1348 if (!s) return NULL;
1349 for (j=i=0;i<len;i++) {
1350 s[j] = '\\';
1351 s[j+1] = hex[((unsigned char)buf[i]) >> 4];
1352 s[j+2] = hex[((unsigned char)buf[i]) & 0xF];
1353 j += 3;
1355 s[j] = 0;
1356 return s;
1359 #ifndef HAVE_STRNLEN
1360 /*******************************************************************
1361 Some platforms don't have strnlen
1362 ********************************************************************/
1364 size_t strnlen(const char *s, size_t n)
1366 int i;
1367 for (i=0; s[i] && i<n; i++)
1368 /* noop */ ;
1369 return i;
1371 #endif
1373 #ifndef HAVE_STRNDUP
1374 /*******************************************************************
1375 Some platforms don't have strndup.
1376 ********************************************************************/
1378 char *strndup(const char *s, size_t n)
1380 char *ret;
1382 n = strnlen(s, n);
1383 ret = malloc(n+1);
1384 if (!ret)
1385 return NULL;
1386 memcpy(ret, s, n);
1387 ret[n] = 0;
1389 return ret;
1391 #endif