[go: up one dir, main page]

File: random-pathname.c

package info (click to toggle)
trinity 1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,252 kB
  • ctags: 2,738
  • sloc: ansic: 24,011; sh: 322; makefile: 141
file content (68 lines) | stat: -rw-r--r-- 1,332 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arch.h"
#include "log.h"
#include "maps.h"
#include "random.h"
#include "sanitise.h"

const char * generate_pathname(void)
{
	const char *pathname = get_filename();
	char *newpath;
	unsigned int len;
	unsigned int i;

	if (pathname == NULL)		/* handle -n correctly. */
		return NULL;

	/* 90% chance of returning an unmangled filename */
	if ((rand() % 100) < 90)
		return pathname;

	/* Create a bogus filename. */
	newpath = malloc(page_size);	// FIXME: We leak this.
	if (newpath == NULL)
		return pathname;	// give up.

	len = strlen(pathname);

	/* empty string. */
	if ((rand() % 100) == 0) {
		memset(newpath, 0, page_size);
		goto out;
	}

	generate_random_page(newpath);

	/* sometimes, just complete junk. */
	if (rand_bool())
		goto out;

	/* Sometimes, pathname + junk */
	if (rand_bool())
		(void) strncpy(newpath, pathname, len);
	else {
		/* make it look relative to cwd */
		newpath[0] = '.';
		newpath[1] = '/';
		(void) strncpy(newpath + 2, pathname, len);
	}

	/* Sometimes, remove all /'s */
	if (rand_bool()) {
		for (i = 0; i < len; i++) {
			if (newpath[i] == '/')
				newpath[i] = rand();
		}
	}
out:
	/* 50/50 chance of making it look like a dir */
	if (rand_bool()) {
		newpath[len] = '/';
		newpath[len + 1] = 0;
	}

	return newpath;
}