-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
main.c
91 lines (75 loc) · 2.01 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include "optparse.h"
#include "config.h"
#include "file.h"
#include "hmap.h"
#include "tag_duplicates.h"
#include "user_input.h"
#include "status.h"
#include "error.h"
struct conf g_conf = {
.infile_name = NULL,
.outfile_name = NULL,
.threads = 0,
.line_max_size = DEFAULT_LINE_MAX_SIZE,
.hmap_size = 0,
.chunk_size = 0,
.filter_printable = 0,
.memlimit = LONG_MAX,
};
struct file *g_file;
struct hmap g_hmap;
/** Rewrite file, ignoring lines tagged with 'DISABLED_LINE'
*/
static void remove_duplicates(void)
{
t_chunk file_chunk;
t_line line;
size_t line_size;
char *dst;
char *base_ptr;
int i;
file_chunk.ptr = g_file->addr;
file_chunk.endptr = g_file->addr + g_file->info.st_size;
i = 0;
base_ptr = file_chunk.ptr;
dst = file_chunk.ptr;
while (get_next_line(&line, &file_chunk))
{
line_size = LINE_SIZE(line);
memmove(dst, LINE_ADDR(line), line_size);
dst += line_size;
if (dst != file_chunk.endptr)
*dst++ = '\n';
i++;
if (i == 500000) {
set_status(FCLEAN_BYTES, (size_t)(file_chunk.ptr - base_ptr));
base_ptr = file_chunk.ptr;
i = 0;
}
}
/* update file size */
g_file->info.st_size = dst - g_file->addr;
set_status(FCLEAN_BYTES, (size_t)(file_chunk.ptr - base_ptr));
}
int main(int argc, char **argv)
{
optparse(argc, argv); /* setup g_conf options */
if (isatty(STDIN_FILENO))
watch_user_input();
update_status(FCOPY_START);
init_file(g_conf.infile_name, g_conf.outfile_name);
config(); /* configure g_conf options */
set_status(CHUNK_SIZE, g_conf.chunk_size);
init_hmap(g_conf.hmap_size);
update_status(TAGDUP_START);
tag_duplicates();
destroy_hmap();
update_status(FCLEAN_START);
remove_duplicates();
destroy_file();
return (0);
}