Dash:
[t2.git] / misc / archive / cdromchk.c
blob3446e7c36d7aedd69334cd89b7df15c2884c3242
1 /* gcc -O2 -Wall cdromchk.c -o cdromchk
3 * --- T2-COPYRIGHT-NOTE-BEGIN ---
4 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 *
6 * T2 SDE: misc/archive/cdromchk.c
7 * Copyright (C) 2004 - 2005 The T2 SDE Project
8 * Copyright (C) 1998 - 2003 ROCK Linux Project
9 *
10 * More information can be found in the files COPYING and README.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License. A copy of the
15 * GNU General Public License can be found in the file COPYING.
16 * --- T2-COPYRIGHT-NOTE-END ---
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
27 int main(int argc, char ** argv) {
28 int cdrom,file;
29 char buf1[4096];
30 char buf2[4096];
31 int rc1,rc2,rc3;
32 int c1=0,c2=0;
34 if (argc != 3) {
35 fprintf(stderr,"Usage: %s <cdrom> <iso-file>\n",argv[0]);
36 return 1;
39 fprintf(stderr,"Checking CD-ROM ...");
41 if ( (cdrom=open(argv[1],O_RDONLY|O_SYNC)) == 0 ) {
42 fprintf(stderr,"\n%s: Can't open %s: %s\n",
43 argv[0],argv[1],strerror(errno));
44 return 1;
47 if ( (file=open(argv[2],O_RDONLY)) == 0 ) {
48 fprintf(stderr,"\n%s: Can't open %s: %s\n",
49 argv[0],argv[2],strerror(errno));
50 return 1;
53 while ( (rc1=read(file,buf1,4096)) > 0 ) {
54 for (rc2=0; rc2 < rc1; rc2+=rc3) {
55 rc3=read(cdrom,buf2+rc2,rc1-rc2);
56 if (rc3 == -1) {
57 fprintf(stderr,"\n%s: cdrom read error: %s\n",
58 argv[0],strerror(errno));
59 return 1;
61 if (rc3 == 0) break;
63 if (rc2 != rc1) {
64 fprintf(stderr,"\n%s: cdrom read error: %d of "
65 "%d bytes\n",argv[0],rc2,rc1);
66 return 1;
68 if (memcmp(buf1,buf2,rc2)) {\
69 fprintf(stderr,"\n%s: cdrom data differs from "
70 "the iso file\n",argv[0]);
71 return 1;
73 c1+=rc2; c2+=rc2;
74 if (c1 > (1048576*16)) {
75 c1=0;
76 fprintf(stderr,".");
80 if (rc1 == -1) {
81 fprintf(stderr,"\n%s: file read error: %s\n",
82 argv[0],strerror(errno));
83 return 1;
86 fprintf(stderr," OK (%d MB).\n",c2/1048576);
87 return 0;