File: rawwrite.c

package info (click to toggle)
atari-bootstrap 3.3-3
  • links: PTS
  • area: main
  • in suites: potato, woody
  • size: 468 kB
  • ctags: 528
  • sloc: ansic: 3,531; perl: 529; makefile: 115; sh: 26
file content (111 lines) | stat: -rw-r--r-- 2,275 bytes parent folder | download | duplicates (4)
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
/*
 * rawwrite.c: write contents of a file to a raw disk
 *
 * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
 *
 * 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 or
 * (at your option) any later version.
 *
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <osbind.h>

#define SECTORSIZE	512
#define DISKBUF		(18*SECTORSIZE)

char buf[DISKBUF];

char *bioserrs[20] = {
	"No error",
	"General, unspecified error",
	"Drive not ready",
	"Unknown command",
	"CRC error",
	"Bad request",
	"Seek error",
	"Unknown media",
	"Sector not found",
	"Out of paper",
	"Write fault",
	"Read fault",
	"General error",
	"Write-protected medium",
	"Medium changed",
	"Unknown device",
	"Bad sectors on format",
	"Insert other disk",
	"Insert disk!",
	"Device not responding"
};

char *bioserr( long err )
{
	if (-err > 0 && -err < sizeof(bioserrs)/sizeof(*bioserrs))
		return( bioserrs[-err] );
	else
		return "";
}

	
void usage( void )
{
	fprintf( stderr, "Usage: rawwrite file a:|b:\n" );
	exit( 1 );
}


int main( int argc, char *argv[] )
{
	FILE *f;
	int dstdev, sector, non_whole;
	long err;
	unsigned long nread, nsect;
	
	if (argc != 3)
		usage();

	dstdev = tolower(argv[2][0]);
	if ((dstdev != 'a' && dstdev != 'b') ||
		!(argv[2][1] == 0 || (argv[2][1] == ':' && argv[2][2] == 0)))
		usage();
	dstdev -= 'a';

	if (!(f = fopen( argv[1], "rb" ))) {
		perror( argv[1] );
		exit( 1 );
	}

	sector = non_whole = 0;
	while( (nread = fread( buf, 1, DISKBUF, f )) > 0) {
		if (non_whole) {
			fprintf( stderr, "Internal error: read returned "
					 "non-sector-aligned chunks\n" );
			exit( 1 );
		}
		nsect = (nread+SECTORSIZE-1)/SECTORSIZE;
		non_whole = nread % SECTORSIZE;
		if ((err = Rwabs( 3, buf, nsect, sector, dstdev ))) {
			fprintf( stderr, "Rwabs error #%ld: %s\n", err, bioserr(err) );
			exit( 1 );
		}
		sector += nsect;
		printf( "%5d sectors written\r", sector );
		fflush( stdout );
	}
	if (nread < 0) {
		perror( "read" );
		exit( 1 );
	}

	printf( "%5d sectors ", sector );
	if (non_whole) {
		printf( "+ %d bytes ", non_whole );
	}
	printf( "written.\n" );
	return( 0 );
}