When using multiple [...] blocks in RANDOMSTRING
authorRonnie sahlberg <ronniesahlberg@gmail.com>
Tue, 18 May 2010 07:14:37 +0000 (17:14 +1000)
committerRonnie sahlberg <ronniesahlberg@gmail.com>
Tue, 18 May 2010 07:14:37 +0000 (17:14 +1000)
all [...] blocks would be replacved by the string picked for the firs
toccurence.

Update the code so we use different strings for each for "extra
randomness".

child.c
proto.h
util.c

diff --git a/child.c b/child.c
index 3b565dd2c093e2d7aac5f005f1c1707b7bb16235..88d144b1bff1527b481d30d5c64f27d9daf9446c 100644 (file)
--- a/child.c
+++ b/child.c
@@ -270,7 +270,7 @@ again:
        rndc[0] = str[random()%num + 1];
        rndc[1] = '\0';
 
-       all_string_sub(line, str, rndc);
+       single_string_sub(line, str, rndc);
        goto again;
 
 
diff --git a/proto.h b/proto.h
index 5ab72e2dcd81de95960ea3cb788c8a0e9ffec1c8..1431b27afa9399638483de9f974b8e315269c26e 100644 (file)
--- a/proto.h
+++ b/proto.h
@@ -76,6 +76,7 @@ int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size
 
 void *shm_setup(int size);
 void all_string_sub(char *s,const char *pattern,const char *insert);
+void single_string_sub(char *s,const char *pattern,const char *insert);
 BOOL next_token(char **ptr,char *buff,char *sep);
 struct timeval timeval_current(void);
 double timeval_elapsed(struct timeval *tv);
diff --git a/util.c b/util.c
index 10145ac9f914e35ca985d12550e0a18374d42051..d630471d5376e4ef7a058fa1fc56e0791c88de64 100644 (file)
--- a/util.c
+++ b/util.c
@@ -91,6 +91,25 @@ void all_string_sub(char *s,const char *pattern,const char *insert)
        }
 }
 
+void single_string_sub(char *s,const char *pattern,const char *insert)
+{
+       char *p;
+       size_t ls,lp,li;
+
+       if (!insert || !pattern || !s) return;
+
+       ls = strlen(s);
+       lp = strlen(pattern);
+       li = strlen(insert);
+
+       if (!*pattern) return;
+       
+       p = strstr(s,pattern);
+       memmove(p+li,p+lp,ls + 1 - (((int)(p-s)) + lp));
+       memcpy(p, insert, li);
+       s = p + li;
+       ls += (li-lp);
+}
 
 /****************************************************************************
   Get the next token from a string, return False if none found