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 /****************************************************************************
27 Get the next token from a string, return False if none found
28 handles double-quotes.
29 Based on a routine by GJC@VILLAGE.COM.
30 Extensively modified by Andrew.Tridgell@anu.edu.au
31 ****************************************************************************/
32 BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
38 if (!ptr) return(False);
42 /* default to simple separators */
43 if (!sep) sep = " \t\n\r";
45 /* find the first non sep char */
46 while (*s && strchr(sep,*s)) s++;
49 if (! *s) return(False);
51 /* copy over the token */
52 for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) {
61 *ptr = (*s) ? s+1 : s;
69 /****************************************************************************
70 This is like next_token but is not re-entrant and "remembers" the first
71 parameter so you can pass NULL. This is useful for user interface code
72 but beware the fact that it is not re-entrant!
73 ****************************************************************************/
74 static char *last_ptr=NULL;
76 BOOL next_token_nr(char **ptr,char *buff,char *sep, size_t bufsize)
79 if (!ptr) ptr = &last_ptr;
81 ret = next_token(ptr, buff, sep, bufsize);
86 void set_first_token(char *ptr)
92 /****************************************************************************
93 Convert list of tokens to array; dependent on above routine.
94 Uses last_ptr from above - bit of a hack.
95 ****************************************************************************/
96 char **toktocliplist(int *ctok, char *sep)
102 if (!sep) sep = " \t\n\r";
104 while(*s && strchr(sep,*s)) s++;
107 if (!*s) return(NULL);
111 while(*s && (!strchr(sep,*s))) s++;
112 while(*s && strchr(sep,*s)) *s++=0;
118 if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
130 /*******************************************************************
131 case insensitive string compararison
132 ********************************************************************/
133 int StrCaseCmp(const char *s, const char *t)
135 /* compare until we run out of string, either t or s, or find a difference */
136 /* We *must* use toupper rather than tolower here due to the
137 asynchronous upper to lower mapping.
139 #if !defined(KANJI_WIN95_COMPATIBILITY)
141 * For completeness we should put in equivalent code for code pages
142 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
143 * doubt anyone wants Samba to behave differently from Win95 and WinNT
144 * here. They both treat full width ascii characters as case senstive
145 * filenames (ie. they don't do the work we do here).
149 if(lp_client_code_page() == KANJI_CODEPAGE)
151 /* Win95 treats full width ascii characters as case sensitive. */
156 return toupper (*s) - toupper (*t);
157 else if (is_sj_alph (*s) && is_sj_alph (*t))
159 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
165 else if (is_shift_jis (*s) && is_shift_jis (*t))
167 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
170 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
176 else if (is_shift_jis (*s))
178 else if (is_shift_jis (*t))
182 diff = toupper (*s) - toupper (*t);
191 #endif /* KANJI_WIN95_COMPATIBILITY */
193 while (*s && *t && toupper(*s) == toupper(*t))
199 return(toupper(*s) - toupper(*t));
203 /*******************************************************************
204 case insensitive string compararison, length limited
205 ********************************************************************/
206 int StrnCaseCmp(const char *s, const char *t, size_t n)
208 /* compare until we run out of string, either t or s, or chars */
209 /* We *must* use toupper rather than tolower here due to the
210 asynchronous upper to lower mapping.
212 #if !defined(KANJI_WIN95_COMPATIBILITY)
214 * For completeness we should put in equivalent code for code pages
215 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
216 * doubt anyone wants Samba to behave differently from Win95 and WinNT
217 * here. They both treat full width ascii characters as case senstive
218 * filenames (ie. they don't do the work we do here).
222 if(lp_client_code_page() == KANJI_CODEPAGE)
224 /* Win95 treats full width ascii characters as case sensitive. */
229 return toupper (*s) - toupper (*t);
230 else if (is_sj_alph (*s) && is_sj_alph (*t))
232 diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1));
239 else if (is_shift_jis (*s) && is_shift_jis (*t))
241 diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t);
244 diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1));
251 else if (is_shift_jis (*s))
253 else if (is_shift_jis (*t))
257 diff = toupper (*s) - toupper (*t);
268 #endif /* KANJI_WIN95_COMPATIBILITY */
270 while (n && *s && *t && toupper(*s) == toupper(*t))
277 /* not run out of chars - strings are different lengths */
279 return(toupper(*s) - toupper(*t));
281 /* identical up to where we run out of chars,
282 and strings are same length */
287 /*******************************************************************
289 ********************************************************************/
290 BOOL strequal(const char *s1, const char *s2)
292 if (s1 == s2) return(True);
293 if (!s1 || !s2) return(False);
295 return(StrCaseCmp(s1,s2)==0);
298 /*******************************************************************
299 compare 2 strings up to and including the nth char.
300 ******************************************************************/
301 BOOL strnequal(const char *s1,const char *s2,size_t n)
303 if (s1 == s2) return(True);
304 if (!s1 || !s2 || !n) return(False);
306 return(StrnCaseCmp(s1,s2,n)==0);
309 /*******************************************************************
310 compare 2 strings (case sensitive)
311 ********************************************************************/
312 BOOL strcsequal(const char *s1,const char *s2)
314 if (s1 == s2) return(True);
315 if (!s1 || !s2) return(False);
317 return(strcmp(s1,s2)==0);
320 /***************************************************************************
321 Do a case-insensitive, whitespace-ignoring string compare.
322 ***************************************************************************/
323 int strwicmp(char *psz1, char *psz2)
325 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
326 /* appropriate value. */
329 else if (psz1 == NULL)
331 else if (psz2 == NULL)
334 /* sync the strings on first non-whitespace */
337 while (isspace(*psz1))
339 while (isspace(*psz2))
341 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
347 return (*psz1 - *psz2);
351 /*******************************************************************
352 convert a string to lower case
353 ********************************************************************/
354 void strlower(char *s)
358 #if !defined(KANJI_WIN95_COMPATIBILITY)
360 * For completeness we should put in equivalent code for code pages
361 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
362 * doubt anyone wants Samba to behave differently from Win95 and WinNT
363 * here. They both treat full width ascii characters as case senstive
364 * filenames (ie. they don't do the work we do here).
368 if(lp_client_code_page() == KANJI_CODEPAGE)
370 /* Win95 treats full width ascii characters as case sensitive. */
371 if (is_shift_jis (*s))
373 if (is_sj_upper (s[0], s[1]))
374 s[1] = sj_tolower2 (s[1]);
377 else if (is_kana (*s))
389 #endif /* KANJI_WIN95_COMPATIBILITY */
391 size_t skip = get_character_len( *s );
404 /*******************************************************************
405 convert a string to upper case
406 ********************************************************************/
407 void strupper(char *s)
411 #if !defined(KANJI_WIN95_COMPATIBILITY)
413 * For completeness we should put in equivalent code for code pages
414 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
415 * doubt anyone wants Samba to behave differently from Win95 and WinNT
416 * here. They both treat full width ascii characters as case senstive
417 * filenames (ie. they don't do the work we do here).
421 if(lp_client_code_page() == KANJI_CODEPAGE)
423 /* Win95 treats full width ascii characters as case sensitive. */
424 if (is_shift_jis (*s))
426 if (is_sj_lower (s[0], s[1]))
427 s[1] = sj_toupper2 (s[1]);
430 else if (is_kana (*s))
442 #endif /* KANJI_WIN95_COMPATIBILITY */
444 size_t skip = get_character_len( *s );
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)
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 /****************************************************************************
484 ****************************************************************************/
485 void string_replace(char *s,char oldc,char newc)
492 if(!global_is_multibyte_codepage) {
501 skip = get_character_len( *s );
515 /*******************************************************************
516 skip past some strings in a buffer
517 ********************************************************************/
518 char *skip_string(char *buf,size_t n)
521 buf += strlen(buf) + 1;
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)
539 if(!global_is_multibyte_codepage) {
543 int skip = get_character_len(*s);
544 s += (skip ? skip : 1);
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)
563 /* Ignore null or empty strings. */
565 if ( !s || (s[0] == '\0'))
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,
577 if ( front && front_len > 1 ) {
578 while (( s_len >= front_len )&&
579 ( memcmp( sP, front, front_len - 1 )) == 0 ) {
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
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.
609 if ( back && back_len > 1 && s_len >= back_len) {
610 char *bP = sP + s_len - back_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
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 */
633 /* trace string from start. */
635 while ( cP < sP + s_len - back_len ) {
637 skip = skip_multibyte_char( *cP );
638 cP += ( skip ? skip : 1 );
640 /* you found the match */
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 */
657 /* slide string to buffer top */
658 memmove( s, sP, s_len );
664 /****************************************************************************
665 does a string have any uppercase chars in it?
666 ****************************************************************************/
667 BOOL strhasupper(const char *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).
681 if(lp_client_code_page() == KANJI_CODEPAGE)
683 /* Win95 treats full width ascii characters as case sensitive. */
684 if (is_shift_jis (*s))
686 else if (is_kana (*s))
696 #endif /* KANJI_WIN95_COMPATIBILITY */
698 size_t skip = get_character_len( *s );
711 /****************************************************************************
712 does a string have any lowercase chars in it?
713 ****************************************************************************/
714 BOOL strhaslower(const char *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).
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]))
735 if (is_sj_lower (s[0], s[1]))
739 else if (is_kana (*s))
751 #endif /* KANJI_WIN95_COMPATIBILITY */
753 size_t skip = get_character_len( *s );
766 /****************************************************************************
767 find the number of chars in a string
768 ****************************************************************************/
769 size_t count_chars(const char *s,char c)
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).
783 if(lp_client_code_page() == KANJI_CODEPAGE)
785 /* Win95 treats full width ascii characters as case sensitive. */
788 if (is_shift_jis (*s))
799 #endif /* KANJI_WIN95_COMPATIBILITY */
803 size_t skip = get_character_len( *s );
816 /*******************************************************************
817 Return True if a string consists only of one particular character.
818 ********************************************************************/
820 BOOL str_is_all(const char *s,char c)
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).
837 if(lp_client_code_page() == KANJI_CODEPAGE)
839 /* Win95 treats full width ascii characters as case sensitive. */
842 if (is_shift_jis (*s))
853 #endif /* KANJI_WIN95_COMPATIBILITY */
857 size_t skip = get_character_len( *s );
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)
880 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
891 if (len > maxlength) {
892 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
893 (int)(len-maxlength), src));
897 memmove(dest, src, len);
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;
912 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
920 src_len = strlen(src);
921 dest_len = strlen(dest);
923 if (src_len + dest_len > maxlength) {
924 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
925 (int)(src_len + dest_len - maxlength), src));
926 src_len = maxlength - dest_len;
929 memcpy(&dest[dest_len], src, src_len);
930 dest[dest_len + src_len] = 0;
934 /*******************************************************************
935 Paranoid strcpy into a buffer of given length (includes terminating
936 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
937 and replaces with '_'. Deliberately does *NOT* check for multibyte
938 characters. Don't change it !
939 ********************************************************************/
941 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
946 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
956 if (len >= maxlength)
959 if (!other_safe_chars)
960 other_safe_chars = "";
962 for(i = 0; i < len; i++) {
963 int val = (src[i] & 0xff);
964 if(isupper(val) || islower(val) || isdigit(val) || strchr(other_safe_chars, val))
975 /****************************************************************************
976 Like strncpy but always null terminates. Make sure there is room!
977 The variable n should always be one less than the available size.
978 ****************************************************************************/
980 char *StrnCpy(char *dest,const char *src,size_t n)
983 if (!dest) return(NULL);
988 while (n-- && (*d++ = *src++)) ;
993 /****************************************************************************
994 like strncpy but copies up to the character marker. always null terminates.
995 returns a pointer to the character marker in the source string (src).
996 ****************************************************************************/
997 char *strncpyn(char *dest, const char *src,size_t n, char c)
1005 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
1009 str_len = PTR_DIFF(p, src);
1010 strncpy(dest, src, MIN(n, str_len));
1011 dest[str_len] = '\0';
1017 /*************************************************************
1018 Routine to get hex characters and turn them into a 16 byte array.
1019 the array can be variable length, and any non-hex-numeric
1020 characters are skipped. "0xnn" or "0Xnn" is specially catered
1023 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1025 **************************************************************/
1026 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1029 size_t num_chars = 0;
1030 unsigned char lonybble, hinybble;
1031 char *hexchars = "0123456789ABCDEF";
1032 char *p1 = NULL, *p2 = NULL;
1034 for (i = 0; i < len && strhex[i] != 0; i++)
1036 if (strnequal(hexchars, "0x", 2))
1038 i++; /* skip two chars */
1042 if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
1047 i++; /* next hex digit */
1049 if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
1054 /* get the two nybbles */
1055 hinybble = PTR_DIFF(p1, hexchars);
1056 lonybble = PTR_DIFF(p2, hexchars);
1058 p[num_chars] = (hinybble << 4) | lonybble;
1067 /****************************************************************************
1068 check if a string is part of a list
1069 ****************************************************************************/
1070 BOOL in_list(char *s,char *list,BOOL casesensitive)
1075 if (!list) return(False);
1077 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
1078 if (casesensitive) {
1079 if (strcmp(tok,s) == 0)
1082 if (StrCaseCmp(tok,s) == 0)
1089 /* this is used to prevent lots of mallocs of size 1 */
1090 static char *null_string = NULL;
1092 /****************************************************************************
1093 set a string value, allocing the space for the string
1094 ****************************************************************************/
1095 static BOOL string_init(char **dest,const char *src)
1106 if((null_string = (char *)malloc(1)) == NULL) {
1107 DEBUG(0,("string_init: malloc fail for null_string.\n"));
1112 *dest = null_string;
1116 (*dest) = (char *)malloc(l+1);
1117 if ((*dest) == NULL) {
1118 DEBUG(0,("Out of memory in string_init\n"));
1127 /****************************************************************************
1129 ****************************************************************************/
1130 void string_free(char **s)
1132 if (!s || !(*s)) return;
1133 if (*s == null_string)
1139 /****************************************************************************
1140 set a string value, allocing the space for the string, and deallocating any
1142 ****************************************************************************/
1143 BOOL string_set(char **dest,const char *src)
1147 return(string_init(dest,src));
1151 /****************************************************************************
1152 substitute a string for a pattern in another string. Make sure there is
1155 This routine looks for pattern in s and replaces it with
1156 insert. It may do multiple replacements.
1158 any of " ; ' $ or ` in the insert string are replaced with _
1159 if len==0 then no length check is performed
1160 ****************************************************************************/
1161 void string_sub(char *s,const char *pattern,const char *insert, size_t len)
1164 ssize_t ls,lp,li, i;
1166 if (!insert || !pattern || !s) return;
1168 ls = (ssize_t)strlen(s);
1169 lp = (ssize_t)strlen(pattern);
1170 li = (ssize_t)strlen(insert);
1172 if (!*pattern) return;
1174 while (lp <= ls && (p = strstr(s,pattern))) {
1175 if (len && (ls + (li-lp) >= len)) {
1176 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
1177 (int)(ls + (li-lp) - len),
1178 pattern, (int)len));
1182 memmove(p+li,p+lp,strlen(p+lp)+1);
1184 for (i=0;i<li;i++) {
1185 switch (insert[i]) {
1205 void fstring_sub(char *s,const char *pattern,const char *insert)
1207 string_sub(s, pattern, insert, sizeof(fstring));
1210 void pstring_sub(char *s,const char *pattern,const char *insert)
1212 string_sub(s, pattern, insert, sizeof(pstring));
1215 /****************************************************************************
1216 similar to string_sub() but allows for any character to be substituted.
1218 if len==0 then no length check is performed
1219 ****************************************************************************/
1220 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1225 if (!insert || !pattern || !s) return;
1227 ls = (ssize_t)strlen(s);
1228 lp = (ssize_t)strlen(pattern);
1229 li = (ssize_t)strlen(insert);
1231 if (!*pattern) return;
1233 while (lp <= ls && (p = strstr(s,pattern))) {
1234 if (len && (ls + (li-lp) >= len)) {
1235 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
1236 (int)(ls + (li-lp) - len),
1237 pattern, (int)len));
1241 memmove(p+li,p+lp,strlen(p+lp)+1);
1243 memcpy(p, insert, li);
1249 /****************************************************************************
1250 splits out the front and back at a separator.
1251 ****************************************************************************/
1252 void split_at_last_component(char *path, char *front, char sep, char *back)
1254 char *p = strrchr(path, sep);
1262 pstrcpy(front, path);
1282 /****************************************************************************
1283 write an octal as a string
1284 ****************************************************************************/
1285 char *octal_string(int i)
1287 static char ret[64];
1291 slprintf(ret, sizeof(ret)-1, "0%o", i);
1296 /****************************************************************************
1297 truncate a string at a specified length
1298 ****************************************************************************/
1299 char *string_truncate(char *s, int length)
1301 if (s && strlen(s) > length) {