56
|
1 |
/**
|
|
2 |
* test/demos/files.c
|
|
3 |
* Copyright (C) Markus Broeker
|
|
4 |
*/
|
|
5 |
|
|
6 |
#include <stdio.h>
|
|
7 |
#include <stdlib.h>
|
|
8 |
#include <unistd.h>
|
|
9 |
#include <fcntl.h>
|
|
10 |
#include <sys/types.h>
|
|
11 |
#include <sys/stat.h>
|
|
12 |
#include <time.h>
|
|
13 |
|
|
14 |
#define MAXDIRS 1000
|
|
15 |
#define MAXFILES 1000
|
|
16 |
|
|
17 |
#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
|
|
18 |
|
|
19 |
char *GetRandomNumberString ()
|
|
20 |
{
|
|
21 |
static char buffer[12];
|
|
22 |
|
|
23 |
sprintf (buffer, "%d", GETRANDOM (65535));
|
|
24 |
|
|
25 |
return buffer;
|
|
26 |
}
|
|
27 |
|
|
28 |
int main (int argc, char **argv)
|
|
29 |
{
|
|
30 |
int i, j;
|
|
31 |
int fd;
|
|
32 |
char *s;
|
|
33 |
|
|
34 |
if (argc != 2) {
|
|
35 |
printf ("Usage: %s <basedir>\n", argv[0]);
|
|
36 |
return EXIT_FAILURE;
|
|
37 |
}
|
|
38 |
|
|
39 |
if (chdir (argv[1]) == -1) {
|
|
40 |
printf ("Initial Chdir to %s failed, aborting...\n", argv[1]);
|
|
41 |
return EXIT_FAILURE;
|
|
42 |
}
|
|
43 |
|
|
44 |
srand (time (NULL));
|
|
45 |
|
|
46 |
for (i = 0; i < MAXDIRS; i++) {
|
|
47 |
s = GetRandomNumberString ();
|
|
48 |
mkdir (s, 0777);
|
|
49 |
if (chdir (s) == -1) {
|
|
50 |
printf ("Chdir to %s failed, aborting\n", s);
|
|
51 |
return EXIT_FAILURE;
|
|
52 |
}
|
|
53 |
for (j = 0; j < MAXFILES; j++) {
|
|
54 |
s = GetRandomNumberString ();
|
|
55 |
if ((fd = open (s, O_RDWR | O_CREAT, 0666)) < 3) {
|
|
56 |
perror ("CREATE");
|
|
57 |
return EXIT_FAILURE;
|
|
58 |
}
|
|
59 |
close (fd);
|
|
60 |
}
|
|
61 |
chdir ("..");
|
|
62 |
}
|
|
63 |
return EXIT_SUCCESS;
|
|
64 |
}
|