summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 1cc20a43752be2feb405273637a861dfc232f8a9 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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 "opk.h"
#include "ustar.h"

#include "config.h"

#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#else
#include <unistd.h>
#endif

extern const char *PACKAGE_VERSION_GIT;

const char *_optstring = "I:fchV";
#ifdef HAVE_GETOPT_LONG
struct option _longopts[] = {
	{
		.name    = "info",
		.has_arg = 1,
		.flag    = NULL,
		.val     = 'I',
	},
	{
		.name    = "control",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'f',
	},
	{
		.name    = "contents",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'c',
	},
	{
		.name    = "help",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'h',
	},
	{
		.name    = "version",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'V',
	},
};
#endif

static void
_version(void)
{
	printf("%s %s%s\n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_VERSION_GIT);
	printf("Copyright (C) %s  %s\n", "2023", "Patrick McDermott");
	puts("License GPLv3+: GNU GPL version 3 or later "
			"<http://gnu.org/licenses/gpl.html>.\n"
			"This is free software: you are free to change and "
			"redistribute it.\n"
			"There is NO WARRANTY, to the extent permitted by "
			"law.\n");
	printf("Please report bugs to <%s>.\n", PACKAGE_BUGREPORT);
}

int
main(int argc, char *argv[])
{
	struct opkg_opk_ustar_seek_name *control_files;
	int                              list_members;
	int                              opt;
	struct opkg_opk_opk             *opk;

	control_files = NULL;
	list_members  = 0;
#ifdef HAVE_GETOPT_LONG
	while ((opt = getopt_long(argc, argv, _optstring, _longopts, NULL))
			!= -1) {
#else
	while ((opt = getopt(argc, argv, _optstring)) != -1) {
#endif
		switch(opt) {
			case 'I':
				if (opkg_opk_ustar_add_seek_name(&control_files,
							optarg) !=
						OPKG_OPK_OK) {
					goto error0;
				}
				break;
			case 'f':
				if (opkg_opk_ustar_add_seek_name(&control_files,
							"control") !=
						OPKG_OPK_OK) {
					goto error0;
				}
				break;
			case 'c':
				list_members = 1;
				break;
			case 'h':
				/* TODO */
				break;
			case 'V':
				_version();
				return EXIT_SUCCESS;
			default:
				/* TODO */
				break;
		}
	}
	argc -= optind;
	argv += optind;

	/* Initialize outer archive. */
	opk = opkg_opk_opk_init(argv[0]);
	if (opk == NULL) {
		goto error0;
	}

	/* Read control file. */
	if (control_files != NULL) {
		if (opkg_opk_opk_read_control(opk, control_files) !=
				OPKG_OPK_OK) {
			goto error1;
		}
	}

	/* List data files. */
	if (list_members == 1) {
		if (opkg_opk_opk_list_members(opk) != OPKG_OPK_OK) {
			goto error1;
		}
	}

	opkg_opk_opk_free(opk);
	if (control_files != NULL) {
		opkg_opk_ustar_free_seek_names(control_files);
	}
	return EXIT_SUCCESS;

 error1:
	opkg_opk_opk_free(opk);
 error0:
	if (control_files != NULL) {
		opkg_opk_ustar_free_seek_names(control_files);
	}
	return EXIT_FAILURE;
}