ckdist.c (9d21c94eb045d7ca4bf8f3c9d52a1b268c93d841) | ckdist.c (dcf118f17cfafbc38a1312903fb686cc7dafcdfb) |
---|---|
1/* 2 * Copyright (c) 1997 Robert Nordier 3 * All rights reserved. 4 * | 1/* 2 * Copyright (c) 1997 Robert Nordier 3 * All rights reserved. 4 * |
5 * $Id: ckdist.c,v 1.6 1997/01/20 16:47:06 rnordier Exp $ 6 * |
|
5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the --- 7 unchanged lines hidden (view full) --- 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 | 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the --- 7 unchanged lines hidden (view full) --- 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 |
28#ifndef lint 29static const char rcsid[] = 30 "$FreeBSD$"; 31#endif /* not lint */ 32 | |
33#include <sys/types.h> 34#include <sys/stat.h> | 30#include <sys/types.h> 31#include <sys/stat.h> |
35 36#include <err.h> 37#include <errno.h> | |
38#include <fcntl.h> | 32#include <fcntl.h> |
33#include <unistd.h> |
|
39#include <fts.h> | 34#include <fts.h> |
35#include <err.h> |
|
40#include <md5.h> | 36#include <md5.h> |
41#include <stdio.h> 42#include <stdint.h> | 37 38#include <errno.h> |
43#include <stdlib.h> 44#include <string.h> | 39#include <stdlib.h> 40#include <string.h> |
45#include <unistd.h> | 41#include <stdio.h> |
46 | 42 |
47extern int crc(int fd, uint32_t *cval, off_t *clen); | 43extern int crc(int fd, u_long * cval, u_long * clen); |
48 49#define DISTMD5 1 /* MD5 format */ 50#define DISTINF 2 /* .inf format */ 51#define DISTTYPES 2 /* types supported */ 52 53#define E_UNKNOWN 1 /* Unknown format */ 54#define E_BADMD5 2 /* Invalid MD5 format */ 55#define E_BADINF 3 /* Invalid .inf format */ --- 19 unchanged lines hidden (view full) --- 75static int opt_exist; /* just verify existence */ 76 77static int ckdist(const char *path, int type); 78static int chkmd5(FILE * fp, const char *path); 79static int chkinf(FILE * fp, const char *path); 80static int report(const char *path, const char *name, int error); 81static const char *distname(const char *path, const char *name, 82 const char *ext); | 44 45#define DISTMD5 1 /* MD5 format */ 46#define DISTINF 2 /* .inf format */ 47#define DISTTYPES 2 /* types supported */ 48 49#define E_UNKNOWN 1 /* Unknown format */ 50#define E_BADMD5 2 /* Invalid MD5 format */ 51#define E_BADINF 3 /* Invalid .inf format */ --- 19 unchanged lines hidden (view full) --- 71static int opt_exist; /* just verify existence */ 72 73static int ckdist(const char *path, int type); 74static int chkmd5(FILE * fp, const char *path); 75static int chkinf(FILE * fp, const char *path); 76static int report(const char *path, const char *name, int error); 77static const char *distname(const char *path, const char *name, 78 const char *ext); |
83static const char *stripath(const char *path); | 79static char *stripath(const char *path); |
84static int distfile(const char *path); 85static int disttype(const char *name); 86static int fail(const char *path, const char *msg); 87static void usage(void); 88 89int 90main(int argc, char *argv[]) 91{ --- 36 unchanged lines hidden (view full) --- 128 usage(); 129 } 130 argc -= optind; 131 argv += optind; 132 if (argc < 1) 133 usage(); 134 if (opt_dir) { 135 if (stat(opt_dir, &sb)) | 80static int distfile(const char *path); 81static int disttype(const char *name); 82static int fail(const char *path, const char *msg); 83static void usage(void); 84 85int 86main(int argc, char *argv[]) 87{ --- 36 unchanged lines hidden (view full) --- 124 usage(); 125 } 126 argc -= optind; 127 argv += optind; 128 if (argc < 1) 129 usage(); 130 if (opt_dir) { 131 if (stat(opt_dir, &sb)) |
136 err(2, "%s", opt_dir); | 132 err(2, opt_dir); |
137 if (!S_ISDIR(sb.st_mode)) | 133 if (!S_ISDIR(sb.st_mode)) |
138 errx(2, "%s: not a directory", opt_dir); | 134 errx(2, "%s: Not a directory", opt_dir); |
139 } 140 rval = 0; 141 do { 142 if (isstdin(*argv)) 143 rval |= ckdist(*argv, opt_type); 144 else if (stat(*argv, &sb)) 145 rval |= fail(*argv, NULL); 146 else if (S_ISREG(sb.st_mode)) --- 59 unchanged lines hidden (view full) --- 206 break; 207 case DISTINF: 208 rval = chkinf(fp, path); 209 break; 210 default: 211 rval = report(path, NULL, E_UNKNOWN); 212 } 213 if (ferror(fp)) | 135 } 136 rval = 0; 137 do { 138 if (isstdin(*argv)) 139 rval |= ckdist(*argv, opt_type); 140 else if (stat(*argv, &sb)) 141 rval |= fail(*argv, NULL); 142 else if (S_ISREG(sb.st_mode)) --- 59 unchanged lines hidden (view full) --- 202 break; 203 case DISTINF: 204 rval = chkinf(fp, path); 205 break; 206 default: 207 rval = report(path, NULL, E_UNKNOWN); 208 } 209 if (ferror(fp)) |
214 warn("%s", path); | 210 warn(path); |
215 if (fp != stdin && fclose(fp)) | 211 if (fp != stdin && fclose(fp)) |
216 err(2, "%s", path); | 212 err(2, path); |
217 return rval; 218} 219 220static int 221chkmd5(FILE * fp, const char *path) 222{ 223 char buf[298]; /* "MD5 (NAMESIZE = MDSUMLEN" */ 224 char name[NAMESIZE + 1]; --- 16 unchanged lines hidden (view full) --- 241 else { 242 *s = 0; 243 if ((dname = distname(path, name, NULL)) == NULL) 244 error = E_NAME; 245 else if (opt_exist) { 246 if ((fd = open(dname, O_RDONLY)) == -1) 247 error = E_ERRNO; 248 else if (close(fd)) | 213 return rval; 214} 215 216static int 217chkmd5(FILE * fp, const char *path) 218{ 219 char buf[298]; /* "MD5 (NAMESIZE = MDSUMLEN" */ 220 char name[NAMESIZE + 1]; --- 16 unchanged lines hidden (view full) --- 237 else { 238 *s = 0; 239 if ((dname = distname(path, name, NULL)) == NULL) 240 error = E_NAME; 241 else if (opt_exist) { 242 if ((fd = open(dname, O_RDONLY)) == -1) 243 error = E_ERRNO; 244 else if (close(fd)) |
249 err(2, "%s", dname); 250 } else if (!MD5File(dname, chk)) | 245 err(2, dname); 246 } else if (!MD5File((char *)dname, chk)) |
251 error = E_ERRNO; 252 else if (strcmp(chk, sum)) 253 error = E_CHKSUM; 254 } 255 if (opt_ignore && error == E_ERRNO && errno == ENOENT) 256 continue; 257 if (error || opt_all) 258 rval |= report(path, dname, error); --- 5 unchanged lines hidden (view full) --- 264 265static int 266chkinf(FILE * fp, const char *path) 267{ 268 char buf[30]; /* "cksum.2 = 10 6" */ 269 char ext[3]; 270 struct stat sb; 271 const char *dname; | 247 error = E_ERRNO; 248 else if (strcmp(chk, sum)) 249 error = E_CHKSUM; 250 } 251 if (opt_ignore && error == E_ERRNO && errno == ENOENT) 252 continue; 253 if (error || opt_all) 254 rval |= report(path, dname, error); --- 5 unchanged lines hidden (view full) --- 260 261static int 262chkinf(FILE * fp, const char *path) 263{ 264 char buf[30]; /* "cksum.2 = 10 6" */ 265 char ext[3]; 266 struct stat sb; 267 const char *dname; |
272 off_t len; 273 u_long sum; 274 intmax_t sumlen; 275 uint32_t chk; | 268 u_long sum, len, chk; |
276 int rval, error, c, pieces, cnt, fd; 277 char ch; 278 279 rval = 0; 280 for (cnt = -1; fgets(buf, sizeof(buf), fp); cnt++) { 281 fd = -1; 282 dname = NULL; 283 error = 0; 284 if (cnt == -1) { 285 if ((c = sscanf(buf, "Pieces = %d%c", &pieces, &ch)) != 2 || 286 ch != '\n' || pieces < 1) 287 error = E_BADINF; | 269 int rval, error, c, pieces, cnt, fd; 270 char ch; 271 272 rval = 0; 273 for (cnt = -1; fgets(buf, sizeof(buf), fp); cnt++) { 274 fd = -1; 275 dname = NULL; 276 error = 0; 277 if (cnt == -1) { 278 if ((c = sscanf(buf, "Pieces = %d%c", &pieces, &ch)) != 2 || 279 ch != '\n' || pieces < 1) 280 error = E_BADINF; |
288 } else if (((c = sscanf(buf, "cksum.%2s = %lu %jd%c", ext, &sum, 289 &sumlen, &ch)) != 4 && | 281 } else if (((c = sscanf(buf, "cksum.%2s = %lu %lu%c", ext, &sum, 282 &len, &ch)) != 4 && |
290 (!feof(fp) || c != 3)) || (c == 4 && ch != '\n') || 291 ext[0] != 'a' + cnt / 26 || ext[1] != 'a' + cnt % 26) 292 error = E_BADINF; 293 else if ((dname = distname(fp == stdin ? NULL : path, NULL, 294 ext)) == NULL) 295 error = E_NAME; 296 else if ((fd = open(dname, O_RDONLY)) == -1) 297 error = E_ERRNO; 298 else if (fstat(fd, &sb)) 299 error = E_ERRNO; | 283 (!feof(fp) || c != 3)) || (c == 4 && ch != '\n') || 284 ext[0] != 'a' + cnt / 26 || ext[1] != 'a' + cnt % 26) 285 error = E_BADINF; 286 else if ((dname = distname(fp == stdin ? NULL : path, NULL, 287 ext)) == NULL) 288 error = E_NAME; 289 else if ((fd = open(dname, O_RDONLY)) == -1) 290 error = E_ERRNO; 291 else if (fstat(fd, &sb)) 292 error = E_ERRNO; |
300 else if (sb.st_size != (off_t)sumlen) | 293 else if (sb.st_size != (off_t)len) |
301 error = E_LENGTH; 302 else if (!opt_exist) { 303 if (crc(fd, &chk, &len)) 304 error = E_ERRNO; 305 else if (chk != sum) 306 error = E_CHKSUM; 307 } 308 if (fd != -1 && close(fd)) | 294 error = E_LENGTH; 295 else if (!opt_exist) { 296 if (crc(fd, &chk, &len)) 297 error = E_ERRNO; 298 else if (chk != sum) 299 error = E_CHKSUM; 300 } 301 if (fd != -1 && close(fd)) |
309 err(2, "%s", dname); | 302 err(2, dname); |
310 if (opt_ignore && error == E_ERRNO && errno == ENOENT) 311 continue; 312 if (error || (opt_all && cnt >= 0)) 313 rval |= report(path, dname, error); 314 if (isfatal(error)) 315 break; 316 } 317 return rval; --- 70 unchanged lines hidden (view full) --- 388 *s++ = '.'; 389 memcpy(s, ext, 2); 390 s += 2; 391 } 392 *s = 0; 393 return buf; 394} 395 | 303 if (opt_ignore && error == E_ERRNO && errno == ENOENT) 304 continue; 305 if (error || (opt_all && cnt >= 0)) 306 rval |= report(path, dname, error); 307 if (isfatal(error)) 308 break; 309 } 310 return rval; --- 70 unchanged lines hidden (view full) --- 381 *s++ = '.'; 382 memcpy(s, ext, 2); 383 s += 2; 384 } 385 *s = 0; 386 return buf; 387} 388 |
396static const char * | 389static char * |
397stripath(const char *path) 398{ 399 const char *s; 400 | 390stripath(const char *path) 391{ 392 const char *s; 393 |
401 return ((s = strrchr(path, '/')) != NULL && s[1] ? | 394 return (char *)((s = strrchr(path, '/')) != NULL && s[1] ? |
402 s + 1 : path); 403} 404 405static int 406distfile(const char *path) 407{ 408 const char *s; 409 int type; --- 25 unchanged lines hidden (view full) --- 435 warnx("%s: %s", path, msg ? msg : sys_errlist[errno]); 436 return 2; 437} 438 439static void 440usage(void) 441{ 442 fprintf(stderr, | 395 s + 1 : path); 396} 397 398static int 399distfile(const char *path) 400{ 401 const char *s; 402 int type; --- 25 unchanged lines hidden (view full) --- 428 warnx("%s: %s", path, msg ? msg : sys_errlist[errno]); 429 return 2; 430} 431 432static void 433usage(void) 434{ 435 fprintf(stderr, |
443 "usage: ckdist [-airsx] [-d dir] [-n name] [-t type] file ...\n"); | 436 "usage: ckdist [-airsx] [-d dir] [-n name] [-t type] file" 437 " ...\n"); |
444 exit(2); 445} | 438 exit(2); 439} |