2 Unix SMB/Netbios implementation.
4 Samba utility functions
6 Copyright (C) Andrew Tridgell 1992-1998
9 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 extern int DEBUGLEVEL
;
31 static char *last_ptr
= NULL
;
34 set_first_token (char *ptr
)
39 /****************************************************************************
40 Get the next token from a string, return False if none found
41 handles double-quotes.
42 Based on a routine by GJC@VILLAGE.COM.
43 Extensively modified by Andrew.Tridgell@anu.edu.au
44 ****************************************************************************/
46 next_token (char **ptr
, char *buff
, const char *sep
, size_t bufsize
)
59 /* default to simple separators */
63 /* find the first non sep char */
64 while (*s
&& strchr (sep
, *s
))
71 /* copy over the token */
72 for (quoted
= False
; len
< bufsize
&& *s
&& (quoted
|| !strchr (sep
, *s
)); s
++)
85 *ptr
= (*s
) ? s
+ 1 : s
;
93 /****************************************************************************
94 Convert list of tokens to array; dependent on above routine.
95 Uses last_ptr from above - bit of a hack.
96 ****************************************************************************/
98 toktocliplist (int *ctok
, char *sep
)
107 while (*s
&& strchr (sep
, *s
))
117 while (*s
&& (!strchr (sep
, *s
)))
119 while (*s
&& strchr (sep
, *s
))
127 if (!(ret
= iret
= malloc (ictok
* sizeof (char *))))
142 /*******************************************************************
143 case insensitive string compararison
144 ********************************************************************/
146 StrCaseCmp (const char *s
, const char *t
)
148 /* compare until we run out of string, either t or s, or find a difference */
149 /* We *must* use toupper rather than tolower here due to the
150 asynchronous upper to lower mapping.
152 #if !defined(KANJI_WIN95_COMPATIBILITY)
154 * For completeness we should put in equivalent code for code pages
155 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
156 * doubt anyone wants Samba to behave differently from Win95 and WinNT
157 * here. They both treat full width ascii characters as case senstive
158 * filenames (ie. they don't do the work we do here).
162 if (lp_client_code_page () == KANJI_CODEPAGE
)
164 /* Win95 treats full width ascii characters as case sensitive. */
169 return toupper (*s
) - toupper (*t
);
170 else if (is_sj_alph (*s
) && is_sj_alph (*t
))
172 diff
= sj_toupper2 (*(s
+ 1)) - sj_toupper2 (*(t
+ 1));
178 else if (is_shift_jis (*s
) && is_shift_jis (*t
))
180 diff
= ((int) (unsigned char) *s
) - ((int) (unsigned char) *t
);
183 diff
= ((int) (unsigned char) *(s
+ 1)) - ((int) (unsigned char) *(t
+ 1));
189 else if (is_shift_jis (*s
))
191 else if (is_shift_jis (*t
))
195 diff
= toupper (*s
) - toupper (*t
);
204 #endif /* KANJI_WIN95_COMPATIBILITY */
206 while (*s
&& *t
&& toupper (*s
) == toupper (*t
))
212 return (toupper (*s
) - toupper (*t
));
216 /*******************************************************************
217 case insensitive string compararison, length limited
218 ********************************************************************/
220 StrnCaseCmp (const char *s
, const char *t
, size_t n
)
222 /* compare until we run out of string, either t or s, or chars */
223 /* We *must* use toupper rather than tolower here due to the
224 asynchronous upper to lower mapping.
226 #if !defined(KANJI_WIN95_COMPATIBILITY)
228 * For completeness we should put in equivalent code for code pages
229 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
230 * doubt anyone wants Samba to behave differently from Win95 and WinNT
231 * here. They both treat full width ascii characters as case senstive
232 * filenames (ie. they don't do the work we do here).
236 if (lp_client_code_page () == KANJI_CODEPAGE
)
238 /* Win95 treats full width ascii characters as case sensitive. */
243 return toupper (*s
) - toupper (*t
);
244 else if (is_sj_alph (*s
) && is_sj_alph (*t
))
246 diff
= sj_toupper2 (*(s
+ 1)) - sj_toupper2 (*(t
+ 1));
253 else if (is_shift_jis (*s
) && is_shift_jis (*t
))
255 diff
= ((int) (unsigned char) *s
) - ((int) (unsigned char) *t
);
258 diff
= ((int) (unsigned char) *(s
+ 1)) - ((int) (unsigned char) *(t
+ 1));
265 else if (is_shift_jis (*s
))
267 else if (is_shift_jis (*t
))
271 diff
= toupper (*s
) - toupper (*t
);
282 #endif /* KANJI_WIN95_COMPATIBILITY */
284 while (n
&& *s
&& *t
&& toupper (*s
) == toupper (*t
))
291 /* not run out of chars - strings are different lengths */
293 return (toupper (*s
) - toupper (*t
));
295 /* identical up to where we run out of chars,
296 and strings are same length */
301 /*******************************************************************
303 ********************************************************************/
305 strequal (const char *s1
, const char *s2
)
312 return (StrCaseCmp (s1
, s2
) == 0);
315 /*******************************************************************
316 compare 2 strings up to and including the nth char.
317 ******************************************************************/
319 strnequal (const char *s1
, const char *s2
, size_t n
)
323 if (!s1
|| !s2
|| !n
)
326 return (StrnCaseCmp (s1
, s2
, n
) == 0);
329 /*******************************************************************
330 compare 2 strings (case sensitive)
331 ********************************************************************/
333 strcsequal (const char *s1
, const char *s2
)
340 return (strcmp (s1
, s2
) == 0);
344 /*******************************************************************
345 convert a string to lower case
346 ********************************************************************/
352 #if !defined(KANJI_WIN95_COMPATIBILITY)
354 * For completeness we should put in equivalent code for code pages
355 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
356 * doubt anyone wants Samba to behave differently from Win95 and WinNT
357 * here. They both treat full width ascii characters as case senstive
358 * filenames (ie. they don't do the work we do here).
362 if (lp_client_code_page () == KANJI_CODEPAGE
)
364 /* Win95 treats full width ascii characters as case sensitive. */
365 if (is_shift_jis (*s
))
367 if (is_sj_upper (s
[0], s
[1]))
368 s
[1] = sj_tolower2 (s
[1]);
371 else if (is_kana (*s
))
383 #endif /* KANJI_WIN95_COMPATIBILITY */
385 size_t skip
= skip_multibyte_char (*s
);
398 /*******************************************************************
399 convert a string to upper case
400 ********************************************************************/
406 #if !defined(KANJI_WIN95_COMPATIBILITY)
408 * For completeness we should put in equivalent code for code pages
409 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
410 * doubt anyone wants Samba to behave differently from Win95 and WinNT
411 * here. They both treat full width ascii characters as case senstive
412 * filenames (ie. they don't do the work we do here).
416 if (lp_client_code_page () == KANJI_CODEPAGE
)
418 /* Win95 treats full width ascii characters as case sensitive. */
419 if (is_shift_jis (*s
))
421 if (is_sj_lower (s
[0], s
[1]))
422 s
[1] = sj_toupper2 (s
[1]);
425 else if (is_kana (*s
))
437 #endif /* KANJI_WIN95_COMPATIBILITY */
439 size_t skip
= skip_multibyte_char (*s
);
453 /*******************************************************************
454 convert a string to "normal" form
455 ********************************************************************/
459 extern int case_default
;
460 if (case_default
== CASE_UPPER
)
466 /*******************************************************************
467 check if a string is in "normal" case
468 ********************************************************************/
470 strisnormal (char *s
)
472 extern int case_default
;
473 if (case_default
== CASE_UPPER
)
474 return (!strhaslower (s
));
476 return (!strhasupper (s
));
480 /****************************************************************************
482 ****************************************************************************/
484 string_replace (char *s
, char oldc
, char newc
)
489 skip
= skip_multibyte_char (*s
);
502 /*******************************************************************
503 skip past some strings in a buffer
504 ********************************************************************/
506 skip_string (char *buf
, size_t n
)
509 buf
+= strlen (buf
) + 1;
513 /*******************************************************************
514 Count the number of characters in a string. Normally this will
515 be the same as the number of bytes in a string for single byte strings,
516 but will be different for multibyte.
517 16.oct.98, jdblair@cobaltnet.com.
518 ********************************************************************/
521 str_charnum (const char *s
)
527 int skip
= skip_multibyte_char (*s
);
528 s
+= (skip
? skip
: 1);
534 /*******************************************************************
535 trim the specified elements off the front and back of a string
536 ********************************************************************/
539 trim_string (char *s
, const char *front
, const char *back
)
542 size_t front_len
= (front
&& *front
) ? strlen (front
) : 0;
543 size_t back_len
= (back
&& *back
) ? strlen (back
) : 0;
546 while (front_len
&& strncmp (s
, front
, front_len
) == 0)
552 if (!(*p
= p
[front_len
]))
559 * We split out the multibyte code page
560 * case here for speed purposes. Under a
561 * multibyte code page we need to walk the
562 * string forwards only and multiple times.
563 * Thanks to John Blair for finding this
569 if (!is_multibyte_codepage ())
572 while ((s_len
>= back_len
) && (strncmp (s
+ s_len
- back_len
, back
, back_len
) == 0))
575 s
[s_len
- back_len
] = '\0';
583 * Multibyte code page case.
584 * Keep going through the string, trying
585 * to match the 'back' string with the end
586 * of the string. If we get a match, truncate
587 * 'back' off the end of the string and
588 * go through the string again from the
589 * start. Keep doing this until we have
590 * gone through the string with no match
594 size_t mb_back_len
= str_charnum (back
);
595 size_t mb_s_len
= str_charnum (s
);
597 while (mb_s_len
>= mb_back_len
)
599 size_t charcount
= 0;
602 while (charcount
< (mb_s_len
- mb_back_len
))
604 size_t skip
= skip_multibyte_char (*mbp
);
605 mbp
+= (skip
? skip
: 1);
610 * mbp now points at mb_back_len multibyte
611 * characters from the end of s.
614 if (strcmp (mbp
, back
) == 0)
618 mb_s_len
= str_charnum (s
);
623 } /* end while mb_s_len... */
625 } /* end if back_len .. */
631 /****************************************************************************
632 does a string have any uppercase chars in it?
633 ****************************************************************************/
635 strhasupper (const char *s
)
639 #if !defined(KANJI_WIN95_COMPATIBILITY)
641 * For completeness we should put in equivalent code for code pages
642 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
643 * doubt anyone wants Samba to behave differently from Win95 and WinNT
644 * here. They both treat full width ascii characters as case senstive
645 * filenames (ie. they don't do the work we do here).
649 if (lp_client_code_page () == KANJI_CODEPAGE
)
651 /* Win95 treats full width ascii characters as case sensitive. */
652 if (is_shift_jis (*s
))
654 else if (is_kana (*s
))
664 #endif /* KANJI_WIN95_COMPATIBILITY */
666 size_t skip
= skip_multibyte_char (*s
);
681 /****************************************************************************
682 does a string have any lowercase chars in it?
683 ****************************************************************************/
685 strhaslower (const char *s
)
689 #if !defined(KANJI_WIN95_COMPATIBILITY)
691 * For completeness we should put in equivalent code for code pages
692 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
693 * doubt anyone wants Samba to behave differently from Win95 and WinNT
694 * here. They both treat full width ascii characters as case senstive
695 * filenames (ie. they don't do the work we do here).
699 if (lp_client_code_page () == KANJI_CODEPAGE
)
701 /* Win95 treats full width ascii characters as case sensitive. */
702 if (is_shift_jis (*s
))
704 if (is_sj_upper (s
[0], s
[1]))
706 if (is_sj_lower (s
[0], s
[1]))
710 else if (is_kana (*s
))
722 #endif /* KANJI_WIN95_COMPATIBILITY */
724 size_t skip
= skip_multibyte_char (*s
);
739 /****************************************************************************
740 find the number of chars in a string
741 ****************************************************************************/
743 count_chars (const char *s
, char c
)
747 #if !defined(KANJI_WIN95_COMPATIBILITY)
749 * For completeness we should put in equivalent code for code pages
750 * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but
751 * doubt anyone wants Samba to behave differently from Win95 and WinNT
752 * here. They both treat full width ascii characters as case senstive
753 * filenames (ie. they don't do the work we do here).
757 if (lp_client_code_page () == KANJI_CODEPAGE
)
759 /* Win95 treats full width ascii characters as case sensitive. */
762 if (is_shift_jis (*s
))
773 #endif /* KANJI_WIN95_COMPATIBILITY */
777 size_t skip
= skip_multibyte_char (*s
);
793 /*******************************************************************
794 safe string copy into a known length string. maxlength does not
795 include the terminating zero.
796 ********************************************************************/
798 safe_strcpy (char *dest
, const char *src
, size_t maxlength
)
804 DEBUG (0, ("ERROR: NULL dest in safe_strcpy\n"));
818 DEBUG (0, ("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
819 (int) (len
- maxlength
), src
));
823 memcpy (dest
, src
, len
);
828 /*******************************************************************
829 safe string cat into a string. maxlength does not
830 include the terminating zero.
831 ********************************************************************/
833 safe_strcat (char *dest
, const char *src
, size_t maxlength
)
835 size_t src_len
, dest_len
;
839 DEBUG (0, ("ERROR: NULL dest in safe_strcat\n"));
848 src_len
= strlen (src
);
849 dest_len
= strlen (dest
);
851 if (src_len
+ dest_len
> maxlength
)
853 DEBUG (0, ("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
854 (int) (src_len
+ dest_len
- maxlength
), src
));
855 src_len
= maxlength
- dest_len
;
858 memcpy (&dest
[dest_len
], src
, src_len
);
859 dest
[dest_len
+ src_len
] = 0;
863 /****************************************************************************
864 this is a safer strcpy(), meant to prevent core dumps when nasty things happen
865 ****************************************************************************/
867 StrCpy (char *dest
, const char *src
)
871 /* I don't want to get lazy with these ... */
872 SMB_ASSERT (dest
&& src
);
881 while ((*d
++ = *src
++));
885 /****************************************************************************
886 like strncpy but always null terminates. Make sure there is room!
887 ****************************************************************************/
889 StrnCpy (char *dest
, const char *src
, size_t n
)
899 while (n
-- && (*d
++ = *src
++));
905 /****************************************************************************
906 like strncpy but copies up to the character marker. always null terminates.
907 returns a pointer to the character marker in the source string (src).
908 ****************************************************************************/
910 strncpyn (char *dest
, const char *src
, size_t n
, char c
)
918 DEBUG (5, ("strncpyn: separator character (%c) not found\n", c
));
922 str_len
= PTR_DIFF (p
, src
);
923 strncpy (dest
, src
, MIN (n
, str_len
));
924 dest
[str_len
] = '\0';
930 /*************************************************************
931 Routine to get hex characters and turn them into a 16 byte array.
932 the array can be variable length, and any non-hex-numeric
933 characters are skipped. "0xnn" or "0Xnn" is specially catered
936 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
938 **************************************************************/
940 strhex_to_str (char *p
, size_t len
, const char *strhex
)
943 size_t num_chars
= 0;
944 unsigned char lonybble
, hinybble
;
945 char *hexchars
= "0123456789ABCDEF";
946 char *p1
= NULL
, *p2
= NULL
;
948 for (i
= 0; i
< len
&& strhex
[i
] != 0; i
++)
950 if (strnequal (hexchars
, "0x", 2))
952 i
++; /* skip two chars */
956 if (!(p1
= strchr (hexchars
, toupper (strhex
[i
]))))
961 i
++; /* next hex digit */
963 if (!(p2
= strchr (hexchars
, toupper (strhex
[i
]))))
968 /* get the two nybbles */
969 hinybble
= PTR_DIFF (p1
, hexchars
);
970 lonybble
= PTR_DIFF (p2
, hexchars
);
972 p
[num_chars
] = (hinybble
<< 4) | lonybble
;
981 /****************************************************************************
982 check if a string is part of a list
983 ****************************************************************************/
985 in_list (char *s
, char *list
, BOOL casesensitive
)
993 while (next_token (&p
, tok
, LIST_SEP
, sizeof (tok
)))
997 if (strcmp (tok
, s
) == 0)
1002 if (StrCaseCmp (tok
, s
) == 0)
1010 /* this is used to prevent lots of mallocs of size 1 */
1011 static char *null_string
= NULL
;
1013 /****************************************************************************
1014 set a string value, allocing the space for the string
1015 ****************************************************************************/
1017 string_init (char **dest
, const char *src
)
1029 if ((null_string
= (char *) malloc (1)) == NULL
)
1031 DEBUG (0, ("string_init: malloc fail for null_string.\n"));
1036 *dest
= null_string
;
1040 (*dest
) = (char *) malloc (l
+ 1);
1041 if ((*dest
) == NULL
)
1043 DEBUG (0, ("Out of memory in string_init\n"));
1047 pstrcpy (*dest
, src
);
1052 /****************************************************************************
1054 ****************************************************************************/
1056 string_free (char **s
)
1060 if (*s
== null_string
)
1067 /****************************************************************************
1068 set a string value, allocing the space for the string, and deallocating any
1070 ****************************************************************************/
1072 string_set (char **dest
, const char *src
)
1076 return (string_init (dest
, src
));
1080 /****************************************************************************
1081 substitute a string for a pattern in another string. Make sure there is
1084 This routine looks for pattern in s and replaces it with
1085 insert. It may do multiple replacements.
1087 any of " ; ' or ` in the insert string are replaced with _
1088 ****************************************************************************/
1090 string_sub (char *s
, const char *pattern
, const char *insert
)
1093 size_t ls
, lp
, li
, i
;
1095 if (!insert
|| !pattern
|| !s
)
1099 lp
= strlen (pattern
);
1100 li
= strlen (insert
);
1105 while (lp
<= ls
&& (p
= strstr (s
, pattern
)))
1107 memmove (p
+ li
, p
+ lp
, ls
+ 1 - (PTR_DIFF (p
, s
) + lp
));
1108 for (i
= 0; i
< li
; i
++)
1128 /****************************************************************************
1129 similar to string_sub() but allows for any character to be substituted.
1131 ****************************************************************************/
1133 all_string_sub (char *s
, const char *pattern
, const char *insert
)
1138 if (!insert
|| !pattern
|| !s
)
1142 lp
= strlen (pattern
);
1143 li
= strlen (insert
);
1148 while (lp
<= ls
&& (p
= strstr (s
, pattern
)))
1150 memmove (p
+ li
, p
+ lp
, ls
+ 1 - (PTR_DIFF (p
, s
) + lp
));
1151 memcpy (p
, insert
, li
);
1158 /****************************************************************************
1159 splits out the front and back at a separator.
1160 ****************************************************************************/
1162 split_at_last_component (char *path
, char *front
, char sep
, char *back
)
1164 char *p
= strrchr (path
, sep
);
1172 pstrcpy (front
, path
);
1178 pstrcpy (back
, p
+ 1);