increase log level for this failed setsockopt call. EINVAL is a normal error on Solar...
[jra/samba/.git] / source / lib / util_str.c
index bcb9197141c71ea0bbf5ba9e374f722873c1c7d7..6310e2464d06e2209b4a6b9338b7351d5669e61b 100644 (file)
@@ -1841,149 +1841,93 @@ int fstr_sprintf(fstring s, const char *fmt, ...)
 
 #define S_LIST_ABS 16 /* List Allocation Block Size */
 
-static char **str_list_make_internal(TALLOC_CTX *mem_ctx,
-               const char *string,
-               const char *sep)
+char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
 {
-       char **list, **rlist;
+       char **list;
        const char *str;
        char *s;
        int num, lsize;
        char *tok;
-       TALLOC_CTX *frame = NULL;
 
        if (!string || !*string)
                return NULL;
-       if (mem_ctx) {
-               s = talloc_strdup(mem_ctx, string);
-       } else {
-               s = SMB_STRDUP(string);
+
+       list = TALLOC_ARRAY(mem_ctx, char *, S_LIST_ABS+1);
+       if (list == NULL) {
+               return NULL;
        }
-       if (!s) {
+       lsize = S_LIST_ABS;
+
+       s = talloc_strdup(list, string);
+       if (s == NULL) {
                DEBUG(0,("str_list_make: Unable to allocate memory"));
+               TALLOC_FREE(list);
                return NULL;
        }
        if (!sep) sep = LIST_SEP;
 
-       num = lsize = 0;
-       list = NULL;
-
+       num = 0;
        str = s;
-       frame = talloc_stackframe();
-       while (next_token_talloc(frame, &str, &tok, sep)) {
+
+       while (next_token_talloc(list, &str, &tok, sep)) {
+
                if (num == lsize) {
+                       char **tmp;
+
                        lsize += S_LIST_ABS;
-                       if (mem_ctx) {
-                               rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list,
-                                               char *, lsize +1);
-                       } else {
-                               /* We need to keep the old list on
-                                * error so we can free the elements
-                                  if the realloc fails. */
-                               rlist =SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list,
-                                               char *, lsize +1);
-                       }
-                       if (!rlist) {
+
+                       tmp = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *,
+                                                  lsize + 1);
+                       if (tmp == NULL) {
                                DEBUG(0,("str_list_make: "
                                        "Unable to allocate memory"));
-                               str_list_free(&list);
-                               if (mem_ctx) {
-                                       TALLOC_FREE(s);
-                               } else {
-                                       SAFE_FREE(s);
-                               }
-                               TALLOC_FREE(frame);
+                               TALLOC_FREE(list);
                                return NULL;
-                       } else {
-                               list = rlist;
                        }
-                       memset (&list[num], 0,
-                                       ((sizeof(char**)) * (S_LIST_ABS +1)));
-               }
 
-               if (mem_ctx) {
-                       list[num] = talloc_strdup(mem_ctx, tok);
-               } else {
-                       list[num] = SMB_STRDUP(tok);
-               }
+                       list = tmp;
 
-               if (!list[num]) {
-                       DEBUG(0,("str_list_make: Unable to allocate memory"));
-                       str_list_free(&list);
-                       if (mem_ctx) {
-                               TALLOC_FREE(s);
-                       } else {
-                               SAFE_FREE(s);
-                       }
-                       TALLOC_FREE(frame);
-                       return NULL;
+                       memset (&list[num], 0,
+                               ((sizeof(char**)) * (S_LIST_ABS +1)));
                }
 
-               num++;
+               list[num] = tok;
+               num += 1;
        }
 
-       TALLOC_FREE(frame);
-
-       if (mem_ctx) {
-               TALLOC_FREE(s);
-       } else {
-               SAFE_FREE(s);
-       }
+       list[num] = NULL;
 
+       TALLOC_FREE(s);
        return list;
 }
 
-char **str_list_make_talloc(TALLOC_CTX *mem_ctx,
-               const char *string,
-               const char *sep)
-{
-       return str_list_make_internal(mem_ctx, string, sep);
-}
-
-char **str_list_make(const char *string, const char *sep)
-{
-       return str_list_make_internal(NULL, string, sep);
-}
-
-bool str_list_copy(char ***dest, const char **src)
+bool str_list_copy(TALLOC_CTX *mem_ctx, char ***dest, const char **src)
 {
-       char **list, **rlist;
-       int num, lsize;
+       char **list;
+       int i, num;
 
        *dest = NULL;
        if (!src)
                return false;
 
-       num = lsize = 0;
-       list = NULL;
+       num = 0;
+       while (src[num] != NULL) {
+               num += 1;
+       }
 
-       while (src[num]) {
-               if (num == lsize) {
-                       lsize += S_LIST_ABS;
-                       rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list,
-                                       char *, lsize +1);
-                       if (!rlist) {
-                               DEBUG(0,("str_list_copy: "
-                                       "Unable to re-allocate memory"));
-                               str_list_free(&list);
-                               return false;
-                       } else {
-                               list = rlist;
-                       }
-                       memset (&list[num], 0,
-                                       ((sizeof(char **)) * (S_LIST_ABS +1)));
-               }
+       list = TALLOC_ARRAY(mem_ctx, char *, num+1);
+       if (list == NULL) {
+               return false;
+       }
 
-               list[num] = SMB_STRDUP(src[num]);
-               if (!list[num]) {
-                       DEBUG(0,("str_list_copy: Unable to allocate memory"));
-                       str_list_free(&list);
+       for (i=0; i<num; i++) {
+               list[i] = talloc_strdup(list, src[i]);
+               if (list[i] == NULL) {
+                       TALLOC_FREE(list);
                        return false;
                }
-
-               num++;
        }
-
+       list[i] = NULL;
        *dest = list;
        return true;
 }
@@ -2010,37 +1954,6 @@ bool str_list_compare(char **list1, char **list2)
        return true;
 }
 
-static void str_list_free_internal(TALLOC_CTX *mem_ctx, char ***list)
-{
-       char **tlist;
-
-       if (!list || !*list)
-               return;
-       tlist = *list;
-       for(; *tlist; tlist++) {
-               if (mem_ctx) {
-                       TALLOC_FREE(*tlist);
-               } else {
-                       SAFE_FREE(*tlist);
-               }
-       }
-       if (mem_ctx) {
-               TALLOC_FREE(*tlist);
-       } else {
-               SAFE_FREE(*list);
-       }
-}
-
-void str_list_free_talloc(TALLOC_CTX *mem_ctx, char ***list)
-{
-       str_list_free_internal(mem_ctx, list);
-}
-
-void str_list_free(char ***list)
-{
-       str_list_free_internal(NULL, list);
-}
-
 /******************************************************************************
  *****************************************************************************/
 
@@ -2059,25 +1972,26 @@ int str_list_count( const char **list )
 }
 
 /******************************************************************************
- version of standard_sub_basic() for string lists; uses alloc_sub_basic()
+ version of standard_sub_basic() for string lists; uses talloc_sub_basic()
  for the work
  *****************************************************************************/
 
 bool str_list_sub_basic( char **list, const char *smb_name,
                         const char *domain_name )
 {
+       TALLOC_CTX *ctx = list;
        char *s, *tmpstr;
 
        while ( *list ) {
                s = *list;
-               tmpstr = alloc_sub_basic(smb_name, domain_name, s);
+               tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
                if ( !tmpstr ) {
                        DEBUG(0,("str_list_sub_basic: "
                                "alloc_sub_basic() return NULL!\n"));
                        return false;
                }
 
-               SAFE_FREE(*list);
+               TALLOC_FREE(*list);
                *list = tmpstr;
 
                list++;
@@ -2173,6 +2087,7 @@ static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service)
 {
        char *new_ipstr = NULL;
        char addr_buf[INET6_ADDRSTRLEN];
+       int ret;
 
        /* arguments checking */
        if (!ipstr_list || !service) {
@@ -2187,33 +2102,30 @@ static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service)
        if (*ipstr_list) {
                if (service->ss.ss_family == AF_INET) {
                        /* IPv4 */
-                       asprintf(&new_ipstr, "%s%s%s:%d",
-                                       *ipstr_list,
-                                       IPSTR_LIST_SEP,
-                                       addr_buf,
-                                       service->port);
+                       ret = asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list,
+                                      IPSTR_LIST_SEP, addr_buf,
+                                      service->port);
                } else {
                        /* IPv6 */
-                       asprintf(&new_ipstr, "%s%s[%s]:%d",
-                                       *ipstr_list,
-                                       IPSTR_LIST_SEP,
-                                       addr_buf,
-                                       service->port);
+                       ret = asprintf(&new_ipstr, "%s%s[%s]:%d", *ipstr_list,
+                                      IPSTR_LIST_SEP, addr_buf,
+                                      service->port);
                }
                SAFE_FREE(*ipstr_list);
        } else {
                if (service->ss.ss_family == AF_INET) {
                        /* IPv4 */
-                       asprintf(&new_ipstr, "%s:%d",
-                               addr_buf,
-                               service->port);
+                       ret = asprintf(&new_ipstr, "%s:%d", addr_buf,
+                                      service->port);
                } else {
                        /* IPv6 */
-                       asprintf(&new_ipstr, "[%s]:%d",
-                               addr_buf,
-                               service->port);
+                       ret = asprintf(&new_ipstr, "[%s]:%d", addr_buf,
+                                      service->port);
                }
        }
+       if (ret == -1) {
+               return NULL;
+       }
        *ipstr_list = new_ipstr;
        return *ipstr_list;
 }
@@ -2258,7 +2170,7 @@ char *ipstr_list_make(char **ipstr_list,
  * @param ipstr ip string list to be parsed
  * @param ip_list pointer to array of ip addresses which is
  *        allocated by this function and must be freed by caller
- * @return number of succesfully parsed addresses
+ * @return number of successfully parsed addresses
  **/
 
 int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list)