summaryrefslogtreecommitdiff
path: root/source3/lib/util_str.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib/util_str.c')
-rw-r--r--source3/lib/util_str.c165
1 files changed, 72 insertions, 93 deletions
diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c
index 19d92eec8f..88a72f1536 100644
--- a/source3/lib/util_str.c
+++ b/source3/lib/util_str.c
@@ -212,18 +212,6 @@ int strwicmp(const char *psz1, const char *psz2)
}
-/* Convert a string to upper case, but don't modify it */
-
-char *strupper_static(const char *s)
-{
- static pstring str;
-
- pstrcpy(str, s);
- strupper(str);
-
- return str;
-}
-
/*******************************************************************
convert a string to "normal" form
********************************************************************/
@@ -311,7 +299,7 @@ BOOL trim_string(char *s,const char *front,const char *back)
}
if (back_len) {
- while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
+ while (strncmp(s+len-back_len,back,back_len)==0) {
s[len-back_len]='\0';
len -= back_len;
ret=True;
@@ -679,7 +667,7 @@ void string_sub(char *s,const char *pattern, const char *insert, size_t len)
li = (ssize_t)strlen(insert);
if (len == 0)
- len = ls + 1; /* len is number of *bytes* */
+ len = ls;
while (lp <= ls && (p = strstr(s,pattern))) {
if (ls + (li-lp) >= len) {
@@ -810,7 +798,7 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
return;
if (len == 0)
- len = ls + 1; /* len is number of *bytes* */
+ len = ls;
while (lp <= ls && (p = strstr(s,pattern))) {
if (ls + (li-lp) >= len) {
@@ -838,8 +826,7 @@ return a new allocate unicode string.
smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern,
const smb_ucs2_t *insert)
{
- smb_ucs2_t *r, *rp;
- const smb_ucs2_t *sp;
+ smb_ucs2_t *r, *rp, *sp;
size_t lr, lp, li, lt;
if (!insert || !pattern || !*pattern || !s) return NULL;
@@ -849,7 +836,7 @@ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern,
li = (size_t)strlen_w(insert);
if (li > lp) {
- const smb_ucs2_t *st = s;
+ smb_ucs2_t *st = s;
int ld = li - lp;
while ((sp = strstr_w(st, pattern))) {
st = sp + lp;
@@ -892,59 +879,68 @@ smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern,
}
/****************************************************************************
- Splits out the front and back at a separator.
+ splits out the front and back at a separator.
****************************************************************************/
-
void split_at_last_component(char *path, char *front, char sep, char *back)
{
char *p = strrchr_m(path, sep);
if (p != NULL)
+ {
*p = 0;
-
+ }
if (front != NULL)
+ {
pstrcpy(front, path);
-
- if (p != NULL) {
+ }
+ if (p != NULL)
+ {
if (back != NULL)
+ {
pstrcpy(back, p+1);
+ }
*p = '\\';
- } else {
+ }
+ else
+ {
if (back != NULL)
+ {
back[0] = 0;
+ }
}
}
+
/****************************************************************************
- Write an octal as a string.
+write an octal as a string
****************************************************************************/
-
char *octal_string(int i)
{
static char ret[64];
- if (i == -1)
+ if (i == -1) {
return "-1";
+ }
slprintf(ret, sizeof(ret)-1, "0%o", i);
return ret;
}
/****************************************************************************
- Truncate a string at a specified length.
+truncate a string at a specified length
****************************************************************************/
-
char *string_truncate(char *s, int length)
{
- if (s && strlen(s) > length)
+ if (s && strlen(s) > length) {
s[length] = 0;
+ }
return s;
}
+
/****************************************************************************
- Strchr and strrchr_m are very hard to do on general multi-byte strings.
- We convert via ucs2 for now.
+strchr and strrchr_m are very hard to do on general multi-byte strings.
+we convert via ucs2 for now
****************************************************************************/
-
char *strchr_m(const char *s, char c)
{
wpstring ws;
@@ -953,8 +949,7 @@ char *strchr_m(const char *s, char c)
push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
p = strchr_w(ws, UCS2_CHAR(c));
- if (!p)
- return NULL;
+ if (!p) return NULL;
*p = 0;
pull_ucs2_pstring(s2, ws);
return (char *)(s+strlen(s2));
@@ -968,29 +963,26 @@ char *strrchr_m(const char *s, char c)
push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
p = strrchr_w(ws, UCS2_CHAR(c));
- if (!p)
- return NULL;
+ if (!p) return NULL;
*p = 0;
pull_ucs2_pstring(s2, ws);
return (char *)(s+strlen(s2));
}
/*******************************************************************
- Convert a string to lower case.
+ convert a string to lower case
********************************************************************/
-
void strlower_m(char *s)
{
/* this is quite a common operation, so we want it to be
fast. We optimise for the ascii case, knowing that all our
supported multi-byte character sets are ascii-compatible
(ie. they match for the first 128 chars) */
-
- while (*s && !(((unsigned char)s[0]) & 0x7F))
+ while (*s && !(((unsigned char)s[0]) & 0x7F)) {
*s++ = tolower((unsigned char)*s);
+ }
- if (!*s)
- return;
+ if (!*s) return;
/* I assume that lowercased string takes the same number of bytes
* as source string even in UTF-8 encoding. (VIV) */
@@ -998,9 +990,8 @@ void strlower_m(char *s)
}
/*******************************************************************
- Duplicate convert a string to lower case.
+ duplicate convert a string to lower case
********************************************************************/
-
char *strdup_lower(const char *s)
{
char *t = strdup(s);
@@ -1013,21 +1004,19 @@ char *strdup_lower(const char *s)
}
/*******************************************************************
- Convert a string to upper case.
+ convert a string to upper case
********************************************************************/
-
void strupper_m(char *s)
{
/* this is quite a common operation, so we want it to be
fast. We optimise for the ascii case, knowing that all our
supported multi-byte character sets are ascii-compatible
(ie. they match for the first 128 chars) */
-
- while (*s && !(((unsigned char)s[0]) & 0x7F))
+ while (*s && !(((unsigned char)s[0]) & 0x7F)) {
*s++ = toupper((unsigned char)*s);
+ }
- if (!*s)
- return;
+ if (!*s) return;
/* I assume that lowercased string takes the same number of bytes
* as source string even in multibyte encoding. (VIV) */
@@ -1035,9 +1024,8 @@ void strupper_m(char *s)
}
/*******************************************************************
- Convert a string to upper case.
+ convert a string to upper case
********************************************************************/
-
char *strdup_upper(const char *s)
{
char *t = strdup(s);
@@ -1060,8 +1048,7 @@ char *binary_string(char *buf, int len)
int i, j;
const char *hex = "0123456789ABCDEF";
s = malloc(len * 3 + 1);
- if (!s)
- return NULL;
+ if (!s) return NULL;
for (j=i=0;i<len;i++) {
s[j] = '\\';
s[j+1] = hex[((unsigned char)buf[i]) >> 4];
@@ -1072,8 +1059,8 @@ char *binary_string(char *buf, int len)
return s;
}
-/* Just a typesafety wrapper for snprintf into a pstring */
+/* Just a typesafety wrapper for snprintf into a pstring */
int pstr_sprintf(pstring s, const char *fmt, ...)
{
va_list ap;
@@ -1085,8 +1072,8 @@ int pstr_sprintf(pstring s, const char *fmt, ...)
return ret;
}
-/* Just a typesafety wrapper for snprintf into a fstring */
+/* Just a typesafety wrapper for snprintf into a fstring */
int fstr_sprintf(fstring s, const char *fmt, ...)
{
va_list ap;
@@ -1098,19 +1085,18 @@ int fstr_sprintf(fstring s, const char *fmt, ...)
return ret;
}
+
#ifndef HAVE_STRNDUP
/*******************************************************************
- Some platforms don't have strndup.
+some platforms don't have strndup
********************************************************************/
-
char *strndup(const char *s, size_t n)
{
char *ret;
n = strnlen(s, n);
ret = malloc(n+1);
- if (!ret)
- return NULL;
+ if (!ret) return NULL;
memcpy(ret, s, n);
ret[n] = 0;
@@ -1125,39 +1111,39 @@ some platforms don't have strnlen
size_t strnlen(const char *s, size_t n)
{
int i;
- for (i=0; s[i] && i<n; i++)
- /* noop */ ;
+ for (i=0; s[i] && i<n; i++) /* noop */ ;
return i;
}
#endif
+
+
/***********************************************************
List of Strings manipulation functions
***********************************************************/
#define S_LIST_ABS 16 /* List Allocation Block Size */
-char **str_list_make(const char *string, const char *sep)
+char **str_list_make(const char *string)
{
char **list, **rlist;
char *str, *s;
int num, lsize;
pstring tok;
- if (!string || !*string)
- return NULL;
+ if (!string || !*string) return NULL;
s = strdup(string);
if (!s) {
DEBUG(0,("str_list_make: Unable to allocate memory"));
return NULL;
}
- if (!sep) sep = LIST_SEP;
num = lsize = 0;
list = NULL;
str = s;
- while (next_token(&str, tok, sep, sizeof(tok))) {
+ while (next_token(&str, tok, LIST_SEP, sizeof(tok)))
+ {
if (num == lsize) {
lsize += S_LIST_ABS;
rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
@@ -1192,13 +1178,13 @@ BOOL str_list_copy(char ***dest, char **src)
int num, lsize;
*dest = NULL;
- if (!src)
- return False;
+ if (!src) return False;
num = lsize = 0;
list = NULL;
- while (src[num]) {
+ while (src[num])
+ {
if (num == lsize) {
lsize += S_LIST_ABS;
rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
@@ -1226,22 +1212,17 @@ BOOL str_list_copy(char ***dest, char **src)
}
/* return true if all the elemnts of the list matches exactly */
-
BOOL str_list_compare(char **list1, char **list2)
{
int num;
- if (!list1 || !list2)
- return (list1 == list2);
+ if (!list1 || !list2) return (list1 == list2);
for (num = 0; list1[num]; num++) {
- if (!list2[num])
- return False;
- if (!strcsequal(list1[num], list2[num]))
- return False;
+ if (!list2[num]) return False;
+ if (!strcsequal(list1[num], list2[num])) return False;
}
- if (list2[num])
- return False; /* if list2 has more elements than list1 fail */
+ if (list2[num]) return False; /* if list2 has more elements than list1 fail */
return True;
}
@@ -1250,11 +1231,9 @@ void str_list_free(char ***list)
{
char **tlist;
- if (!list || !*list)
- return;
+ if (!list || !*list) return;
tlist = *list;
- for(; *tlist; tlist++)
- SAFE_FREE(*tlist);
+ for(; *tlist; tlist++) SAFE_FREE(*tlist);
SAFE_FREE(*list);
}
@@ -1263,25 +1242,25 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
char *p, *s, *t;
ssize_t ls, lp, li, ld, i, d;
- if (!list)
- return False;
- if (!pattern)
- return False;
- if (!insert)
- return False;
+ if (!list) return False;
+ if (!pattern) return False;
+ if (!insert) return False;
lp = (ssize_t)strlen(pattern);
li = (ssize_t)strlen(insert);
ld = li -lp;
- while (*list) {
+ while (*list)
+ {
s = *list;
ls = (ssize_t)strlen(s);
- while ((p = strstr(s, pattern))) {
+ while ((p = strstr(s, pattern)))
+ {
t = *list;
d = p -t;
- if (ld) {
+ if (ld)
+ {
t = (char *) malloc(ls +ld +1);
if (!t) {
DEBUG(0,("str_list_substitute: Unable to allocate memory"));