summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2017-06-17 18:30:12 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2017-06-17 23:44:22 +0200
commitba7822e19ca358ef8a75b20d1774e277b4551c4e (patch)
treed876462771ebf2c7c0281389d290e90faa867b0b /main.c
parent48e74865011722a7ac0ca3e1b45ea407f8c79db5 (diff)
downloadaports-cache-ba7822e19ca358ef8a75b20d1774e277b4551c4e.tar.bz2
aports-cache-ba7822e19ca358ef8a75b20d1774e277b4551c4e.tar.xz
move out main function to its own file
Diffstat (limited to 'main.c')
-rw-r--r--main.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..23de13b
--- /dev/null
+++ b/main.c
@@ -0,0 +1,91 @@
+
+#define _GNU_SOURCE
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <limits.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include "aports-cache.h"
+
+static void usage(int exitcode)
+{
+ printf("usage %s [OPTS] DIR\n"
+ "options:\n"
+ " -h,--help show this help\n"
+ " -c,--check only check if cache is updated\n"
+ " -u,--update force cache update\n"
+ " -v,--verbose enable verbose mode\n"
+ " -s,--shell PROG use PROG as shell instead of /bin/sh\n"
+ "\n", program_invocation_name);
+}
+
+int main(int argc, char *argv[])
+{
+ static struct option opts[] = {
+ {"check", no_argument, 0, 'c' },
+ {"help", no_argument, 0, 'h' },
+
+ {"verbose", no_argument, 0, 'v' },
+ {"update", no_argument, 0, 'u' },
+ {"shell", required_argument, 0, 's' },
+ { 0, 0, 0, 0}
+ };
+ int i, c, verbose=0, check_only=0, force_update=0;
+ const char *cachefile = ".aports.cache.yaml";
+ char dirpath_buf[PATH_MAX];
+ char *dirpath = dirpath_buf;
+ int dirfd;
+
+ char *shell_argv[] = {"/bin/sh", NULL};
+
+ while ((c = getopt_long(argc, argv, "fs:v", opts, &i)) != -1) {
+ switch(c) {
+ case 'c':
+ check_only = 1;
+ break;
+ case 'h':
+ usage(0);
+ break;
+ case 's':
+ shell_argv[0] = optarg;
+ break;
+ case 'u':
+ force_update = 1;
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ }
+ }
+
+ if (optind < argc) {
+ dirpath = argv[optind];
+ } else {
+ dirpath = getcwd(dirpath_buf, sizeof(dirpath_buf));
+ }
+
+ dirfd = open(dirpath, O_DIRECTORY);
+ if (dirfd < 0)
+ err(1, "%s", dirpath);
+ fchdir(dirfd);
+
+ if (!force_update)
+ c =aports_cache_check(dirfd, cachefile);
+
+ if (check_only || !c) {
+ if (verbose)
+ printf("cache is %sup to date\n", c ? "not " : "");
+ return c;
+ }
+
+ if (verbose)
+ printf("updating cache for %s\n", dirpath);
+ aports_cache_refresh(dirfd, cachefile, shell_argv);
+ close(dirfd);
+
+ return 0;
+}
+