1 // Program name: listcab.cpp
2 // Programmed by: Anthony Barbachan
3 // Programmed in: C++ (Turbo C++ 3.0 Compatable)
4 // Purpose: Source file for a cabinet file object.
5 // Version: 1.00
6 // Last modified on: 11/5/1998
7 
8 #include <stdio.h>
9 #include <string.h>
10 //#include <iomanip.h>
11 #include <fstream>
12 #include <iostream>
13 #include "cftypes.h"
14 #include "cfheader.h"
15 #include "cffolder.h"
16 #include "cffile.h"
17 #include "cfdblock.h"
18 #include "cffdrmgr.h"
19 #include "cfreader.h"
20 #include "cfcreate.h"
21 
22 using std::endl;
23 using std::cout;
24 using std::cerr;
25 
26 ////////////////////////////////////////****************************************
27 
28 void display_header_info(cabinet_header& cfh);
29 void display_folder_info(cabinet_folder_header& folder, word foldernumber);
30 void display_file_info(cabinet_file_header& file, word filenumber);
31 //void ListFilesWithInfo(CabinetFile& cf, ostream& out);
32 void print_file_info(cabinet_file_header& file, ostream& out);
33 
34 ////////////////////////////////////////****************************************
35 
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38 	if(argc == 1)
39 	{
40 		int retval = 0;
41 		cabinet_creator cab;
42 		char filename[256];
43 
44 		if((retval = cab.open()) != OK)
45 		{
46 			cerr << "Open Error: " << retval << endl;
47 			return 1;
48 		}
49 		if((retval = cab.new_folder(cfc_folderinfo::MSZIP_COMPRESSION)) != OK)
50 		{
51 			cerr << "New Folder Error: " << retval << endl;
52 			return 1;
53 		}
54 		int n;
55 		do{
56 			cout << "Enter filename: ";
57 			fgets(filename, 256, stdin);
58 			n = strlen (filename);
59 
60 			if (n == 1 && filename[0] == '\n')
61 				n = 0;
62 
63 			if(n > 0)
64 			{
65 				if (filename[n-1] == '\n')
66 					filename[n-1] = '\0';
67 				if((retval = cab.add_file(filename)) != OK)
68 				{
69 					perror("read");
70 					cerr << "Add file Error: " << retval << endl;
71 					cerr << "Unable to open: " << filename << endl;
72 					return 1;
73 				}
74 			}
75 		}while(n > 0);
76 
77 		cout << "Enter filename for cabinet: ";
78 		fgets(filename, 256, stdin);
79 		n = strlen (filename);
80 		if (n > 0 && filename[n-1] == '\n')
81 			filename[n-1] = '\0';
82 
83 		if((retval = cab.close(filename)) != OK)
84 		{
85 			cerr << "Close Error: " << retval << endl;
86 			return 1;
87 		}
88 
89 		return 0;
90 	}
91 
92 		int err = 0;
93 		word pos = 0u;
94 		cabinet_reader cr;
95 		cout << "Using new codebase..." << endl;
96 
97 		if((err = cr.open(argv[1])) != OK)
98 		{
99 			cerr << "Unable to open cabinet file: " << argv[1] << ": " << err << endl;
100 			return 1;
101 		}
102 
103 		display_header_info(cr);
104 
105 		cout << "Number of files within archive: " << cr.get_nfiles() << endl;
106 		char line[81];
107 		sprintf(line, "%-31s%-15s%-15s%-9s%-10s", "Name", "Size", "Date", "Time", "Attributes");
108 		cout << line;
109 		memset(line, '-', 80);
110 		line[80] = '\0';
111 		cout << line;
112 
113 		for(pos = 0u; pos < cr.get_nfiles(); pos++)
114 		{
115 			//cout << "File " << pos << ": " << cr.file(pos).get_name() << endl;
116 			print_file_info(cr.file(pos), cout);
117 		}
118 
119 		for(int index = 2; index < argc; index++)
120 		{
121 			if((err = cr.extract(argv[index])) != OK)
122 			{
123 				cerr << "Failed to extract: " << argv[index] << endl
124 					<< "Reason for failure: "
125 					<< get_cabinet_error_string(err) << endl;
126 				return 1;
127 			}
128 		}
129 
130 		cr.close();
131 		return 0;
132 
133 	//cerr << "Invalid syntax." << endl;
134 /*
135 	char fname[256];
136 	CabinetFile cf(argv[1]);
137 
138 	if(cf.ErrorCode() != OK)
139 	{
140 		cout << "Error encountered while opening cabinet file: " << cf.error_string() << endl;
141 		return -1;
142 	}
143 
144 	DisplayHeaderInfo(cf);
145 
146 	for(word cell = 0u; cell < cf.NumFolders(); cell++)
147 	{
148 		DisplayFolderInfo(cf.Folder(cell), cell);
149 	}
150 
151 	for(cell = 0u; cell < cf.NumFiles(); cell++)
152 	{
153 		DisplayFileInfo(cf.File(cell), cell);
154 	}
155 
156 	ListFilesWithInfo(cf, cout);
157 
158 
159 	cout << "Enter file to extract: ";
160 	cin >> fname;
161 //	ofstream out(fname, ios::out | ios::binary);
162 
163 	switch(cf.Extract(fname))
164 	{
165 		case OK:
166 			cout << fname << ": extracted." << endl;
167 			//out.close();
168 			break;
169 		case 0:
170 			cerr << fname << ": not in archive." << endl;
171 			out.close();
172 			unlink(fname);
173 			break;
174 		default:
175 			cerr << fname << ": extraction encountered an error: "
176 				<< cf.error_string() << endl;
177 			//out.close();
178 			//unlink(fname);
179 			break;
180 	}
181 
182 	argc = argc;
183 	*/
184 }
185 
186 /****************************************************************************/
187 
display_header_info(cabinet_header & cfh)188 void display_header_info(cabinet_header& cfh)
189 {
190 	cout << "This cabinet file is " << ((cfh.valid_header()) ? "" : "not ")
191 		<< "valid." << endl
192 		<< "This cabinet file is: " << cfh.get_size() << " bytes." << endl
193 		<< "This cabinet file's version is: "
194 		<< (unsigned) cfh.get_version_major()
195 		<< '.' << (unsigned) cfh.get_version_minor() << endl
196 		<< "This cabinet file has " << cfh.get_nfolders() << " folders."
197 		<< endl
198 		<< "This cabinet file has " << cfh.get_nfiles() << " files."
199 		<< endl
200 		<< "This cabinet is " << ((cfh.is_first()) ? "" : "not ")
201 		<< "the first cabinet in this series." << endl
202 		<< "This cabinet is " << ((cfh.is_last()) ? "" : "not ")
203 		<< "the last cabinet in this series." << endl
204 		<< "This cabinet does " << ((cfh.has_reserved_area()) ? "" : "not ")
205 		<< "has reserved fields." << endl;
206 
207 	if(cfh.has_reserved_area())
208 	{
209 		cout << "The reserved information is: " << cfh.get_reserved_area()
210 			<< endl;
211 	}
212 
213 	if(cfh.has_prev())
214 	{
215 		cout << "The previous cabinet is: " << cfh.get_prev_cabinet() << endl;
216 		cout << "The previous disk is: " << cfh.get_prev_disk() << endl;
217 	}
218 
219 	if(cfh.has_next())
220 	{
221 		cout << "The next cabinet is: " << cfh.get_next_cabinet() << endl;
222 		cout << "The next disk is: " << cfh.get_next_disk() << endl;
223 	}
224 }
225 
226 /****************************************************************************/
227 
display_folder_info(cabinet_folder_header & folder,word foldernumber)228 void display_folder_info(cabinet_folder_header& folder, word foldernumber)
229 {
230 	char buf[(sizeof(word) * 2) + 1];
231 
232 	sprintf(buf, "%x", folder.get_compression_type());
233 
234 	cout << "Folder " << foldernumber << ':' << endl
235 		<< "\tnumber of data blocks: " << folder.get_ndatablocks() << endl
236 		<< "\tcompression type: " << buf << endl
237 		<< "\thas reserved field: "
238 		<< ((folder.has_reserved_area()) ? "Yes" : "No") << endl;
239 }
240 
241 /****************************************************************************/
242 
display_file_info(cabinet_file_header & file,word filenumber)243 void display_file_info(cabinet_file_header& file, word filenumber)
244 {
245 	cout << "File " << filenumber << ':' << endl
246 		<< "\tname: " << file.get_name() << endl
247 		<< "\tsize: " << file.get_size() << endl
248 		<< "\tin folder: " << file.get_folder() << endl
249 		<< "\tdate: " << file.month() << '-'
250 				    << file.day() << '-'
251 				    << file.year() << endl
252 		<< "\ttime: " << file.hour() << ':'
253 				    << file.minute() << ':'
254 				    << file.second() << endl
255 		<< "\tContinued From Previous Cabinet: "
256 			<< ((file.continued_from_prev()) ? "Yes" : "No") << endl
257 		<< "\tContinued In Next Cabinet: "
258 			<< ((file.continues_in_next()) ? "Yes" : "No") << endl
259 		<< "\tContinued From Previous Cabinet and In Next Cabinet: "
260 			<< ((file.continued_from_prev_and_in_next()) ? "Yes" : "No")
261 		<< endl << "\tattributes: ";
262 	if(file.is_readonly()) cout << 'R';
263 	if(file.is_hidden()) cout << 'H';
264 	if(file.is_system()) cout << 'S';
265 	if(file.is_archive()) cout << 'A';
266 	if(file.must_execute()) cout << 'E';
267 	if(file.name_is_utf()) cout << 'U';
268 	cout << endl;
269 }
270 
271 /****************************************************************************/
272 
print_file_info(cabinet_file_header & file,ostream & out)273 void print_file_info(cabinet_file_header& file, ostream& out)
274 {
275 	char line[81];
276 
277 	sprintf(line, "%-26.26s     %10lu     %02u-%02u-%04u     %02u:%02u:%02u     %c%c%c%c%c%c",
278 			file.get_name(),
279 			file.get_size(),
280 			file.month(), file.day(), file.year(),
281 			file.hour(), file.minute(), file.second(),
282 			((file.is_readonly()) ? 'R' : '-'),
283 			((file.is_hidden()) ? 'H' : '-'),
284 			((file.is_system()) ? 'S' : '-'),
285 			((file.is_archive()) ? 'A' : '-'),
286 			((file.must_execute()) ? 'E' : '-'),
287 			((file.name_is_utf()) ? 'U' : '-'));
288 	out << line;
289 }
290 
291 /****************************************************************************/
292