aboutsummaryrefslogtreecommitdiff
path: root/jindroush/adir/adir.cpp
blob: 7dceb01c026342414d105e10b9a776fb3a266c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include <sys/stat.h>

#include "jintypes.h"
#include <fnmatch.h>

#include "pub.def"

#include "adsk.h"

#include "cfs.h"
#include "cfs_dos2.h"
#include "cfs_dos3.h"
#include "cfs_dosm.h"
#include "cfs_doss.h"
#include "cfs_kboo.h"
#include "cfs_b2b.h"
#include "cfs_jonw.h"
#include "cfs_howf.h"
#include "cfs_robc.h"
#include "cfs_boot.h"

typedef enum
{
	DOS_AUTO,
	DOS_2,
	DOS_3,
	DOS_MY,
	DOS_SP,
	DOS_KBOOT,
	DOS_B2B,
	DOS_JONW,
	DOS_HOWF,
	DOS_ROBC,
	DOS_BOOT,
} DOS_TYPE;

void ReadDir( char* szDirName, CDirEntry* pEntry );
BOOL CheckMask( char* szFname, char* szMask );
void MakePath( char* szDest, char* szDir, char* szFileName );
BOOL InitializeFs( DOS_TYPE dostype, BOOL bVerbose );
// BOOL SWFN_DOST( void* );
char* GetFSName( DOS_TYPE dostype );

BOOL g_bIsMyDos = FALSE;
BOOL g_bToLower = FALSE;
BOOL g_bVerbose = FALSE;
BOOL g_bListOnly = TRUE;
BOOL g_bRepairAuto = TRUE;

DOS_TYPE g_dostype = DOS_AUTO;
DISK_TYPE g_dtypeIn = DISK_AUTO;

char g_szMask[ 256 ] = "*";
char g_szPath[ 256 ] = ".";

ADisk* g_pDisk;
CFs* g_pFs;

#define SHEADER PRG_NAME " v" PRG_VERSION " (c) " PRG_COPYRIGHT " " PRG_AUTHOR "\n"

#define HEADER SHEADER \
   PRG_DESC "\n" \
	"  Latest version can be found at " PRG_URL "\n" \
	"  Published under GPL. See GPL.TXT.\n\n"

#define USAGE HEADER "Usage:  " PRG_NAME " " PRG_USAGE

#include "switches.cpp"

