repo

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 598515bc64f263d554b574f3c4452bb626d3d23b
Author: z3bra <willyatmailoodotorg>
Date:   Sat Dec 10 09:01:50 +0100

Initial commit

Diffstat:
arg.h | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
config.mk | 13+++++++++++++
mkfile | 18++++++++++++++++++
repo.c | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 236 insertions(+), 0 deletions(-)
diff --git a/arg.h b/arg.h @@ -0,0 +1,65 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +/* Handles obsolete -NUM syntax */ +#define ARGNUM case '0':\ + case '1':\ + case '2':\ + case '3':\ + case '4':\ + case '5':\ + case '6':\ + case '7':\ + case '8':\ + case '9' + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX)) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define LNGARG() &argv[0][0] + +#endif diff --git a/config.mk b/config.mk @@ -0,0 +1,13 @@ +VERSION = 0.0 + +CC = cc +LD = ${CC} + +PREFIX = /usr/local +MANDIR = ${PREFIX}/man + +CPPFLAGS = -DVERSION=\"${VERSION}\" +CFLAGS = ${CPPFLAGS} -Wall -Wextra -pedantic -g +LDFLAGS = -static +LIBS = -lcurl -lssl -lcrypto -ldl -lz -lpthread + diff --git a/mkfile b/mkfile @@ -0,0 +1,18 @@ +<config.mk + +repo: repo.o + $LD -o $target $prereq $LDFLAGS $LIBS + +%.o: %.c + $CC $CFLAGS -c $stem.c -o $stem.o + +clean:V: + rm -f *.o repo + +install:V: all + mkdir -p ${DESTDIR}${PREFIX}/bin + cp repo ${DESTDIR}${PREFIX}/bin/repo + chmod 755 ${DESTDIR}${PREFIX}/bin/repo + +uninstall:V: + rm ${DESTDIR}${PREFIX}/bin/repo diff --git a/repo.c b/repo.c @@ -0,0 +1,139 @@ +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <curl/curl.h> + +#include "arg.h" + +#define LISTFILE "/.list" +#define LOCALREPO "local" +#define REMOTEREPO "http://localhost" + +struct pack { + char *name; + char *version; + char *filename; +}; + +void usage(char *); +int local_list(char *); +int remote_sync(char *, char *); + +void +usage(char *name) +{ + fprintf(stderr, "usage: %s [-sl] [PACK..]\n", name); + exit(1); +} + +int +local_list(char *local) +{ + int ret = 0; + ssize_t len = 0; + char *fn; + char buf[LINE_MAX] = ""; + FILE *list; + + len = strlen(local) + strlen(LISTFILE); + fn = malloc(len + 1); + if (!fn) { + perror("malloc"); + return -1; + } + snprintf(fn, len + 1, "%s%s", local, LISTFILE); + fn[len] = 0; + list = fopen(fn, "r"); + if (!list) { + perror("fopen"); + free(fn); + return -1; + } + + while ((len = fread(buf, 1, LINE_MAX, list))) { + if (len < 0) { + perror("fread"); + return ret = -1; + } + fwrite(buf, 1, len, stdout); + } + + fclose(list); + free(fn); + return ret; +} + +int +remote_sync(char *remote, char *local) +{ + int ret = 0; + ssize_t len; + char *url, *fn; + FILE *list; + CURL *c; + CURLcode r; + + c = curl_easy_init(); + if (!c) + return -1; + + len = strlen(remote) + strlen(LISTFILE); + url = malloc(len + 1); + if (!url) { + perror("malloc"); + ret = -1; + goto synccleanup; + } + snprintf(url, len + 1, "%s%s", remote, LISTFILE); + url[len] = 0; + + len = strlen(local) + strlen(LISTFILE); + fn = malloc(len + 1); + if (!fn) { + perror("malloc"); + ret = 1; + goto synccleanup; + } + snprintf(fn, len + 1, "%s%s", local, LISTFILE); + fn[len] = 0; + list = fopen(fn, "w"); + if (!list) { + perror("fopen"); + ret = -1; + goto synccleanup; + } + + curl_easy_setopt(c, CURLOPT_URL, url); + curl_easy_setopt(c, CURLOPT_WRITEDATA, list); + r = curl_easy_perform(c); + if (r != CURLE_OK) + fprintf(stderr, "%s\n", curl_easy_strerror(r)); +synccleanup: + if (url) + free(url); + if (fn) + free(fn); + curl_easy_cleanup(c); + if (list) + fclose(list); + return ret; +} + +int +main (int argc, char *argv[]) +{ + char *argv0; + ARGBEGIN{ + case 's': + remote_sync(REMOTEREPO, LOCALREPO); + break; + case 'l': + local_list(LOCALREPO); + break; + default: + usage(argv0); + }ARGEND; + return 0; +}+ \ No newline at end of file