2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
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_m(sep,*s)) s++;
49 if (! *s) return(False);
51 /* copy over the token */
52 for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(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 static uint16 tmpbuf[sizeof(pstring)];
88 void set_first_token(char *ptr)
94 /****************************************************************************
95 Convert list of tokens to array; dependent on above routine.
96 Uses last_ptr from above - bit of a hack.
97 ****************************************************************************/
98 char **toktocliplist(int *ctok, char *sep)
104 if (!sep) sep = " \t\n\r";
106 while(*s && strchr_m(sep,*s)) s++;
109 if (!*s) return(NULL);
113 while(*s && (!strchr_m(sep,*s))) s++;
114 while(*s && strchr_m(sep,*s)) *s++=0;
120 if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL;
131 /*******************************************************************
132 case insensitive string compararison
133 ********************************************************************/
134 int StrCaseCmp(const char *s, const char *t)
137 unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1));
138 unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2));
139 return strcmp(buf1,buf2);
142 /*******************************************************************
143 case insensitive string compararison, length limited
144 ********************************************************************/
145 int StrnCaseCmp(const char *s, const char *t, size_t n)
148 unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1));
149 unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2));
150 return strncmp(buf1,buf2,n);
153 /*******************************************************************
155 ********************************************************************/
156 BOOL strequal(const char *s1, const char *s2)
158 if (s1 == s2) return(True);
159 if (!s1 || !s2) return(False);
161 return(StrCaseCmp(s1,s2)==0);
164 /*******************************************************************
165 compare 2 strings up to and including the nth char.
166 ******************************************************************/
167 BOOL strnequal(const char *s1,const char *s2,size_t n)
169 if (s1 == s2) return(True);
170 if (!s1 || !s2 || !n) return(False);
172 return(StrnCaseCmp(s1,s2,n)==0);
175 /*******************************************************************
176 compare 2 strings (case sensitive)
177 ********************************************************************/
178 BOOL strcsequal(const char *s1,const char *s2)
180 if (s1 == s2) return(True);
181 if (!s1 || !s2) return(False);
183 return(strcmp(s1,s2)==0);
186 /***************************************************************************
187 Do a case-insensitive, whitespace-ignoring string compare.
188 ***************************************************************************/
189 int strwicmp(char *psz1, char *psz2)
191 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
192 /* appropriate value. */
195 else if (psz1 == NULL)
197 else if (psz2 == NULL)
200 /* sync the strings on first non-whitespace */
203 while (isspace(*psz1))
205 while (isspace(*psz2))
207 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
213 return (*psz1 - *psz2);
217 /*******************************************************************
218 convert a string to "normal" form
219 ********************************************************************/
220 void strnorm(char *s)
222 extern int case_default;
223 if (case_default == CASE_UPPER)
229 /*******************************************************************
230 check if a string is in "normal" case
231 ********************************************************************/
232 BOOL strisnormal(char *s)
234 extern int case_default;
235 if (case_default == CASE_UPPER)
236 return(!strhaslower(s));
238 return(!strhasupper(s));
242 /****************************************************************************
244 NOTE: oldc and newc must be 7 bit characters
245 ****************************************************************************/
246 void string_replace(char *s,char oldc,char newc)
249 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
250 for(ptr=tmpbuf;*ptr;ptr++) {
251 if(*ptr==UCS2_CHAR(oldc)) *ptr = UCS2_CHAR(newc);
253 pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE);
257 /*******************************************************************
258 skip past some strings in a buffer
259 ********************************************************************/
260 char *skip_string(char *buf,size_t n)
263 buf += strlen(buf) + 1;
267 /*******************************************************************
268 Count the number of characters in a string. Normally this will
269 be the same as the number of bytes in a string for single byte strings,
270 but will be different for multibyte.
271 ********************************************************************/
272 size_t str_charnum(const char *s)
274 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
275 return strlen_w(tmpbuf);
278 /*******************************************************************
279 trim the specified elements off the front and back of a string
280 ********************************************************************/
282 BOOL trim_string(char *s,const char *front,const char *back)
289 /* Ignore null or empty strings. */
290 if (!s || (s[0] == '\0'))
293 front_len = front? strlen(front) : 0;
294 back_len = back? strlen(back) : 0;
299 while (len && strncmp(s, front, front_len)==0) {
300 memcpy(s, s+front_len, (len-front_len)+1);
307 while (strncmp(s+len-back_len,back,back_len)==0) {
308 s[len-back_len]='\0';
317 /****************************************************************************
318 does a string have any uppercase chars in it?
319 ****************************************************************************/
320 BOOL strhasupper(const char *s)
323 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
324 for(ptr=tmpbuf;*ptr;ptr++)
325 if(isupper_w(*ptr)) return True;
329 /****************************************************************************
330 does a string have any lowercase chars in it?
331 ****************************************************************************/
332 BOOL strhaslower(const char *s)
335 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
336 for(ptr=tmpbuf;*ptr;ptr++)
337 if(islower_w(*ptr)) return True;
341 /****************************************************************************
342 find the number of 'c' chars in a string
343 ****************************************************************************/
344 size_t count_chars(const char *s,char c)
348 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
349 for(count=0,ptr=tmpbuf;*ptr;ptr++) if(*ptr==UCS2_CHAR(c)) count++;
353 /*******************************************************************
354 Return True if a string consists only of one particular character.
355 ********************************************************************/
357 BOOL str_is_all(const char *s,char c)
361 if(s == NULL) return False;
362 if(!*s) return False;
364 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
365 for(ptr=tmpbuf;*ptr;ptr++) if(*ptr!=UCS2_CHAR(c)) return False;
370 /*******************************************************************
371 safe string copy into a known length string. maxlength does not
372 include the terminating zero.
373 ********************************************************************/
375 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
380 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
391 if (len > maxlength) {
392 DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n",
393 (int)(len-maxlength), src));
397 memmove(dest, src, len);
402 /*******************************************************************
403 safe string cat into a string. maxlength does not
404 include the terminating zero.
405 ********************************************************************/
407 char *safe_strcat(char *dest, const char *src, size_t maxlength)
409 size_t src_len, dest_len;
412 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
420 src_len = strlen(src);
421 dest_len = strlen(dest);
423 if (src_len + dest_len > maxlength) {
424 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
425 (int)(src_len + dest_len - maxlength), src));
426 src_len = maxlength - dest_len;
429 memcpy(&dest[dest_len], src, src_len);
430 dest[dest_len + src_len] = 0;
434 /*******************************************************************
435 Paranoid strcpy into a buffer of given length (includes terminating
436 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
437 and replaces with '_'. Deliberately does *NOT* check for multibyte
438 characters. Don't change it !
439 ********************************************************************/
441 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
446 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
456 if (len >= maxlength)
459 if (!other_safe_chars)
460 other_safe_chars = "";
462 for(i = 0; i < len; i++) {
463 int val = (src[i] & 0xff);
464 if(isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val))
475 /****************************************************************************
476 Like strncpy but always null terminates. Make sure there is room!
477 The variable n should always be one less than the available size.
478 ****************************************************************************/
480 char *StrnCpy(char *dest,const char *src,size_t n)
483 if (!dest) return(NULL);
488 while (n-- && (*d++ = *src++)) ;
493 /****************************************************************************
494 like strncpy but copies up to the character marker. always null terminates.
495 returns a pointer to the character marker in the source string (src).
496 ****************************************************************************/
497 char *strncpyn(char *dest, const char *src,size_t n, char c)
502 p = strchr_m(src, c);
505 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
509 str_len = PTR_DIFF(p, src);
510 strncpy(dest, src, MIN(n, str_len));
511 dest[str_len] = '\0';
517 /*************************************************************
518 Routine to get hex characters and turn them into a 16 byte array.
519 the array can be variable length, and any non-hex-numeric
520 characters are skipped. "0xnn" or "0Xnn" is specially catered
523 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
525 **************************************************************/
526 size_t strhex_to_str(char *p, size_t len, const char *strhex)
529 size_t num_chars = 0;
530 unsigned char lonybble, hinybble;
531 char *hexchars = "0123456789ABCDEF";
532 char *p1 = NULL, *p2 = NULL;
534 for (i = 0; i < len && strhex[i] != 0; i++)
536 if (strnequal(hexchars, "0x", 2))
538 i++; /* skip two chars */
542 if (!(p1 = strchr_m(hexchars, toupper(strhex[i]))))
547 i++; /* next hex digit */
549 if (!(p2 = strchr_m(hexchars, toupper(strhex[i]))))
554 /* get the two nybbles */
555 hinybble = PTR_DIFF(p1, hexchars);
556 lonybble = PTR_DIFF(p2, hexchars);
558 p[num_chars] = (hinybble << 4) | lonybble;
567 /****************************************************************************
568 check if a string is part of a list
569 ****************************************************************************/
570 BOOL in_list(char *s,char *list,BOOL casesensitive)
575 if (!list) return(False);
577 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
579 if (strcmp(tok,s) == 0)
582 if (StrCaseCmp(tok,s) == 0)
589 /* this is used to prevent lots of mallocs of size 1 */
590 static char *null_string = NULL;
592 /****************************************************************************
593 set a string value, allocing the space for the string
594 ****************************************************************************/
595 static BOOL string_init(char **dest,const char *src)
606 if((null_string = (char *)malloc(1)) == NULL) {
607 DEBUG(0,("string_init: malloc fail for null_string.\n"));
616 (*dest) = (char *)malloc(l+1);
617 if ((*dest) == NULL) {
618 DEBUG(0,("Out of memory in string_init\n"));
627 /****************************************************************************
629 ****************************************************************************/
630 void string_free(char **s)
632 if (!s || !(*s)) return;
633 if (*s == null_string)
638 /****************************************************************************
639 set a string value, allocing the space for the string, and deallocating any
641 ****************************************************************************/
642 BOOL string_set(char **dest,const char *src)
646 return(string_init(dest,src));
650 /****************************************************************************
651 substitute a string for a pattern in another string. Make sure there is
654 This routine looks for pattern in s and replaces it with
655 insert. It may do multiple replacements.
657 any of " ; ' $ or ` in the insert string are replaced with _
658 if len==0 then no length check is performed
659 ****************************************************************************/
660 void string_sub(char *s,const char *pattern,const char *insert, size_t len)
665 if (!insert || !pattern || !s) return;
667 ls = (ssize_t)strlen(s);
668 lp = (ssize_t)strlen(pattern);
669 li = (ssize_t)strlen(insert);
671 if (!*pattern) return;
673 while (lp <= ls && (p = strstr(s,pattern))) {
674 if (len && (ls + (li-lp) >= len)) {
675 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
676 (int)(ls + (li-lp) - len),
681 memmove(p+li,p+lp,strlen(p+lp)+1);
704 void fstring_sub(char *s,const char *pattern,const char *insert)
706 string_sub(s, pattern, insert, sizeof(fstring));
709 void pstring_sub(char *s,const char *pattern,const char *insert)
711 string_sub(s, pattern, insert, sizeof(pstring));
714 /****************************************************************************
715 similar to string_sub() but allows for any character to be substituted.
717 if len==0 then no length check is performed
718 ****************************************************************************/
719 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
724 if (!insert || !pattern || !s) return;
726 ls = (ssize_t)strlen(s);
727 lp = (ssize_t)strlen(pattern);
728 li = (ssize_t)strlen(insert);
730 if (!*pattern) return;
732 while (lp <= ls && (p = strstr(s,pattern))) {
733 if (len && (ls + (li-lp) >= len)) {
734 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
735 (int)(ls + (li-lp) - len),
740 memmove(p+li,p+lp,strlen(p+lp)+1);
742 memcpy(p, insert, li);
748 /****************************************************************************
749 splits out the front and back at a separator.
750 ****************************************************************************/
751 void split_at_last_component(char *path, char *front, char sep, char *back)
753 char *p = strrchr_m(path, sep);
761 pstrcpy(front, path);
781 /****************************************************************************
782 write an octal as a string
783 ****************************************************************************/
784 char *octal_string(int i)
790 slprintf(ret, sizeof(ret)-1, "0%o", i);
795 /****************************************************************************
796 truncate a string at a specified length
797 ****************************************************************************/
798 char *string_truncate(char *s, int length)
800 if (s && strlen(s) > length) {
807 /****************************************************************************
808 strchr and strrchr_m are very hard to do on general multi-byte strings.
809 we convert via ucs2 for now
810 ****************************************************************************/
811 char *strchr_m(const char *s, char c)
817 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
818 p = strchr_wa(ws, c);
821 pull_ucs2_pstring(s2, ws);
822 return (char *)(s+strlen(s2));
825 char *strrchr_m(const char *s, char c)
831 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
832 p = strrchr_wa(ws, c);
835 pull_ucs2_pstring(s2, ws);
836 return (char *)(s+strlen(s2));
839 /*******************************************************************
840 convert a string to lower case
841 ********************************************************************/
842 void strlower_m(char *s)
844 /* I assume that lowercased string takes the same number of bytes
845 * as source string even in UTF-8 encoding. (VIV) */
846 unix_strlower(s,strlen(s)+1,s,strlen(s)+1);
849 /*******************************************************************
850 convert a string to upper case
851 ********************************************************************/
852 void strupper_m(char *s)
854 /* I assume that lowercased string takes the same number of bytes
855 * as source string even in multibyte encoding. (VIV) */
856 unix_strupper(s,strlen(s)+1,s,strlen(s)+1);