-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdisk.c
363 lines (279 loc) · 6.45 KB
/
mkdisk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*2:*/
#line 24 "mkdisk.w"
/*22:*/
#line 320 "mkdisk.w"
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#ifdef __linux__
#include <stdint.h>
#endif
#include <locale.h>
#include <langinfo.h>
#include <iconv.h>
#include <argp.h>
/*:22*/
#line 25 "mkdisk.w"
#define MKDOS_ID 0123456
#define MKDOS_DIR_ID 051414
#define MKDOS_FILE_STATUS_NORMAL 00
#define MKDOS_FILE_STATUS_PROTECTED 01
#define MKDOS_FILE_STATUS_LDISK 02
#define MKDOS_FILE_STATUS_BAD 0200
#define MKDOS_FILE_STATUS_DELETED 0377
#define DISK_SIZE 1600
#define DISK_CATALOG_SIZE 024
#define MKDOS_NUM_FILES 172
#define MKDOS_MAX_NAME_LEN 14 \
#define BLOCK_SIZE 01000
#define VERSION "0.9" \
#define ERR_SYNTAX 1
#define ERR_CANTOPEN 2
#define ERR_CANTCREATE 3
#define ERR_TOO_MANY_FILES 4
#define ERR_CREATE_DIR 5
#define ERR_ENCODING 6
#define ERR_TOO_BIG 7
#line 26 "mkdisk.w"
/*15:*/
#line 234 "mkdisk.w"
const char*argp_program_version= "mkdisk, "VERSION;
const char*argp_program_bug_address= "<[email protected]>";
/*:15*/
#line 27 "mkdisk.w"
/*10:*/
#line 115 "mkdisk.w"
typedef struct _DiskHeader{
uint8_t dummy0[030];
uint16_t num_files;
uint16_t num_used_blocks;
uint8_t dummy1[0400-4-030];
uint16_t mkdos_id;
uint16_t dir_id;
uint8_t dummy2[0466-4-0400];
uint16_t num_blocks;
uint16_t first_block;
uint8_t dummy3[0500-4-0466];
}DiskHeader;
typedef struct _DirRecord{
uint8_t status;
uint8_t subdir_num;
char name[MKDOS_MAX_NAME_LEN];
uint16_t block;
uint16_t block_len;
uint16_t addr;
uint16_t len;
}DirRecord;
/*:10*//*18:*/
#line 256 "mkdisk.w"
typedef struct _Arguments{
int verbosity;
char output_filename[FILENAME_MAX];
int num_src;
char**srcnames;
}Arguments;
/*:18*/
#line 28 "mkdisk.w"
/*3:*/
#line 63 "mkdisk.w"
static int cur_src;
/*:3*//*9:*/
#line 99 "mkdisk.w"
static iconv_t cd;
/*:9*//*13:*/
#line 221 "mkdisk.w"
static void handleOneFile(FILE*,FILE*);
static int createDir(FILE*);
static uint8_t buf[BLOCK_SIZE];
static DirRecord dir[MKDOS_NUM_FILES+1];
static unsigned int total_size;
/*:13*//*16:*/
#line 238 "mkdisk.w"
static char argp_program_doc[]= "Make MKDOS disk image";
/*:16*//*17:*/
#line 246 "mkdisk.w"
static struct argp_option options[]= {
{"output",'o',"FILENAME",0,"Output filename"},
{"verbose",'v',NULL,0,"Verbose output"},
{0}
};
static error_t parse_opt(int,char*,struct argp_state*);
static struct argp argp= {options,parse_opt,NULL,argp_program_doc};
/*:17*//*19:*/
#line 265 "mkdisk.w"
static Arguments config= {0,{0},0,NULL,};
/*:19*//*23:*/
#line 336 "mkdisk.w"
#define PRINTVERB(level, fmt, a...) (((config.verbosity) >= level) ? printf(\
(fmt), ## a) : 0)
#define PRINTERR(fmt, a...) fprintf(stderr, (fmt), ## a)
/*:23*/
#line 29 "mkdisk.w"
int
main(int argc,char*argv[])
{
/*4:*/
#line 66 "mkdisk.w"
FILE*fsrc,*fresult;
/*:4*/
#line 33 "mkdisk.w"
const char*srcname;
int i;
/*21:*/
#line 307 "mkdisk.w"
argp_parse(&argp,argc,argv,0,0,&config);
if(config.srcnames==NULL){
PRINTERR("No input filenames specified\n");
return(ERR_SYNTAX);
}
for(config.num_src= 0;config.srcnames[config.num_src]!=NULL;++config.num_src){
if(config.num_src>=MKDOS_NUM_FILES){
PRINTERR("Must be <= %d files.\n",MKDOS_NUM_FILES);
return(ERR_TOO_MANY_FILES);
}
}
/*:21*/
#line 37 "mkdisk.w"
/*7:*/
#line 89 "mkdisk.w"
setlocale(LC_ALL,"");
cd= iconv_open("KOI8-R",nl_langinfo(CODESET));
if(cd==(iconv_t)-1){
PRINTERR("Can't open encoding converter\n");
return(ERR_ENCODING);
}
/*:7*/
#line 38 "mkdisk.w"
/*6:*/
#line 75 "mkdisk.w"
fresult= fopen(config.output_filename,"w");
if(fresult==NULL){
PRINTERR("Can't create %s\n",config.output_filename);
return(ERR_CANTOPEN);
}
memset(buf,0,BLOCK_SIZE);
for(i= 0;i<DISK_CATALOG_SIZE;++i){
fwrite(buf,BLOCK_SIZE,1,fresult);
}
memset(dir,0,sizeof(dir));
/*:6*/
#line 39 "mkdisk.w"
total_size= 0;
cur_src= 0;
while((srcname= config.srcnames[cur_src])!=NULL){
/*5:*/
#line 69 "mkdisk.w"
fsrc= fopen(srcname,"r");
if(fsrc==NULL){
PRINTERR("Can't open %s\n",srcname);
return(ERR_CANTOPEN);
}
/*:5*/
#line 45 "mkdisk.w"
handleOneFile(fsrc,fresult);
fclose(fsrc);
if(total_size>=DISK_SIZE){
PRINTERR("Files are too big\n");
return(ERR_TOO_BIG);
}
++cur_src;
}
if(createDir(fresult)!=0){
return(ERR_CREATE_DIR);
}
fclose(fresult);
/*8:*/
#line 96 "mkdisk.w"
iconv_close(cd);
/*:8*/
#line 59 "mkdisk.w"
return(0);
}
/*:2*//*11:*/
#line 143 "mkdisk.w"
static int createDir(FILE*fresult){
DiskHeader*hdr;
uint16_t first_free_block,free_area_len;
int i;
rewind(fresult);
memset(buf,0,BLOCK_SIZE);
hdr= (DiskHeader*)buf;
hdr->mkdos_id= MKDOS_ID;
hdr->dir_id= MKDOS_DIR_ID;
hdr->num_files= config.num_src;
hdr->num_used_blocks= DISK_CATALOG_SIZE+total_size;
hdr->num_blocks= DISK_SIZE;
hdr->first_block= DISK_CATALOG_SIZE;
fwrite(buf,sizeof(DiskHeader),1,fresult);
for(i= 0;i<config.num_src;++i){
fwrite(dir+i,sizeof(DirRecord),1,fresult);
}
first_free_block= dir[i-1].block+dir[i-1].block_len;
free_area_len= DISK_SIZE-DISK_CATALOG_SIZE-first_free_block;
dir[i].block= first_free_block;
dir[i].block_len= free_area_len;
fwrite(dir+i,sizeof(DirRecord),1,fresult);
return(0);
}
/*:11*//*12:*/
#line 180 "mkdisk.w"
static void
handleOneFile(FILE*fsrc,FILE*fresult){
char name[MKDOS_MAX_NAME_LEN+1],*pname;
const char*sname;
size_t slen,dlen;
int block_len,size,start_block;
size= 0;
start_block= total_size+DISK_CATALOG_SIZE;
while(!feof(fsrc)){
block_len= fread(buf,1,BLOCK_SIZE,fsrc);
if(block_len==0){
break;
}
++total_size;
size+= block_len;
fwrite(buf,BLOCK_SIZE,1,fresult);
}
PRINTVERB(1,"File: %s, length: %d, total_blocks: %d.\n",
config.srcnames[cur_src],size,total_size);
dir[cur_src].block= start_block;
dir[cur_src].block_len= total_size-start_block+DISK_CATALOG_SIZE;
dir[cur_src].addr= 01000;
dir[cur_src].len= size;
pname= name;
sname= basename(config.srcnames[cur_src]);
slen= MKDOS_MAX_NAME_LEN;
dlen= slen;
PRINTVERB(2,"Src name:%s, slen:%ld, dst name:%s, dlen:%ld.\n",
sname,slen,pname,dlen);
iconv(cd,&sname,&slen,&pname,&dlen);
PRINTVERB(2,"PSrc:%s, slen:%ld, PDst:%s, dlen:%ld.\n",
sname,slen,pname,dlen);
strncpy(dir[cur_src].name,name,MKDOS_MAX_NAME_LEN);
}
/*:12*//*20:*/
#line 271 "mkdisk.w"
static error_t
parse_opt(int key,char*arg,struct argp_state*state){
Arguments*arguments;
arguments= (Arguments*)state->input;
switch(key){
case'v':
++arguments->verbosity;
break;
case'o':
if(strlen(arg)==0)
return(ARGP_ERR_UNKNOWN);
strncpy(arguments->output_filename,arg,FILENAME_MAX-1);
break;
case ARGP_KEY_ARG:
arguments->srcnames= &state->argv[state->next-1];
state->next= state->argc;
break;
default:
break;
return(ARGP_ERR_UNKNOWN);
}
return(0);
}
/*:20*/