Skip to content

Commit c614a06

Browse files
committed
[acquire/tests/test_{amalgamation,checksum}.h] Fix NUM_FORMAT definition and use same one everywhere ; [acquire/cli] Try and dynamically allocate because macOS has a small stack buffer
1 parent b99a4f2 commit c614a06

File tree

5 files changed

+103
-73
lines changed

5 files changed

+103
-73
lines changed

acquire/cli/cli.c

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ int parse_args(struct Tokens *ts, struct Elements *elements) {
224224
}
225225

226226
int elems_to_args(struct Elements *elements, struct DocoptArgs *args,
227-
const int help, const char *version) {
227+
const bool help, const char *version) {
228228
struct Command *command;
229229
struct Argument *argument;
230230
struct Option *option;
@@ -282,39 +282,15 @@ int elems_to_args(struct Elements *elements, struct DocoptArgs *args,
282282
args->url = (char *)argument->value;
283283
}
284284
}
285-
return 0;
285+
return EXIT_SUCCESS;
286286
}
287287

288288
/*
289289
* Main docopt function
290290
*/
291-
struct DocoptArgs docopt(int argc, char *argv[], const int help,
292-
const char *version) {
293-
struct DocoptArgs args = {
294-
NULL,
295-
0,
296-
0,
297-
0,
298-
NULL,
299-
NULL,
300-
NULL,
301-
NULL,
302-
usage_pattern,
303-
{"acquire: The core for your package manager, minus the dependency graph "
304-
"components. Download, verify, and extract.",
305-
"", "Usage:",
306-
" acquire --check --directory=<d> --hash=<h> --checksum=<sha> <url>...",
307-
" acquire --directory=<d> --hash=<h> --checksum=<sha> <url>...",
308-
" acquire --output=<f> <url>...", " acquire --help",
309-
" acquire --version", "",
310-
"Options:", " -h --help Show this screen.",
311-
" --version Show version.",
312-
" --check Check if already downloaded.",
313-
" --hash=<h> Hash to verify.",
314-
" --checksum=<sha> Checksum algorithm, e.g., SHA256 or SHA512.",
315-
" -d=<d>, --directory=<d> Location to download files to.",
316-
" -o=<f>, --output=<f> Output file. If not specified, will derive "
317-
"from URL."}};
291+
int docopt(struct DocoptArgs *args, int argc, char *argv[], const bool help,
292+
const char *version) {
293+
int rc = EXIT_SUCCESS;
318294
struct Command commands[1];
319295
struct Argument arguments[] = {{"<url>", NULL, {NULL}}};
320296
struct Option options[] = {
@@ -323,7 +299,35 @@ struct DocoptArgs docopt(int argc, char *argv[], const int help,
323299
{"-d", "--directory", 1, 0, NULL}, {NULL, "--hash", 1, 0, NULL},
324300
{"-o", "--output", 1, 0, NULL}};
325301
struct Elements elements;
326-
int return_code = 0;
302+
const char *help_message[17] = {
303+
"acquire: The core for your package manager, minus the dependency graph "
304+
"components. Download, verify, and extract.",
305+
"",
306+
"Usage:",
307+
" acquire --check --directory=<d> --hash=<h> --checksum=<sha> <url>...",
308+
" acquire --directory=<d> --hash=<h> --checksum=<sha> <url>...",
309+
" acquire --output=<f> <url>...",
310+
" acquire --help",
311+
" acquire --version",
312+
"",
313+
"Options:",
314+
" -h --help Show this screen.",
315+
" --version Show version.",
316+
" --check Check if already downloaded.",
317+
" --hash=<h> Hash to verify.",
318+
" --checksum=<sha> Checksum algorithm, e.g., SHA256 or SHA512.",
319+
" -d=<d>, --directory=<d> Location to download files to.",
320+
" -o=<f>, --output=<f> Output file. If not specified, will derive "
321+
"from URL."};
322+
args->url = NULL;
323+
args->check = 0;
324+
args->version = 0;
325+
args->checksum = NULL;
326+
args->directory = NULL;
327+
args->hash = NULL;
328+
args->output = NULL;
329+
args->usage_pattern = usage_pattern;
330+
memcpy(args->help_message, help_message, sizeof help_message);
327331

328332
elements.n_commands = 0;
329333
elements.n_arguments = 1;
@@ -336,15 +340,15 @@ struct DocoptArgs docopt(int argc, char *argv[], const int help,
336340
/* No arguments, default to --help */
337341
argv[argc++] = "--help";
338342
argv[argc++] = NULL;
339-
return_code = 1;
343+
rc = 1;
340344
}
341345

342346
{
343347
struct Tokens ts = tokens_new(argc, argv);
344348
if (parse_args(&ts, &elements))
345-
exit(1);
349+
return EXIT_FAILURE;
346350
}
347-
if (elems_to_args(&elements, &args, help, version))
348-
exit(return_code);
349-
return args;
351+
if (elems_to_args(&elements, args, help, version))
352+
rc = EXIT_FAILURE;
353+
return rc;
350354
}

acquire/cli/cli.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
#ifndef LIBACQUIRE_DOCOPT_CLI_H
88
#define LIBACQUIRE_DOCOPT_CLI_H
99

10-
#include <stddef.h>
11-
#include <string.h>
12-
1310
#include "acquire_cli_lib_export.h"
1411

15-
#if defined(__STDC__) && defined(__STDC_VERSION__) && \
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#elif defined(__STDC__) && defined(__STDC_VERSION__) && \
1615
__STDC_VERSION__ >= 199901L
1716

1817
#include <stdbool.h>
@@ -38,6 +37,10 @@ typedef size_t bool;
3837

3938
#endif
4039

40+
#include <stddef.h>
41+
#include <string.h>
42+
43+
4144
/* ARG_MAX definition block kept as-is for compatibility */
4245
#if defined(_AIX)
4346

@@ -114,7 +117,11 @@ struct DocoptArgs {
114117
const char *help_message[17];
115118
};
116119

117-
extern ACQUIRE_CLI_LIB_EXPORT struct DocoptArgs docopt(int, char *[], int,
118-
const char *);
120+
extern ACQUIRE_CLI_LIB_EXPORT int docopt(struct DocoptArgs *, int, char *[],
121+
bool, const char *);
122+
123+
#ifdef __cplusplus
124+
}
125+
#endif /* __cplusplus */
119126

120127
#endif /* !LIBACQUIRE_DOCOPT_CLI_H */

acquire/cli/main.c

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,23 @@
4343
#endif
4444

4545
int main(int argc, char *argv[]) {
46-
struct DocoptArgs args =
47-
docopt(argc, argv, /* help */ 1, /* version */ VERSION);
46+
int rc = EXIT_SUCCESS;
47+
struct DocoptArgs *args = malloc(sizeof *args);
4848
enum Checksum checksum = LIBACQUIRE_SHA256;
4949
char output_full_path[NAME_MAX + 1];
5050
const char *check_env = NULL;
5151
int i;
52+
if (args == NULL) {
53+
fputs("Out of memory\n", stderr);
54+
return ENOMEM;
55+
} else {
56+
rc = docopt(args, argc, argv, /* help */ true,
57+
/* version */ VERSION);
58+
if (rc != EXIT_SUCCESS) {
59+
free(args);
60+
return rc;
61+
}
62+
}
5263

5364
/* Ensure environment variable CHECK is read safely as a bool */
5465
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
@@ -64,62 +75,62 @@ int main(int argc, char *argv[]) {
6475
check_env = getenv("CHECK");
6576
#endif
6677

67-
if (check_env != NULL && args.check == 0) {
78+
if (check_env != NULL && args->check == 0) {
6879
if (check_env[0] == '1' || check_env[0] == 't' || check_env[0] == 'T' ||
6980
check_env[0] == 'y' || check_env[0] == 'Y') {
70-
args.check = true;
81+
args->check = true;
7182
}
7283
}
7384

74-
if (args.output != NULL && args.directory != NULL) {
75-
size_t dir_len = strlen(args.directory);
76-
size_t out_len = strlen(args.output);
85+
if (args->output != NULL && args->directory != NULL) {
86+
size_t dir_len = strlen(args->directory);
87+
size_t out_len = strlen(args->output);
7788

7889
if (dir_len + 1 + out_len >= sizeof(output_full_path)) {
7990
fprintf(stderr, "Output path too long.\n");
8091
return 1;
8192
}
8293

8394
snprintf(output_full_path, sizeof(output_full_path), "%s%s%s",
84-
args.directory,
85-
(args.directory[dir_len - 1] == PATH_SEP[0]) ? "" : PATH_SEP,
86-
args.output);
87-
args.output = output_full_path;
88-
} else if (args.output == NULL && args.directory == NULL) {
89-
args.directory = (char *)TMPDIR;
90-
args.output = (char *)TMPDIR;
91-
} else if (args.output == NULL) {
92-
args.output = args.directory;
93-
} else if (args.directory == NULL) {
94-
args.directory = args.output;
95+
args->directory,
96+
(args->directory[dir_len - 1] == PATH_SEP[0]) ? "" : PATH_SEP,
97+
args->output);
98+
args->output = output_full_path;
99+
} else if (args->output == NULL && args->directory == NULL) {
100+
args->directory = (char *)TMPDIR;
101+
args->output = (char *)TMPDIR;
102+
} else if (args->output == NULL) {
103+
args->output = args->directory;
104+
} else if (args->directory == NULL) {
105+
args->directory = args->output;
95106
}
96107

97-
if (args.url == NULL) {
108+
if (args->url == NULL) {
98109
for (i = argc - 1; i > 0; i--) {
99110
if (is_url(argv[i])) {
100-
args.url = argv[i];
111+
args->url = argv[i];
101112
break;
102113
}
103114
}
104-
if (args.url == NULL) {
115+
if (args->url == NULL) {
105116
fprintf(stderr, "No valid URL specified.\n");
106117
return UNIMPLEMENTED;
107118
}
108119
}
109120

110-
if (args.checksum != NULL) {
111-
checksum = string2checksum((const char *)args.checksum);
121+
if (args->checksum != NULL) {
122+
checksum = string2checksum((const char *)args->checksum);
112123
if (checksum == LIBACQUIRE_UNSUPPORTED_CHECKSUM) {
113-
fprintf(stderr, "Unsupported checksum algorithm: %s\n", args.checksum);
124+
fprintf(stderr, "Unsupported checksum algorithm: %s\n", args->checksum);
114125
return UNIMPLEMENTED;
115126
}
116127
}
117128

118-
if (args.check) {
119-
return is_downloaded(args.url, checksum, args.hash, args.output)
129+
if (args->check) {
130+
return is_downloaded(args->url, checksum, args->hash, args->output)
120131
? EXIT_SUCCESS
121132
: EXIT_FAILURE;
122133
}
123134

124-
return download(args.url, checksum, args.hash, args.output, false, 0, 0);
135+
return download(args->url, checksum, args->hash, args->output, false, 0, 0);
125136
}

acquire/tests/test_amalgamation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#define LIBACQUIRE_IMPLEMENTATION
88

9+
#include <acquire.h>
910
#include <acquire_config.h>
1011
#include <config_for_tests.h>
11-
#include <acquire.h>
1212

1313
TEST x_test_file_downloads(void) {
1414
const int download_resp =

acquire/tests/test_checksum.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#ifndef TEST_CHECKSUM_H
22
#define TEST_CHECKSUM_H
33

4-
#include <greatest.h>
54
#include <stdbool.h>
65

6+
#include <greatest.h>
7+
78
#include <acquire_common_defs.h>
89
#include <acquire_config.h>
910
#include <config_for_tests.h>
@@ -22,16 +23,23 @@
2223
#include <acquire_wincrypt.h>
2324
#endif
2425

26+
#ifndef NUM_FORMAT
2527
#ifdef _MSC_VER
2628
#define NUM_FORMAT "zu"
2729
#define BOOL_FORMAT NUM_FORMAT
28-
#elifse ifdefined(__linux__) || defined(linux) || defined(__linux)
30+
typedef size_t num_type;
31+
#elif defined(__linux__) || defined(linux) || defined(__linux)
32+
#define NUM_FORMAT "d"
33+
typedef int num_type;
34+
#else
2935
#define NUM_FORMAT "d"
3036
#define BOOL_FORMAT "lu"
31-
#endif
37+
typedef unsigned long num_type;
38+
#endif /* _MSC_VER */
39+
#endif /* !NUM_FORMAT */
3240

3341
TEST x_test_crc32c_should_be_true(void) {
34-
printf("crc32c(GREATEST_FILE, \"%s\"): %" BOOL_FORMAendif, GREATE/* ! */ST_CRC32C,
42+
printf("crc32c(GREATEST_FILE, \"%s\"): %" BOOL_FORMAT "\n", GREATEST_CRC32C,
3543
crc32c(GREATEST_FILE, GREATEST_CRC32C));
3644
ASSERT_FALSE(!crc32c(GREATEST_FILE, GREATEST_CRC32C));
3745
PASS();

0 commit comments

Comments
 (0)