Skip to content

Commit

Permalink
Port of StripZIP from Zee framework to be standalone
Browse files Browse the repository at this point in the history
StripZIP application removed of Zee framework error handling with
independent makefile.

Readme updated with better usage example and some notes.
  • Loading branch information
Matt Walker committed Feb 11, 2016
1 parent 692cb0f commit 609a5c7
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o

stripzip
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GCC_OPTS = -std=gnu11
GCC_OPTS += -Wall -Wextra -Werror
GCC_OPTS += -fdiagnostics-show-option
GCC_OPTS += -Wcast-qual -Wconversion -Wno-sign-conversion -Wfloat-equal -Wno-missing-field-initializers

GCC_OPTS += -O2

all:
gcc $(GCC_OPTS) src/stripzip_app.c -o stripzip

clean:
rm *o stripzip

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,22 @@ dependencies. It does not currently have an installer; when running make the
emitted binary will be in the source folder.

To build:

$ make

Usage
-----

$ zip archive.zip -r folder_of_stuff
$ stripzip archive.zip

Notes:
- Currently StripZIP will modify the archive in place
- ZIP on Linux will add extra metadata which although StripZIP can clean so
that builds are repeatable on the same machine, it's better not to add it at
all. In this case, it's better to run ZIP with the `-X` or `--no-extra`
flags.

Contribute
----------

Expand Down
108 changes: 108 additions & 0 deletions src/err.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @file
* Error reporting convienence macros.
* Only the subset required for StripZIP.
*
* Copyright (c) 2016, Zee.Aero
* All rights reserved.
*/

/* Shim for error printing outside of a bigger framework */
#define err_printf(...) printf(__VA_ARGS__)

#define ERR_PREFIX_(file, line, func) \
do { \
err_printf("%s:%d: %s:\n", file, line, func); \
} while (0)
#define ERR_PREFIX() ERR_PREFIX_(__FILE__, __LINE__, __func__)

/**
* Print an error message and abort the current function (that is, return
* \a ret) if the expression is false/zero.
*
* @param expr The expression.
* @param ret The return value if \a expr is false/zero.
*/
#define ERR_RET_IF_NOT(expr, ret) \
do { \
typeof(expr) expr_ = (expr); \
typeof(ret) ret_ = (ret); \
if (!expr_) \
{ \
ERR_PREFIX(); \
err_printf(" ERR_RET_IF_NOT(%s, %s)\n", \
(#expr), #ret); \
return ret_; \
} \
} while (0)

/**
* Print an error message if the two expressions are not equal.
*
* @param expr_1 The first expression.
* @param expr_2 The second expression.
*
* @return True/nonzero if the message was printed (\a expr_1 and \a
* expr_2 were not equal).
*/
#define ERR_IF_NEQ(expr_1, expr_2) \
({ \
typeof(expr_1) expr_1_ = (expr_1); \
typeof(expr_2) expr_2_ = (expr_2); \
bool test = expr_1_ != expr_2_; \
if (test) \
{ \
ERR_PREFIX(); \
err_printf(" ERR_IF_NEQ(%s, %s)\n", \
(#expr_1), #expr_2); \
err_printf("\t%s = %jd\n", #expr_1, (intmax_t)expr_1_); \
err_printf("\t%s = %jd\n", #expr_2, (intmax_t)expr_2_); \
} \
test; \
})

/**
* Print an error message and abort the current function (that is, return
* \a ret) if the two expressions are not equal.
*
* @param expr_1 The first expression.
* @param expr_2 The second expression.
* @param ret The return value if two expressions are not equal.
*/
#define ERR_RET_IF_NEQ(expr_1, expr_2, ret) \
do { \
typeof(expr_1) expr_1_ = (expr_1); \
typeof(expr_2) expr_2_ = (expr_2); \
typeof(ret) ret_ = (ret); \
if (expr_1_ != expr_2_) \
{ \
ERR_PREFIX(); \
err_printf(" ERR_RET_IF_NEQ(%s, %s, %s)\n", \
(#expr_1), #expr_2, #ret); \
err_printf("\t%s = %jd\n", #expr_1, (intmax_t)expr_1_); \
err_printf("\t%s = %jd\n", #expr_2, (intmax_t)expr_2_); \
return ret_; \
} \
} while (0)

/**
* Print an error message and abort the current function (that is, return
* \a ret) if \a expr is negative. The message contains information
* from errno.
*
* @param expr The expression.
* @param ret The return value if \a expr is negative.
*/
#define ERR_RET_ON_ERRNO(expr, ret) \
do { \
typeof(expr) expr_ = (expr); \
typeof(ret) ret_ = (ret); \
if (expr_ < 0) \
{ \
ERR_PREFIX(); \
err_printf(" ERR_RET_ON_ERRNO(%s, %s): errno = %s (%d)\n", \
(#expr), #ret, strerror(errno), errno); \
err_printf("\t%s = %d\n", #expr, expr_); \
return ret_; \
} \
} while (0)
Loading

0 comments on commit 609a5c7

Please sign in to comment.