2 Unix SMB/Netbios implementation.
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.
24 extern int DEBUGLEVEL;
26 static char *last_ptr=NULL;
28 void set_first_token(char *ptr)
33 /****************************************************************************
34 Get the next token from a string, return False if none found
35 handles double-quotes.
36 Based on a routine by GJC@VILLAGE.COM.
37 Extensively modified by Andrew.Tridgell@anu.edu.au
38 ****************************************************************************/
39 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
45 if (!ptr) ptr = &last_ptr;
46 if (!ptr) return(False);
50 /* default to simple separators */
51 if (!sep) sep = " \t\n\r";
53 /* find the first non sep char */
54 while(*s && strchr(sep,*s)) s++;
57 if (! *s) return(False);
59 /* copy over the token */
60 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++)
70 *ptr = (*s) ? s+1 : s;
77 /****************************************************************************
78 Convert list of tokens to array; dependent on above routine.
79 Uses last_ptr from above - bit of a hack.
80 ****************************************************************************/
81 char **toktocliplist(int *ctok, char *sep)
87 if (!sep) sep = " \t\n\r";
89 while(*s && strchr(sep,*s)) s++;
92 if (!*s) return(NULL);
96 while(*s && (!strchr(sep,*s))) s++;
97 while(*s && strchr(sep,*s)) *s++=0;
103 if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
115 /*******************************************************************
116 case insensitive string compararison
117 ********************************************************************/
118 int StrCaseCmp(const char *s, const char *t)
120 /* compare until we run out of string, either t or s, or find a difference */
121 /* We *must* use toupper rather than tolower here due to the
122 asynchronous upper to lower mapping.
124 #if !defined(KANJI_WIN95_COMPATIBILITY)
126 * For completeness we should put in equivalent code for code pages
127 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
128 * doubt anyone wants Samba to behave differently from Win95 and WinNT
129 * here. They both treat full width ascii characters as case senstive
130 * filenames (ie. they don't do the work we do here).
134 if(lp_client_code_page() == KANJI_CODEPAGE)
136 /* Win95 treats full width ascii characters as case sensitive. */
141 return toupper (*s) - toupper (*t);
142 else if (is_sj_alph (*s) && is_sj_alph (*t))
144 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
150 else if (is_shift_jis (*s) && is_shift_jis (*t))
152 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
155 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
161 else if (is_shift_jis (*s))
163 else if (is_shift_jis (*t))
167 diff = toupper (*s) - toupper (*t);
176 #endif /* KANJI_WIN95_COMPATIBILITY */
178 while (*s && *t && toupper(*s) == toupper(*t))
184 return(toupper(*s) - toupper(*t));
188 /*******************************************************************
189 case insensitive string compararison, length limited
190 ********************************************************************/
191 int StrnCaseCmp(const char *s, const char *t, size_t n)
193 /* compare until we run out of string, either t or s, or chars */
194 /* We *must* use toupper rather than tolower here due to the
195 asynchronous upper to lower mapping.
197 #if !defined(KANJI_WIN95_COMPATIBILITY)
199 * For completeness we should put in equivalent code for code pages
200 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
201 * doubt anyone wants Samba to behave differently from Win95 and WinNT
202 * here. They both treat full width ascii characters as case senstive
203 * filenames (ie. they don't do the work we do here).
207 if(lp_client_code_page() == KANJI_CODEPAGE)
209 /* Win95 treats full width ascii characters as case sensitive. */
214 return toupper (*s) - toupper (*t);
215 else if (is_sj_alph (*s) && is_sj_alph (*t))
217 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
224 else if (is_shift_jis (*s) && is_shift_jis (*t))
226 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
229 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
236 else if (is_shift_jis (*s))
238 else if (is_shift_jis (*t))
242 diff = toupper (*s) - toupper (*t);
253 #endif /* KANJI_WIN95_COMPATIBILITY */
255 while (n && *s && *t && toupper(*s) == toupper(*t))
262 /* not run out of chars - strings are different lengths */
264 return(toupper(*s) - toupper(*t));
266 /* identical up to where we run out of chars,
267 and strings are same length */
272 /*******************************************************************
274 ********************************************************************/
275 BOOL strequal(const char *s1, const char *s2)
277 if (s1 == s2) return(True);
278 if (!s1 || !s2) return(False);
280 return(StrCaseCmp(s1,s2)==0);
283 /*******************************************************************
284 compare 2 strings up to and including the nth char.
285 ******************************************************************/
286 BOOL strnequal(const char *s1,const char *s2,size_t n)
288 if (s1 == s2) return(True);
289 if (!s1 || !s2 || !n) return(False);
291 return(StrnCaseCmp(s1,s2,n)==0);
294 /*******************************************************************
295 compare 2 strings (case sensitive)
296 ********************************************************************/
297 BOOL strcsequal(const char *s1,const char *s2)
299 if (s1 == s2) return(True);
300 if (!s1 || !s2) return(False);
302 return(strcmp(s1,s2)==0);
306 /*******************************************************************
307 convert a string to lower case
308 ********************************************************************/
309 void strlower(char *s)
313 #if !defined(KANJI_WIN95_COMPATIBILITY)
315 * For completeness we should put in equivalent code for code pages
316 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
317 * doubt anyone wants Samba to behave differently from Win95 and WinNT
318 * here. They both treat full width ascii characters as case senstive
319 * filenames (ie. they don't do the work we do here).
323 if(lp_client_code_page() == KANJI_CODEPAGE)
325 /* Win95 treats full width ascii characters as case sensitive. */
326 if (is_shift_jis (*s))
328 if (is_sj_upper (s[0], s[1]))
329 s[1] = sj_tolower2 (s[1]);
332 else if (is_kana (*s))
344 #endif /* KANJI_WIN95_COMPATIBILITY */
346 size_t skip = skip_multibyte_char( *s );
359 /*******************************************************************
360 convert a string to upper case
361 ********************************************************************/
362 void strupper(char *s)
366 #if !defined(KANJI_WIN95_COMPATIBILITY)
368 * For completeness we should put in equivalent code for code pages
369 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
370 * doubt anyone wants Samba to behave differently from Win95 and WinNT
371 * here. They both treat full width ascii characters as case senstive
372 * filenames (ie. they don't do the work we do here).
376 if(lp_client_code_page() == KANJI_CODEPAGE)
378 /* Win95 treats full width ascii characters as case sensitive. */
379 if (is_shift_jis (*s))
381 if (is_sj_lower (s[0], s[1]))
382 s[1] = sj_toupper2 (s[1]);
385 else if (is_kana (*s))
397 #endif /* KANJI_WIN95_COMPATIBILITY */
399 size_t skip = skip_multibyte_char( *s );
412 /*******************************************************************
413 convert a string to "normal" form
414 ********************************************************************/
415 void strnorm(char *s)
417 extern int case_default;
418 if (case_default == CASE_UPPER)
424 /*******************************************************************
425 check if a string is in "normal" case
426 ********************************************************************/
427 BOOL strisnormal(char *s)
429 extern int case_default;
430 if (case_default == CASE_UPPER)
431 return(!strhaslower(s));
433 return(!strhasupper(s));
437 /****************************************************************************
439 ****************************************************************************/
440 void string_replace(char *s,char oldc,char newc)
445 skip = skip_multibyte_char( *s );
458 /*******************************************************************
459 skip past some strings in a buffer
460 ********************************************************************/
461 char *skip_string(const char *buf,size_t n)
464 buf += strlen(buf) + 1;
468 /*******************************************************************
469 Count the number of characters in a string. Normally this will
470 be the same as the number of bytes in a string for single byte strings,
471 but will be different for multibyte.
472 16.oct.98, jdblair@cobaltnet.com.
473 ********************************************************************/
475 size_t str_charnum(const char *s)
480 int skip = skip_multibyte_char(*s);
481 s += (skip ? skip : 1);
487 /*******************************************************************
488 trim the specified elements off the front and back of a string
489 ********************************************************************/
491 BOOL trim_string(char *s,const char *front,const char *back)
494 size_t front_len = (front && *front) ? strlen(front) : 0;
495 size_t back_len = (back && *back) ? strlen(back) : 0;
498 while (front_len && strncmp(s, front, front_len) == 0)
504 if (!(*p = p[front_len]))
511 * We split out the multibyte code page
512 * case here for speed purposes. Under a
513 * multibyte code page we need to walk the
514 * string forwards only and multiple times.
515 * Thanks to John Blair for finding this
521 if(!is_multibyte_codepage())
524 while ((s_len >= back_len) &&
525 (strncmp(s + s_len - back_len, back, back_len)==0))
528 s[s_len - back_len] = '\0';
536 * Multibyte code page case.
537 * Keep going through the string, trying
538 * to match the 'back' string with the end
539 * of the string. If we get a match, truncate
540 * 'back' off the end of the string and
541 * go through the string again from the
542 * start. Keep doing this until we have
543 * gone through the string with no match
547 size_t mb_back_len = str_charnum(back);
548 size_t mb_s_len = str_charnum(s);
550 while(mb_s_len >= mb_back_len)
552 size_t charcount = 0;
555 while(charcount < (mb_s_len - mb_back_len))
557 size_t skip = skip_multibyte_char(*mbp);
558 mbp += (skip ? skip : 1);
563 * mbp now points at mb_back_len multibyte
564 * characters from the end of s.
567 if(strcmp(mbp, back) == 0)
571 mb_s_len = str_charnum(s);
576 } /* end while mb_s_len... */
578 } /* end if back_len .. */
584 /****************************************************************************
585 does a string have any uppercase chars in it?
586 ****************************************************************************/
587 BOOL strhasupper(const char *s)
591 #if !defined(KANJI_WIN95_COMPATIBILITY)
593 * For completeness we should put in equivalent code for code pages
594 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
595 * doubt anyone wants Samba to behave differently from Win95 and WinNT
596 * here. They both treat full width ascii characters as case senstive
597 * filenames (ie. they don't do the work we do here).
601 if(lp_client_code_page() == KANJI_CODEPAGE)
603 /* Win95 treats full width ascii characters as case sensitive. */
604 if (is_shift_jis (*s))
606 else if (is_kana (*s))
616 #endif /* KANJI_WIN95_COMPATIBILITY */
618 size_t skip = skip_multibyte_char( *s );
631 /****************************************************************************
632 does a string have any lowercase chars in it?
633 ****************************************************************************/
634 BOOL strhaslower(const char *s)
638 #if !defined(KANJI_WIN95_COMPATIBILITY)
640 * For completeness we should put in equivalent code for code pages
641 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
642 * doubt anyone wants Samba to behave differently from Win95 and WinNT
643 * here. They both treat full width ascii characters as case senstive
644 * filenames (ie. they don't do the work we do here).
648 if(lp_client_code_page() == KANJI_CODEPAGE)
650 /* Win95 treats full width ascii characters as case sensitive. */
651 if (is_shift_jis (*s))
653 if (is_sj_upper (s[0], s[1]))
655 if (is_sj_lower (s[0], s[1]))
659 else if (is_kana (*s))
671 #endif /* KANJI_WIN95_COMPATIBILITY */
673 size_t skip = skip_multibyte_char( *s );
686 /****************************************************************************
687 find the number of chars in a string
688 ****************************************************************************/
689 size_t count_chars(const char *s,char c)
693 #if !defined(KANJI_WIN95_COMPATIBILITY)
695 * For completeness we should put in equivalent code for code pages
696 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
697 * doubt anyone wants Samba to behave differently from Win95 and WinNT
698 * here. They both treat full width ascii characters as case senstive
699 * filenames (ie. they don't do the work we do here).
703 if(lp_client_code_page() == KANJI_CODEPAGE)
705 /* Win95 treats full width ascii characters as case sensitive. */
708 if (is_shift_jis (*s))
719 #endif /* KANJI_WIN95_COMPATIBILITY */
723 size_t skip = skip_multibyte_char( *s );
738 /*******************************************************************
739 safe string copy into a known length string. maxlength does not
740 include the terminating zero.
741 ********************************************************************/
742 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
747 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
758 if (len > maxlength) {
759 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
760 len-maxlength, src));
764 memcpy(dest, src, len);
769 /*******************************************************************
770 safe string cat into a string. maxlength does not
771 include the terminating zero.
772 ********************************************************************/
773 char *safe_strcat(char *dest, const char *src, size_t maxlength)
775 size_t src_len, dest_len;
778 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
786 src_len = strlen(src);
787 dest_len = strlen(dest);
789 if (src_len + dest_len > maxlength) {
790 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
791 src_len + dest_len - maxlength, src));
792 src_len = maxlength - dest_len;
795 memcpy(&dest[dest_len], src, src_len);
796 dest[dest_len + src_len] = 0;
800 /****************************************************************************
801 this is a safer strcpy(), meant to prevent core dumps when nasty things happen
802 ****************************************************************************/
803 char *StrCpy(char *dest,const char *src)
807 /* I don't want to get lazy with these ... */
808 SMB_ASSERT(dest && src);
810 if (!dest) return(NULL);
815 while ((*d++ = *src++)) ;
819 /****************************************************************************
820 like strncpy but always null terminates. Make sure there is room!
821 ****************************************************************************/
822 char *StrnCpy(char *dest,const char *src,size_t n)
825 if (!dest) return(NULL);
830 while (n-- && (*d++ = *src++)) ;
836 /****************************************************************************
837 like strncpy but copies up to the character marker. always null terminates.
838 returns a pointer to the character marker in the source string (src).
839 ****************************************************************************/
840 char *strncpyn(char *dest, const char *src,size_t n, char c)
848 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
852 str_len = PTR_DIFF(p, src);
853 strncpy(dest, src, MIN(n, str_len));
854 dest[str_len] = '\0';
860 /*************************************************************
861 Routine to get hex characters and turn them into a 16 byte array.
862 the array can be variable length, and any non-hex-numeric
863 characters are skipped. "0xnn" or "0Xnn" is specially catered
866 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
868 **************************************************************/
869 size_t strhex_to_str(char *p, size_t len, const char *strhex)
872 size_t num_chars = 0;
873 unsigned char lonybble, hinybble;
874 char *hexchars = "0123456789ABCDEF";
875 char *p1 = NULL, *p2 = NULL;
877 for (i = 0; i < len && strhex[i] != 0; i++)
879 if (strnequal(hexchars, "0x", 2))
881 i++; /* skip two chars */
885 while (!(p1 = strchr(hexchars, toupper(strhex[i]))))
890 i++; /* next hex digit */
892 while (!(p2 = strchr(hexchars, toupper(strhex[i]))))
897 /* get the two nybbles */
898 hinybble = PTR_DIFF(p1, hexchars);
899 lonybble = PTR_DIFF(p2, hexchars);
901 p[num_chars] = (hinybble << 4) | lonybble;
910 /****************************************************************************
911 check if a string is part of a list
912 ****************************************************************************/
913 BOOL in_list(char *s,char *list,BOOL casesensitive)
918 if (!list) return(False);
920 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
922 if (strcmp(tok,s) == 0)
925 if (StrCaseCmp(tok,s) == 0)
932 /* this is used to prevent lots of mallocs of size 1 */
933 static char *null_string = NULL;
935 /****************************************************************************
936 set a string value, allocing the space for the string
937 ****************************************************************************/
938 BOOL string_init(char **dest,const char *src)
949 if((null_string = (char *)malloc(1)) == NULL) {
950 DEBUG(0,("string_init: malloc fail for null_string.\n"));
959 (*dest) = (char *)malloc(l+1);
960 if ((*dest) == NULL) {
961 DEBUG(0,("Out of memory in string_init\n"));
970 /****************************************************************************
972 ****************************************************************************/
973 void string_free(char **s)
975 if (!s || !(*s)) return;
976 if (*s == null_string)
982 /****************************************************************************
983 set a string value, allocing the space for the string, and deallocating any
985 ****************************************************************************/
986 BOOL string_set(char **dest,const char *src)
990 return(string_init(dest,src));
993 /****************************************************************************
994 substitute a string for a pattern in another string. Make sure there is
997 This routine looks for pattern in s and replaces it with
998 insert. It may do multiple replacements.
1000 return True if a substitution was done.
1001 ****************************************************************************/
1002 BOOL string_sub(char *s,const char *pattern,const char *insert)
1008 if (!insert || !pattern || !s) return(False);
1011 lp = strlen(pattern);
1012 li = strlen(insert);
1014 if (!*pattern) return(False);
1016 while (lp <= ls && (p = strstr(s,pattern)))
1019 memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
1020 memcpy(p,insert,li);
1027 /****************************************************************************
1028 splits out the front and back at a separator.
1029 ****************************************************************************/
1030 void split_at_last_component(char *path, char *front, char sep, char *back)
1032 char *p = strrchr(path, sep);
1040 pstrcpy(front, path);