summaryrefslogtreecommitdiff
path: root/btshow.c
blob: 506e3ddc516157065f2a30534a0c3451af71b733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* $Id$ */
/*
 * Copyright (c) 2005 Dimitri Sokolyuk <demon@dim13.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>
#include <time.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>	/* basename(3) */
#include "meta.h"
#include "tools.h"
#include "version.h"

#ifndef lint
static const char rcsid[] = "@(#)$Id$";
#endif /* not lint */

const char usagearg[] = "torrent [torrent ...]";

extern char *__progname;

int
main(int argc, char **argv)
{
	struct btmeta *mp;
	struct btfile *fp;
	char hash[HASHSZS];

	if (argc < 2)
		usage(usagearg);

	printf("%s %d.%d - decode BitTorrent metainfo files\n\n",
		__progname, VERSION_MAJOR, VERSION_MINOR);

	while(*++argv != NULL) {
			if ((mp = btreadmeta(*argv)) == NULL) {
				warnx("cannot read %s\n", *argv);
				continue;
			}

			printf("metainfo file.: %s\n", basename(mp->fname));
			bthexdump(hash, mp->infohash, sizeof(hash));
			printf("info hash.....: %s\n", hash);

			if (mp->nfiles == 1) {
				printf("file name.....: %s\n", mp->name);
			} else {
				printf("directory name: %s\n", mp->name);
				printf("files.........: %d\n", mp->nfiles);

				SIMPLEQ_FOREACH(fp, &mp->flist, link) {
					printf("   %s (%lld) %s\n",
							fp->path,
							fp->length,
							metric((double) fp->length));
				}
			}

			printf("%s %lld (%lld * %lld + %lld) %s\n",
					mp->nfiles == 1 ?
						"file size.....:" :
						"archive size..:",
					mp->size, mp->size / mp->plength,
					mp->plength, mp->size % mp->plength,
					metric((double) mp->size));

			printf("announce url..: http://%s", mp->announce->host);
			if (mp->announce->port != 80)
				printf(":%d", mp->announce->port);
			putchar('/');
			if (mp->announce->path != NULL)
				printf("%s", mp->announce->path);
			putchar('\n');

			/* optional fields */
			if (mp->date != 0)
				printf("creation date.: %s", ctime(&mp->date));
			if (mp->comment != NULL)
				printf("comment.......: %s\n", mp->comment);
			if (mp->by != NULL)
				printf("created by....: %s\n", mp->by);
			
			btfreemeta(mp);
			printf("\n");
	}

	return 0;
}