[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Tyson Smith <tyson.w.smith@xxxxxxxxx>

This will generate a path that will likely not exist but may
look somewhat valid or totally crazy depending on rand.
---
 random-pathname.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/random-pathname.c b/random-pathname.c
index 4d093d1..d8f5094 100644
--- a/random-pathname.c
+++ b/random-pathname.c
@@ -9,6 +9,52 @@
 
 #define MAX_PATH_LEN 4096
 
+/*
+ * Generate a path that will likely not exist but may look
+ * somewhat valid or totally crazy depending on rand.
+ */
+static void random_pathstring(char *path, unsigned int len) {
+	const char *fmts = "dns";
+	unsigned int i;
+
+	if (len < 1)
+		return;
+
+	switch(rand() % 5) {
+	case 0:
+		// single repeating random ASCII character
+		(void) memset(path, (rand() % 95) + 32, rand() % len);
+		path[len-1] = '\0';
+		break;
+	case 1:
+		// random ASCII characters 32(space) -> 126(~)
+		for (i=0; i < len; i++)
+			path[i] = (char) ((rand() % 95) + 32);
+		path[len-1] = '\0';
+		break;
+	case 2:
+		// random . or /
+		for (i=0; i < len; i++)
+			path[i] = RAND_BOOL() ? '.' : '/';
+		path[len-1] = '\0';
+		break;
+	case 3:
+		// format strings
+		for (i=0; i < (len - 2); i+=2) {
+			path[i] = '%';
+			path[i+1] = fmts[rand() % 3];
+		}
+		path[len-1] = '\0';
+		break;
+	case 4:
+		// junk
+		for (i=0; i < len; i++)
+			path[i] = (char) RAND_BYTE();
+		path[len-1] = '\0';
+		break;
+	}
+}
+
 const char * generate_pathname(void)
 {
 	const char *pathname = get_filename();
@@ -25,6 +71,13 @@ const char * generate_pathname(void)
 	/* Create a bogus filename. */
 	newpath = zmalloc(MAX_PATH_LEN);	// FIXME: We leak this.
 
+	/* 50/50 chance using a random path string */
+	if (RAND_BOOL()) {
+		len = RAND_BOOL() ? RAND_BYTE() : rand() % MAX_PATH_LEN;
+		random_pathstring(newpath, len);
+		return newpath;
+	}
+
 	len = strlen(pathname);
 
 	if (RAND_BOOL())
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe trinity" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux SCSI]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux