1 | /* SAP2
|
---|
2 | * Version 2.1
|
---|
3 | * Copyright (C) 2000-2003 Eric Botcazou
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* version 1.0: programme fonctionnel sous MSDOS et Linux
|
---|
21 | * 1.1: retour au format PC à la sortie du programme
|
---|
22 | * 1.2: ajout d'un mode de fonctionnement batch
|
---|
23 | * 2.0: support des disquettes 5"25 et de deux lecteurs PC
|
---|
24 | * 2.1: support de la simple densité
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | #include <ctype.h>
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include "libsap.h"
|
---|
33 | #include "floppy.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | #define SAP2_VERSION_STR "2.0.93"
|
---|
37 |
|
---|
38 | #ifdef linux
|
---|
39 | #define SAP2_PLATFORM_STR "Linux"
|
---|
40 | #else
|
---|
41 | #define SAP2_PLATFORM_STR "MSDOS"
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #define FILENAME_LENGTH 128
|
---|
45 |
|
---|
46 |
|
---|
47 | /* horrible hack pour supporter les accents... */
|
---|
48 | #ifdef linux
|
---|
49 | static char eacute[] = "é";
|
---|
50 | static char egrave[] = "è";
|
---|
51 | static char agrave[] = "à";
|
---|
52 | static char ugrave[] = "ù";
|
---|
53 | #else
|
---|
54 | static char eacute[] = "";
|
---|
55 | static char egrave[] = "";
|
---|
56 | static char agrave[] = "
|
---|
57 | ";
|
---|
58 | static char ugrave[] = "";
|
---|
59 | #endif
|
---|
60 |
|
---|
61 |
|
---|
62 | /* pour l'affichage du répertoire */
|
---|
63 | #define PAGE_HEIGHT 22
|
---|
64 |
|
---|
65 |
|
---|
66 | static struct floppy_info fi;
|
---|
67 | static char *drive_type_name[7] = { "non disponible",
|
---|
68 | "5\"25 - 360 ko",
|
---|
69 | "5\"25 - 1.2 Mo",
|
---|
70 | "3\"5 - 720 ko",
|
---|
71 | "3\"5 - 1.44 Mo",
|
---|
72 | "3\"5 - 2.88 Mo",
|
---|
73 | "3\"5 - 2.88 Mo" };
|
---|
74 |
|
---|
75 | #define IS_5_INCHES(drive) ((fi.drive_type[drive]>0) && (fi.drive_type[drive]<3))
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | /* term_puts:
|
---|
80 | * Affiche une chaîne de caractères en respectant la taille du terminal.
|
---|
81 | */
|
---|
82 | static void term_puts(const char buffer[], int lines, int page_height)
|
---|
83 | {
|
---|
84 | const char *start_cur, *end_cur;
|
---|
85 | char trash[8];
|
---|
86 | int i;
|
---|
87 | char* ret;
|
---|
88 |
|
---|
89 | start_cur = end_cur = buffer;
|
---|
90 |
|
---|
91 | while (lines>page_height) {
|
---|
92 | for (i=0; i<page_height; i++) {
|
---|
93 | while (*end_cur++ != '\n')
|
---|
94 | ;
|
---|
95 | }
|
---|
96 |
|
---|
97 | fwrite(start_cur, sizeof(char), end_cur-start_cur, stdout);
|
---|
98 |
|
---|
99 | ret=fgets(trash, 7, stdin);
|
---|
100 | printf("Appuyer sur <Return>");
|
---|
101 | getchar();
|
---|
102 | printf("\n");
|
---|
103 | ungetc('\n', stdin);
|
---|
104 |
|
---|
105 | start_cur = end_cur;
|
---|
106 | lines -= page_height;
|
---|
107 | }
|
---|
108 |
|
---|
109 | puts(end_cur);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /* ViewDiskDir:
|
---|
115 | * Affiche le répertoire d'une disquette TO.
|
---|
116 | */
|
---|
117 | static int ViewDiskDir(int drive, int density, int page_height)
|
---|
118 | {
|
---|
119 | unsigned char data[512*SAP_NSECTS];
|
---|
120 | char buffer[4096];
|
---|
121 | int lines;
|
---|
122 |
|
---|
123 | if (FloppyReadSector(drive, density, 20, 1, SAP_NSECTS, data) != 0)
|
---|
124 | return 1;
|
---|
125 |
|
---|
126 | /* on réduit la FAT à 80 blocks pour les disquettes 5"25 DD */
|
---|
127 | if (IS_5_INCHES(drive) && density == 2)
|
---|
128 | memset(data + SAP_SECTSIZE1 + 1 + SAP_NTRACKS1, 0xFE, SAP_NTRACKS1);
|
---|
129 |
|
---|
130 | /* fonction bas-niveau de libSAP */
|
---|
131 | lines = _ExtractDir(buffer, sizeof(buffer), drive, density, data);
|
---|
132 |
|
---|
133 | if ((page_height<0) || (lines<=page_height))
|
---|
134 | puts(buffer);
|
---|
135 | else
|
---|
136 | term_puts(buffer, lines, page_height);
|
---|
137 |
|
---|
138 | return 0;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 |
|
---|
143 | /* ViewArchiveDir:
|
---|
144 | * Affiche le répertoire d'une archive SAP.
|
---|
145 | */
|
---|
146 | static int ViewArchiveDir(const char sap_name[], int page_height)
|
---|
147 | {
|
---|
148 | char buffer[4096];
|
---|
149 | sapID sap_file;
|
---|
150 | int format, lines;
|
---|
151 |
|
---|
152 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR)
|
---|
153 | return 1;
|
---|
154 |
|
---|
155 | lines = sap_ListArchive(sap_file, buffer, sizeof(buffer));
|
---|
156 |
|
---|
157 | if (lines == 0)
|
---|
158 | return 2;
|
---|
159 |
|
---|
160 | if ((page_height<0) || (lines<=page_height))
|
---|
161 | puts(buffer);
|
---|
162 | else
|
---|
163 | term_puts(buffer, lines, page_height);
|
---|
164 |
|
---|
165 | sap_CloseArchive(sap_file);
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 | /* CreateEmptyArchive:
|
---|
172 | * Crée une archive SAP vide (mais formatée).
|
---|
173 | */
|
---|
174 | static int CreateEmptyArchive(const char sap_name[], int format, int capacity)
|
---|
175 | {
|
---|
176 | sapID sap_file;
|
---|
177 |
|
---|
178 | if ((sap_file=sap_CreateArchive(sap_name, format)) == SAP_ERROR)
|
---|
179 | return 1;
|
---|
180 |
|
---|
181 | sap_FormatArchive(sap_file, capacity);
|
---|
182 | sap_CloseArchive(sap_file);
|
---|
183 |
|
---|
184 | return 0;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 | /* BuildSectorMap:
|
---|
190 | * Construit la carte des secteurs d'une piste
|
---|
191 | * en fonction du facteur d'entrelacement.
|
---|
192 | */
|
---|
193 | static void BuildSectorMap(int *sector_map, int factor)
|
---|
194 | {
|
---|
195 | int sect, loc=0;
|
---|
196 |
|
---|
197 | /* mise à zéro de la table */
|
---|
198 | memset(sector_map, 0, sizeof(int)*SAP_NSECTS);
|
---|
199 |
|
---|
200 | for (sect=1; sect<=SAP_NSECTS; sect++) {
|
---|
201 | while (sector_map[loc] != 0)
|
---|
202 | loc=(loc+1)%SAP_NSECTS;
|
---|
203 |
|
---|
204 | sector_map[loc]=sect;
|
---|
205 |
|
---|
206 | loc=(loc+factor)%SAP_NSECTS;
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 | /* FormatDisk:
|
---|
213 | * Formate une disquette 3"5 - 720 ko au format Thomson.
|
---|
214 | */
|
---|
215 | static int FormatDisk(int drive, int density, int factor, int verbose)
|
---|
216 | {
|
---|
217 | int num_tracks, sect_size;
|
---|
218 | int track, sect, pos, i;
|
---|
219 | int sector_map[SAP_NSECTS];
|
---|
220 | unsigned char header_table[512];
|
---|
221 | unsigned char buffer[512*SAP_NSECTS];
|
---|
222 |
|
---|
223 | /* détermination du nombre de pistes */
|
---|
224 | if (IS_5_INCHES(drive))
|
---|
225 | num_tracks = SAP_NTRACKS2;
|
---|
226 | else
|
---|
227 | num_tracks = SAP_NTRACKS1;
|
---|
228 |
|
---|
229 | /* et de la taille du secteur */
|
---|
230 | if (density == 1)
|
---|
231 | sect_size = SAP_SECTSIZE2;
|
---|
232 | else
|
---|
233 | sect_size = SAP_SECTSIZE1;
|
---|
234 |
|
---|
235 | /* construction de la carte des secteurs pour chaque piste,
|
---|
236 | à partir du facteur d'entrelacement */
|
---|
237 | BuildSectorMap(sector_map, factor);
|
---|
238 |
|
---|
239 | /* formatage des pistes */
|
---|
240 | for (track=0; track<num_tracks; track++) {
|
---|
241 | if (verbose) {
|
---|
242 | printf("\r formatage piste %d", track);
|
---|
243 | fflush(stdout);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /* construction de la table des headers */
|
---|
247 | for (sect=1, pos=0; sect<=SAP_NSECTS; sect++, pos+=4) {
|
---|
248 | header_table[pos] = track;
|
---|
249 | header_table[pos+1] = 0;
|
---|
250 | header_table[pos+2] = sector_map[sect-1];
|
---|
251 | header_table[pos+3] = density - 1;
|
---|
252 | }
|
---|
253 |
|
---|
254 | if (FloppyFormatTrack(drive, density, track, header_table) != 0)
|
---|
255 | return 1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (verbose) {
|
---|
259 | printf("\n construction de la FAT (piste 20)");
|
---|
260 | fflush(stdout);
|
---|
261 | }
|
---|
262 |
|
---|
263 | for (i=0; i<sect_size*SAP_NSECTS; i++)
|
---|
264 | buffer[i] = 0xFF;
|
---|
265 |
|
---|
266 | /* pour MS-DOS (voir teo/src/dos/ddisk.c) */
|
---|
267 | buffer[i]=0;
|
---|
268 |
|
---|
269 | if (FloppyWriteSector(drive, density, 20, 1, SAP_NSECTS, buffer) != 0)
|
---|
270 | return 2;
|
---|
271 |
|
---|
272 | buffer[0] = 0;
|
---|
273 | buffer[41] = 0xFE;
|
---|
274 | buffer[42] = 0xFE;
|
---|
275 |
|
---|
276 | /* pour MS-DOS (voir teo/src/dos/ddisk.c) */
|
---|
277 | buffer[sect_size] = 0;
|
---|
278 |
|
---|
279 | if (FloppyWriteSector(drive, density, 20, 2, 1, buffer) != 0)
|
---|
280 | return 2;
|
---|
281 |
|
---|
282 | if (verbose)
|
---|
283 | printf("\n");
|
---|
284 |
|
---|
285 | return 0;
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 |
|
---|
290 | /* shrink_fat_160_to_80:
|
---|
291 | * Réduit la taille de la FAT de 160 à 80 blocks.
|
---|
292 | */
|
---|
293 | static void shrink_fat_160_to_80(unsigned char fat_data[])
|
---|
294 | {
|
---|
295 | int i;
|
---|
296 |
|
---|
297 | for (i=0; i<SAP_NTRACKS1; i++) {
|
---|
298 | if (fat_data[1 + SAP_NTRACKS1 + i] != 0xFF)
|
---|
299 | return;
|
---|
300 | }
|
---|
301 |
|
---|
302 | memset(fat_data + 1 + SAP_NTRACKS1, 0xFE, SAP_NTRACKS1);
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 |
|
---|
307 | /* PackArchive:
|
---|
308 | * Transfère le contenu d'une disquette TO dans une archive SAP.
|
---|
309 | */
|
---|
310 | static int PackArchive(const char sap_name[], int drive, int density, int verbose)
|
---|
311 | {
|
---|
312 | int num_tracks, sect_size;
|
---|
313 | int track, sect;
|
---|
314 | sapID sap_file;
|
---|
315 | sapsector_t sapsector;
|
---|
316 | unsigned char buffer[512*SAP_NSECTS];
|
---|
317 |
|
---|
318 | if ((sap_file=sap_CreateArchive(sap_name, density == 1 ? SAP_FORMAT2 : SAP_FORMAT1)) == SAP_ERROR)
|
---|
319 | return 1;
|
---|
320 |
|
---|
321 | /* détermination du nombre de pistes */
|
---|
322 | if (IS_5_INCHES(drive))
|
---|
323 | num_tracks = SAP_NTRACKS2;
|
---|
324 | else
|
---|
325 | num_tracks = SAP_NTRACKS1;
|
---|
326 |
|
---|
327 | /* et de la taille du secteur */
|
---|
328 | if (density == 1)
|
---|
329 | sect_size = SAP_SECTSIZE2;
|
---|
330 | else
|
---|
331 | sect_size = SAP_SECTSIZE1;
|
---|
332 |
|
---|
333 | for (track=0; track<num_tracks; track++) {
|
---|
334 | if (verbose) {
|
---|
335 | printf("\r lecture piste %d", track);
|
---|
336 | fflush(stdout);
|
---|
337 | }
|
---|
338 |
|
---|
339 | if (FloppyReadSector(drive, density, track, 1, SAP_NSECTS, buffer) != 0) {
|
---|
340 | /* erreur dans la piste, on essaie secteur par secteur */
|
---|
341 | for (sect=1; sect<=SAP_NSECTS; sect++) {
|
---|
342 | sapsector.format = 0;
|
---|
343 | sapsector.protection = 0;
|
---|
344 | sapsector.track = track;
|
---|
345 | sapsector.sector = sect;
|
---|
346 |
|
---|
347 | if (FloppyReadSector(drive, density, track, sect, 1, buffer) != 0) {
|
---|
348 | /* erreur de lecture du secteur */
|
---|
349 | if (verbose)
|
---|
350 | printf("\n secteur %d illisible", sect);
|
---|
351 |
|
---|
352 | sapsector.format = 4;
|
---|
353 | memset(sapsector.data, 0xF7, sect_size);
|
---|
354 | }
|
---|
355 | else {
|
---|
356 | memcpy(sapsector.data, buffer, sect_size);
|
---|
357 | }
|
---|
358 |
|
---|
359 | /* traitement spécial de la FAT pour les disquettes 5"25 DD */
|
---|
360 | if ((track == 20) && (sect == 2) && IS_5_INCHES(drive) && (density == 2))
|
---|
361 | shrink_fat_160_to_80(sapsector.data);
|
---|
362 |
|
---|
363 | sap_FillArchive(sap_file, &sapsector);
|
---|
364 | }
|
---|
365 |
|
---|
366 | if ((track < num_tracks-1) && verbose)
|
---|
367 | printf("\n");
|
---|
368 | }
|
---|
369 | else {
|
---|
370 | for (sect=1; sect<=SAP_NSECTS; sect++) {
|
---|
371 | sapsector.format = 0;
|
---|
372 | sapsector.protection = 0;
|
---|
373 | sapsector.track = track;
|
---|
374 | sapsector.sector = sect;
|
---|
375 |
|
---|
376 | memcpy(sapsector.data, buffer+(sect-1)*sect_size, sect_size);
|
---|
377 |
|
---|
378 | /* traitement spécial de la FAT pour les disquettes 5"25 DD */
|
---|
379 | if ((track == 20) && (sect == 2) && IS_5_INCHES(drive) && (density == 2))
|
---|
380 | shrink_fat_160_to_80(sapsector.data);
|
---|
381 |
|
---|
382 | sap_FillArchive(sap_file, &sapsector);
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | if (verbose)
|
---|
388 | printf("\n");
|
---|
389 |
|
---|
390 | sap_CloseArchive(sap_file);
|
---|
391 | return 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 |
|
---|
396 | /* UnpackArchive:
|
---|
397 | * Transfère le contenu d'une archive SAP vers une disquette TO.
|
---|
398 | */
|
---|
399 | static int UnpackArchive(const char sap_name[], int drive, int density, int verbose)
|
---|
400 | {
|
---|
401 | int format, num_tracks, sect_size;
|
---|
402 | int track, sect;
|
---|
403 | sapID sap_file;
|
---|
404 | sapsector_t sapsector;
|
---|
405 | unsigned char buffer[512*SAP_NSECTS];
|
---|
406 |
|
---|
407 | if ((sap_file=sap_OpenArchive(sap_name, &format)) == SAP_ERROR)
|
---|
408 | return 1;
|
---|
409 |
|
---|
410 | /* vérification du format */
|
---|
411 | if (((format == SAP_FORMAT1) && (density != 2)) || ((format == SAP_FORMAT2) && (density != 1)))
|
---|
412 | return 1;
|
---|
413 |
|
---|
414 | /* détermination du nombre de pistes */
|
---|
415 | if (IS_5_INCHES(drive))
|
---|
416 | num_tracks = SAP_NTRACKS2;
|
---|
417 | else
|
---|
418 | num_tracks = SAP_NTRACKS1;
|
---|
419 |
|
---|
420 | /* et de la taille du secteur */
|
---|
421 | if (density == 1)
|
---|
422 | sect_size = SAP_SECTSIZE2;
|
---|
423 | else
|
---|
424 | sect_size = SAP_SECTSIZE1;
|
---|
425 |
|
---|
426 | for (track=0; track<num_tracks; track++) {
|
---|
427 | if (verbose) {
|
---|
428 | printf("\r %scriture piste %d", eacute, track);
|
---|
429 | fflush(stdout);
|
---|
430 | }
|
---|
431 |
|
---|
432 | for (sect=1; sect<=SAP_NSECTS; sect++) {
|
---|
433 | sapsector.format = 0;
|
---|
434 | sapsector.protection = 0;
|
---|
435 | sapsector.track = track;
|
---|
436 | sapsector.sector = sect;
|
---|
437 |
|
---|
438 | if (sap_ReadSector(sap_file, track, sect, &sapsector) == SAP_ERROR) {
|
---|
439 | sap_CloseArchive(sap_file);
|
---|
440 | return 2;
|
---|
441 | }
|
---|
442 | else {
|
---|
443 | memcpy(buffer+(sect-1)*sect_size, sapsector.data, sect_size);
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | /* pour MS-DOS (voir teo/src/dos/ddisk.c) */
|
---|
448 | buffer[SAP_NSECTS*sect_size] = 0;
|
---|
449 |
|
---|
450 | if (FloppyWriteSector(drive, density, track, 1, SAP_NSECTS, buffer) != 0) {
|
---|
451 | sap_CloseArchive(sap_file);
|
---|
452 | return 3;
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (verbose)
|
---|
457 | printf("\n");
|
---|
458 |
|
---|
459 | sap_CloseArchive(sap_file);
|
---|
460 | return 0;
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 |
|
---|
465 | /* get_drive:
|
---|
466 | * Helper pour obtenir le numéro de lecteur.
|
---|
467 | */
|
---|
468 | static void get_drive(int *drive, int *density)
|
---|
469 | {
|
---|
470 | char trash[8];
|
---|
471 | char* ret;
|
---|
472 |
|
---|
473 | if (fi.num_drives > 1) {
|
---|
474 | do {
|
---|
475 | printf(" num%sro du lecteur: ", eacute);
|
---|
476 | if (!scanf("%d", drive))
|
---|
477 | ret=fgets(trash, 7, stdin);
|
---|
478 | }
|
---|
479 | while ((*drive<0) || (*drive>3) || (fi.drive_type[*drive]==0));
|
---|
480 | }
|
---|
481 | else {
|
---|
482 | for (*drive=0; *drive<4; (*drive)++) {
|
---|
483 | if (fi.drive_type[*drive] > 0)
|
---|
484 | break;
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | if (fi.fm_support && IS_5_INCHES(*drive)) {
|
---|
489 | do {
|
---|
490 | printf(" densit%s (1:simple, 2:double): ", eacute);
|
---|
491 | if (!scanf("%d", density))
|
---|
492 | ret=fgets(trash, 7, stdin);
|
---|
493 | }
|
---|
494 | while ((*density<1) || (*density>2));
|
---|
495 | }
|
---|
496 | else
|
---|
497 | *density = 2;
|
---|
498 | }
|
---|
499 |
|
---|
500 |
|
---|
501 |
|
---|
502 | /* interactive_main:
|
---|
503 | * Point d'entrée du programme interactif.
|
---|
504 | */
|
---|
505 | static void interactive_main(void)
|
---|
506 | {
|
---|
507 | int c, ret;
|
---|
508 | char trash[8];
|
---|
509 | int drive, density, format, factor;
|
---|
510 | char sap_name[FILENAME_LENGTH];
|
---|
511 | char* ret2;
|
---|
512 |
|
---|
513 | while (1) {
|
---|
514 | printf("SAP2, syst%sme d'archivage pour disquettes 3\"5 et 5\"25 Thomson\n", egrave);
|
---|
515 | printf("version "SAP2_VERSION_STR" ("SAP2_PLATFORM_STR") copyright (C) 2000-2003 Eric Botcazou\n");
|
---|
516 | printf(" bas%s sur SAP copyright (C) Alexandre Pukall Avril 1998\n\n", eacute);
|
---|
517 |
|
---|
518 | if (fi.drive_type[0] > 0) {
|
---|
519 | printf("Lecteur A: (%s) --> lecteur 0", drive_type_name[fi.drive_type[0]]);
|
---|
520 |
|
---|
521 | if (fi.drive_type[1] > 0)
|
---|
522 | printf(" + lecteur 1\n");
|
---|
523 | else
|
---|
524 | printf("\n");
|
---|
525 | }
|
---|
526 | else {
|
---|
527 | printf("Lecteur A: (%s)\n", drive_type_name[0]);
|
---|
528 | }
|
---|
529 |
|
---|
530 | if (fi.drive_type[2] > 0) {
|
---|
531 | printf("Lecteur B: (%s) --> lecteur 2", drive_type_name[fi.drive_type[2]]);
|
---|
532 |
|
---|
533 | if (fi.drive_type[3] > 0)
|
---|
534 | printf(" + lecteur 3\n\n");
|
---|
535 | else
|
---|
536 | printf("\n\n");
|
---|
537 | }
|
---|
538 | else {
|
---|
539 | printf("Lecteur B: (%s)\n\n", drive_type_name[0]);
|
---|
540 | }
|
---|
541 |
|
---|
542 | printf("Transfert TO-->PC:\n");
|
---|
543 | printf(" 1. Visualiser le contenu d'une disquette Thomson\n");
|
---|
544 | printf(" 2. Cr%ser une archive SAP vide\n", eacute);
|
---|
545 | printf(" 3. Archiver une disquette Thomson vers une archive SAP\n");
|
---|
546 | printf("Transfert PC-->TO:\n");
|
---|
547 | printf(" 4. Visualiser le contenu d'une archive SAP\n");
|
---|
548 | printf(" 5. Formater une disquette 3\"5 (720 ko) ou 5\"25 au format Thomson\n");
|
---|
549 | printf(" 6. D%ssarchiver une archive SAP vers une disquette Thomson\n", eacute);
|
---|
550 | printf("Autres commandes:\n");
|
---|
551 | printf(" 7. Quitter\n\n");
|
---|
552 |
|
---|
553 | do {
|
---|
554 | printf("Votre choix: ");
|
---|
555 | if (!scanf("%d", &c))
|
---|
556 | ret2=fgets(trash, 7, stdin);
|
---|
557 | }
|
---|
558 | while ((c<1) || (c>7));
|
---|
559 |
|
---|
560 | switch (c) {
|
---|
561 |
|
---|
562 | case 1:
|
---|
563 | printf("Visualisation du contenu d'une disquette:\n");
|
---|
564 | get_drive(&drive, &density);
|
---|
565 | printf("\n");
|
---|
566 |
|
---|
567 | ret = ViewDiskDir(drive, density, PAGE_HEIGHT);
|
---|
568 | if (ret == 1)
|
---|
569 | printf("*** Erreur: r%spertoire illisible ***\n", eacute);
|
---|
570 |
|
---|
571 | break;
|
---|
572 |
|
---|
573 | case 2:
|
---|
574 | printf("Cr%sation d'une archive vide:\n", eacute);
|
---|
575 |
|
---|
576 | printf(" nom de l'archive %s cr%ser (sans extension): ", agrave, eacute);
|
---|
577 | ret=scanf("%s", sap_name);
|
---|
578 | strcat(sap_name, ".sap");
|
---|
579 |
|
---|
580 | do {
|
---|
581 | printf(" format (1:5\"25 SD, 2:5\"25 DD, 3:3\"5 DD): ");
|
---|
582 | if (!scanf("%d", &format))
|
---|
583 | ret2=fgets(trash, 7, stdin);
|
---|
584 | }
|
---|
585 | while ((format<1) || (format>3));
|
---|
586 |
|
---|
587 | ret = CreateEmptyArchive(sap_name, (format == 1 ? SAP_FORMAT2 : SAP_FORMAT1), (format == 3 ? SAP_TRK80 : SAP_TRK40));
|
---|
588 | if (ret == 1)
|
---|
589 | printf("*** Erreur: impossible de cr%ser le fichier %s ***\n", eacute, sap_name);
|
---|
590 |
|
---|
591 | break;
|
---|
592 |
|
---|
593 | case 3:
|
---|
594 | printf("Archivage d'une disquette:\n");
|
---|
595 |
|
---|
596 | printf(" nom de l'archive %s cr%ser (sans extension): ", agrave, eacute);
|
---|
597 | ret=scanf("%s", sap_name);
|
---|
598 | strcat(sap_name, ".sap");
|
---|
599 |
|
---|
600 | get_drive(&drive, &density);
|
---|
601 | ret = PackArchive(sap_name, drive, density, 1);
|
---|
602 | if (ret == 1)
|
---|
603 | printf("*** Erreur: impossible de cr%ser le fichier %s ***\n", eacute, sap_name);
|
---|
604 |
|
---|
605 | break;
|
---|
606 |
|
---|
607 | case 4:
|
---|
608 | printf("Visualisation du contenu d'une archive:\n");
|
---|
609 |
|
---|
610 | printf(" nom de l'archive (sans extension): ");
|
---|
611 | ret=scanf("%s", sap_name);
|
---|
612 | strcat(sap_name, ".sap");
|
---|
613 |
|
---|
614 | printf("\n");
|
---|
615 |
|
---|
616 | ret = ViewArchiveDir(sap_name, PAGE_HEIGHT);
|
---|
617 | if (ret == 1)
|
---|
618 | printf("*** Erreur: impossible d'ouvrir le fichier %s ***\n", sap_name);
|
---|
619 | else if (ret == 2)
|
---|
620 | printf("*** Erreur: archive SAP corrompue ***\n");
|
---|
621 |
|
---|
622 | break;
|
---|
623 |
|
---|
624 | case 5:
|
---|
625 | printf("Formatage d'une disquette au format Thomson:\n");
|
---|
626 | printf(" Si la disquette est une 3\"5 - 1.44 Mo, occulter l'encoche\n");
|
---|
627 | printf(" de droite (recto et verso) avec un morceau de scotch.\n");
|
---|
628 |
|
---|
629 | get_drive(&drive, &density);
|
---|
630 |
|
---|
631 | do {
|
---|
632 | printf(" facteur d'entrelacement ([1..%d], %d recommand%s): ", SAP_NSECTS-1, (SAP_NSECTS-1)/2, eacute);
|
---|
633 | if (!scanf("%d", &factor))
|
---|
634 | ret2=fgets(trash, 7, stdin);
|
---|
635 | }
|
---|
636 | while ((factor<1) || (factor>(SAP_NSECTS-1)));
|
---|
637 |
|
---|
638 | ret = FormatDisk(drive, density, factor, 1);
|
---|
639 | if (ret == 1)
|
---|
640 | printf("\n*** Erreur: impossible de formater la disquette ***\n");
|
---|
641 | else if (ret == 2)
|
---|
642 | printf("*** Erreur: impossible d'%scrire sur la disquette ***\n", eacute);
|
---|
643 | break;
|
---|
644 |
|
---|
645 | case 6:
|
---|
646 | printf("D%ssarchivage vers une disquette:\n", eacute);
|
---|
647 |
|
---|
648 | get_drive(&drive, &density);
|
---|
649 |
|
---|
650 | printf(" nom de l'archive (sans extension): ");
|
---|
651 | ret=scanf("%s", sap_name);
|
---|
652 | strcat(sap_name, ".sap");
|
---|
653 |
|
---|
654 | ret = UnpackArchive(sap_name, drive, density, 1);
|
---|
655 | if (ret == 1)
|
---|
656 | printf("*** Erreur: impossible d'ouvrir le fichier %s ***\n", sap_name);
|
---|
657 | else if (ret == 2)
|
---|
658 | printf("*** Erreur: archive SAP corrompue ***\n");
|
---|
659 | else if (ret == 3)
|
---|
660 | printf("\n*** Erreur: impossible d'%scrire sur la disquette ***\n", eacute);
|
---|
661 | break;
|
---|
662 |
|
---|
663 | case 7:
|
---|
664 | return;
|
---|
665 | }
|
---|
666 |
|
---|
667 | ret2=fgets(trash, 7, stdin);
|
---|
668 | printf("Appuyer sur <Return>");
|
---|
669 | fflush(stdout);
|
---|
670 | getchar();
|
---|
671 | printf("\n");
|
---|
672 | }
|
---|
673 | }
|
---|
674 |
|
---|
675 |
|
---|
676 | #define COMMAND_MAX 8
|
---|
677 |
|
---|
678 | static char *short_command[] = {"-h", "-v", "-d", "-c", "-p", "-t", "-f", "-u"};
|
---|
679 | static char *long_command[] = {"--help", "--version", "--dir", "--create", "--pack", "--list", "--format", "--unpack"};
|
---|
680 |
|
---|
681 |
|
---|
682 |
|
---|
683 | /* usage:
|
---|
684 | * Affiche les commandes d'utilisation du programme et quitte.
|
---|
685 | */
|
---|
686 | static void usage(const char prog_name[])
|
---|
687 | {
|
---|
688 | fprintf(stderr, "Usage: %s -h --help | -v --version | -t --list | -p --pack | -u --unpack\n", prog_name);
|
---|
689 | fprintf(stderr, " -c --create | -d --dir | -f --format\n");
|
---|
690 | exit(EXIT_FAILURE);
|
---|
691 | }
|
---|
692 |
|
---|
693 |
|
---|
694 |
|
---|
695 | /* main:
|
---|
696 | * Point d'entrée du programme.
|
---|
697 | */
|
---|
698 | int main(int argc, char *argv[])
|
---|
699 | {
|
---|
700 | int ret = 0;
|
---|
701 | int i;
|
---|
702 |
|
---|
703 | if (argc < 2) { /* no argument? */
|
---|
704 | if (FloppyInit(&fi, 1) > 0) {
|
---|
705 | interactive_main();
|
---|
706 | FloppyExit();
|
---|
707 | }
|
---|
708 | else {
|
---|
709 | ret = 1;
|
---|
710 | }
|
---|
711 | }
|
---|
712 | else {
|
---|
713 | if (argv[1][0] == '-') {
|
---|
714 |
|
---|
715 | switch (argv[1][1]) {
|
---|
716 |
|
---|
717 | case '-': /* long commands */
|
---|
718 | for (i=0; i<COMMAND_MAX; i++) {
|
---|
719 | if (strcmp(argv[1], long_command[i]) == 0) {
|
---|
720 | argv[1] = short_command[i];
|
---|
721 | return main(argc, argv);
|
---|
722 | }
|
---|
723 | }
|
---|
724 |
|
---|
725 | usage(argv[0]);
|
---|
726 |
|
---|
727 | case 'h': /* help */
|
---|
728 | printf("SAP2 est un syst%sme d'archivage pour disquettes 3\"5 et 5\"25 Thomson. Il permet\n", egrave);
|
---|
729 | printf("de cr%ser des images de disquettes sous forme d'archive SAP.\n\n", eacute);
|
---|
730 | printf("Usage:\n");
|
---|
731 | printf(" %s (mode interactif)\n", argv[0]);
|
---|
732 | printf(" %s commande1 archive.sap [lecteur] [densit%s]\n", argv[0], eacute);
|
---|
733 | printf(" %s commande2 archive.sap [nb pistes] [densit%s]\n", argv[0], eacute);
|
---|
734 | printf(" %s commande3 lecteur [densit%s] [entrelacement]\n", argv[0], eacute);
|
---|
735 | printf("o%s la commande1 est prise parmi les suivantes:\n", ugrave);
|
---|
736 | printf(" -h, --help affiche cette aide\n");
|
---|
737 | printf(" -v, --version affiche la version du programme\n");
|
---|
738 | printf(" -t, --list affiche la liste des fichiers de l'archive SAP\n");
|
---|
739 | printf(" -p, --pack archive une disquette Thomson vers une archive SAP\n");
|
---|
740 | printf(" -u, --unpack d%ssarchive une archive SAP vers une disquette Thomson\n", eacute);
|
---|
741 | printf("et o%s la commande2 est prise parmi les suivantes:\n", ugrave);
|
---|
742 | printf(" -c, --create cr%se une archive SAP vide\n", eacute);
|
---|
743 | printf("et o%s la commande3 est prise parmi les suivantes:\n", ugrave);
|
---|
744 | printf(" -d, --dir affiche le contenu d'une disquette Thomson\n");
|
---|
745 | printf(" -f, --format formate une disquette au format Thomson\n");
|
---|
746 | break;
|
---|
747 |
|
---|
748 | case 'v': /* version */
|
---|
749 | printf("SAP2 version "SAP2_VERSION_STR" pour "SAP2_PLATFORM_STR", copyright (C) 2000-2003 Eric Botcazou.\n");
|
---|
750 | break;
|
---|
751 |
|
---|
752 | case 'd': /* dir */
|
---|
753 | if (argc < 3)
|
---|
754 | usage(argv[0]);
|
---|
755 |
|
---|
756 | if (FloppyInit(&fi, 0) > 0) {
|
---|
757 | ret = ViewDiskDir(atoi(argv[2]), argc > 3 ? atoi(argv[3]) : 2, -1);
|
---|
758 | FloppyExit();
|
---|
759 | }
|
---|
760 | else {
|
---|
761 | ret = 1;
|
---|
762 | }
|
---|
763 | break;
|
---|
764 |
|
---|
765 | case 'c': /* create */
|
---|
766 | if (argc < 3)
|
---|
767 | usage(argv[0]);
|
---|
768 |
|
---|
769 | if (argc < 4) {
|
---|
770 | ret = CreateEmptyArchive(argv[2], SAP_FORMAT1, SAP_TRK80);
|
---|
771 | }
|
---|
772 | else if (argc == 4){
|
---|
773 | if (atoi(argv[3]) == 40)
|
---|
774 | ret = CreateEmptyArchive(argv[2], SAP_FORMAT1, SAP_TRK40);
|
---|
775 | else if (atoi(argv[3]) == 80)
|
---|
776 | ret = CreateEmptyArchive(argv[2], SAP_FORMAT1, SAP_TRK80);
|
---|
777 | else
|
---|
778 | ret = 1;
|
---|
779 | }
|
---|
780 | else {
|
---|
781 | if (atoi(argv[3]) == 40)
|
---|
782 | ret = CreateEmptyArchive(argv[2], (atoi(argv[4]) == 1 ? SAP_FORMAT2 : SAP_FORMAT1), SAP_TRK40);
|
---|
783 | else if (atoi(argv[3]) == 80)
|
---|
784 | ret = CreateEmptyArchive(argv[2], (atoi(argv[4]) == 1 ? SAP_FORMAT2 : SAP_FORMAT1), SAP_TRK80);
|
---|
785 | else
|
---|
786 | ret = 1;
|
---|
787 | }
|
---|
788 | break;
|
---|
789 |
|
---|
790 | case 'p': /* pack */
|
---|
791 | if (argc < 4)
|
---|
792 | usage(argv[0]);
|
---|
793 |
|
---|
794 | if (FloppyInit(&fi, 0) > 0) {
|
---|
795 | ret = PackArchive(argv[2], atoi(argv[3]), argc > 4 ? atoi(argv[4]) : 2, 0);
|
---|
796 | FloppyExit();
|
---|
797 | }
|
---|
798 | else {
|
---|
799 | ret = 1;
|
---|
800 | }
|
---|
801 | break;
|
---|
802 |
|
---|
803 | case 't': /* list */
|
---|
804 | if (argc < 3)
|
---|
805 | usage(argv[0]);
|
---|
806 |
|
---|
807 | ret = ViewArchiveDir(argv[2], -1);
|
---|
808 | break;
|
---|
809 |
|
---|
810 | case 'f': /* format */
|
---|
811 | if (argc < 3)
|
---|
812 | usage(argv[0]);
|
---|
813 |
|
---|
814 | if (FloppyInit(&fi, 1) > 0) {
|
---|
815 | if (argc == 3)
|
---|
816 | ret = FormatDisk(atoi(argv[2]), 2, (SAP_NSECTS-1)/2, 0);
|
---|
817 | else if (argc == 4)
|
---|
818 | ret = FormatDisk(atoi(argv[2]), atoi(argv[3]), (SAP_NSECTS-1)/2, 0);
|
---|
819 | else
|
---|
820 | ret = FormatDisk(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), 0);
|
---|
821 |
|
---|
822 | FloppyExit();
|
---|
823 | }
|
---|
824 | else {
|
---|
825 | ret = 1;
|
---|
826 | }
|
---|
827 | break;
|
---|
828 |
|
---|
829 | case 'u': /* unpack */
|
---|
830 | if (argc < 4)
|
---|
831 | usage(argv[0]);
|
---|
832 |
|
---|
833 | if (FloppyInit(&fi, 1) > 0) {
|
---|
834 | ret = UnpackArchive(argv[2], atoi(argv[3]), argc > 4 ? atoi(argv[4]) : 2, 0);
|
---|
835 | FloppyExit();
|
---|
836 | }
|
---|
837 | else {
|
---|
838 | ret = 1;
|
---|
839 | }
|
---|
840 | break;
|
---|
841 |
|
---|
842 | default:
|
---|
843 | usage(argv[0]);
|
---|
844 | } /* end of switch */
|
---|
845 | }
|
---|
846 | else {
|
---|
847 | usage(argv[0]);
|
---|
848 | }
|
---|
849 | }
|
---|
850 |
|
---|
851 | return ret;
|
---|
852 | }
|
---|
853 |
|
---|