summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 26e60fa8aa2acdee31ff7bbad2a64abbd16423e8 (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
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opk.
 *
 * opkg-opk is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opk is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opkg-opk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
#include "gzip.h"
#include "ustar.h"

struct _opkg_opk_main_file {
	FILE *file;
	char  buffer[8192];
};

static int
_opkg_opk_main_file_read(void *user_data, char **buffer, size_t *size)
{
	struct _opkg_opk_main_file *file = user_data;

	*buffer = file->buffer;
	*size   = fread(file->buffer, 1, 8192, file->file);
	if (feof(file->file)) {
		return OPKG_OPK_END;
	} else if (ferror(file->file) || *size == 0) {
		return OPKG_OPK_ERROR;
	} else {
		return OPKG_OPK_OK;
	}
}

int
main(int argc, char *argv[])
{
	struct _opkg_opk_main_file    file;
	struct opkg_opk_gzip         *gzip;
	struct opkg_opk_ustar        *ustar;
	struct opkg_opk_ustar_member  member;
	int                           ret;

	file.file = fopen(argv[1], "rb");
	if (file.file == NULL) {
		return EXIT_FAILURE;
	}
	gzip = opkg_opk_gzip_init(&_opkg_opk_main_file_read, &file);
	if (gzip == NULL) {
		fclose(file.file);
		return EXIT_FAILURE;
	}
	ustar = opkg_opk_ustar_init(gzip);
	if (ustar == NULL) {
		/* TODO: Free gzip. */
		fclose(file.file);
		return EXIT_FAILURE;
	}
	while ((ret = opkg_opk_ustar_list(ustar, &member)) == OPKG_OPK_OK) {
		puts(member.name);
	}
	/* TODO: Free ustar and gzip. */
	fclose(file.file);

	return EXIT_SUCCESS;
}