From 5a0a7425cc75e38e8ea476179cc4c9108297dcd6 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Thu, 5 Oct 2023 15:25:30 +0200 Subject: [PATCH] tools: support longopts This change introduces initial longopts support for the command line tools. It also updates the man pages to reflect the new options. --- tools/man/sqsh-cat.1.in | 4 ++-- tools/man/sqsh-ls.1.in | 8 ++++---- tools/man/sqsh-stat.1.in | 7 +++---- tools/man/sqsh-unpack.1.in | 10 +++++++--- tools/man/sqsh-xattr.1.in | 6 +++--- tools/src/cat.c | 10 +++++++++- tools/src/common.h | 7 +++---- tools/src/ls.c | 12 +++++++++++- tools/src/stat.c | 10 +++++++++- tools/src/unpack.c | 12 +++++++++++- tools/src/xattr.c | 10 +++++++++- 11 files changed, 71 insertions(+), 25 deletions(-) diff --git a/tools/man/sqsh-cat.1.in b/tools/man/sqsh-cat.1.in index 7a3ffa110..8d00a12e7 100644 --- a/tools/man/sqsh-cat.1.in +++ b/tools/man/sqsh-cat.1.in @@ -27,11 +27,11 @@ output. .SH OPTIONS .TP -.BR \-o " " \fIOFFSET\fR +.BR \-o " " \fIOFFSET\fR ", " \-\-offset " " \fIOFFSET\fR skip OFFSET bytes at start of FILESYSTEM. .TP -.BR \-v +.BR \-v ", " \-\-version Print the version of \fBsqsh-cat\fR and exit. .SH ARGUMENTS diff --git a/tools/man/sqsh-ls.1.in b/tools/man/sqsh-ls.1.in index c3c9ac274..31d88aebd 100644 --- a/tools/man/sqsh-ls.1.in +++ b/tools/man/sqsh-ls.1.in @@ -24,21 +24,21 @@ within the archive. .SH OPTIONS .TP -.BR \-o " " \fIOFFSET\fR +.BR \-o " " \fIOFFSET\fR ", " \fB\-\-offset " " \fIOFFSET\fR skip OFFSET bytes at start of FILESYSTEM. .TP -.BR \-r +.BR \-r ", " \fB\-\-recursive Recursively list the contents of all subdirectories within the specified path. .TP -.BR \-l +.BR \-l ", " \fB\-\-long Print a long format listing of files and directories, similar to the output of the \fBls -l\fR command. .TP -.BR \-v +.BR \-v ", " \fB\-\-version Print the version of \fBsqsh-ls\fR and exit. .SH ARGUMENTS diff --git a/tools/man/sqsh-stat.1.in b/tools/man/sqsh-stat.1.in index 41afc1499..194837e53 100644 --- a/tools/man/sqsh-stat.1.in +++ b/tools/man/sqsh-stat.1.in @@ -29,11 +29,11 @@ type. .SH OPTIONS .TP -.BR \-o " " \fIOFFSET\fR +.BR \-o " " \fIOFFSET\fR ", " \-\-offset " " \fIOFFSET\fR skip OFFSET bytes at start of FILESYSTEM. .TP -.BR \-v +.BR \-v ", " \-\-version Print the version of \fBsqsh-stat\fR and exit. .SH ARGUMENTS @@ -62,8 +62,7 @@ archive: To display information about multiple paths within the squashfs archive: -.BR sqsh-stat " " /path/to/filesystem.sqsh " " /path/to/file1 -/path/to/directory/ +.BR sqsh-stat " " /path/to/filesystem.sqsh " " /path/to/file1 " "/path/to/dir/ To print the version of \fBsqsh-stat\fR: diff --git a/tools/man/sqsh-unpack.1.in b/tools/man/sqsh-unpack.1.in index 281fa09ce..5218f2677 100644 --- a/tools/man/sqsh-unpack.1.in +++ b/tools/man/sqsh-unpack.1.in @@ -26,16 +26,20 @@ the archive. .SH OPTIONS .TP -.BR \-o " " \fIOFFSET\fR +.BR \-v ", " \-\-version +Print the version of \fBsqsh-stat\fR and exit. + +.TP +.BR \-o " " \fIOFFSET\fR ", " \fB\-\-offset " " \fIOFFSET\fR skip OFFSET bytes at start of FILESYSTEM. .TP -.BR \-c +.BR \-c ", " \fB\-\-chown\fR Change the owner and group of extracted files to match the user and group ID of the current user. .TP -.BR \-V +.BR \-V ", " \fB\-\-verbose\fR Print more verbose output during unpacking. .SH ARGUMENTS diff --git a/tools/man/sqsh-xattr.1.in b/tools/man/sqsh-xattr.1.in index 6360297e4..ca36261b0 100644 --- a/tools/man/sqsh-xattr.1.in +++ b/tools/man/sqsh-xattr.1.in @@ -17,12 +17,12 @@ The command operates in read-only mode. .SH OPTIONS .TP -.BR \-v -Print the version of \fBsqsh-xattr\fR. +.BR \-v ", " \-\-version +Print the version of \fBsqsh-xattr\fR and exit. .SH ARGUMENTS .TP -.BR \-o " " \fIOFFSET\fR +.BR \-o " " \fIOFFSET\fR ", " \-\-offset " " \fIOFFSET\fR skip OFFSET bytes at start of FILESYSTEM. .TP diff --git a/tools/src/cat.c b/tools/src/cat.c index 0552d48cb..018b6b707 100644 --- a/tools/src/cat.c +++ b/tools/src/cat.c @@ -71,6 +71,14 @@ cat_path(struct SqshArchive *archive, char *path) { return rv; } +static const char opts[] = "o:vh"; +static const struct option long_opts[] = { + {"offset", required_argument, NULL, 'o'}, + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {0}, +}; + int main(int argc, char *argv[]) { int rv = 0; @@ -79,7 +87,7 @@ main(int argc, char *argv[]) { struct SqshArchive *sqsh = NULL; uint64_t offset = 0; - while ((opt = getopt(argc, argv, "o:vh")) != -1) { + while ((opt = getopt_long(argc, argv, opts, long_opts, NULL)) != -1) { switch (opt) { case 'o': offset = strtoull(optarg, NULL, 0); diff --git a/tools/src/common.h b/tools/src/common.h index 16d25419e..5f729ace9 100644 --- a/tools/src/common.h +++ b/tools/src/common.h @@ -7,12 +7,11 @@ #define TOOLS_COMMON_H -#ifndef _DEFAULT_SOURCE -# define _DEFAULT_SOURCE -#endif +#define _GNU_SOURCE -#include "../include/sqsh.h" #include +#include +#include #include #include diff --git a/tools/src/ls.c b/tools/src/ls.c index 91f2a268a..405a33e56 100644 --- a/tools/src/ls.c +++ b/tools/src/ls.c @@ -253,6 +253,16 @@ ls_path(struct SqshArchive *archive, char *path) { return rv; } +static const char opts[] = "o:vrhl"; +static const struct option long_opts[] = { + {"offset", required_argument, NULL, 'o'}, + {"version", no_argument, NULL, 'v'}, + {"recursive", no_argument, NULL, 'r'}, + {"help", no_argument, NULL, 'h'}, + {"long", no_argument, NULL, 'l'}, + {0}, +}; + int main(int argc, char *argv[]) { bool has_listed = false; @@ -262,7 +272,7 @@ main(int argc, char *argv[]) { struct SqshArchive *archive; uint64_t offset = 0; - while ((opt = getopt(argc, argv, "o:vrhl")) != -1) { + while ((opt = getopt_long(argc, argv, opts, long_opts, NULL)) != -1) { switch (opt) { case 'o': offset = strtoull(optarg, NULL, 0); diff --git a/tools/src/stat.c b/tools/src/stat.c index 939310c39..b9e0853a8 100644 --- a/tools/src/stat.c +++ b/tools/src/stat.c @@ -319,6 +319,14 @@ stat_file(struct SqshArchive *archive, const char *path) { return rv; } +static const char opts[] = "o:vh"; +static const struct option long_opts[] = { + {"offset", required_argument, NULL, 'o'}, + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {0}, +}; + int main(int argc, char *argv[]) { int rv = 0; @@ -327,7 +335,7 @@ main(int argc, char *argv[]) { struct SqshArchive *archive; uint64_t offset = 0; - while ((opt = getopt(argc, argv, "o:vh")) != -1) { + while ((opt = getopt_long(argc, argv, opts, long_opts, NULL)) != -1) { switch (opt) { case 'o': offset = strtoull(optarg, NULL, 0); diff --git a/tools/src/unpack.c b/tools/src/unpack.c index fec293b9c..bee89b1e0 100644 --- a/tools/src/unpack.c +++ b/tools/src/unpack.c @@ -327,6 +327,16 @@ extract(const char *filename, struct SqshFile *file, return rv; } +static const char opts[] = "co:vVh"; +static const struct option long_opts[] = { + {"chown", no_argument, NULL, 'c'}, + {"offset", required_argument, NULL, 'o'}, + {"version", no_argument, NULL, 'v'}, + {"verbose", no_argument, NULL, 'V'}, + {"help", no_argument, NULL, 'h'}, + {0}, +}; + int main(int argc, char *argv[]) { int rv = 0; @@ -339,7 +349,7 @@ main(int argc, char *argv[]) { struct SqshFile *file = NULL; uint64_t offset = 0; - while ((opt = getopt(argc, argv, "co:vVh")) != -1) { + while ((opt = getopt_long(argc, argv, opts, long_opts, NULL)) != -1) { switch (opt) { case 'c': do_chown = true; diff --git a/tools/src/xattr.c b/tools/src/xattr.c index 04fd2583e..ffea28134 100644 --- a/tools/src/xattr.c +++ b/tools/src/xattr.c @@ -103,6 +103,14 @@ fattr_path(struct SqshArchive *archive, char *path) { return rv; } +static const char opts[] = "o:vh"; +static const struct option long_opts[] = { + {"offset", required_argument, NULL, 'o'}, + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {0}, +}; + int main(int argc, char *argv[]) { int rv = 0; @@ -111,7 +119,7 @@ main(int argc, char *argv[]) { struct SqshArchive *archive; uint64_t offset = 0; - while ((opt = getopt(argc, argv, "o:vh")) != -1) { + while ((opt = getopt_long(argc, argv, opts, long_opts, NULL)) != -1) { switch (opt) { case 'o': offset = strtoull(optarg, NULL, 0);