void reopen(const char *infname, const char *outfname) { if (fin) { if (hclose(fin) != 0) fail("hclose(input)"); } if (fout) { if (hclose(fout) != 0) fail("hclose(output)"); } fin = hopen(infname, "r"); if (fin == NULL) fail("hopen(\"%s\")", infname); fout = hopen(outfname, "w"); if (fout == NULL) fail("hopen(\"%s\")", outfname); }
int main(int argc, char **argv) { static const struct option options[] = { { "header-only", no_argument, NULL, 'h' }, { "no-header", no_argument, NULL, 'H' }, { "view", no_argument, NULL, 'c' }, { "help", no_argument, NULL, '?' }, { NULL, 0, NULL, 0 } }; int status = EXIT_SUCCESS; int c, i; while ((c = getopt_long(argc, argv, "chH", options, NULL)) >= 0) switch (c) { case 'c': mode = view_all; break; case 'h': mode = view_headers; show_headers = 1; break; case 'H': show_headers = 0; break; case '?': usage(stdout, EXIT_SUCCESS); break; default: usage(stderr, EXIT_FAILURE); break; } if (optind == argc) usage(stderr, EXIT_FAILURE); for (i = optind; i < argc; i++) { htsFormat fmt; hFILE *fp = hopen(argv[i], "r"); if (fp == NULL) { fprintf(stderr, "htsfile: can't open \"%s\": %s\n", argv[i], strerror(errno)); status = EXIT_FAILURE; continue; } if (hts_detect_format(fp, &fmt) < 0) { fprintf(stderr, "htsfile: detecting \"%s\" format failed: %s\n", argv[i], strerror(errno)); hclose_abruptly(fp); status = EXIT_FAILURE; continue; } if (mode == identify) { printf("%s:\t%s\n", argv[i], hts_format_description(&fmt)); } else switch (fmt.category) { case sequence_data: if (view_sam(fp, argv[i])) fp = NULL; break; case variant_data: if (view_vcf(fp, argv[i])) fp = NULL; break; default: fprintf(stderr, "htsfile: can't view %s: unknown format\n", argv[i]); status = EXIT_FAILURE; break; } if (fp && hclose(fp) < 0) { fprintf(stderr, "htsfile: closing %s failed\n", argv[i]); status = EXIT_FAILURE; } } return status; }
UFILE *uopen(char *fname, void *arg) { UFILE *rp = NULL; errno = 0; if ( strncmp(fname,"http://",7)==0) { HTTP *http = hopen(fname,(size_t)arg); if ( http==NULL ) return NULL; rp = (UFILE*)malloc(sizeof(UFILE)); if ( rp==NULL ) { hclose(http); return NULL; } rp->type = UFT_HTTP; rp->handle = http; return rp; } else { FILE *fp = fopen(fname,(char*)arg); if ( fp==NULL ) return NULL; rp = (UFILE*)malloc(sizeof(UFILE)); if ( rp==NULL ) { fclose(fp); return NULL; } rp->type = UFT_FILE; rp->handle = fp; return rp; } }
/*-------------------------------------------------------------------*/ int open_awstape (DEVBLK *dev, BYTE *unitstat,BYTE code) { int rc = -1; /* Return code */ char pathname[MAX_PATH]; /* file path in host format */ /* Check for no tape in drive */ if (!strcmp (dev->filename, TAPE_UNLOADED)) { build_senseX(TAPE_BSENSE_TAPEUNLOADED,dev,unitstat,code); return -1; } /* Open the AWSTAPE file */ hostpath(pathname, dev->filename, sizeof(pathname)); if(!dev->tdparms.logical_readonly) { rc = hopen(pathname, O_RDWR | O_BINARY); } /* If file is read-only, attempt to open again */ if (dev->tdparms.logical_readonly || (rc < 0 && (EROFS == errno || EACCES == errno))) { dev->readonly = 1; rc = hopen(pathname, O_RDONLY | O_BINARY); } /* Check for successful open */ if (rc < 0) { logmsg (_("HHCTA102E %4.4X: Error opening %s: %s\n"), dev->devnum, dev->filename, strerror(errno)); strcpy(dev->filename, TAPE_UNLOADED); build_senseX(TAPE_BSENSE_TAPELOADFAIL,dev,unitstat,code); return -1; } /* Store the file descriptor in the device block */ dev->fd = rc; rc=rewind_awstape(dev,unitstat,code); return rc; } /* end function open_awstape */
static ssize_t multipart_read(hFILE *fpv, void *buffer, size_t nbytes) { hFILE_multipart *fp = (hFILE_multipart *) fpv; size_t n; open_next: if (fp->currentfp == NULL) { if (fp->current < fp->nparts) { const hfile_part *p = &fp->parts[fp->current]; hts_log_debug("Opening part #%zu of %zu: \"%.120s%s\"", fp->current+1, fp->nparts, p->url, (strlen(p->url) > 120)? "..." : ""); fp->currentfp = p->headers? hopen(p->url, "r:", "httphdr:v", p->headers, NULL) : hopen(p->url, "r"); if (fp->currentfp == NULL) return -1; } else return 0; // No more parts, so we're truly at EOF } n = fp->currentfp->mobile? fp->currentfp->backend->read(fp->currentfp, buffer, nbytes) : hread(fp->currentfp, buffer, nbytes); if (n == 0) { // We're at EOF on this part, so set up the next part hFILE *prevfp = fp->currentfp; free_part(&fp->parts[fp->current]); fp->current++; fp->currentfp = NULL; if (hclose(prevfp) < 0) return -1; goto open_next; } return n; // Number of bytes read by (or an error from) fp->currentfp }
static void http_download(WEBBLK *webblk, char *filename) { char buffer[HTTP_PATH_LENGTH]; char tbuf[80]; int fd, length; char *filetype; char fullname[HTTP_PATH_LENGTH]; struct stat st; MIMETAB *mime_type = mime_types; strlcpy( fullname, sysblk.httproot, sizeof(fullname) ); strlcat( fullname, filename, sizeof(fullname) ); http_verify_path(webblk,fullname); if(stat(fullname,&st)) http_error(webblk, "404 File Not Found","", strerror(errno)); if(!S_ISREG(st.st_mode)) http_error(webblk, "404 File Not Found","", "The requested file is not a regular file"); fd = hopen(fullname,O_RDONLY|O_BINARY,0); if (fd == -1) http_error(webblk, "404 File Not Found","", strerror(errno)); hprintf(webblk->sock,"HTTP/1.0 200 OK\n"); if ((filetype = strrchr(filename,'.'))) for(mime_type++;mime_type->suffix && strcasecmp(mime_type->suffix,filetype + 1); mime_type++); if(mime_type->type) hprintf(webblk->sock,"Content-Type: %s\n", mime_type->type); hprintf(webblk->sock,"Expires: %s\n", http_timestring(tbuf,sizeof(tbuf),time(NULL)+HTML_STATIC_EXPIRY_TIME)); hprintf(webblk->sock,"Content-Length: %d\n\n", (int)st.st_size); while ((length = read(fd, buffer, sizeof(buffer))) > 0) hwrite(webblk->sock,buffer, length); close(fd); http_exit(webblk); }
mFILE *find_file_url(char *file, char *url) { char buf[8192], *cp; mFILE *mf = NULL; int maxlen = 8190 - strlen(file), len; hFILE *hf; /* Expand %s for the trace name */ for (cp = buf; *url && cp - buf < maxlen; url++) { if (*url == '%' && *(url+1) == 's') { url++; cp += strlen(strcpy(cp, file)); } else { *cp++ = *url; } } *cp++ = 0; if (!(hf = hopen(buf, "r"))) return NULL; if (NULL == (mf = mfcreate(NULL, 0))) return NULL; while ((len = hread(hf, buf, 8192)) > 0) { if (mfwrite(buf, len, 1, mf) <= 0) { hclose_abruptly(hf); mfdestroy(mf); return NULL; } } if (hclose(hf) < 0 || len < 0) { mfdestroy(mf); return NULL; } mrewind(mf); return mf; }
int main(void) { static const int size[] = { 1, 13, 403, 999, 30000 }; char buffer[40000]; char *original; int c, i; ssize_t n; off_t off; reopen("vcf.c", "test/hfile1.tmp"); while ((c = hgetc(fin)) != EOF) { if (hputc(c, fout) == EOF) fail("hputc"); } if (herrno(fin)) { errno = herrno(fin); fail("hgetc"); } reopen("test/hfile1.tmp", "test/hfile2.tmp"); if (hpeek(fin, buffer, 50) < 0) fail("hpeek"); while ((n = hread(fin, buffer, 17)) > 0) { if (hwrite(fout, buffer, n) != n) fail("hwrite"); } if (n < 0) fail("hread"); reopen("test/hfile2.tmp", "test/hfile3.tmp"); while ((n = hread(fin, buffer, sizeof buffer)) > 0) { if (hwrite(fout, buffer, n) != n) fail("hwrite"); if (hpeek(fin, buffer, 700) < 0) fail("hpeek"); } if (n < 0) fail("hread"); reopen("test/hfile3.tmp", "test/hfile4.tmp"); i = 0; off = 0; while ((n = hread(fin, buffer, size[i++ % 5])) > 0) { off += n; buffer[n] = '\0'; check_offset(fin, off, "pre-peek"); if (hputs(buffer, fout) == EOF) fail("hputs"); if ((n = hpeek(fin, buffer, size[(i+3) % 5])) < 0) fail("hpeek"); check_offset(fin, off, "post-peek"); } if (n < 0) fail("hread"); reopen("test/hfile4.tmp", "test/hfile5.tmp"); n = hread(fin, buffer, 200); if (n < 0) fail("hread"); else if (n != 200) fail("hread only got %d", (int)n); if (hwrite(fout, buffer, 1000) != 1000) fail("hwrite"); check_offset(fin, 200, "input/first200"); check_offset(fout, 1000, "output/first200"); if (hseek(fin, 800, SEEK_CUR) < 0) fail("hseek/cur"); check_offset(fin, 1000, "input/seek"); for (off = 1000; (n = hread(fin, buffer, sizeof buffer)) > 0; off += n) if (hwrite(fout, buffer, n) != n) fail("hwrite"); if (n < 0) fail("hread"); check_offset(fin, off, "input/eof"); check_offset(fout, off, "output/eof"); if (hseek(fin, 200, SEEK_SET) < 0) fail("hseek/set"); if (hseek(fout, 200, SEEK_SET) < 0) fail("hseek(output)"); check_offset(fin, 200, "input/backto200"); check_offset(fout, 200, "output/backto200"); n = hread(fin, buffer, 800); if (n < 0) fail("hread"); else if (n != 800) fail("hread only got %d", (int)n); if (hwrite(fout, buffer, 800) != 800) fail("hwrite"); check_offset(fin, 1000, "input/wrote800"); check_offset(fout, 1000, "output/wrote800"); if (hflush(fout) == EOF) fail("hflush"); original = slurp("vcf.c"); for (i = 1; i <= 5; i++) { char *text; sprintf(buffer, "test/hfile%d.tmp", i); text = slurp(buffer); if (strcmp(original, text) != 0) { fprintf(stderr, "%s differs from vcf.c\n", buffer); return EXIT_FAILURE; } free(text); } free(original); if (hclose(fin) != 0) fail("hclose(input)"); if (hclose(fout) != 0) fail("hclose(output)"); fout = hopen("test/hfile_chars.tmp", "w"); if (fout == NULL) fail("hopen(\"test/hfile_chars.tmp\")"); for (i = 0; i < 256; i++) if (hputc(i, fout) != i) fail("chars: hputc (%d)", i); if (hclose(fout) != 0) fail("hclose(test/hfile_chars.tmp)"); fin = hopen("test/hfile_chars.tmp", "r"); if (fin == NULL) fail("hopen(\"test/hfile_chars.tmp\") for reading"); for (i = 0; i < 256; i++) if ((c = hgetc(fin)) != i) fail("chars: hgetc (%d = 0x%x) returned %d = 0x%x", i, i, c, c); if ((c = hgetc(fin)) != EOF) fail("chars: hgetc (EOF) returned %d", c); if (hclose(fin) != 0) fail("hclose(test/hfile_chars.tmp) for reading"); fin = hopen("data:hello, world!\n", "r"); if (fin == NULL) fail("hopen(\"data:...\")"); n = hread(fin, buffer, 300); if (n < 0) fail("hread"); buffer[n] = '\0'; if (strcmp(buffer, "hello, world!\n") != 0) fail("hread result"); if (hclose(fin) != 0) fail("hclose(\"data:...\")"); return EXIT_SUCCESS; }
static hFILE * s3_rewrite(const char *s3url, const char *mode, va_list *argsp) { const char *bucket, *path; char *header_list[4], **header = header_list; kstring_t url = { 0, 0, NULL }; kstring_t profile = { 0, 0, NULL }; kstring_t host_base = { 0, 0, NULL }; kstring_t token_hdr = { 0, 0, NULL }; s3_auth_data *ad = calloc(1, sizeof(*ad)); if (!ad) return NULL; ad->mode = strchr(mode, 'r') ? 'r' : 'w'; // Our S3 URL format is s3[+SCHEME]://[ID[:SECRET[:TOKEN]]@]BUCKET/PATH if (s3url[2] == '+') { bucket = strchr(s3url, ':') + 1; kputsn(&s3url[3], bucket - &s3url[3], &url); } else { kputs("https:", &url); bucket = &s3url[3]; } while (*bucket == '/') kputc(*bucket++, &url); path = bucket + strcspn(bucket, "/?#@"); if (*path == '@') { const char *colon = strpbrk(bucket, ":@"); if (*colon != ':') { urldecode_kput(bucket, colon - bucket, &profile); } else { const char *colon2 = strpbrk(&colon[1], ":@"); urldecode_kput(bucket, colon - bucket, &ad->id); urldecode_kput(&colon[1], colon2 - &colon[1], &ad->secret); if (*colon2 == ':') urldecode_kput(&colon2[1], path - &colon2[1], &ad->token); } bucket = &path[1]; path = bucket + strcspn(bucket, "/?#"); } else { // If the URL has no ID[:SECRET]@, consider environment variables. const char *v; if ((v = getenv("AWS_ACCESS_KEY_ID")) != NULL) kputs(v, &ad->id); if ((v = getenv("AWS_SECRET_ACCESS_KEY")) != NULL) kputs(v, &ad->secret); if ((v = getenv("AWS_SESSION_TOKEN")) != NULL) kputs(v, &ad->token); if ((v = getenv("AWS_DEFAULT_PROFILE")) != NULL) kputs(v, &profile); else if ((v = getenv("AWS_PROFILE")) != NULL) kputs(v, &profile); else kputs("default", &profile); } if (ad->id.l == 0) { const char *v = getenv("AWS_SHARED_CREDENTIALS_FILE"); parse_ini(v? v : "~/.aws/credentials", profile.s, "aws_access_key_id", &ad->id, "aws_secret_access_key", &ad->secret, "aws_session_token", &ad->token, NULL); } if (ad->id.l == 0) parse_ini("~/.s3cfg", profile.s, "access_key", &ad->id, "secret_key", &ad->secret, "access_token", &ad->token, "host_base", &host_base, NULL); if (ad->id.l == 0) parse_simple("~/.awssecret", &ad->id, &ad->secret); if (host_base.l == 0) kputs("s3.amazonaws.com", &host_base); // Use virtual hosted-style access if possible, otherwise path-style. if (is_dns_compliant(bucket, path)) { kputsn(bucket, path - bucket, &url); kputc('.', &url); kputs(host_base.s, &url); } else { kputs(host_base.s, &url); kputc('/', &url); kputsn(bucket, path - bucket, &url); } kputs(path, &url); if (ad->token.l > 0) { kputs("X-Amz-Security-Token: ", &token_hdr); kputs(ad->token.s, &token_hdr); *header++ = token_hdr.s; } ad->bucket = strdup(bucket); if (!ad->bucket) goto fail; *header = NULL; hFILE *fp = hopen(url.s, mode, "va_list", argsp, "httphdr:v", header_list, "httphdr_callback", auth_header_callback, "httphdr_callback_data", ad, NULL); if (!fp) goto fail; free(url.s); free(profile.s); free(host_base.s); free(token_hdr.s); return fp; fail: free(url.s); free(profile.s); free(host_base.s); free(token_hdr.s); free_auth_data(ad); return NULL; }
/* * Loads a CRAM .crai index into memory. * * Returns 0 for success * -1 for failure */ int cram_index_load(cram_fd *fd, const char *fn) { char fn2[PATH_MAX]; char buf[65536]; ssize_t len; kstring_t kstr = {0}; hFILE *fp; cram_index *idx; cram_index **idx_stack = NULL, *ep, e; int idx_stack_alloc = 0, idx_stack_ptr = 0; size_t pos = 0; /* Check if already loaded */ if (fd->index) return 0; fd->index = calloc((fd->index_sz = 1), sizeof(*fd->index)); if (!fd->index) return -1; idx = &fd->index[0]; idx->refid = -1; idx->start = INT_MIN; idx->end = INT_MAX; idx_stack = calloc(++idx_stack_alloc, sizeof(*idx_stack)); idx_stack[idx_stack_ptr] = idx; sprintf(fn2, "%s.crai", fn); if (!(fp = hopen(fn2, "r"))) { perror(fn2); free(idx_stack); return -1; } // Load the file into memory while ((len = hread(fp, buf, 65536)) > 0) kputsn(buf, len, &kstr); if (len < 0 || kstr.l < 2) { if (kstr.s) free(kstr.s); free(idx_stack); return -1; } if (hclose(fp)) { if (kstr.s) free(kstr.s); free(idx_stack); return -1; } // Uncompress if required if (kstr.s[0] == 31 && (uc)kstr.s[1] == 139) { size_t l; char *s = zlib_mem_inflate(kstr.s, kstr.l, &l); free(kstr.s); if (!s) { free(idx_stack); return -1; } kstr.s = s; kstr.l = l; kstr.m = l; // conservative estimate of the size allocated kputsn("", 0, &kstr); // ensure kstr.s is NUL-terminated } // Parse it line at a time do { /* 1.1 layout */ if (kget_int32(&kstr, &pos, &e.refid) == -1) { free(kstr.s); free(idx_stack); return -1; } if (kget_int32(&kstr, &pos, &e.start) == -1) { free(kstr.s); free(idx_stack); return -1; } if (kget_int32(&kstr, &pos, &e.end) == -1) { free(kstr.s); free(idx_stack); return -1; } if (kget_int64(&kstr, &pos, &e.offset) == -1) { free(kstr.s); free(idx_stack); return -1; } if (kget_int32(&kstr, &pos, &e.slice) == -1) { free(kstr.s); free(idx_stack); return -1; } if (kget_int32(&kstr, &pos, &e.len) == -1) { free(kstr.s); free(idx_stack); return -1; } e.end += e.start-1; //printf("%d/%d..%d\n", e.refid, e.start, e.end); if (e.refid < -1) { free(kstr.s); free(idx_stack); fprintf(stderr, "Malformed index file, refid %d\n", e.refid); return -1; } if (e.refid != idx->refid) { if (fd->index_sz < e.refid+2) { size_t index_end = fd->index_sz * sizeof(*fd->index); fd->index_sz = e.refid+2; fd->index = realloc(fd->index, fd->index_sz * sizeof(*fd->index)); memset(((char *)fd->index) + index_end, 0, fd->index_sz * sizeof(*fd->index) - index_end); } idx = &fd->index[e.refid+1]; idx->refid = e.refid; idx->start = INT_MIN; idx->end = INT_MAX; idx->nslice = idx->nalloc = 0; idx->e = NULL; idx_stack[(idx_stack_ptr = 0)] = idx; } while (!(e.start >= idx->start && e.end <= idx->end)) { idx = idx_stack[--idx_stack_ptr]; } // Now contains, so append if (idx->nslice+1 >= idx->nalloc) { idx->nalloc = idx->nalloc ? idx->nalloc*2 : 16; idx->e = realloc(idx->e, idx->nalloc * sizeof(*idx->e)); } e.nalloc = e.nslice = 0; e.e = NULL; *(ep = &idx->e[idx->nslice++]) = e; idx = ep; if (++idx_stack_ptr >= idx_stack_alloc) { idx_stack_alloc *= 2; idx_stack = realloc(idx_stack, idx_stack_alloc*sizeof(*idx_stack)); } idx_stack[idx_stack_ptr] = idx; while (pos < kstr.l && kstr.s[pos] != '\n') pos++; pos++; } while (pos < kstr.l); free(idx_stack); free(kstr.s); // dump_index(fd); return 0; }
/*-------------------------------------------------------------------*/ int main (int argc, char *argv[]) { char *pgm; /* -> Program name */ int ckddasd=-1; /* 1=CKD 0=FBA */ int rc; /* Return code */ int quiet=0; /* 1=Don't display status */ int comp=255; /* Compression algorithm */ int cyls=-1, blks=-1; /* Size of output file */ int lfs=0; /* 1=Create 1 large file */ int alt=0; /* 1=Create alt cyls */ int r=0; /* 1=Replace output file */ int in=0, out=0; /* Input/Output file types */ int fd; /* Input file descriptor */ char *ifile, *ofile; /* -> Input/Output file names*/ char *sfile=NULL; /* -> Input shadow file name */ CIFBLK *icif, *ocif; /* -> Input/Output CIFBLK */ DEVBLK *idev, *odev; /* -> Input/Output DEVBLK */ CKDDEV *ckd=NULL; /* -> CKD device table entry */ FBADEV *fba=NULL; /* -> FBA device table entry */ int i, n, max; /* Loop index, limits */ BYTE unitstat; /* Device unit status */ char msgbuf[512]; /* Message buffer */ size_t fba_bytes_remaining=0; /* FBA bytes to be copied */ int nullfmt = CKDDASD_NULLTRK_FMT0; /* Null track format */ char pathname[MAX_PATH]; /* file path in host format */ char pgmpath[MAX_PATH]; /* prog path in host format */ INITIALIZE_UTILITY("dasdcopy"); /* Figure out processing based on the program name */ hostpath(pgmpath, argv[0], sizeof(pgmpath)); pgm = strrchr (pgmpath, '/'); if (pgm) pgm++; else pgm = argv[0]; strtok (pgm, "."); if (strcmp(pgm, "ckd2cckd") == 0) { in = CKD; out = CCKD; } else if (strcmp(pgm, "cckd2ckd") == 0) { in = CCKD; out = CKD; } else if (strcmp(pgm, "fba2cfba") == 0) { in = FBA; out = CFBA; } else if (strcmp(pgm, "cfba2fba") == 0) { in = CFBA; out = FBA; } /* Process the arguments */ for (argc--, argv++ ; argc > 0 ; argc--, argv++) { if (argv[0][0] != '-') break; if (strcmp(argv[0], "-v") == 0) { snprintf (msgbuf, 512, _("Hercules %s copy program "), pgm); display_version (stderr, msgbuf, FALSE); return 0; } else if (strcmp(argv[0], "-h") == 0) { syntax(pgm); return 0; } else if (strcmp(argv[0], "-q") == 0 || strcmp(argv[0], "-quiet") == 0) quiet = 1; else if (strcmp(argv[0], "-r") == 0) r = 1; #ifdef CCKD_COMPRESS_ZLIB else if (strcmp(argv[0], "-z") == 0) comp = CCKD_COMPRESS_ZLIB; #endif #ifdef CCKD_COMPRESS_BZIP2 else if (strcmp(argv[0], "-bz2") == 0) comp = CCKD_COMPRESS_BZIP2; #endif else if (strcmp(argv[0], "-0") == 0) comp = CCKD_COMPRESS_NONE; else if ((strcmp(argv[0], "-cyl") == 0 || strcmp(argv[0], "-cyls") == 0) && cyls < 0) { if (argc < 2 || (cyls = atoi(argv[1])) < 0) return syntax(pgm); argc--; argv++; } else if ((strcmp(argv[0], "-blk") == 0 || strcmp(argv[0], "-blks") == 0) && blks < 0) { if (argc < 2 || (blks = atoi(argv[1])) < 0) return syntax(pgm); argc--; argv++; } else if (strcmp(argv[0], "-a") == 0 || strcmp(argv[0], "-alt") == 0 || strcmp(argv[0], "-alts") == 0) alt = 1; else if (strcmp(argv[0], "-lfs") == 0) lfs = 1; else if (out == 0 && strcmp(argv[0], "-o") == 0) { if (argc < 2 || out != 0) return syntax(pgm); if (strcasecmp(argv[1], "ckd") == 0) out = CKD; else if (strcasecmp(argv[1], "cckd") == 0) out = CCKD; else if (strcasecmp(argv[1], "fba") == 0) out = FBA; else if (strcasecmp(argv[1], "cfba") == 0) out = CFBA; else return syntax(pgm); argc--; argv++; } else return syntax(pgm); } /* Get the file names: input-file [sf=shadow-file] output-file */ if (argc < 2 || argc > 3) return syntax(pgm); ifile = argv[0]; if (argc < 3) ofile = argv[1]; else { if (strlen(argv[1]) < 4 || memcmp(argv[1], "sf=", 3)) return syntax(pgm); sfile = argv[1]; ofile = argv[2]; } /* If we don't know what the input file is then find out */ if (in == 0) { BYTE buf[8]; hostpath(pathname, ifile, sizeof(pathname)); fd = hopen(pathname, O_RDONLY|O_BINARY); if (fd < 0) { fprintf (stderr, _("HHCDC001E %s: %s open error: %s\n"), pgm, ifile, strerror(errno)); return -1; } rc = read (fd, buf, 8); if (rc < 8) { fprintf (stderr, _("HHCDC002E %s: %s read error: %s\n"), pgm, ifile, strerror(errno)); return -1; } if (memcmp(buf, "CKD_P370", 8) == 0) in = CKD; else if (memcmp(buf, "CKD_C370", 8) == 0) in = CCKD; else if (memcmp(buf, "FBA_C370", 8) == 0) in = CFBA; else in = FBA; close (fd); } /* If we don't know what the output file type is then derive it from the input file type */ if (out == 0) { switch (in) { case CKD: if (!lfs) out = CCKD; else out = CKD; break; case CCKD: if (comp == 255) out = CKD; else out = CCKD; break; case FBA: if (!lfs) out = CFBA; else out = FBA; break; case CFBA: if (comp == 255) out = FBA; else out = CFBA; break; } } /* Set default compression if out file is to be compressed */ if (comp == 255 && (out & COMPMASK)) #ifdef CCKD_COMPRESS_ZLIB comp = CCKD_COMPRESS_ZLIB; #else comp = CCKD_COMPRESS_NONE; #endif /* Perform sanity checks on the options */ if ((in & CKDMASK) && !(out & CKDMASK)) return syntax(pgm); if ((in & FBAMASK) && !(out & FBAMASK)) return syntax(pgm); if (sfile && !(in & COMPMASK)) return syntax(pgm); if (comp != 255 && !(out & COMPMASK)) return syntax(pgm); if (lfs && (out & COMPMASK)) return syntax(pgm); if (cyls >= 0 && !(in & CKDMASK)) return syntax(pgm); if (blks >= 0 && !(in & FBAMASK)) return syntax(pgm); if (!(in & CKDMASK) && alt) return syntax(pgm); /* Set the type of processing (ckd or fba) */ ckddasd = (in & CKDMASK); /* Open the input file */ if (ckddasd) icif = open_ckd_image (ifile, sfile, O_RDONLY|O_BINARY, 0); else icif = open_fba_image (ifile, sfile, O_RDONLY|O_BINARY, 0); if (icif == NULL) { fprintf (stderr, _("HHCDC003E %s: %s open failed\n"), pgm, ifile); return -1; } idev = &icif->devblk; if (idev->oslinux) nullfmt = CKDDASD_NULLTRK_FMT2; /* Calculate the number of tracks or blocks to copy */ if (ckddasd) { if (cyls < 0) cyls = idev->ckdcyls; else if (cyls == 0) cyls = (idev->hnd->used)(idev); ckd = dasd_lookup (DASD_CKDDEV, NULL, idev->devtype, cyls); if (ckd == NULL) { fprintf (stderr, _("HHCDC004E %s: ckd lookup failed for %4.4X " "cyls %d\n"), pgm, idev->devtype, cyls); close_image_file (icif); return -1; } if (out == CCKD) cyls = ckd->cyls; if (cyls <= ckd->cyls && alt) cyls = ckd->cyls + ckd->altcyls; n = cyls * idev->ckdheads; max = idev->ckdtrks; if (max < n && out == CCKD) n = max; } else { fba_bytes_remaining = idev->fbanumblk * idev->fbablksiz; if (blks < 0) blks = idev->fbanumblk; else if (blks == 0) blks = (idev->hnd->used)(idev); fba = dasd_lookup (DASD_FBADEV, NULL, idev->devtype, blks); if (fba == NULL) { fprintf (stderr, _("HHCDC005E %s: fba lookup failed, blks %d\n"), pgm, blks); close_image_file (icif); return -1; } n = blks; max = idev->fbanumblk; if (max < n && out == CFBA) n = max; n = (n + FBA_BLKS_PER_GRP - 1) / FBA_BLKS_PER_GRP; max = (max + FBA_BLKS_PER_GRP - 1) / FBA_BLKS_PER_GRP; } /* Create the output file */ if (ckddasd) rc = create_ckd(ofile, idev->devtype, idev->ckdheads, ckd->r1, cyls, "", comp, lfs, 1+r, nullfmt, 0); else rc = create_fba(ofile, idev->devtype, fba->size, blks, "", comp, lfs, 1+r, 0); if (rc < 0) { fprintf (stderr, _("HHCDC006E %s: %s create failed\n"), pgm, ofile); close_image_file (icif); return -1; } /* Open the output file */ if (ckddasd) ocif = open_ckd_image (ofile, NULL, O_RDWR|O_BINARY, 1); else ocif = open_fba_image (ofile, NULL, O_RDWR|O_BINARY, 1); if (ocif == NULL) { fprintf (stderr, _("HHCDC007E %s: %s open failed\n"), pgm, ofile); close_image_file (icif); return -1; } odev = &ocif->devblk; /* Copy the files */ #ifdef EXTERNALGUI if (extgui) /* Notify GUI of total #of tracks or blocks being copied... */ fprintf (stderr, "TRKS=%d\n", n); else #endif /*EXTERNALGUI*/ if (!quiet) printf (" %3d%% %7d of %d", 0, 0, n); for (i = 0; i < n; i++) { /* Read a track or block */ if (ckddasd) { if (i < max) rc = (idev->hnd->read)(idev, i, &unitstat); else { memset (idev->buf, 0, idev->ckdtrksz); rc = nulltrk(idev->buf, i, idev->ckdheads, nullfmt); } } else { if (i < max) rc = (idev->hnd->read)(idev, i, &unitstat); else memset (idev->buf, 0, FBA_BLKGRP_SIZE); rc = 0; } if (rc < 0) { fprintf (stderr, _("HHCDC008E %s: %s read error %s %d " "stat=%2.2X, null %s substituted\n"), pgm, ifile, ckddasd ? "track" : "block", i, unitstat, ckddasd ? "track" : "block"); if (ckddasd) nulltrk(idev->buf, i, idev->ckdheads, nullfmt); else memset (idev->buf, 0, FBA_BLKGRP_SIZE); if (!quiet) { printf (_(" %3d%% %7d of %d"), 0, 0, n); status (i, n); } } /* Write the track or block just read */ if (ckddasd) { rc = (odev->hnd->write)(odev, i, 0, idev->buf, idev->ckdtrksz, &unitstat); } else { if (fba_bytes_remaining >= (size_t)idev->buflen) { rc = (odev->hnd->write)(odev, i, 0, idev->buf, idev->buflen, &unitstat); fba_bytes_remaining -= (size_t)idev->buflen; } else { ASSERT(fba_bytes_remaining > 0 && (i+1) >= n); rc = (odev->hnd->write)(odev, i, 0, idev->buf, (int)fba_bytes_remaining, &unitstat); fba_bytes_remaining = 0; } } if (rc < 0) { fprintf (stderr, _("HHCDC009E %s: %s write error %s %d " "stat=%2.2X\n"), pgm, ofile, ckddasd ? "track" : "block", i, unitstat); close_image_file(icif); close_image_file(ocif); return -1; } /* Update the status indicator */ if (!quiet) status (i+1, n); } close_image_file(icif); close_image_file(ocif); if (!quiet) printf (_("\r")); printf (_("HHCDC010I %s successfully completed.\n"), pgm); return 0; }
/*-------------------------------------------------------------------*/ int main (int argc, char *argv[]) { int i; /* Array subscript */ int len; /* Length of actual read */ char *filename; /* -> Input file name */ int infd = -1; /* Input file descriptor */ DEVMAP_CTLR controller; /* Controller record */ DEVMAP_DEV device; /* Device record */ char output_type[5]; /* Device type to print */ char *output_filename; /* -> filename to print */ int more_devices; /* More devices this ctlr? */ char pathname[MAX_PATH]; /* file path in host format */ INITIALIZE_UTILITY("dmap2hrc"); /* Display the program identification message */ display_version (stderr, "P/390 DEVMAP to Hercules conversion program\n", FALSE); /* The only argument is the DEVMAP file name */ if (argc == 2 && argv[1] != NULL) { filename = argv[1]; } else { fprintf (stderr,"Usage: dmap2hrc filename\n"); exit (1); } /* Open the devmap file */ hostpath(pathname, filename, sizeof(pathname)); infd = hopen(pathname, O_RDONLY | O_BINARY); if (infd < 0) { fprintf (stderr,"dmap2hrc: Error opening %s: %s\n", filename, strerror(errno)); exit (2); } /* Skip the file header */ for (i = 0; i < 9; i++) { len = read (infd, (void *)&controller, sizeof(DEVMAP_CTLR)); if (len < 0) { fprintf (stderr, "dmap2hrc: error reading header records from %s: %s\n", filename, strerror(errno)); exit (3); } } /* Read records from the input file and convert them */ while (1) { /* Read a controller record. */ len = read (infd, (void *)&controller, sizeof(DEVMAP_CTLR)); if (len < 0) { fprintf (stderr, "dmap2hrc: error reading controller record from %s:" " %s\n", filename, strerror(errno)); exit (4); } /* Did we finish too soon? */ if ((len > 0) && (len < (int)sizeof(DEVMAP_CTLR))) { fprintf (stderr, "dmap2hrc: incomplete controller record on %s\n", filename); exit(5); } /* Check for end of file. */ if (len == 0) { fprintf(stderr, "End of input file.\n"); break; } /* Read devices on this controller. */ more_devices = 1; while (more_devices) { /* Read a device record. */ len = read (infd, (void *)&device, sizeof(DEVMAP_DEV)); if (len < 0) { fprintf (stderr, "dmap2hrc: error reading device record from %s:" " %s\n", filename, strerror(errno)); exit (6); } /* Did we finish too soon? */ if ((len > 0) && (len < (int)sizeof(DEVMAP_DEV))) { fprintf (stderr, "dmap2hrc: incomplete device record on %s\n", filename); exit(7); } /* Check for end of file. */ if (len == 0) { fprintf (stderr,"dmap2hrc: premature end of input file\n"); exit(8); } /* Is this the dummy device record at the end of the controller's set of devices? */ if (strncmp(device.type," ",4) == 0) { more_devices = 0; break; } /* It's a real device. Fix the type so Hercules can use it and locate the output filename. */ strncpy(output_type, device.type, 4); output_type[4] = '\0'; if (isprint(device.parms.disk.volser[0])) output_filename = device.parms.disk.filename; else output_filename = device.parms.other.filename; if (strncmp(device.type, "3278", 4) == 0) { strcpy(output_type, "3270"); output_filename = ""; } if (strncmp(device.type, "2540", 4) == 0) strcpy(output_type, "3505"); /* Emit the Hercules config file entry. */ printf("%02X%02X %s", device.highaddr, device.lowaddr, output_type); if (strlen(output_filename) > 0) printf(" %s", output_filename); puts(""); /* newline */ } /* end while more_devices) */ } /* end while (1) */ /* Close files and exit */ close (infd); return 0; } /* end function main */
/*-------------------------------------------------------------------*/ int main (int argc, char *argv[]) { int i; /* Index */ int rc; /* Return code */ int level=1; /* Chkdsk level checking */ int ro=0; /* 1=Open readonly */ int force=0; /* 1=Check if OPENED bit on */ CCKDDASD_DEVHDR cdevhdr; /* Compressed CKD device hdr */ DEVBLK devblk; /* DEVBLK */ DEVBLK *dev=&devblk; /* -> DEVBLK */ INITIALIZE_UTILITY("cckdcdsk"); /* parse the arguments */ for (argc--, argv++ ; argc > 0 ; argc--, argv++) { if(**argv != '-') break; switch(argv[0][1]) { case '0': case '1': case '2': case '3': case '4': if (argv[0][2] != '\0') return syntax (); level = (argv[0][1] & 0xf); break; case 'f': if (argv[0][2] != '\0') return syntax (); force = 1; break; case 'r': if (argv[0][2] == 'o' && argv[0][3] == '\0') ro = 1; else return syntax (); break; case 'v': if (argv[0][2] != '\0') return syntax (); display_version (stderr, "Hercules cckd chkdsk program ", FALSE); return 0; default: return syntax (); } } if (argc < 1) return syntax (); for (i = 0; i < argc; i++) { memset (dev, 0, sizeof(DEVBLK)); dev->batch = 1; /* open the file */ hostpath(dev->filename, argv[i], sizeof(dev->filename)); dev->fd = hopen(dev->filename, ro ? O_RDONLY|O_BINARY : O_RDWR|O_BINARY); if (dev->fd < 0) { cckdumsg (dev, 700, "open error: %s\n", strerror(errno)); continue; } /* Check CCKD_OPENED bit if -f not specified */ if (!force) { if (lseek (dev->fd, CCKD_DEVHDR_POS, SEEK_SET) < 0) { cckdumsg (dev, 702, "lseek error offset 0x%" I64_FMT "x: %s\n", (long long)CCKD_DEVHDR_POS, strerror(errno)); close (dev->fd); continue; } if ((rc = read (dev->fd, &cdevhdr, CCKD_DEVHDR_SIZE)) < CCKD_DEVHDR_SIZE) { cckdumsg (dev, 703, "read error rc=%d offset 0x%" I64_FMT "x len %d: %s\n", rc, (long long)CCKD_DEVHDR_POS, CCKD_DEVHDR_SIZE, rc < 0 ? strerror(errno) : "incomplete"); close (dev->fd); continue; } if (cdevhdr.options & CCKD_OPENED) { cckdumsg (dev, 707, "OPENED bit is on, use -f\n"); close (dev->fd); continue; } } /* if (!force) */ rc = cckd_chkdsk (dev, level); close (dev->fd); } /* for each arg */ return 0; }
/*-------------------------------------------------------------------*/ int open_omatape (DEVBLK *dev, BYTE *unitstat,BYTE code) { int fd; /* File descriptor integer */ int rc; /* Return code */ OMATAPE_DESC *omadesc; /* -> OMA descriptor entry */ char pathname[MAX_PATH]; /* file path in host format */ /* Check for no tape in drive */ if (!strcmp (dev->filename, TAPE_UNLOADED)) { build_senseX(TAPE_BSENSE_TAPEUNLOADED,dev,unitstat,code); return -1; } /* Read the OMA descriptor file if necessary */ if (dev->omadesc == NULL) { rc = read_omadesc (dev); if (rc < 0) { build_senseX(TAPE_BSENSE_TAPELOADFAIL,dev,unitstat,code); return -1; } dev->blockid = 0; } dev->fenced = 0; /* Unit exception if beyond end of tape */ /* ISW: CHANGED PROCESSING - RETURN UNDEFINITE Tape Marks */ /* NOTE: The last entry in the TDF table is ALWAYS */ /* an EOT Condition */ /* This is ensured by the TDF reading routine */ #if 0 if (dev->curfilen >= dev->omafiles) { logmsg (_("HHCTA250E %4.4X: Attempt to access beyond end of tape %s\n"), dev->devnum, dev->filename); build_senseX(TAPE_BSENSE_ENDOFTAPE,dev,unitstat,code); return -1; } #else if(dev->curfilen>dev->omafiles) { dev->curfilen=dev->omafiles; return(0); } #endif /* Point to the current file entry in the OMA descriptor table */ omadesc = (OMATAPE_DESC*)(dev->omadesc); omadesc += (dev->curfilen-1); if(omadesc->format=='X') { return 0; } if(omadesc->format=='E') { return 0; } /* Open the OMATAPE file */ hostpath(pathname, omadesc->filename, sizeof(pathname)); fd = hopen(pathname, O_RDONLY | O_BINARY); /* Check for successful open */ if (fd < 0 || lseek (fd, 0, SEEK_END) > LONG_MAX) { if (fd >= 0) /* (if open was successful, then it) */ errno = EOVERFLOW; /* (must have been a lseek overflow) */ logmsg (_("HHCTA251E %4.4X: Error opening %s: %s\n"), dev->devnum, omadesc->filename, strerror(errno)); if (fd >= 0) close(fd); /* (close the file if it was opened) */ build_senseX(TAPE_BSENSE_TAPELOADFAIL,dev,unitstat,code); return -1; } /* OMA tapes are always read-only */ dev->readonly = 1; /* Store the file descriptor in the device block */ dev->fd = fd; return 0; } /* end function open_omatape */
HTTPRESULT *http_read(char *url, int maxlen) { HTTPRESULT *result = http_new_result(); if ( strncmp(url,"http://",7)==0 ) { HTTP *fp = hopen(url,maxlen); if ( fp==NULL ) { output_error("http_read('%s'): unable to access url", url); result->status = errno; } else { size_t len = (int)hfilelength(fp); size_t hlen = len; char *buffer = (char*)malloc(len+1); char *data = NULL; if ( hread(buffer,len,fp)<=0 ) { output_error("http_read('%s'): unable to read file", url); result->status = errno; } else { buffer[len]='\0'; data = strstr(buffer,"\r\n\r\n"); if ( data!=NULL ) { hlen = data - buffer; *data++ = '\0'; result->body.size = (int)(len - hlen - 1); while ( isspace(*data) ) { data++; result->body.size--; } // if ( sscanf(data,"%x",&dlen)==1 ) // { // data = strchr(data,'\n'); // while ( isspace(*data) ) {data++;} // result->body.size = dlen; // result->body.data = malloc(dlen+1); // } // else // { result->body.data = malloc(result->body.size+1); // } memcpy(result->body.data,data,result->body.size); result->body.data[result->body.size] = '\0'; } else { result->body.data = ""; result->body.size = 0; } result->header.size = (int)hlen; result->header.data = malloc(hlen+1); strcpy(result->header.data,buffer); result->status = 0; } } hclose(fp); } else if ( strncmp(url,"file://",7)==0 ) { FILE *fp = fopen(url+7,"rt"); if ( fp==NULL ) { output_error("http_read('%s'): unable to access file", url); result->status = errno; } else { result->body.size = filelength(fileno(fp))+1; result->body.data = malloc(result->body.size); memset(result->body.data,0,result->body.size); if ( fread(result->body.data,1,result->body.size,fp)<=0 ) { output_error("http_read('%s'): unable to read file", url); result->status = errno; } else result->status = 0; } fclose(fp); } return result; }
/*-------------------------------------------------------------------*/ int main (int argc, char *argv[]) { int i; /* Array subscript */ int len; /* Block length */ int prevlen; /* Previous block length */ char *filename; /* -> Input file name */ int infd = -1; /* Input file descriptor */ int fileno; /* Tape file number */ int blkcount; /* Block count */ int curblkl; /* Current block length */ int minblksz; /* Minimum block size */ int maxblksz; /* Maximum block size */ BYTE labelrec[81]; /* Standard label (ASCIIZ) */ AWSTAPE_BLKHDR awshdr; /* AWSTAPE block header */ char pathname[MAX_PATH]; /* file path in host format */ INITIALIZE_UTILITY("tapemap"); /* Display the program identification message */ display_version (stderr, "Hercules tape map program ", FALSE); /* The only argument is the tape image file name */ if (argc == 2 && argv[1] != NULL) { filename = argv[1]; } else { printf ("Usage: tapemap filename\n"); exit (1); } /* Open the tape device */ hostpath(pathname, filename, sizeof(pathname)); infd = hopen(pathname, O_RDONLY | O_BINARY); if (infd < 0) { printf ("tapemap: Error opening %s: %s\n", filename, strerror(errno)); exit (2); } /* Read blocks from the input file and report on them */ fileno = 1; blkcount = 0; minblksz = 0; maxblksz = 0; len = 0; while (1) { #ifdef EXTERNALGUI if (extgui) { /* Report progress every nnnK */ if( ( curpos & PROGRESS_MASK ) != ( prevpos & PROGRESS_MASK ) ) { prevpos = curpos; fprintf( stderr, "IPOS=%ld\n", curpos ); } } #endif /*EXTERNALGUI*/ /* Save previous block length */ prevlen = len; /* Read a block from the tape */ len = read (infd, buf, sizeof(AWSTAPE_BLKHDR)); #ifdef EXTERNALGUI if (extgui) curpos += len; #endif /*EXTERNALGUI*/ if (len < 0) { printf ("tapemap: error reading header block from %s: %s\n", filename, strerror(errno)); exit (3); } /* Did we finish too soon? */ if ((len > 0) && (len < (int)sizeof(AWSTAPE_BLKHDR))) { printf ("tapemap: incomplete block header on %s\n", filename); exit(4); } /* Check for end of tape. */ if (len == 0) { printf ("End of tape.\n"); break; } /* Parse the block header */ memcpy(&awshdr, buf, sizeof(AWSTAPE_BLKHDR)); /* Tapemark? */ if ((awshdr.flags1 & AWSTAPE_FLAG1_TAPEMARK) != 0) { /* Print summary of current file */ printf ("File %u: Blocks=%u, block size min=%u, max=%u\n", fileno, blkcount, minblksz, maxblksz); /* Reset counters for next file */ fileno++; minblksz = 0; maxblksz = 0; blkcount = 0; } else /* if(tapemark) */ { /* Count blocks and block sizes */ blkcount++; curblkl = awshdr.curblkl[0] + (awshdr.curblkl[1] << 8); if (curblkl > maxblksz) maxblksz = curblkl; if (minblksz == 0 || curblkl < minblksz) minblksz = curblkl; /* Read the data block. */ len = read (infd, buf, curblkl); #ifdef EXTERNALGUI if (extgui) curpos += len; #endif /*EXTERNALGUI*/ if (len < 0) { printf ("tapemap: error reading data block from %s: %s\n", filename, strerror(errno)); exit (5); } /* Did we finish too soon? */ if ((len > 0) && (len < curblkl)) { printf ("tapemap: incomplete final data block on %s, " "expected %d bytes, got %d\n", filename, curblkl, len); exit(6); } /* Check for end of tape */ if (len == 0) { printf ("tapemap: header block with no data on %s\n", filename); exit(7); } /* Print standard labels */ if (len == 80 && blkcount < 4 && (memcmp(buf, vollbl, 3) == 0 || memcmp(buf, hdrlbl, 3) == 0 || memcmp(buf, eoflbl, 3) == 0 || memcmp(buf, eovlbl, 3) == 0)) { for (i=0; i < 80; i++) labelrec[i] = guest_to_host(buf[i]); labelrec[i] = '\0'; printf ("%s\n", labelrec); } } /* end if(tapemark) */ } /* end while */ /* Close files and exit */ close (infd); return 0; } /* end function main */
/*-------------------------------------------------------------------*/ static void convert_ckd_file (IFD ifd, char *ifname, int itrklen, BYTE *itrkbuf, int repl, int quiet, char *ofname, int fseqn, U16 devtype, U32 heads, U32 trksize, BYTE *obuf, U32 start, U32 end, U32 volcyls, BYTE *volser) { int rc; /* Return code */ int ofd; /* Output file descriptor */ CKDDASD_DEVHDR devhdr; /* Output device header */ CKDDASD_TRKHDR *trkhdr; /* -> Output track header */ CKDDASD_RECHDR *rechdr; /* -> Output record header */ U32 cyl; /* Cylinder number */ U32 head; /* Head number */ int fileseq; /* CKD header sequence number*/ int highcyl; /* CKD header high cyl number*/ BYTE *opos; /* -> Byte in output buffer */ BYTE klen; /* Key length */ U16 dlen; /* Data length */ BYTE rec; /* Record number */ BYTE *iptr; /* -> Byte in input buffer */ BYTE *kptr; /* -> Key in input buffer */ BYTE *dptr; /* -> Data in input buffer */ int ilen; /* Bytes left in input buffer*/ H30CKD_TRKHDR *ith; /* -> Input track header */ U32 ihc, ihh; /* Input trk header cyl,head */ U32 offset; /* Current input file offset */ char pathname[MAX_PATH]; /* file path in host format */ UNREFERENCED(volser); /* Set file sequence number to zero if this is the only file */ if (fseqn == 1 && end + 1 == volcyls) fileseq = 0; else fileseq = fseqn; /* Set high cylinder number to zero if this is the last file */ if (end + 1 == volcyls) highcyl = 0; else highcyl = end; /* Create the AWSCKD image file */ hostpath(pathname, (char *)ofname, sizeof(pathname)); ofd = hopen(pathname, O_WRONLY | O_CREAT | O_BINARY | (repl ? 0 : O_EXCL), S_IRUSR | S_IWUSR | S_IRGRP); if (ofd < 0) { fprintf (stderr, "%s open error: %s\n", ofname, strerror(errno)); EXIT(8); } /* Create the device header */ memset(&devhdr, 0, CKDDASD_DEVHDR_SIZE); memcpy(devhdr.devid, "CKD_P370", 8); devhdr.heads[3] = (heads >> 24) & 0xFF; devhdr.heads[2] = (heads >> 16) & 0xFF; devhdr.heads[1] = (heads >> 8) & 0xFF; devhdr.heads[0] = heads & 0xFF; devhdr.trksize[3] = (trksize >> 24) & 0xFF; devhdr.trksize[2] = (trksize >> 16) & 0xFF; devhdr.trksize[1] = (trksize >> 8) & 0xFF; devhdr.trksize[0] = trksize & 0xFF; devhdr.devtype = devtype & 0xFF; devhdr.fileseq = fileseq; devhdr.highcyl[1] = (highcyl >> 8) & 0xFF; devhdr.highcyl[0] = highcyl & 0xFF; /* Write the device header */ rc = write (ofd, &devhdr, CKDDASD_DEVHDR_SIZE); if (rc < CKDDASD_DEVHDR_SIZE) { fprintf (stderr, "%s device header write error: %s\n", ofname, errno ? strerror(errno) : "incomplete"); EXIT(1); } /* Write each cylinder */ for (cyl = start; cyl <= end; cyl++) { /* Display progress message every 10 cylinders */ if ((cyl % 10) == 0) { #ifdef EXTERNALGUI if (extgui) fprintf (stderr, "CYL=%u\n", cyl); else #endif /*EXTERNALGUI*/ if (quiet == 0) fprintf (stderr, "Writing cylinder %u\r", cyl); } for (head = 0; head < heads; head++) { /* Calculate the current offset in the file */ offset = ((cyl*heads)+head)*itrklen; /* Read the input track image (except cyl 0 head 0 already read by the open_input_image procedure) */ if (cyl > 0 || head > 0) { read_input_data (ifd, ifname, itrkbuf, itrklen, offset); } /* end if(cyl>0||head>0) */ /* Validate the track header */ ith = (H30CKD_TRKHDR*)itrkbuf; FETCH_HW (ihc, ith->cyl); FETCH_HW (ihh, ith->head); if (ihc != cyl || ihh != head) { fprintf (stderr, "Invalid track header found at offset %8.8X\n" "in input file %s\n" "Expected cyl=%4.4X head=%4.4X\n" " Found cyl=%4.4X head=%4.4X\n", offset, ifname, cyl, head, ihc, ihh); EXIT(8); } /* Clear the output track image to zeroes */ memset (obuf, 0, trksize); /* Build the output track header */ trkhdr = (CKDDASD_TRKHDR*)obuf; trkhdr->bin = 0; STORE_HW (trkhdr->cyl, cyl); STORE_HW (trkhdr->head, head); opos = obuf + CKDDASD_TRKHDR_SIZE; /* Copy each record from the input buffer */ iptr = itrkbuf + H30CKD_TRKHDR_SIZE; ilen = itrklen - H30CKD_TRKHDR_SIZE; while (1) { /* Locate the next input record */ rc = find_input_record (itrkbuf, &iptr, &ilen, &klen, &kptr, &dlen, &dptr, &ihc, &ihh, &rec); /* Exit at end of track */ if (rc == 1) break; /* Error if invalid record header detected */ if (rc > 1) { fprintf (stderr, "Invalid record header (reason %d)\n" "at offset %4.4X" " in track at cyl %4.4X head %4.4X\n" "at offset %8.8X in input file %s\n", rc, (unsigned int)(iptr-itrkbuf), cyl, head, offset, ifname); EXIT(9); } /* Build AWSCKD record header in output buffer */ rechdr = (CKDDASD_RECHDR*)opos; opos += CKDDASD_RECHDR_SIZE; STORE_HW (rechdr->cyl, ihc); STORE_HW (rechdr->head, ihh); rechdr->rec = rec; rechdr->klen = klen; STORE_HW (rechdr->dlen, dlen); /* Copy key and data to output buffer */ if (klen != 0) { memcpy (opos, kptr, klen); opos += klen; } if (dlen != 0) { memcpy (opos, dptr, dlen); opos += dlen; } } /* end while */ /* Build the end of track marker */ memcpy (opos, eighthexFF, 8); /* Write the track to the file */ rc = write (ofd, obuf, trksize); if (rc < 0 || (U32)rc < trksize) { fprintf (stderr, "%s cylinder %u head %u write error: %s\n", ofname, cyl, head, errno ? strerror(errno) : "incomplete"); EXIT(1); } } /* end for(head) */ } /* end for(cyl) */ /* Close the AWSCKD image file */ rc = close (ofd); if (rc < 0) { fprintf (stderr, "%s close error: %s\n", ofname, strerror(errno)); EXIT(10); } /* Display completion message */ fprintf (stderr, "%u cylinders successfully written to file %s\n", cyl - start, ofname); } /* end function convert_ckd_file */
/*-------------------------------------------------------------------*/ static IFD open_input_image (char *ifname, U16 *devt, U32 *vcyls, U32 *itrkl, BYTE **itrkb, BYTE *volser) { int rc; /* Return code */ H30CKD_TRKHDR h30trkhdr; /* Input track header */ IFD ifd; /* Input file descriptor */ int len; /* Length of input */ U16 code; /* Device type code */ U16 dt; /* Device type */ U32 cyls; /* Device size (pri+alt cyls)*/ U32 alts; /* Number of alternate cyls */ BYTE *itrkbuf; /* -> Input track buffer */ U32 itrklen; /* Input track length */ BYTE *pbuf; /* Current byte in input buf */ BYTE klen; /* Key length */ U16 dlen; /* Data length */ BYTE *kptr; /* -> Key in input buffer */ BYTE *dptr; /* -> Data in input buffer */ U32 cyl; /* Cylinder number */ U32 head; /* Head number */ BYTE rec; /* Record number */ char pathname[MAX_PATH]; /* file path in host format */ hostpath(pathname, (char *)ifname, sizeof(pathname)); /* Open the HDR-30 CKD image file */ #if defined(HAVE_LIBZ) if (strcmp(ifname, "-") == 0) ifd = gzdopen (STDIN_FILENO, "rb"); else ifd = gzopen (pathname, "rb"); if (ifd == NULL) { fprintf (stderr, "Cannot open %s: %s\n", ifname, errno == 0 ? "gzopen error" : strerror(errno)); EXIT(3); } #else /*!defined(HAVE_LIBZ)*/ if (strcmp(ifname, "-") == 0) ifd = STDIN_FILENO; else { ifd = hopen(pathname, O_RDONLY | O_BINARY); if (ifd < 0) { fprintf (stderr, "Cannot open %s: %s\n", ifname, strerror(errno)); EXIT(3); } } #endif /*!defined(HAVE_LIBZ)*/ /* Read the first track header */ read_input_data (ifd, ifname, (BYTE*)&h30trkhdr, H30CKD_TRKHDR_SIZE, 0); #if !defined(HAVE_LIBZ) /* Reject input if compressed and we lack gzip support */ if (memcmp(h30trkhdr.devcode, gz_magic_id, sizeof(gz_magic_id)) == 0) { fprintf (stderr, "Input file %s appears to be a .gz file\n" "but this program was compiled without compression support\n", ifname); EXIT(3); } #endif /*!defined(HAVE_LIBZ)*/ /* Reject input if it is already in CKD or CCKD format */ if (memcmp((BYTE*)&h30trkhdr, ckd_ident, sizeof(ckd_ident)) == 0) { fprintf (stderr, "Input file %s is already in CKD format, use dasdcopy\n", ifname); EXIT(3); } /* Extract the device type code from the track header */ FETCH_HW (code, h30trkhdr.devcode); /* Determine the input device type and size from the device code */ switch (code) { case 0x01: dt=0x3330; cyls=411; alts=7; break; /* 3330 */ case 0x02: dt=0x3330; cyls=815; alts=7; break; /* 3330-11 */ case 0x03: dt=0x3340; cyls=351; alts=1; break; /* 3340-35 */ case 0x04: dt=0x3340; cyls=701; alts=1; break; /* 3340-70 */ case 0x05: dt=0x3350; cyls=562; alts=7; break; /* 3350 */ case 0x06: dt=0x3375; cyls=962; alts=3; break; /* 3375 */ case 0x08: dt=0x3380; cyls=888; alts=3; break; /* 3380-A,D,J*/ case 0x09: dt=0x3380; cyls=1774; alts=4; break; /* 3380-E */ case 0x0A: dt=0x3380; cyls=2660; alts=5; break; /* 3380-K */ case 0x0B: dt=0x3390; cyls=1117; alts=4; break; /* 3390-1 */ case 0x0C: dt=0x3390; cyls=2230; alts=4; break; /* 3390-2 */ case 0x0D: dt=0x3390; cyls=3343; alts=4; break; /* 3390-3 */ case 0x12: dt=0x2314; cyls=203; alts=3; break; /* 2314 */ case 0x13: dt=0x3390; cyls=10038; alts=21; break; /* 3390-9 */ case 0x14: dt=0x9345; cyls=1454; alts=14; break; /* 9345-1 */ case 0x15: dt=0x9345; cyls=2170; alts=14; break; /* 9345-2 */ default: fprintf (stderr, "Unknown device code %4.4X" \ " at offset 00000000 in input file %s\n", code, ifname); EXIT(3); } /* end switch(code) */ /* Use the device type to determine the input image track size */ switch (dt) { case 0x2314: itrklen = 0x2000; break; case 0x3330: itrklen = 0x3400; break; case 0x3340: itrklen = 0x2400; break; case 0x3350: itrklen = 0x4C00; break; case 0x3375: itrklen = 0x9000; break; case 0x3380: itrklen = 0xBC00; break; case 0x3390: itrklen = 0xE400; break; case 0x9345: itrklen = 0xBC00; break; default: fprintf (stderr, "Unknown device type: %4.4X\n", dt); EXIT(3); } /* end switch(dt) */ /* Obtain the input track buffer */ itrkbuf = malloc (itrklen); if (itrkbuf == NULL) { fprintf (stderr, "Cannot obtain storage for input track buffer: %s\n", strerror(errno)); EXIT(3); } /* Copy the first track header to the input track buffer */ memcpy (itrkbuf, &h30trkhdr, H30CKD_TRKHDR_SIZE); /* Read the remainder of the first track into the buffer */ read_input_data (ifd, ifname, itrkbuf + H30CKD_TRKHDR_SIZE, itrklen - H30CKD_TRKHDR_SIZE, H30CKD_TRKHDR_SIZE); /* Initialize the volume serial number */ strcpy ((char *)volser, "(NONE)"); /* Search for volume label in record 3 of first track */ pbuf = itrkbuf + H30CKD_TRKHDR_SIZE; len = itrklen - H30CKD_TRKHDR_SIZE; while (1) { /* Find next input record */ rc = find_input_record (itrkbuf, &pbuf, &len, &klen, &kptr, &dlen, &dptr, &cyl, &head, &rec); /* Give up if error or end of track */ if (rc != 0) break; /* Process when record 3 is found */ if (cyl == 0 && head == 0 && rec == 3) { /* Extract volser if it is a volume label */ if (klen == 4 && memcmp(kptr, ebcdicvol1, 4) == 0 && dlen == 80 && memcmp(dptr, ebcdicvol1, 4) == 0) make_asciiz ((char *)volser, 7, dptr+4, 6); break; } } /* end while */ /* Set output variables and return the input file descriptor */ *devt = dt; *vcyls = cyls - alts; *itrkl = itrklen; *itrkb = itrkbuf; return ifd; } /* end function open_input_image */
static hFILE *hopen_preload(const char *url, const char *mode){ hFILE* fp = hopen(url + 8, mode); return hpreload(fp); }
/*-------------------------------------------------------------------*/ int main (int argc, char *argv[]) { int cckd_diag_rc = 0; /* Program return code */ char *fn; /* File name */ CKDDASD_DEVHDR devhdr; /* [C]CKD device hdr */ CCKDDASD_DEVHDR cdevhdr; /* Compressed CKD device hdr */ CKDDEV *ckd=0; /* CKD DASD table entry */ FBADEV *fba=0; /* FBA DASD table entry */ int cmd_devhdr = 0; /* display DEVHDR */ int cmd_cdevhdr = 0; /* display CDEVHDR */ int cmd_l1tab = 0; /* display L1TAB */ int cmd_l2tab = 0; /* display L2TAB */ int cmd_trkdata = 0; /* display track data */ int cmd_hexdump = 0; /* display track data (hex) */ int cmd_offset = 0; /* 1 = display data at */ int op_offset = 0; /* op_offset of length */ int op_length = 0; /* op_length */ int cmd_cchh = 0; /* 1 = display CCHH data */ int op_cc = 0; /* CC = cylinder */ int op_hh = 0; /* HH = head */ int cmd_tt = 0; /* 1 = display TT data */ int op_tt = 0; /* relative track # */ int swapend; /* 1 = New endianess doesn't match machine endianess */ int n, trk=0, l1ndx=0, l2ndx=0; off_t l2taboff=0; /* offset to assoc. L2 table */ int ckddasd; /* 1=CKD dasd 0=FBA dasd */ int heads=0; /* Heads per cylinder */ int blks; /* Number fba blocks */ off_t trkhdroff=0; /* offset to assoc. trk hdr */ int imglen=0; /* track length */ char pathname[MAX_PATH]; /* file path in host format */ INITIALIZE_UTILITY("cckddiag"); /* parse the arguments */ argc--; argv++ ; while (argc > 0) { if(**argv != '-') break; switch(argv[0][1]) { case 'v': if (argv[0][2] != '\0') return syntax (); display_version (stdout, "Hercules CCKD diagnostic program\n", FALSE); return 0; case 'd': if (argv[0][2] != '\0') return syntax (); cmd_devhdr = 1; break; case 'c': if (argv[0][2] != '\0') return syntax (); cmd_cdevhdr = 1; break; case '1': if (argv[0][2] != '\0') return syntax (); cmd_l1tab = 1; break; case '2': if (argv[0][2] != '\0') return syntax (); cmd_l2tab = 1; break; case 'a': if (argv[0][2] != '\0') return syntax (); cmd_cchh = 1; argc--; argv++; op_cc = offtify(*argv); argc--; argv++; op_hh = offtify(*argv); break; case 'r': if (argv[0][2] != '\0') return syntax (); cmd_tt = 1; argc--; argv++; op_tt = offtify(*argv); break; case 'o': if (argv[0][2] != '\0') return syntax (); cmd_offset = 1; argc--; argv++; op_offset = offtify(*argv); argc--; argv++; op_length = offtify(*argv); break; case 't': if (argv[0][2] != '\0') return syntax (); cmd_trkdata = 1; break; case 'x': if (argv[0][2] != '\0') return syntax (); cmd_hexdump = 1; cmd_trkdata = 1; break; case 'g': if (argv[0][2] != '\0') return syntax (); debug = 1; break; default: return syntax (); } argc--; argv++; } if (argc != 1) return syntax (); fn = argv[0]; /* open the file */ hostpath(pathname, fn, sizeof(pathname)); fd = hopen(pathname, O_RDONLY | O_BINARY); if (fd < 0) { fprintf(stdout, _("cckddiag: error opening file %s: %s\n"), fn, strerror(errno)); return -1; } /*---------------------------------------------------------------*/ /* display DEVHDR - first 512 bytes of dasd image */ /*---------------------------------------------------------------*/ readpos(fd, &devhdr, 0, sizeof(devhdr)); if (cmd_devhdr) { fprintf(stdout, "\nDEVHDR - %"SIZE_T_FMT"d (decimal) bytes:\n", sizeof(devhdr)); data_dump(&devhdr, sizeof(devhdr)); } /*---------------------------------------------------------------*/ /* Determine CKD or FBA device type */ /*---------------------------------------------------------------*/ if (memcmp(devhdr.devid, "CKD_C370", 8) == 0 || memcmp(devhdr.devid, "CKD_S370", 8) == 0) { ckddasd = 1; ckd = dasd_lookup(DASD_CKDDEV, NULL, devhdr.devtype, 0); if (ckd == NULL) { fprintf(stdout, "DASD table entry not found for devtype 0x%2.2X\n", devhdr.devtype); clean(); exit(5); } } else if (memcmp(devhdr.devid, "FBA_C370", 8) == 0 || memcmp(devhdr.devid, "FBA_S370", 8) == 0) { ckddasd = 0; fba = dasd_lookup(DASD_FBADEV, NULL, devhdr.devtype, 0); if (fba == NULL) { fprintf(stdout, "DASD table entry not found for " "devtype 0x%2.2X\n", DEFAULT_FBA_TYPE); clean(); exit(6); } } else { fprintf(stdout, "incorrect header id\n"); clean(); return -1; } /*---------------------------------------------------------------*/ /* Set up device characteristics */ /*---------------------------------------------------------------*/ if (ckddasd) { heads = ((U32)(devhdr.heads[3]) << 24) | ((U32)(devhdr.heads[2]) << 16) | ((U32)(devhdr.heads[1]) << 8) | (U32)(devhdr.heads[0]); if (debug) fprintf(stdout, "\n%s device has %d heads/cylinder\n", ckd->name, heads); } else { blks = 0; #if 0 /* cdevhdr is uninitialized and blks is never referenced... */ blks = ((U32)(cdevhdr.cyls[0]) << 24) | ((U32)(cdevhdr.cyls[2]) << 16) | ((U32)(cdevhdr.cyls[1]) << 8) | (U32)(cdevhdr.cyls[0]); #endif } /*---------------------------------------------------------------*/ /* display CDEVHDR - follows DEVHDR */ /*---------------------------------------------------------------*/ readpos(fd, &cdevhdr, CKDDASD_DEVHDR_SIZE, sizeof(cdevhdr)); if (cmd_cdevhdr) { fprintf(stdout, "\nCDEVHDR - %"SIZE_T_FMT"d (decimal) bytes:\n", sizeof(cdevhdr)); data_dump(&cdevhdr, sizeof(cdevhdr)); } /*---------------------------------------------------------------*/ /* Find machine endian-ness */ /*---------------------------------------------------------------*/ /* cckd_endian() returns 1 for big-endian machines */ swapend = (cckd_endian() != ((cdevhdr.options & CCKD_BIGENDIAN) != 0)); /*---------------------------------------------------------------*/ /* display L1TAB - follows CDEVHDR */ /*---------------------------------------------------------------*/ /* swap numl1tab if needed */ n = cdevhdr.numl1tab; if (swapend) cckd_swapend4((char *)&n); l1 = makbuf(n * CCKD_L1ENT_SIZE, "L1TAB"); readpos(fd, l1, CCKD_L1TAB_POS, n * CCKD_L1ENT_SIZE); /* L1TAB itself is not adjusted for endian-ness */ if (cmd_l1tab) { fprintf(stdout, "\nL1TAB - %"SIZE_T_FMT"d (0x"SIZE_T_FMTX") bytes:\n", (n * CCKD_L1ENT_SIZE), (n * CCKD_L1ENT_SIZE)); data_dump(l1, n * CCKD_L1ENT_SIZE); } /*---------------------------------------------------------------*/ /* display OFFSET, LENGTH data */ /*---------------------------------------------------------------*/ if (cmd_offset) { bulk = makbuf(op_length, "BULK"); readpos(fd, bulk, op_offset, op_length); fprintf(stdout, "\nIMAGE OFFSET %d (0x%8.8X) " "of length %d (0x%8.8X) bytes:\n", op_offset, op_offset, op_length, op_length); data_dump(bulk, op_length); free(bulk); bulk = NULL; } /*---------------------------------------------------------------*/ /* FBA isn't supported here because I don't know much about FBA */ /*---------------------------------------------------------------*/ if ( (!ckddasd) && ((cmd_cchh) || (cmd_tt)) ) { fprintf(stdout, "CCHH/reltrk not supported for FBA\n"); clean(); exit(3); } /*---------------------------------------------------------------*/ /* Setup CCHH or relative track request */ /*---------------------------------------------------------------*/ if (ckddasd) { if (cmd_tt) { trk = op_tt; op_cc = trk / heads; op_hh = trk % heads; } else { trk = (op_cc * heads) + op_hh; } l1ndx = trk / cdevhdr.numl2tab; l2ndx = trk % cdevhdr.numl2tab; l2taboff = l1[l1ndx]; if (swapend) cckd_swapend4((char *)&l2taboff); } /*---------------------------------------------------------------*/ /* display CKD CCHH or relative track data */ /*---------------------------------------------------------------*/ if ((cmd_cchh) || (cmd_tt)) { fprintf(stdout, "CC %d HH %d = reltrk %d; " "L1 index = %d, L2 index = %d\n" "L1 index %d = L2TAB offset %d (0x%8.8X)\n", op_cc, op_hh, trk, l1ndx, l2ndx, l1ndx, (int)l2taboff, (int)l2taboff); l2 = makbuf(cdevhdr.numl2tab * sizeof(CCKD_L2ENT), "L2TAB"); readpos(fd, l2, l2taboff, cdevhdr.numl2tab * sizeof(CCKD_L2ENT)); if (cmd_l2tab) { fprintf(stdout, "\nL2TAB - %"SIZE_T_FMT"d (decimal) bytes\n", (cdevhdr.numl2tab * sizeof(CCKD_L2ENT))); data_dump(l2, (cdevhdr.numl2tab * sizeof(CCKD_L2ENT)) ); } fprintf(stdout, "\nL2 index %d = L2TAB entry %"SIZE_T_FMT"d bytes\n", l2ndx, sizeof(CCKD_L2ENT) ); data_dump(&l2[l2ndx], sizeof(CCKD_L2ENT) ); trkhdroff = l2[l2ndx].pos; imglen = l2[l2ndx].len; if (swapend) { cckd_swapend4((char *)&trkhdroff); cckd_swapend4((char *)&imglen); } fprintf(stdout, "\nTRKHDR offset %d (0x%8.8X); " "length %d (0x%4.4X)\n", (int)trkhdroff, (int)trkhdroff, imglen, imglen); tbuf = makbuf(imglen, "TRKHDR+DATA"); readpos(fd, tbuf, trkhdroff, imglen); fprintf(stdout, "\nTRKHDR track %d\n", trk); data_dump(tbuf, sizeof(CKDDASD_TRKHDR) ); if (cmd_trkdata) showtrk(tbuf, imglen, trk, cmd_hexdump); free(l2); free(tbuf); l2 = NULL; tbuf = NULL; } /* Close file, exit */ fprintf(stdout, "\n"); clean(); return cckd_diag_rc; }
int main ( int argc, char *argv[]) { int fd; CKDDASD_DEVHDR devhdr; CCKDDASD_DEVHDR cdevhdr; int heads, cyls, devt; char pathname[MAX_PATH]; hostpath(pathname, argv[1], sizeof(pathname)); fd = hopen(pathname, O_RDWR|O_BINARY); if (fd < 0) return 1; read (fd, &devhdr, CKDDASD_DEVHDR_SIZE); read (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE); /* --------------------------------------- */ /* Device header updates */ /* --------------------------------------- */ /* device identifier */ // memcpy (devhdr.devid, "CKD_C370", 8); /* number of heads per cylinder must be in little-endian byte order */ // devhdr.heads[3] = (heads >> 24) & 0xFF; // devhdr.heads[2] = (heads >> 16) & 0xFF; // devhdr.heads[1] = (heads >> 8) & 0xFF; // devhdr.heads[0] = heads & 0xFF; /* device type -- last two digits */ // devhdr.devtype = devt; /* eg 0x90 for 3390 */ /* file sequence number; must be zero for compressed ckd dasd emulation */ // devhdr.fileseq = 0; /* highest cylinder on this file; must be zero for compressed ckd dasd emulation */ // devhdr.highcyl[0] = 0; // devhdr.highcyl[1] = 0; // memset (&devhdr.resv, 0, 492); /* --------------------------------------- */ /* Compressed device header updates */ /* --------------------------------------- */ /* version-release-modification level */ // cdevhdr.vrm[0] = CCKD_VERSION; // cdevhdr.vrm[0] = CCKD_RELEASE; // cdevhdr.vrm[0] = CCKD_MODLVL; /* options byte */ // cdevhdr.options = 0; // cdevhdr.options |= CCKD_NOFUDGE; // cdevhdr.options |= CCKD_BIGENDIAN; // cdevhdr.options |= CCKD_OPENED; /* lookup table sizes*/ // cdevhdr.numl1tab = (cyls * heads) >> 8; // if ((cyls * heads) & 0xff != 0) // cdevhdr.numl1tab++; // cdevhdr.numl2tab = 256; /* free space header -- set to zeroes to force cckdcdsk to rebuild the free space */ // cdevhdr.size = cdevhdr.used = cdevhdr.free = // cdevhdr.free_total = cdevhdr.free_largest = // cdevhdr.free_number = cdevhdr.free_imbed = 0; /* number of cylinders on the emulated device must be in little-endian byte order */ // cdevhdr.cyls[3] = (cyls >> 24) & 0xFF; // cdevhdr.cyls[2] = (cyls >> 16) & 0xFF; // cdevhdr.cyls[1] = (cyls >> 8) & 0xFF; // cdevhdr.cyls[0] = cyls & 0xFF; // cdevhdr.resv1 = 0; /* compression algorithm and parameter */ // cdevhdr.compress = CCKD_COMPRESS_NONE; // cdevhdr.compress_parm = 0; // cdevhdr.compress = CCKD_COMPRESS_ZLIB; // cdevhdr.compress_parm = Z_DEFAULT_COMPRESSION; // cdevhdr.compress = CCKD_COMPRESS_BZIP2; // cdevhdr.compress_parm = 5; // memset (&cdevhdr.resv2, 0, 464); lseek (fd, 0, SEEK_SET); write (fd, &devhdr, CKDDASD_DEVHDR_SIZE); write (fd, &cdevhdr, CCKDDASD_DEVHDR_SIZE); close (fd); return 0; }
/*-------------------------------------------------------------------*/ int read_omadesc (DEVBLK *dev) { int rc; /* Return code */ int i; /* Array subscript */ int pathlen; /* Length of TDF path name */ int tdfsize; /* Size of TDF file in bytes */ int filecount; /* Number of files */ int stmt; /* TDF file statement number */ int fd; /* TDF file descriptor */ struct stat statbuf; /* TDF file information */ U32 blklen; /* Fixed block length */ int tdfpos; /* Position in TDF buffer */ char *tdfbuf; /* -> TDF file buffer */ char *tdfrec; /* -> TDF record */ char *tdffilenm; /* -> Filename in TDF record */ char *tdfformat; /* -> Format in TDF record */ char *tdfreckwd; /* -> Keyword in TDF record */ char *tdfblklen; /* -> Length in TDF record */ OMATAPE_DESC *tdftab; /* -> Tape descriptor array */ BYTE c; /* Work area for sscanf */ char pathname[MAX_PATH]; /* file path in host format */ /* Isolate the base path name of the TDF file */ for (pathlen = strlen(dev->filename); pathlen > 0; ) { pathlen--; if (dev->filename[pathlen-1] == '/') break; } #if 0 // JCS thinks this is bad if (pathlen < 7 || strncasecmp(dev->filename+pathlen-7, "/tapes/", 7) != 0) { logmsg (_("HHCTA232I %4.4X: Invalid filename %s: " "TDF files must be in the TAPES subdirectory\n"), dev->devnum, dev->filename+pathlen); return -1; } pathlen -= 7; #endif /* Open the tape descriptor file */ hostpath(pathname, dev->filename, sizeof(pathname)); fd = hopen(pathname, O_RDONLY | O_BINARY); if (fd < 0) { logmsg (_("HHCTA239E %4.4X: Error opening TDF file %s: %s\n"), dev->devnum, dev->filename, strerror(errno)); return -1; } /* Determine the size of the tape descriptor file */ rc = fstat (fd, &statbuf); if (rc < 0) { logmsg (_("HHCTA240E %4.4X: File %s fstat error: %s\n"), dev->devnum, dev->filename, strerror(errno)); close (fd); return -1; } tdfsize = statbuf.st_size; /* Obtain a buffer for the tape descriptor file */ tdfbuf = malloc (tdfsize); if (tdfbuf == NULL) { logmsg (_("HHCTA241E %4.4X: Cannot obtain buffer for TDF file %s: %s\n"), dev->devnum, dev->filename, strerror(errno)); close (fd); return -1; } /* Read the tape descriptor file into the buffer */ rc = read (fd, tdfbuf, tdfsize); if (rc < tdfsize) { logmsg (_("HHCTA242E %4.4X: Error reading TDF file %s: %s\n"), dev->devnum, dev->filename, strerror(errno)); free (tdfbuf); close (fd); return -1; } /* Close the tape descriptor file */ close (fd); fd = -1; /* Check that the first record is a TDF header */ if (memcmp(tdfbuf, "@TDF", 4) != 0) { logmsg (_("HHCTA243E %4.4X: %s is not a valid TDF file\n"), dev->devnum, dev->filename); free (tdfbuf); return -1; } /* Count the number of linefeeds in the tape descriptor file to determine the size of the descriptor array required */ for (i = 0, filecount = 0; i < tdfsize; i++) { if (tdfbuf[i] == '\n') filecount++; } /* end for(i) */ /* ISW Add 1 to filecount to add an extra EOT marker */ filecount++; /* Obtain storage for the tape descriptor array */ tdftab = (OMATAPE_DESC*)malloc (filecount * sizeof(OMATAPE_DESC)); if (tdftab == NULL) { logmsg (_("HHCTA244E %4.4X: Cannot obtain buffer for TDF array: %s\n"), dev->devnum, strerror(errno)); free (tdfbuf); return -1; } /* Build the tape descriptor array */ for (filecount = 0, tdfpos = 0, stmt = 1; ; filecount++) { /* Clear the tape descriptor array entry */ memset (&(tdftab[filecount]), 0, sizeof(OMATAPE_DESC)); /* Point past the next linefeed in the TDF file */ while (tdfpos < tdfsize && tdfbuf[tdfpos++] != '\n'); stmt++; /* Exit at end of TDF file */ if (tdfpos >= tdfsize) break; /* Mark the end of the TDF record with a null terminator */ tdfrec = tdfbuf + tdfpos; while (tdfpos < tdfsize && tdfbuf[tdfpos]!='\r' && tdfbuf[tdfpos]!='\n') tdfpos++; c = tdfbuf[tdfpos]; if (tdfpos >= tdfsize) break; tdfbuf[tdfpos] = '\0'; /* Exit if TM or EOT record */ if (strcasecmp(tdfrec, "TM") == 0) { tdftab[filecount].format='X'; tdfbuf[tdfpos] = c; continue; } if(strcasecmp(tdfrec, "EOT") == 0) { tdftab[filecount].format='E'; break; } /* Parse the TDF record */ tdffilenm = strtok (tdfrec, " \t"); tdfformat = strtok (NULL, " \t"); tdfreckwd = strtok (NULL, " \t"); tdfblklen = strtok (NULL, " \t"); /* Check for missing fields */ if (tdffilenm == NULL || tdfformat == NULL) { logmsg (_("HHCTA245E %4.4X: Filename or format missing in " "line %d of file %s\n"), dev->devnum, stmt, dev->filename); free (tdftab); free (tdfbuf); return -1; } /* Check that the file name is not too long */ if (pathlen + 1 + strlen(tdffilenm) > sizeof(tdftab[filecount].filename) - 1) { logmsg (_("HHCTA246E %4.4X: Filename %s too long in " "line %d of file %s\n"), dev->devnum, tdffilenm, stmt, dev->filename); free (tdftab); free (tdfbuf); return -1; } /* Convert the file name to Unix format */ for (i = 0; i < (int)strlen(tdffilenm); i++) { if (tdffilenm[i] == '\\') tdffilenm[i] = '/'; /* JCS */ // else // tdffilenm[i] = tolower(tdffilenm[i]); } /* end for(i) */ /* Prefix the file name with the base path name and save it in the tape descriptor array */ /* but only if the filename lacks a leading slash - JCS */ /* strncpy (tdftab[filecount].filename, dev->filename, pathlen); if (tdffilenm[0] != '/') stlrcat ( tdftab[filecount].filename, "/", sizeof(tdftab[filecount].filename) ); strlcat ( tdftab[filecount].filename, tdffilenm, sizeof(tdftab[filecount].filename) ); */ tdftab[filecount].filename[0] = 0; if ((tdffilenm[0] != '/') && (tdffilenm[1] != ':')) { strncpy (tdftab[filecount].filename, dev->filename, pathlen); strlcat (tdftab[filecount].filename, "/", sizeof(tdftab[filecount].filename) ); } strlcat (tdftab[filecount].filename, tdffilenm, sizeof(tdftab[filecount].filename) ); /* Check for valid file format code */ if (strcasecmp(tdfformat, "HEADERS") == 0) { tdftab[filecount].format = 'H'; } else if (strcasecmp(tdfformat, "TEXT") == 0) { tdftab[filecount].format = 'T'; } else if (strcasecmp(tdfformat, "FIXED") == 0) { /* Check for RECSIZE keyword */ if (tdfreckwd == NULL || strcasecmp(tdfreckwd, "RECSIZE") != 0) { logmsg (_("HHCTA247E %4.4X: RECSIZE keyword missing in " "line %d of file %s\n"), dev->devnum, stmt, dev->filename); free (tdftab); free (tdfbuf); return -1; } /* Check for valid fixed block length */ if (tdfblklen == NULL || sscanf(tdfblklen, "%u%c", &blklen, &c) != 1 || blklen < 1 || blklen > MAX_BLKLEN) { logmsg (_("HHCTA248E %4.4X: Invalid record size %s in " "line %d of file %s\n"), dev->devnum, tdfblklen, stmt, dev->filename); free (tdftab); free (tdfbuf); return -1; } /* Set format and block length in descriptor array */ tdftab[filecount].format = 'F'; tdftab[filecount].blklen = blklen; } else { logmsg (_("HHCTA249E %4.4X: Invalid record format %s in " "line %d of file %s\n"), dev->devnum, tdfformat, stmt, dev->filename); free (tdftab); free (tdfbuf); return -1; } tdfbuf[tdfpos] = c; } /* end for(filecount) */ /* Force an EOT as last entry (filecount is correctly adjusted here) */ tdftab[filecount].format='E'; /* Save the file count and TDF array pointer in the device block */ dev->omafiles = filecount+1; dev->omadesc = tdftab; /* Release the TDF file buffer and exit */ free (tdfbuf); return 0; } /* end function read_omadesc */