author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:50:39 +0200 | |
changeset 74 | 829976007e62 |
parent 56 | 966ad681f25d |
child 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
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 |
||
74
829976007e62
getrandom macro fixed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
56
diff
changeset
|
17 |
#define GETRANDOM(max) (1+(int)((float)(max)*rand()/RAND_MAX+1.0)) |
56 | 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 |
} |