comparison simtool/erasefile.c @ 193:bc127ba44488

fc-simtool erase-file command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 Mar 2021 22:55:21 +0000
parents simtool/restorebin.c@bd19dc7591ec
children f1cf569dbba3
comparison
equal deleted inserted replaced
192:edaccdbac95b 193:bc127ba44488
1 /*
2 * This module implements the erase-file command.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11 #include "curfile.h"
12
13 static
14 erase_transparent(fill_byte)
15 {
16 u_char data[255];
17 unsigned off, cc;
18 int rc;
19
20 memset(data, fill_byte, 255);
21 for (off = 0; off < curfile_total_size; off += cc) {
22 cc = curfile_total_size - off;
23 if (cc > 255)
24 cc = 255;
25 rc = update_bin_op(off, data, cc);
26 if (rc < 0)
27 return(rc);
28 }
29 return(0);
30 }
31
32 static
33 erase_records(fill_byte)
34 {
35 u_char data[255];
36 unsigned recno;
37 int rc;
38
39 memset(data, fill_byte, curfile_record_len);
40 for (recno = 1; recno <= curfile_record_count; recno++) {
41 rc = update_rec_op(recno, 0x04, data, curfile_record_len);
42 if (rc < 0)
43 return(rc);
44 }
45 return(0);
46 }
47
48 static
49 erase_cyclic(fill_byte)
50 {
51 u_char data[255];
52 unsigned count;
53 int rc;
54
55 memset(data, fill_byte, curfile_record_len);
56 for (count = 0; count < curfile_record_count; count++) {
57 rc = update_rec_op(0, 0x03, data, curfile_record_len);
58 if (rc < 0)
59 return(rc);
60 }
61 return(0);
62 }
63
64 cmd_erase_file(argc, argv)
65 char **argv;
66 {
67 int file_id, rc;
68 unsigned fill_byte;
69
70 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
71 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
72 file_id = strtoul(argv[1], 0, 16);
73 else
74 file_id = find_symbolic_file_name(argv[1]);
75 if (file_id < 0) {
76 fprintf(stderr,
77 "error: file ID argument is not a hex value or a recognized symbolic name\n");
78 return(-1);
79 }
80 rc = select_op(file_id);
81 if (rc < 0)
82 return(rc);
83 rc = parse_ef_select_response();
84 if (rc < 0)
85 return(rc);
86 if (argc > 2) {
87 fill_byte = strtoul(argv[2], 0, 0);
88 if (fill_byte > 0xFF) {
89 fprintf(stderr, "error: invalid fill byte argument\n");
90 return(-1);
91 }
92 } else
93 fill_byte = 0xFF;
94 switch (curfile_structure) {
95 case 0x00:
96 /* transparent */
97 rc = erase_transparent(fill_byte);
98 break;
99 case 0x01:
100 /* record-based */
101 rc = erase_records(fill_byte);
102 break;
103 case 0x03:
104 /* cyclic */
105 rc = erase_cyclic(fill_byte);
106 break;
107 }
108 return(rc);
109 }