sigchk

sign and check files using ed25519
git clone git://z3bra.org/sick
Log | Files | Refs | Submodules | README | LICENSE

commit c59850226f23d54bd9ae04d4c729f9c22a81d765
parent 5718ccdf09d6f1ef47cfd57d94c2ff9bbdbcbdd6
Author: z3bra <willyatmailoodotorg>
Date:   Sun May  1 23:45:03 2016

Implement ARGLOOP() in arg.h

This is a new macro used to loop through all remaining arguments in
argv, or read arguments on stdin if `argc` equals 0.

Diffstat:
 arg.h  | 16 ++++++++++++++++
 sick.c | 22 +++++++++++-----------
 2 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/arg.h b/arg.h @@ -29,6 +29,20 @@ extern char *argv0; argc_ = argv[0][0];\ switch (argc_) +#define ARGLOOP() while((argc>0 && *argv)||(!argc && !feof(stdin))){\ + char *lf_;\ + char argi_[LINE_MAX];\ + memset(argi_, 0, LINE_MAX);\ + if (argc>0) {\ + strncpy(argi_, *(argv++), LINE_MAX);\ + } else {\ + fgets(argi_, LINE_MAX, stdin);\ + }\ + if (strnlen(argi_, LINE_MAX) > 0) {\ + lf_ = strrchr(argi_, '\n');\ + if (lf_){ *lf_ = 0; } + + /* Handles obsolete -NUM syntax */ #define ARGNUM case '0':\ case '1':\ @@ -60,6 +74,8 @@ extern char *argv0; (&argv[0][1]) :\ (argc--, argv++, argv[0]))) +#define ARGI() argi_ + #define LNGARG() &argv[0][0] #endif diff --git a/sick.c b/sick.c @@ -65,7 +65,7 @@ check_magic(const char *file) if (stat(file, &sb) < 0) { perror(file); - return NULL; + return -1; } magic = malloc(strlen(MAGIC)); @@ -77,7 +77,7 @@ check_magic(const char *file) if (read(fd, magic, strlen(MAGIC)) < (unsigned int)strlen(MAGIC)) { perror(file); close(fd); - return NULL; + return -1; } close(fd); @@ -132,7 +132,7 @@ unsign(const char *file) if (stat(file, &sb) < 0) { perror(file); - return NULL; + return -1; } len = sb.st_size - 64 - strlen(MAGIC); @@ -369,33 +369,33 @@ main(int argc, char **argv) usage(argv0); }ARGEND; + ARGLOOP() { switch(action) { case ACTION_CHECK: - if (check(*argv, keyring) == 0) { + if (check(ARGI(), keyring) == 0) { if (!verbose) - puts(*argv); - } else { - return 1; + puts(ARGI()); } break; case ACTION_GENKEY: keygen(key ? key : DEFAULT_ALIAS); break; case ACTION_UNSIGN: - unsign(*argv); + unsign(ARGI()); break; case ACTION_SIGN: /* TODO: only check the pubkey matchng the private */ - if (check_magic(*argv) != 0) { - sign(*argv, key ? key : DEFAULT_ALIAS ".key"); + if (check_magic(ARGI()) != 0) { + sign(ARGI(), key ? key : DEFAULT_ALIAS ".key"); } else { - fprintf(stderr, "%s: Already signed\n", *argv); + fprintf(stderr, "%s: Already signed\n", ARGI()); return -1; } break; default: usage(argv0); } + }ARGEND; return 0; }