xref: /csrg-svn/old/flcopy/flcopy.c (revision 13911)
1 #ifndef lint
2 static char *sccsid ="@(#)flcopy.c	4.6 (Berkeley) 07/10/83";
3 #endif
4 
5 #include <sys/file.h>
6 
7 int	floppydes;
8 char	*flopname = "/dev/floppy";
9 long	dsize = 77 * 26 * 128;
10 int	hflag;
11 int	rflag;
12 
13 main(argc, argv)
14 	register char **argv;
15 {
16 	static char buff[512];
17 	register long count;
18 	register startad = -26 * 128;
19 	register int n, file;
20 	register char *cp;
21 
22 	while ((cp = *++argv), --argc > 0) {
23 		while (*cp) {
24 			switch(*cp++) {
25 
26 			case '-':
27 				continue;
28 
29 			case 'h':
30 				hflag++;
31 				printf("Halftime!\n");
32 				if ((file = open("floppy", 0)) < 0) {
33 					printf("can't open \"floppy\"\n");
34 					exit(1);
35 				}
36 				continue;
37 
38 			case 'f':
39 				if (argc < 1) {
40 					printf(
41 					    "flcopy: -f: missing file name\n");
42 					exit(1);
43 				}
44 				flopname = *++argv;
45 				argc--;
46 				break;
47 
48 			case 't':
49 				if (*cp >= '0' && *cp <= '9')
50 					dsize = atoi(cp);
51 				else if (argc > 1) {
52 					dsize = atoi(*++argv);
53 					argc--;
54 				} else
55 					dsize = 77;
56 				if (dsize <= 0 || dsize > 77) {
57 					printf("Bad number of tracks\n");
58 					exit(2);
59 				}
60 				dsize *= 26 * 128;
61 				continue;
62 
63 			case 'r':
64 				rflag++;
65 			}
66 			break;
67 		}
68 	}
69 	if (!hflag) {
70 		file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666);
71 		if (file < 0) {
72 			printf("can't open \"floppy\"\n");
73 			exit(1);
74 		}
75 		for (count = dsize; count > 0 ; count -= 512) {
76 			n = count > 512 ? 512 : count;
77 			lread(startad, n, buff);
78 			write(file, buff, n);
79 			startad += 512;
80 		}
81 	}
82 	if (rflag)
83 		exit(0);
84 	printf("Change Floppy, Hit return when done.\n");
85 	gets(buff);
86 	lseek(file, 0, 0);
87 	count = dsize;
88 	startad = -26 * 128;
89 	for ( ; count > 0 ; count -= 512) {
90 		n = count > 512 ? 512 : count;
91 		read(file, buff, n);
92 		lwrite(startad, n, buff);
93 		startad += 512;
94 	}
95 	exit(0);
96 }
97 
98 rt_init()
99 {
100 	static initized = 0;
101 	int mode = 2;
102 
103 	if (initized)
104 		return;
105 	if (rflag)
106 		mode = 0;
107 	initized = 1;
108 	if ((floppydes = open(flopname, mode)) < 0) {
109 		printf("Floppy open failed\n");
110 		exit(1);
111 	}
112 }
113 
114 /*
115  * Logical to physical adress translation
116  */
117 long
118 trans(logical)
119 	register int logical;
120 {
121 	register int sector, bytes, track;
122 
123 	logical += 26 * 128;
124 	bytes = (logical & 127);
125 	logical >>= 7;
126 	sector = logical % 26;
127 	if (sector >= 13)
128 		sector = sector*2 +1;
129 	else
130 		sector *= 2;
131 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
132 	sector %= 26;
133 	return ((((track *26) + sector) << 7) + bytes);
134 }
135 
136 lread(startad, count, obuff)
137 	register startad, count;
138 	register char *obuff;
139 {
140 	long trans();
141 	extern floppydes;
142 
143 	rt_init();
144 	while ((count -= 128) >= 0) {
145 		lseek(floppydes, trans(startad), 0);
146 		read(floppydes, obuff, 128);
147 		obuff += 128;
148 		startad += 128;
149 	}
150 }
151 
152 lwrite(startad, count, obuff)
153 	register startad, count;
154 	register char *obuff;
155 {
156 	long trans();
157 	extern floppydes;
158 
159 	rt_init();
160 	while ((count -= 128) >= 0) {
161 		lseek(floppydes, trans(startad), 0);
162 		write(floppydes, obuff, 128);
163 		obuff += 128;
164 		startad += 128;
165 	}
166 }
167