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);
305 /***************************************************************************
306 Do a case-insensitive, whitespace-ignoring string compare.
307 ***************************************************************************/
308 int strwicmp(char *psz1, char *psz2)
310 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
311 /* appropriate value. */
314 else if (psz1 == NULL)
316 else if (psz2 == NULL)
319 /* sync the strings on first non-whitespace */
322 while (isspace(*psz1))
324 while (isspace(*psz2))
326 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
332 return (*psz1 - *psz2);
336 /*******************************************************************
337 convert a string to lower case
338 ********************************************************************/
339 void strlower(char *s)
343 #if !defined(KANJI_WIN95_COMPATIBILITY)
345 * For completeness we should put in equivalent code for code pages
346 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
347 * doubt anyone wants Samba to behave differently from Win95 and WinNT
348 * here. They both treat full width ascii characters as case senstive
349 * filenames (ie. they don't do the work we do here).
353 if(lp_client_code_page() == KANJI_CODEPAGE)
355 /* Win95 treats full width ascii characters as case sensitive. */
356 if (is_shift_jis (*s))
358 if (is_sj_upper (s[0], s[1]))
359 s[1] = sj_tolower2 (s[1]);
362 else if (is_kana (*s))
374 #endif /* KANJI_WIN95_COMPATIBILITY */
376 size_t skip = get_character_len( *s );
389 /*******************************************************************
390 convert a string to upper case
391 ********************************************************************/
392 void strupper(char *s)
396 #if !defined(KANJI_WIN95_COMPATIBILITY)
398 * For completeness we should put in equivalent code for code pages
399 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
400 * doubt anyone wants Samba to behave differently from Win95 and WinNT
401 * here. They both treat full width ascii characters as case senstive
402 * filenames (ie. they don't do the work we do here).
406 if(lp_client_code_page() == KANJI_CODEPAGE)
408 /* Win95 treats full width ascii characters as case sensitive. */
409 if (is_shift_jis (*s))
411 if (is_sj_lower (s[0], s[1]))
412 s[1] = sj_toupper2 (s[1]);
415 else if (is_kana (*s))
427 #endif /* KANJI_WIN95_COMPATIBILITY */
429 size_t skip = get_character_len( *s );
442 /*******************************************************************
443 convert a string to "normal" form
444 ********************************************************************/
445 void strnorm(char *s)
447 extern int case_default;
448 if (case_default == CASE_UPPER)
454 /*******************************************************************
455 check if a string is in "normal" case
456 ********************************************************************/
457 BOOL strisnormal(char *s)
459 extern int case_default;
460 if (case_default == CASE_UPPER)
461 return(!strhaslower(s));
463 return(!strhasupper(s));
467 /****************************************************************************
469 ****************************************************************************/
470 void string_replace(char *s,char oldc,char newc)
477 if(!global_is_multibyte_codepage) {
486 skip = get_character_len( *s );
500 /*******************************************************************
501 skip past some strings in a buffer
502 ********************************************************************/
503 char *skip_string(char *buf,size_t n)
506 buf += strlen(buf) + 1;
510 /*******************************************************************
511 Count the number of characters in a string. Normally this will
512 be the same as the number of bytes in a string for single byte strings,
513 but will be different for multibyte.
514 16.oct.98, jdblair@cobaltnet.com.
515 ********************************************************************/
517 size_t str_charnum(const char *s)
524 if(!global_is_multibyte_codepage) {
528 int skip = get_character_len(*s);
529 s += (skip ? skip : 1);
536 /*******************************************************************
537 trim the specified elements off the front and back of a string
538 ********************************************************************/
540 BOOL trim_string(char *s,const char *front,const char *back)
548 /* Ignore null or empty strings. */
550 if ( !s || (s[0] == '\0'))
554 s_len = strlen( s ) + 1;
555 front_len = (front) ? strlen( front ) + 1 : 0;
556 back_len = (back) ? strlen( back ) + 1 : 0;
559 * remove "front" string from given "s", if it matches front part,
562 if ( front && front_len > 1 ) {
563 while (( s_len >= front_len )&&
564 ( memcmp( sP, front, front_len - 1 )) == 0 ) {
566 sP += ( front_len - 1 );
567 s_len -= ( front_len - 1 );
572 * we'll memmove sP to s later, after we're done with
573 * back part removal, for minimizing copy.
578 * We split out the multibyte code page
579 * case here for speed purposes. Under a
580 * multibyte code page we need to walk the
581 * string forwards only and multiple times.
582 * Thanks to John Blair for finding this
586 * This JRA's comment is partly correct, but partly wrong.
587 * You can always check from "end" part, and if it did not match,
588 * it means there is no possibility of finding one.
589 * If you found matching point, mark them, then look from front
590 * if marking point suits multi-byte string rule.
594 if ( back && back_len > 1 && s_len > back_len) {
595 char *bP = sP + s_len - back_len;
598 while (( b_len >= back_len )&&
599 ( memcmp( bP, back, back_len - 1 ) == 0 )) {
600 bP -= ( back_len - 1 );
601 b_len -= ( back_len - 1 );
605 * You're here, means you ether have found match multiple times,
606 * or you found none. If you've found match, then bP should be
609 if ( bP != sP + s_len - back_len ) {
610 bP += ( back_len - 1 ); /* slide bP to first matching point. */
612 if( !global_is_multibyte_codepage ) {
613 /* simply terminate */
618 /* trace string from start. */
620 while ( cP < sP + s_len - back_len ) {
622 skip = skip_multibyte_char( *cP );
623 cP += ( skip ? skip : 1 );
625 /* you found the match */
631 while (( cP > bP )&&( bP < sP + s_len - back_len )) {
632 bP += ( back_len - 1 );
633 b_len += ( back_len - 1 );
640 /* if front found matching point */
642 /* slide string to buffer top */
643 memmove( s, sP, s_len );
649 /****************************************************************************
650 does a string have any uppercase chars in it?
651 ****************************************************************************/
652 BOOL strhasupper(const char *s)
656 #if !defined(KANJI_WIN95_COMPATIBILITY)
658 * For completeness we should put in equivalent code for code pages
659 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
660 * doubt anyone wants Samba to behave differently from Win95 and WinNT
661 * here. They both treat full width ascii characters as case senstive
662 * filenames (ie. they don't do the work we do here).
666 if(lp_client_code_page() == KANJI_CODEPAGE)
668 /* Win95 treats full width ascii characters as case sensitive. */
669 if (is_shift_jis (*s))
671 else if (is_kana (*s))
681 #endif /* KANJI_WIN95_COMPATIBILITY */
683 size_t skip = get_character_len( *s );
696 /****************************************************************************
697 does a string have any lowercase chars in it?
698 ****************************************************************************/
699 BOOL strhaslower(const char *s)
703 #if !defined(KANJI_WIN95_COMPATIBILITY)
705 * For completeness we should put in equivalent code for code pages
706 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
707 * doubt anyone wants Samba to behave differently from Win95 and WinNT
708 * here. They both treat full width ascii characters as case senstive
709 * filenames (ie. they don't do the work we do here).
713 if(lp_client_code_page() == KANJI_CODEPAGE)
715 /* Win95 treats full width ascii characters as case sensitive. */
716 if (is_shift_jis (*s))
718 if (is_sj_upper (s[0], s[1]))
720 if (is_sj_lower (s[0], s[1]))
724 else if (is_kana (*s))
736 #endif /* KANJI_WIN95_COMPATIBILITY */
738 size_t skip = get_character_len( *s );
751 /****************************************************************************
752 find the number of chars in a string
753 ****************************************************************************/
754 size_t count_chars(const char *s,char c)
758 #if !defined(KANJI_WIN95_COMPATIBILITY)
760 * For completeness we should put in equivalent code for code pages
761 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
762 * doubt anyone wants Samba to behave differently from Win95 and WinNT
763 * here. They both treat full width ascii characters as case senstive
764 * filenames (ie. they don't do the work we do here).
768 if(lp_client_code_page() == KANJI_CODEPAGE)
770 /* Win95 treats full width ascii characters as case sensitive. */
773 if (is_shift_jis (*s))
784 #endif /* KANJI_WIN95_COMPATIBILITY */
788 size_t skip = get_character_len( *s );
801 /*******************************************************************
802 Return True if a string consists only of one particular character.
803 ********************************************************************/
805 BOOL str_is_all(const char *s,char c)
812 #if !defined(KANJI_WIN95_COMPATIBILITY)
814 * For completeness we should put in equivalent code for code pages
815 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
816 * doubt anyone wants Samba to behave differently from Win95 and WinNT
817 * here. They both treat full width ascii characters as case senstive
818 * filenames (ie. they don't do the work we do here).
822 if(lp_client_code_page() == KANJI_CODEPAGE)
824 /* Win95 treats full width ascii characters as case sensitive. */
827 if (is_shift_jis (*s))
838 #endif /* KANJI_WIN95_COMPATIBILITY */
842 size_t skip = get_character_len( *s );
855 /*******************************************************************
856 safe string copy into a known length string. maxlength does not
857 include the terminating zero.
858 ********************************************************************/
860 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
865 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
876 if (len > maxlength) {
877 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
878 (int)(len-maxlength), src));
882 memcpy(dest, src, len);
887 /*******************************************************************
888 safe string cat into a string. maxlength does not
889 include the terminating zero.
890 ********************************************************************/
892 char *safe_strcat(char *dest, const char *src, size_t maxlength)
894 size_t src_len, dest_len;
897 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
905 src_len = strlen(src);
906 dest_len = strlen(dest);
908 if (src_len + dest_len > maxlength) {
909 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
910 (int)(src_len + dest_len - maxlength), src));
911 src_len = maxlength - dest_len;
914 memcpy(&dest[dest_len], src, src_len);
915 dest[dest_len + src_len] = 0;
919 /*******************************************************************
920 Paranoid strcpy into a buffer of given length (includes terminating
921 zero. Strips out all but 'a-Z0-9' and replaces with '_'. Deliberately
922 does *NOT* check for multibyte characters. Don't change it !
923 ********************************************************************/
925 char *alpha_strcpy(char *dest, const char *src, size_t maxlength)
930 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
940 if (len >= maxlength)
943 for(i = 0; i < len; i++) {
944 int val = (src[i] & 0xff);
945 if(isupper(val) ||islower(val) || isdigit(val))
956 /****************************************************************************
957 Like strncpy but always null terminates. Make sure there is room!
958 The variable n should always be one less than the available size.
959 ****************************************************************************/
961 char *StrnCpy(char *dest,const char *src,size_t n)
964 if (!dest) return(NULL);
969 while (n-- && (*d++ = *src++)) ;
974 /****************************************************************************
975 like strncpy but copies up to the character marker. always null terminates.
976 returns a pointer to the character marker in the source string (src).
977 ****************************************************************************/
978 char *strncpyn(char *dest, const char *src,size_t n, char c)
986 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
990 str_len = PTR_DIFF(p, src);
991 strncpy(dest, src, MIN(n, str_len));
992 dest[str_len] = '\0';
998 /*************************************************************
999 Routine to get hex characters and turn them into a 16 byte array.
1000 the array can be variable length, and any non-hex-numeric
1001 characters are skipped. "0xnn" or "0Xnn" is specially catered
1004 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1006 **************************************************************/
1007 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1010 size_t num_chars = 0;
1011 unsigned char lonybble, hinybble;
1012 char *hexchars = "0123456789ABCDEF";
1013 char *p1 = NULL, *p2 = NULL;
1015 for (i = 0; i < len && strhex[i] != 0; i++)
1017 if (strnequal(hexchars, "0x", 2))
1019 i++; /* skip two chars */
1023 if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
1028 i++; /* next hex digit */
1030 if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
1035 /* get the two nybbles */
1036 hinybble = PTR_DIFF(p1, hexchars);
1037 lonybble = PTR_DIFF(p2, hexchars);
1039 p[num_chars] = (hinybble << 4) | lonybble;
1048 /****************************************************************************
1049 check if a string is part of a list
1050 ****************************************************************************/
1051 BOOL in_list(char *s,char *list,BOOL casesensitive)
1056 if (!list) return(False);
1058 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
1059 if (casesensitive) {
1060 if (strcmp(tok,s) == 0)
1063 if (StrCaseCmp(tok,s) == 0)
1070 /* this is used to prevent lots of mallocs of size 1 */
1071 static char *null_string = NULL;
1073 /****************************************************************************
1074 set a string value, allocing the space for the string
1075 ****************************************************************************/
1076 static BOOL string_init(char **dest,const char *src)
1087 if((null_string = (char *)malloc(1)) == NULL) {
1088 DEBUG(0,("string_init: malloc fail for null_string.\n"));
1093 *dest = null_string;
1097 (*dest) = (char *)malloc(l+1);
1098 if ((*dest) == NULL) {
1099 DEBUG(0,("Out of memory in string_init\n"));
1108 /****************************************************************************
1110 ****************************************************************************/
1111 void string_free(char **s)
1113 if (!s || !(*s)) return;
1114 if (*s == null_string)
1120 /****************************************************************************
1121 set a string value, allocing the space for the string, and deallocating any
1123 ****************************************************************************/
1124 BOOL string_set(char **dest,const char *src)
1128 return(string_init(dest,src));
1132 /****************************************************************************
1133 substitute a string for a pattern in another string. Make sure there is
1136 This routine looks for pattern in s and replaces it with
1137 insert. It may do multiple replacements.
1139 any of " ; ' $ or ` in the insert string are replaced with _
1140 if len==0 then no length check is performed
1141 ****************************************************************************/
1142 void string_sub(char *s,const char *pattern,const char *insert, size_t len)
1145 ssize_t ls,lp,li, i;
1147 if (!insert || !pattern || !s) return;
1149 ls = (ssize_t)strlen(s);
1150 lp = (ssize_t)strlen(pattern);
1151 li = (ssize_t)strlen(insert);
1153 if (!*pattern) return;
1155 while (lp <= ls && (p = strstr(s,pattern))) {
1156 if (len && (ls + (li-lp) >= len)) {
1157 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
1158 (int)(ls + (li-lp) - len),
1159 pattern, (int)len));
1163 memmove(p+li,p+lp,strlen(p+lp)+1);
1165 for (i=0;i<li;i++) {
1166 switch (insert[i]) {
1186 void fstring_sub(char *s,const char *pattern,const char *insert)
1188 string_sub(s, pattern, insert, sizeof(fstring));
1191 void pstring_sub(char *s,const char *pattern,const char *insert)
1193 string_sub(s, pattern, insert, sizeof(pstring));
1196 /****************************************************************************
1197 similar to string_sub() but allows for any character to be substituted.
1199 if len==0 then no length check is performed
1200 ****************************************************************************/
1201 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1206 if (!insert || !pattern || !s) return;
1208 ls = (ssize_t)strlen(s);
1209 lp = (ssize_t)strlen(pattern);
1210 li = (ssize_t)strlen(insert);
1212 if (!*pattern) return;
1214 while (lp <= ls && (p = strstr(s,pattern))) {
1215 if (len && (ls + (li-lp) >= len)) {
1216 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
1217 (int)(ls + (li-lp) - len),
1218 pattern, (int)len));
1222 memmove(p+li,p+lp,strlen(p+lp)+1);
1224 memcpy(p, insert, li);
1230 /****************************************************************************
1231 splits out the front and back at a separator.
1232 ****************************************************************************/
1233 void split_at_last_component(char *path, char *front, char sep, char *back)
1235 char *p = strrchr(path, sep);
1243 pstrcpy(front, path);
1263 /****************************************************************************
1264 write an octal as a string
1265 ****************************************************************************/
1266 char *octal_string(int i)
1268 static char ret[64];
1272 slprintf(ret, sizeof(ret)-1, "0%o", i);
1277 /****************************************************************************
1278 truncate a string at a specified length
1279 ****************************************************************************/
1280 char *string_truncate(char *s, int length)
1282 if (s && strlen(s) > length) {