1 #include <check.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "../test.h"
6 #include "../prng.h"
7 #include "../../src/alloc.h"
8 #include "../../src/attribs.h"
9 #include "../../src/handy.h"
10 #include "../../src/pathcmp.h"
11 #include "../../src/sbuf.h"
12 #include "build.h"
13 
char_gen(void)14 static char char_gen(void)
15 {
16 	char a='\0';
17 	uint32_t r=0;
18 	while(!isalnum(a))
19 	{
20 		if(!r) r=prng_next();
21 		a=r>>=8;
22 	}
23 	return a;
24 }
25 
comp_gen(void)26 static char *comp_gen(void)
27 {
28 	char *c;
29 	int32_t r;
30 	char comp[16]="";
31 	r=5+(prng_next()%10);
32 	for(c=comp; c-comp<r; c++)
33 		*c=char_gen();
34 	*c='\0';
35 	return strdup_w(comp, __func__);
36 }
37 
38 #define COMP_SIZE	20
39 
gen_fullpath(const char * prefix,char ** comps)40 static char *gen_fullpath(const char *prefix, char **comps)
41 {
42 	uint32_t i;
43 	uint32_t number_of_components;
44 	char *path=NULL;
45 	char *file=NULL;
46 	char cwd[PATH_MAX];
47 
48 	number_of_components=prng_next()%6;
49 	fail_unless(getcwd(cwd, sizeof(cwd))!=NULL);
50 	fail_unless(!astrcat(&path, cwd, __func__));
51 
52 	if(prefix)
53 	{
54 		fail_unless(!astrcat(&path, "/", __func__));
55 		fail_unless(!astrcat(&path, prefix, __func__));
56 	}
57 	for(i=0; i<number_of_components; i++)
58 	{
59 		uint32_t choice;
60 		choice=prng_next()%COMP_SIZE;
61 		fail_unless(!astrcat(&path, "/", __func__));
62 		fail_unless(!astrcat(&path, comps[choice], __func__));
63 	}
64 
65 	fail_unless((file=comp_gen())!=NULL);
66 	fail_unless(!astrcat(&path, "/", __func__));
67 	fail_unless(!astrcat(&path, file, __func__));
68 	free_w(&file);
69 	return path;
70 }
71 
mypathcmp(const void * a,const void * b)72 static int mypathcmp(const void *a, const void *b)
73 {
74 	const char *x=*(const char **)a;
75 	const char *y=*(const char **)b;
76 	return pathcmp(x, y);
77 }
78 
build_paths(const char * prefix,int wanted)79 char **build_paths(const char *prefix, int wanted)
80 {
81 	uint32_t i=0;
82 	char **paths;
83 	char *comps[COMP_SIZE];
84 
85 	paths=(char **)calloc_w(wanted, sizeof(char *), __func__);
86 	for(i=0; i<COMP_SIZE; i++)
87 		fail_unless((comps[i]=comp_gen())!=NULL);
88 
89 	for(int j=0; j<wanted; j++)
90 		paths[j]=gen_fullpath(prefix, comps);
91 
92 	qsort(paths, wanted, sizeof(char *), mypathcmp);
93 
94 	for(i=0; i<COMP_SIZE; i++)
95 		free_w(&comps[i]);
96 	return paths;
97 }
98