tMove argv/stdin checks into a macro - pm - barely a pack manager
(HTM) git clone git://z3bra.org/pm
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit ff4971919f603107c9a8f0144f6b3f174c0f3fb2
(DIR) parent 7f7c044bcd9ba084b1b40b31e32efa4ff03ca72e
(HTM) Author: z3bra <willyatmailoodotorg>
Date: Sun, 24 Apr 2016 22:59:58 +0200
Move argv/stdin checks into a macro
Diffstat:
M pm.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
---
(DIR) diff --git a/pm.c b/pm.c
t@@ -14,6 +14,14 @@
#include "arg.h"
+/*
+ * Macro to return the next argument from argv if argc is higher than 0.
+ * In case argv is 0, it will read and return the next line from stdin.
+ */
+#define ARGV_OR_STDIN(argc, argv) ((argc>0) ? \
+ *argv ? strdup(*(argv++)) : NULL : \
+ slurp(0))
+
#define PACK_ROOT (getenv("ROOT")?getenv("ROOT"):"")
#define PACK_DATA "var/pm"
#define PACK_BUFF_SIZE 8192
t@@ -143,6 +151,7 @@ base_name(char *path)
return b ? b + 1 : path;
}
+
/*
* Fills the array passed as argument with lines read from the file descriptor.
* Returns the number of lines read
t@@ -170,6 +179,7 @@ slurp(int fd)
return path;
}
+
/*
* Check for collisions between the filesystem and the tarball
*/
t@@ -717,6 +727,7 @@ pack_free(struct pack *p)
free(p);
}
+
int
main (int argc, char **argv)
{
t@@ -760,8 +771,7 @@ main (int argc, char **argv)
case ACTION_INSTALL:
case ACTION_UPDATE:
case ACTION_DELETE:
- /* These action require a read from stdin if no arg is given */
- while ((n = (argc > 0) ? *argv ? strdup(*argv++) : NULL : slurp(0))) {
+ while ((n = ARGV_OR_STDIN(argc, argv)) != NULL) {
if (action == ACTION_INSTALL)
r += install(rootfs, datadir, n);
if (action == ACTION_UPDATE)