-
Notifications
You must be signed in to change notification settings - Fork 41
/
dasdcat.c
348 lines (292 loc) · 7.31 KB
/
dasdcat.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
/* DASDCAT.C (c) Copyright Roger Bowler, 1999-2012 */
/* DASD "cat" functions */
/*
* dasdcat
*
* Vast swathes copied from dasdpdsu.c (c) Copyright Roger Bowler, 1999-2009
* Changes and additions Copyright 2000-2009 by Malcolm Beattie
*/
#include "hstdinc.h"
#include "hercules.h"
#include "dasdblks.h"
#ifdef WIN32
#include <io.h> // (setmode)
#endif
/* Option flags */
#define OPT_ASCIIFY 0x1
#define OPT_CARDS 0x2
#define OPT_PDS_WILDCARD 0x4
#define OPT_PDS_LISTONLY 0x8
#define OPT_SEQNO 0x10
int end_of_track(BYTE *p)
{
return p[0] == 0xff && p[1] == 0xff && p[2] == 0xff && p[3] == 0xff
&& p[4] == 0xff && p[5] == 0xff && p[6] == 0xff && p[7] == 0xff;
}
int do_cat_cards(BYTE *buf, int len, unsigned long optflags)
{
if (len % 80 != 0) {
fprintf(stderr,
_("HHCDT002E Can't make 80 column card images from block length %d\n"), len);
return -1;
}
while (len) {
char card[81];
make_asciiz(card, sizeof(card), buf, (optflags & OPT_SEQNO) ? 80 : 72);
if (optflags & OPT_PDS_WILDCARD) {
putchar('|');
putchar(' ');
}
puts(card);
len -= 80;
buf += 80;
}
return 0;
}
int process_member(CIFBLK *cif, int noext, DSXTENT extent[],
BYTE *ttr, unsigned long optflags)
{
int rc, trk, len, cyl, head, rec;
BYTE *buf;
set_codepage(NULL);
trk = (ttr[0] << 8) | ttr[1];
rec = ttr[2];
while (1) {
rc = convert_tt(trk, noext, extent, cif->heads, &cyl, &head);
if (rc < 0)
return -1;
rc = read_block(cif, cyl, head, rec, 0, 0, &buf, &len);
if (rc < 0)
return -1;
if (rc > 0) {
trk++;
rec = 1;
continue;
}
if (len == 0)
break;
if (optflags & OPT_CARDS)
do_cat_cards(buf, len, optflags);
else if (optflags & OPT_ASCIIFY) {
BYTE *p;
for (p = buf; len--; p++)
putchar(guest_to_host(*p));
} else {
#if O_BINARY != 0
setmode(fileno(stdout),O_BINARY);
#endif
fwrite(buf, len, 1, stdout);
}
rec++;
}
return 0;
}
int process_dirblk(CIFBLK *cif, int noext, DSXTENT extent[], BYTE *dirblk,
char *pdsmember, unsigned long optflags)
{
int rc;
int dirrem;
char memname[9];
/* Load number of bytes in directory block */
dirrem = (dirblk[0] << 8) | dirblk[1];
if (dirrem < 2 || dirrem > 256) {
fprintf(stderr, _("HHCDT003E Directory block byte count is invalid\n"));
return -1;
}
if (!strcmp(pdsmember, "*"))
optflags |= OPT_PDS_WILDCARD;
else if (!strcmp(pdsmember, "?"))
optflags |= OPT_PDS_LISTONLY;
/* Point to first directory entry */
dirblk += 2;
dirrem -= 2;
while (dirrem > 0) {
PDSDIR *dirent = (PDSDIR*)dirblk;
int k, size;
if (end_of_track(dirent->pds2name))
return 1;
make_asciiz(memname, sizeof(memname), dirent->pds2name, 8);
if (optflags & OPT_PDS_LISTONLY) {
char memname_lc[9];
memcpy(memname_lc, memname, sizeof(memname));
string_to_lower(memname_lc);
puts(memname_lc);
}
else if ((optflags & OPT_PDS_WILDCARD) || !strcmp(pdsmember, memname)) {
if (optflags & OPT_PDS_WILDCARD)
printf("> Member %s\n", memname);
rc = process_member(cif, noext, extent, dirent->pds2ttrp, optflags);
if (rc < 0)
return -1;
}
/* Load the user data halfword count */
k = dirent->pds2indc & PDS2INDC_LUSR;
/* Point to next directory entry */
size = 12 + k*2;
dirblk += size;
dirrem -= size;
}
return 0;
}
int do_cat_pdsmember(CIFBLK *cif, DSXTENT *extent, int noext,
char *pdsmember, unsigned long optflags)
{
int rc, trk, rec;
/* Point to the start of the directory */
trk = 0;
rec = 1;
/* Read the directory */
while (1) {
BYTE *blkptr;
BYTE dirblk[256];
int cyl, head, len;
#ifdef EXTERNALGUI
if (extgui) fprintf(stderr,"CTRK=%d\n",trk);
#endif /*EXTERNALGUI*/
rc = convert_tt(trk, noext, extent, cif->heads, &cyl, &head);
if (rc < 0)
return -1;
rc = read_block(cif, cyl, head, rec, 0, 0, &blkptr, &len);
if (rc < 0)
return -1;
if (rc > 0) {
trk++;
rec = 1;
continue;
}
if (len == 0)
break;
memcpy(dirblk, blkptr, sizeof(dirblk));
rc = process_dirblk(cif, noext, extent, dirblk, pdsmember, optflags);
if (rc < 0)
return -1;
if (rc > 0)
break;
rec++;
}
return rc;
}
int do_cat_nonpds(CIFBLK *cif, DSXTENT *extent, int noext,
unsigned long optflags)
{
UNREFERENCED(cif);
UNREFERENCED(extent);
UNREFERENCED(noext);
UNREFERENCED(optflags);
fprintf(stderr, _("HHCDT004E non-PDS-members not yet supported\n"));
return -1;
}
int do_cat(CIFBLK *cif, char *file)
{
int rc;
DSXTENT extent[16];
int noext;
char buff[100]; /* must fit max length DSNAME/MEMBER..OPTS */
char dsname[45];
unsigned long optflags = 0;
char *p;
char *pdsmember = 0;
if (!cif)
return 1;
strncpy(buff, file, sizeof(buff));
buff[sizeof(buff)-1] = 0;
p = strchr(buff, ':');
if (p) {
*p++ = 0;
for (; *p; p++) {
if (*p == 'a')
optflags |= OPT_ASCIIFY;
else if (*p == 'c')
optflags |= OPT_CARDS;
else if (*p == 's')
optflags |= OPT_SEQNO;
else
fprintf(stderr, _("HHCDT005E unknown dataset name option: '%c'\n"), *p);
}
}
p = strchr(buff, '/');
if (p) {
*p = 0;
pdsmember = p + 1;
string_to_upper(pdsmember);
}
strncpy(dsname, buff, sizeof(dsname));
dsname[sizeof(dsname)-1] = 0;
string_to_upper(dsname);
rc = build_extent_array(cif, dsname, extent, &noext);
if (rc < 0)
return -1;
#ifdef EXTERNALGUI
/* Calculate ending relative track */
if (extgui) {
int bcyl; /* Extent begin cylinder */
int btrk; /* Extent begin head */
int ecyl; /* Extent end cylinder */
int etrk; /* Extent end head */
int trks; /* total tracks in dataset */
int i; /* loop control */
for (i = 0, trks = 0; i < noext; i++) {
bcyl = (extent[i].xtbcyl[0] << 8) | extent[i].xtbcyl[1];
btrk = (extent[i].xtbtrk[0] << 8) | extent[i].xtbtrk[1];
ecyl = (extent[i].xtecyl[0] << 8) | extent[i].xtecyl[1];
etrk = (extent[i].xtetrk[0] << 8) | extent[i].xtetrk[1];
trks += (((ecyl*cif->heads)+etrk)-((bcyl*cif->heads)+btrk))+1;
}
fprintf(stderr,"ETRK=%d\n",trks-1);
}
#endif /*EXTERNALGUI*/
if (pdsmember)
rc = do_cat_pdsmember(cif, extent, noext, pdsmember, optflags);
else
rc = do_cat_nonpds(cif, extent, noext, optflags);
return rc;
}
int main(int argc, char **argv)
{
int rc = 0;
CIFBLK *cif = 0;
char *fn, *sfn;
INITIALIZE_UTILITY("dasdcat");
/* Display program info message */
display_version (stderr, "Hercules DASD cat program ", FALSE);
if (argc < 2) {
fprintf(stderr, "Usage: dasdcat [-i dasd_image [sf=shadow-file-name] dsname...]...\n");
fprintf(stderr, " dsname can (currently must) be pdsname/spec\n");
fprintf(stderr, " spec is memname[:flags], * (all) or ? (list)\n");
fprintf(stderr, " flags can include (c)ard images, (a)scii\n");
exit(2);
}
/*
* If your version of Hercules doesn't have support in its
* dasdutil.c for turning off verbose messages, then remove
* the following line but you'll have to live with chatty
* progress output on stdout.
*/
set_verbose_util(0);
while (*++argv)
{
if (!strcmp(*argv, "-i"))
{
fn = *++argv;
if (*(argv+1) && strlen (*(argv+1)) > 3 && !memcmp(*(argv+1), "sf=", 3))
sfn = *++argv;
else sfn = NULL;
if (cif)
{
close_ckd_image(cif);
cif = 0;
}
cif = open_ckd_image(fn, sfn, O_RDONLY | O_BINARY, 0);
if (!cif)
fprintf(stderr, _("HHCDT001E failed to open image %s\n"), *argv);
}
else
{
if (do_cat(cif, *argv))
rc = 1;
}
}
if (cif)
close_ckd_image(cif);
return rc;
}