int main(int argc,char *argv[])
{
	setbuf( stdout, NULL );
	setbuf( stderr, NULL );

	if ( !SWITCHES_Init( &argc, argv ) )
		return 1;

	if ( argc < 2 )
	{
		SWFN_HELP( USAGE );
		return 1;
	}

	fprintf( stderr, SHEADER );

	char* szInfile = argv[ 1 ];

	if ( argc >= 3 )
	{
		strcpy( g_szMask, argv[ 2 ] );
	}

	DISKINIT_RETCODE ret;
	g_dtypeIn = DISK_ATR;
	if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &g_pDisk, DISK_ATR, szInfile, FALSE, TRUE, g_bRepairAuto ) ) )
	{
		g_dtypeIn = DISK_DI;
		if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &g_pDisk, DISK_DI, szInfile, FALSE, FALSE, FALSE ) ) )
		{
			g_dtypeIn = DISK_DCM;
			if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &g_pDisk, DISK_DCM, szInfile, FALSE, FALSE, FALSE ) ) )
			{
				g_dtypeIn = DISK_SCP;
				if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &g_pDisk, DISK_SCP, szInfile, FALSE, FALSE, FALSE ) ) )
				{
					g_dtypeIn = DISK_XFD;
					if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &g_pDisk, DISK_XFD, szInfile, FALSE, TRUE, g_bRepairAuto ) ) )
					{
						printf( "Unable to determine format of disk image '%s'!\n", szInfile );
						return FALSE;
					}
				}
			}
		}
	}

	if ( ret == DI_RET_CANT_CONTINUE )
		return 1;

	printf( "Input file '%s' (%s)\n", szInfile, GetDiskTypeName( g_dtypeIn ) );


	if ( g_dostype == DOS_AUTO )
	{
		DOS_TYPE bestfs = DOS_AUTO;
		int iBest = 100;

		printf( "Checking '%s' for possible filesystems:\n", argv[1] );

		if ( InitializeFs( DOS_SP, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_SP;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( InitializeFs( DOS_ROBC, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_ROBC;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( InitializeFs( DOS_MY, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_MY;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( InitializeFs( DOS_2, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_2;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;

		}

		if ( InitializeFs( DOS_KBOOT, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_KBOOT;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( InitializeFs( DOS_B2B, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_B2B;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( InitializeFs( DOS_JONW, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_JONW;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( InitializeFs( DOS_HOWF, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_HOWF;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( InitializeFs( DOS_3, FALSE ) )
		{
			if ( iBest > g_pFs->GetInvalidPercent() )
			{
				bestfs = DOS_3;
				iBest = g_pFs->GetInvalidPercent();
			}
			delete g_pFs;
		}

		if ( ( bestfs == DOS_AUTO ) || iBest )
		{
			fprintf( stderr, "\nUnable to guess best filesystem (Best was %s: %d%%). Exiting.\n", GetFSName( bestfs ), iBest );
			delete g_pDisk;
			return 1;
		}
		else
		{
			printf( "\nBest guess:\n" );

			if ( !InitializeFs( bestfs, TRUE ) )
			{
				delete g_pDisk;
				return 1;
			}
		}

	}
	else
	{
		if ( !InitializeFs( g_dostype, TRUE ) )
		{
			delete g_pDisk;
			return 1;
		}
	}

	CDirEntry* pEntry = g_pFs->GetRoot();

	if ( !pEntry )
	{
		fprintf( stderr, "No files found!\n" );
		delete g_pFs;
		delete g_pDisk;
		return 1;
	}

	{
		struct stat sbuf;
		if(stat(g_szPath, &sbuf) != 0)
			mkdir(g_szPath, 0777);
	}
	ReadDir( NULL, pEntry );

	delete g_pFs;
	delete g_pDisk;

	return 0;
}

BOOL InitializeFs( DOS_TYPE dostype, BOOL bVerbose )
{
	switch( dostype )
	{
		case DOS_2:
			g_pFs = new CDos2();
			break;

		case DOS_3:
			g_pFs = new CDos3();
			break;

		case DOS_MY:
			g_pFs = new CDosM();
			break;

		case DOS_SP:
			g_pFs = new CDosS();
			break;

		case DOS_KBOOT:
			g_pFs = new CKBoot();
			break;

		case DOS_B2B:
			g_pFs = new CBas2Boot();
			break;

		case DOS_JONW:
			g_pFs = new CJonw();
			break;

		case DOS_HOWF:
			g_pFs = new CHowf();
			break;

		case DOS_ROBC:
			g_pFs = new CRobc();
			break;

		case DOS_BOOT:
			g_pFs = new CBoot();
			break;

		default:
			if ( bVerbose )
				fprintf( stderr, "Invalid filesystem specified!\n" );
			return FALSE;
	}

	if ( !g_pFs )
	{
		if ( bVerbose )
		 	fprintf( stderr, "Can't initialize filesystem driver!\n" );
	 	return FALSE;
	}

	if ( !g_pFs->Mount( g_pDisk ) )
	{
		if ( bVerbose )
			fprintf( stderr, "Mount failed because:\n%s\n", g_pFs->GetLastError() );
		delete g_pFs;
		return FALSE;
	}

	printf( "Mounting as %s: Invalid %d%%\n", GetFSName( dostype ), g_pFs->GetInvalidPercent() );

	return TRUE;
}

void ReadDir( char* szDirName, CDirEntry* pEntry )
{
	char szCurrEntryName[ 256 ];

	do
	{
		strcpy( szCurrEntryName, pEntry->m_szFname );

		char szFullAtariPath[ 256 ];

		if ( szDirName )
			MakePath( szFullAtariPath, szDirName, szCurrEntryName );
		else
			strcpy( szFullAtariPath, szCurrEntryName );

		char szFullOsPath[ 256 ];
		MakePath( szFullOsPath, g_szPath, szFullAtariPath );

		if ( CheckMask( szCurrEntryName, g_szMask ) )
		{

			if ( pEntry->m_dwFlags & DIRE_SUBDIR )
			{
				if ( !g_bListOnly )
				{
					printf( "Creating dir %s\n", szCurrEntryName );
					mkdir( szFullOsPath, 0777 );
				}

				if ( pEntry->m_pSubdir )
					ReadDir( szFullAtariPath, pEntry->m_pSubdir );

			}
			else
			{
				printf( "%s %s ", pEntry->m_szAscData, szFullAtariPath );

				if ( !g_bListOnly )
				{
					BOOL bRes = g_pFs->ExportFile( szFullOsPath, pEntry );

					if ( bRes )
					{
						printf( "exported\n" );
					}
					else
					{
						printf( "invalid because:\n%s\n", g_pFs->GetLastError() );
					}
				}
				else
				{
					printf( "\n" );
				}
			}
		}

		pEntry = pEntry->m_pNext;

	} while( pEntry );

	return;
}

BOOL CheckMask( char* szFname, char* szMask )
{
	return fnmatch( szMask, szFname, FNM_NOESCAPE ) ? FALSE : TRUE;
}

void MakePath( char* szDest, char* szDir, char* szFileName )
{
	int iLength;

	if ( szDest != szDir )
		strcpy ( szDest, szDir );

	iLength = strlen( szDest );

	if ( iLength )
	{
		if ( szDest[ iLength - 1 ] != '/' )
		{
			szDest[ iLength + 1 ] = '\0';
			szDest[ iLength ] = '/';
		}
	}
	strcat( szDest, szFileName );
}

/*
BOOL SWFN_DOST( void* p )
{
	g_dostype = (DOS_TYPE)(int)p;

	return TRUE;
}
*/

//returns ptr to filesystem name
char* GetFSName( DOS_TYPE dostype )
{
	switch( dostype )
	{
		case DOS_2:
			return "Dos2";

		case DOS_3:
			return "Dos3";

		case DOS_MY:
			return "MyDos";

		case DOS_SP:
			return "SpartaDos";

		case DOS_KBOOT:
			return "KBoot";

		case DOS_B2B:
			return "Bas2Boot";

		case DOS_HOWF:
			return "HowFen";

		case DOS_ROBC:
			return "RobC";

		case DOS_BOOT:
			return "Boot";

		case DOS_JONW:
			return "JonW";

		default:
			return "None";
	}
}

//returns ptr to disk type name