-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGarbageGenerator.c
64 lines (56 loc) · 1.29 KB
/
GarbageGenerator.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#define true 1
#define false 0
#define next (rand() % 15)
char ascii[10], cur[1];
int fd;
//Convert a number to string and put it in the garbage file
void NumWriter(int num){
cur[0] = ascii[1];
if(num >= 10)
write(fd, cur, 1);
cur[0] = ascii[num % 10];
write(fd, cur, 1);
}
//Write a character in the garbage file
void WriteSep(char sep){
cur[0] = sep;
write(fd, cur, 1);
}
//Generate two random numbers and put them
// in the garbage file with a comma between them
void MakePair(){
NumWriter(next);
WriteSep(',');
NumWriter(next);
}
//Generate the garbage file data
void GarbageGenerator(const char **argv){
fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0664);
srand(time(NULL));
for(int i = 0; i < 100; ++i){
MakePair();
for(int j = 1; j < 5; ++j){
WriteSep(' ');
MakePair();
}
WriteSep('\n');
}
close(fd);
}
//Create a mapping list to convert int to char
void InitializeAll(){
ascii[0] = 48;
for(int i = 1; i < 10; ++i)
ascii[i] = ascii[i - 1] + 1;
}
int main(int arg, const char **argv){
InitializeAll();
GarbageGenerator(argv);
return 0;
}