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