save/restore r11 in 32bit syscalls.
[trinity.git] / random-pathname.c
blob004996b5479cca757b0cd9cbc62accba367ad980
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "arch.h"
5 #include "log.h"
6 #include "maps.h"
7 #include "random.h"
8 #include "sanitise.h"
10 const char * generate_pathname(void)
12 const char *pathname = get_filename();
13 char *newpath;
14 unsigned int len;
15 unsigned int i;
17 if (pathname == NULL) /* handle -n correctly. */
18 return NULL;
20 /* 90% chance of returning an unmangled filename */
21 if ((rand() % 100) < 90)
22 return pathname;
24 /* Create a bogus filename. */
25 newpath = malloc(page_size); // FIXME: We leak this.
26 if (newpath == NULL)
27 return pathname; // give up.
29 len = strlen(pathname);
31 /* empty string. */
32 if ((rand() % 100) == 0) {
33 memset(newpath, 0, page_size);
34 goto out;
37 generate_random_page(newpath);
39 /* sometimes, just complete junk. */
40 if (rand_bool())
41 goto out;
43 /* Sometimes, pathname + junk */
44 if (rand_bool())
45 (void) strncpy(newpath, pathname, len);
46 else {
47 /* make it look relative to cwd */
48 newpath[0] = '.';
49 newpath[1] = '/';
50 (void) strncpy(newpath + 2, pathname, len);
53 /* Sometimes, remove all /'s */
54 if (rand_bool()) {
55 for (i = 0; i < len; i++) {
56 if (newpath[i] == '/')
57 newpath[i] = rand();
60 out:
61 /* 50/50 chance of making it look like a dir */
62 if (rand_bool()) {
63 newpath[len] = '/';
64 newpath[len + 1] = 0;
67 return newpath;