libeech

BitTorrent library
git clone git://z3bra.org/libeech.git
Log | Files | Refs | README | LICENSE

commit e7efd126cddc29317299860c1fbd2c8c783c6eda
parent 5d82b5c139f3e89344a9b34ee04dcc7f666ebc40
Author: z3bra <contactatz3bradotorg>
Date:   Fri Jun 29 18:20:51 +0200

Improve torrent example code to use dynamic host:port

Diffstat:
arg.h | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
torrent.c | 33+++++++++++++++++++++++++--------
2 files changed, 90 insertions(+), 8 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/torrent.c b/torrent.c @@ -6,8 +6,18 @@ #include <sys/stat.h> #include <unistd.h> +#include "arg.h" #include "util.h" +char *argv0 = NULL; + +void +usage() +{ + fprintf(stderr, "usage: %s -h HOST -p PORT TORRENT\n", argv0); + exit(1); +} + long readfile(char *f, char **b) { @@ -39,17 +49,24 @@ int main(int argc, char *argv[]) { int i; - long s; - char *b; + long s, p = 6881; + char *b, *h = "localhost"; char hex[41]; struct torrent t; - if (argc < 2) { - fprintf(stderr, "%s TORRENT\n", argv[0]); - return -1; - } + ARGBEGIN { + case 'h': + h = EARGF(usage()); + break; + case 'p': + p = strtol(EARGF(usage()), NULL, 10); + break; + } ARGEND; + + if (argc < 1) + usage(); - s = readfile(argv[1], &b); + s = readfile(argv[0], &b); if (s < 0) return -1; @@ -63,7 +80,7 @@ main(int argc, char *argv[]) for (i=0; i<t.nfile; i++) printf("\t%s\n", t.files[i].path); - glch_addpeer(&t, "localhost", 6937); + glch_addpeer(&t, h, p); for (;;) glch_leech(&t, 5000);