xref: /csrg-svn/old/flcopy/flcopy.c (revision 13837)
1 #ifndef lint
2 static char *sccsid ="@(#)flcopy.c	4.5 (Berkeley) 07/07/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 }
96 
97 rt_init()
98 {
99 	static initized = 0;
100 	int mode = 2;
101 
102 	if (initized)
103 		return;
104 	if (rflag)
105 		mode = 0;
106 	initized = 1;
107 	if ((floppydes = open(flopname, mode)) < 0) {
108 		printf("Floppy open failed\n");
109 		exit(1);
110 	}
111 }
112 
113 /*
114  * Logical to physical adress translation
115  */
116 long
117 trans(logical)
118 	register int logical;
119 {
120 	register int sector, bytes, track;
121 
122 	logical += 26 * 128;
123 	bytes = (logical & 127);
124 	logical >>= 7;
125 	sector = logical % 26;
126 	if (sector >= 13)
127 		sector = sector*2 +1;
128 	else
129 		sector *= 2;
130 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
131 	sector %= 26;
132 	return ((((track *26) + sector) << 7) + bytes);
133 }
134 
135 lread(startad, count, obuff)
136 	register startad, count;
137 	register char *obuff;
138 {
139 	long trans();
140 	extern floppydes;
141 
142 	rt_init();
143 	while ((count -= 128) >= 0) {
144 		lseek(floppydes, trans(startad), 0);
145 		read(floppydes, obuff, 128);
146 		obuff += 128;
147 		startad += 128;
148 	}
149 }
150 
151 lwrite(startad, count, obuff)
152 	register startad, count;
153 	register char *obuff;
154 {
155 	long trans();
156 	extern floppydes;
157 
158 	rt_init();
159 	while ((count -= 128) >= 0) {
160 		lseek(floppydes, trans(startad), 0);
161 		write(floppydes, obuff, 128);
162 		obuff += 128;
163 		startad += 128;
164 	}
165 }
166