1 /* vim: set ts=8 sts=4 sw=4 tw=80 noet: */
2 /*======================================================================
3 Copyright (C) 2004,2005,2009,2011 Walter Doekes <walter+tthsum@wjd.nu>
4 This file is part of tthsum.
5
6 tthsum is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 tthsum is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with tthsum. If not, see <http://www.gnu.org/licenses/>.
18 ======================================================================*/
19
20 #include "getopt.h"
21 #include "tthsum.h"
22 #include <locale.h>
23 #include <stdio.h>
24 #include <string.h>
25
26
27 static void help(int error);
28 static void version();
29
main(int argc,char * const argv[])30 int main(int argc, char* const argv[]) {
31 const char* stdin_file[] = {NULL, NULL};
32 struct tthsum_options opt;
33 int ret;
34 int digest = 0;
35 int quit = 0;
36 char* cp;
37 memset(&opt, 0, sizeof(struct tthsum_options));
38
39 /* set up proper locale */
40 if ((cp = setlocale(LC_CTYPE, "")) == NULL
41 && (cp = setlocale(LC_CTYPE, "C")) == NULL)
42 fprintf(stderr, "tthsum: warning: Check your locale settings "
43 "(the LANG and/or LC_CTYPE environment variables).\n");
44 if (cp && strcmp(cp, "C") != 0 && strcmp(cp, "POSIX") != 0)
45 opt.has_locale = 1;
46
47 /* read options */
48 while ((ret = getopt(argc, argv, "bchmpvVw")) >= 0) {
49 switch(ret) {
50 case 'b':
51 /* md5sum compatibility, don't do anything special,
52 * as we always do "binary" mode */
53 break;
54 case 'c':
55 digest = 1;
56 break;
57 case 'm':
58 opt.use_mmap = 1;
59 break;
60 case 'p':
61 opt.progress_every_mib = 10;
62 break;
63 case 'v':
64 opt.verbose = 1;
65 break;
66 case 'w':
67 opt.warn = 1;
68 break;
69 case 'V':
70 version();
71 quit = 1;
72 break;
73 case 'h':
74 help(0);
75 quit = 1;
76 break;
77 case '?':
78 help(1);
79 return 2;
80 default:
81 /* shouldn't happen */
82 return -1;
83 }
84 }
85
86 if (quit)
87 return 0;
88
89 /* check digest? */
90 if (digest) {
91 /* only accept one digest file */
92 if (optind > argc + 1) {
93 help(1);
94 return 2;
95 }
96 if (optind == argc)
97 ret = tthsum_check_digest(NULL, &opt);
98 else
99 ret = tthsum_check_digest(argv[optind], &opt);
100 /* generate digest? */
101 } else {
102 if (optind == argc) {
103 ret = tthsum_generate_roots(stdin_file, 1, &opt);
104 } else {
105 ret = tthsum_generate_roots((const char**)argv + optind,
106 argc - optind, &opt);
107 }
108 }
109
110 /* done */
111 return ret == 0 ? 0 : 1;
112 }
113
help(int error)114 static void help(int error) {
115 fprintf(error ? stderr : stdout,
116 "usage: tthsum [-bhmpvVw] [-c [file]] | [file...]\n"
117 "Generates or checks TTH Message Digests (root of the Tiger/THEX hash tree)\n"
118 " -b md5sum compatibility option (does absolutely nothing)\n"
119 " -c check message digests (default is generate)\n"
120 " -h show the small help and exit\n"
121 " -m use mmap for reading files\n"
122 " -p show the hashing progress\n"
123 " -v verbose, print file names when checking a digest\n"
124 " -V print the version and exit\n"
125 " -w warn on improperly formatted lines checking a digest\n"
126 );
127 fprintf(error ? stderr : stdout,
128 "The input for -c should be the list of message digests and file names\n"
129 "that is printed on stdout by this program when it generates digests.\n"
130 "If you don't specify a file, input will be read from stdin.\n"
131 );
132 }
133
version()134 static void version() {
135 printf(
136 "tthsum 1.3.2\n"
137 "\n"
138 "Copyright 2004,2005,2009,2011,2012,2013 Walter Doekes.\n"
139 "This is free software; see the source for copying conditions. There is NO\n"
140 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
141 );
142 }
143