Logo Search packages:      
Sourcecode: likewise-open version File versions

char* realloc_string_sub2 ( char *  string,
const char *  pattern,
const char *  insert,
bool  remove_unsafe_characters,
bool  allow_trailing_dollar 
)

Similar to string_sub2, but it will accept only allocated strings and may realloc them so pay attention at what you pass on no pointers inside strings, no const may be passed as string.

Definition at line 1138 of file util_str.c.

{
      char *p, *in;
      char *s;
      ssize_t ls,lp,li,ld, i;

      if (!insert || !pattern || !*pattern || !string || !*string)
            return NULL;

      s = string;

      in = SMB_STRDUP(insert);
      if (!in) {
            DEBUG(0, ("realloc_string_sub: out of memory!\n"));
            return NULL;
      }
      ls = (ssize_t)strlen(s);
      lp = (ssize_t)strlen(pattern);
      li = (ssize_t)strlen(insert);
      ld = li - lp;
      for (i=0;i<li;i++) {
            switch (in[i]) {
                  case '`':
                  case '"':
                  case '\'':
                  case ';':
                  case '$':
                        /* allow a trailing $
                         * (as in machine accounts) */
                        if (allow_trailing_dollar && (i == li - 1 )) {
                              break;
                        }
                  case '%':
                  case '\r':
                  case '\n':
                        if ( remove_unsafe_characters ) {
                              in[i] = '_';
                              break;
                        }
                  default:
                        /* ok */
                        break;
            }
      }

      while ((p = strstr_m(s,pattern))) {
            if (ld > 0) {
                  int offset = PTR_DIFF(s,string);
                  string = (char *)SMB_REALLOC(string, ls + ld + 1);
                  if (!string) {
                        DEBUG(0, ("realloc_string_sub: "
                              "out of memory!\n"));
                        SAFE_FREE(in);
                        return NULL;
                  }
                  p = string + offset + (p - s);
            }
            if (li != lp) {
                  memmove(p+li,p+lp,strlen(p+lp)+1);
            }
            memcpy(p, in, li);
            s = p + li;
            ls += ld;
      }
      SAFE_FREE(in);
      return string;
}


Generated by  Doxygen 1.6.0   Back to index