1 /***************************************************************************
2  *   Copyright (C) 2003 by Danny Kurniawan                                 *
3  *   danny@it.petra.ac.id                                                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <err.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 
30 #include "clonecd.h"
31 
main(int argc,char * argv[])32 int main(int argc, char *argv[])
33 {
34 	ccd_sector src_sect;
35 	int bytes_read, bytes_written, sect_num = 0;
36 	int total_bytes, n_bytes = 0;
37 	struct stat stat_file;
38 
39 	FILE *src_file;
40 	FILE *dst_file;
41 
42 	if ((!argv[1]) || (!argv[2]))
43 	{
44 		printf("(C) Copyright 2003. Danny Kurniawan.\n");
45 		printf("This software is released under GNU Public License\n\n");
46 		printf("Usage: ccd2iso <.img filename> <.iso filename>\n");
47 		return 1;
48 	}
49 
50 	src_file = fopen(argv[1], "r");
51 	if (src_file == NULL)
52 		err(1, "%s", argv[1]);
53 
54 	dst_file = fopen(argv[2], "w");
55 	if (dst_file == NULL)
56 		err(1, "%s", argv[2]);
57 
58 	stat(argv[1], &stat_file);
59 	total_bytes = stat_file.st_size;
60 
61 	while(!feof(src_file))
62 	{
63 		bytes_read = fread(&src_sect, 1, sizeof(ccd_sector), src_file);
64 		n_bytes += bytes_read;
65 
66 		if (bytes_read != 0)
67 		{
68 			if (bytes_read < sizeof(ccd_sector))
69 			{
70 				printf("Error at sector %d.\n", sect_num);
71 				printf("The sector does not contain complete data. Sector size must be %d, while actual data read is %d\n",
72 					sizeof(ccd_sector), bytes_read);
73 				printf("This probably means that you don't have a valid .img file.\n");
74 				return 1;
75 			}
76 
77 			if (sect_num == 0)
78 			{
79 				bytes_written = fwrite(&(src_sect.data[8]), 1, 2040, dst_file);
80 				if (bytes_written < 2040)
81 				{
82 					printf("Error writing to file.\n");
83 					return -1;
84 				}
85 			}
86 			else
87 			{
88 				bytes_written = fwrite(&(src_sect.data), 1, 2048, dst_file);
89 				if (bytes_written < 2048)
90 				{
91 					printf("Error writing to file.\n");
92 					return -1;
93 				}
94 			}
95 
96 			sect_num++;
97 			printf("Processing sector: %d - %d of %d bytes processed\r", sect_num, n_bytes, total_bytes);
98 		}
99 	}
100 	fclose(src_file);
101 	fclose(dst_file);
102 
103 	printf("\nDone.\n");
104 
105 	return 0;
106 }
107