Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
add -v flag, v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRosenstein committed Jan 11, 2017
1 parent b8497a2 commit d37dd99
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ To compile `pbzx`, do

## Changelog

__v1.0.2__

- Add `-v` flag to print version of `pbzx`

__v1.0.1__

- Support unpacking from stdin and and plain pbzx files (see new command-line
Expand Down
20 changes: 15 additions & 5 deletions pbzx.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,27 @@

#define XBSZ 4 * 1024
#define ZBSZ 1024 * XBSZ
#define VERSION "1.0.2"

/* Structure to hold the command-line options. */
struct options {
bool stdin; /* True if data should be read from stdin. */
bool noxar; /* The input data is not a XAR archive but the pbzx Payload. */
bool help; /* Print usage with details and exit. */
bool stdin; /* True if data should be read from stdin. */
bool noxar; /* The input data is not a XAR archive but the pbzx Payload. */
bool help; /* Print usage with details and exit. */
bool version; /* Print version and exit. */
};

/* Prints usage information and exit. Optionally, displays an error message and
* exits with an error code. */
static void usage(char const* error) {
fprintf(stderr, "usage: pbzx [-n] [-] [filename]\n");
fprintf(stderr, "usage: pbzx [-v] [-h] [-n] [-] [filename]\n");
if (error) {
fprintf(stderr, "error: %s\n", error);
exit(EINVAL);
}
fprintf(stderr,
"\n"
"pbzx stream parser\n"
"pbzx v" VERSION " stream parser\n"
"https://github.com/NiklasRosenstein/pbzx\n"
"\n"
"Licensed under GNU GPL v3.\n"
Expand All @@ -59,6 +61,12 @@ static void usage(char const* error) {
exit(0);
}

/* Prints the version and exits. */
static void version() {
printf("pbzx v" VERSION "\n");
exit(0);
}

/* Parses command-line flags into the #options structure and adjusts the
* argument count and values on the fly to remain only positional arguments. */
static void parse_args(int* argc, char const** argv, struct options* opts) {
Expand All @@ -69,6 +77,7 @@ static void parse_args(int* argc, char const** argv, struct options* opts) {
if (strcmp(argv[i], "-") == 0) opts->stdin = true;
else if (strcmp(argv[i], "-n") == 0) opts->noxar = true;
else if (strcmp(argv[i], "-h") == 0) opts->help = true;
else if (strcmp(argv[i], "-v") == 0) opts->version = true;
else usage("unrecognized flag");
/* Move all remaining arguments to the front. */
for (int j = 0; j < (*argc-1); ++j) {
Expand Down Expand Up @@ -193,6 +202,7 @@ int main(int argc, const char** argv) {
/* Parse and validate command-line flags and arguments. */
struct options opts = {0};
parse_args(&argc, argv, &opts);
if (opts.version) version();
if (opts.help) usage(NULL);
if (!opts.stdin && argc < 2)
usage("missing filename argument");
Expand Down

0 comments on commit d37dd99

Please sign in to comment.