summaryrefslogtreecommitdiffstats
path: root/src/tsnif-replay.c
blob: 0f1455ff54a51b7bebb0d8bf08ea157574557fad (plain)
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

#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

#include "storage.h"


static char *filename;
struct tsnif_storage_handle storage_handle;
static struct tsnif_storage_opts storage_opts = {
	.flags    = TSNIF_STORAGE_OPT_READ,
};

int tsnif_debug = 0;

static void usage()
{
	printf("tsnif-replay -f <file>\n");
	_exit(-1);
}

static int get_args(int argc, char **argv)
{
	while (1) {
		int c;
		int option_index = 0;
		static struct option long_options[] = {
			{"file", required_argument, 0, 'f'},
			{"debug", no_argument, 0, 'd'},
			{"help", no_argument, 0, 'h'},
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "f:dh",
			long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
		case 'f':
			filename = optarg;
			break;

		case 'd':
			tsnif_debug = 1;
			break;

		case 'h':
			usage();
			break;

		default:
			printf("unknown option '%c'", c);
		} /* switch(c) */
	} /* while(1) */

	return 0;
}

int main(int argc, char **argv)
{
	int err;

	if (get_args(argc, argv))
		usage();

	err = tsnif_storage_init(&storage_handle, &storage_opts, filename);
	if (err)
		return -1;

	do {
		struct tsnif_storage_rec rec;

		err = tsnif_storage_read(&storage_handle,
					TSNIF_STORAGE_READ_NEXT, &rec);

		if (err)
			break;

		fwrite(rec.ptr, rec.len, 1, stdout);
		fflush(NULL);

	} while(1);

	tsnif_storage_close(&storage_handle);
	return 0;
}