/* || Retrieve and validate a standard label */ int get_sl( SLLABEL *lab ) { int rc; /* || Read a block */ if ( opts.faketape ) rc = fet_read( opts.fetb, blkptr ); else rc = het_read( opts.hetb, blkptr ); if( rc >= 0 ) { /* || Does is look like a standard label? */ if( sl_islabel( lab, blkptr, rc ) == TRUE ) { return( 0 ); } } else { if ( opts.faketape ) WRMSG( HHC00075, "E", "fet_read()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_read()", het_error( rc ) ); } return( -1 ); }
/* || Copy source to dest */ static int copytape( void ) { int rc; char buf[ HETMAX_BLOCKSIZE ]; while( TRUE ) { #ifdef EXTERNALGUI if( extgui ) { off_t curpos; /* Report progress every nnnK */ if ( i_faketape ) curpos = ftell( s_fetb->fd ); else curpos = ftell( s_hetb->fd ); if( ( curpos & PROGRESS_MASK ) != ( prevpos & PROGRESS_MASK ) ) { prevpos = curpos; fprintf( stderr, "IPOS=%" I64_FMT "d\n", (U64)curpos ); } } #endif /*EXTERNALGUI*/ if ( i_faketape ) rc = fet_read( s_fetb, buf ); else rc = het_read( s_hetb, buf ); if( rc == HETE_EOT ) // FAKETAPE and HETTAPE share codes { rc = 0; break; } if( rc == HETE_TAPEMARK ) { if ( o_faketape ) rc = fet_tapemark( d_fetb ); else rc = het_tapemark( d_hetb ); if( rc < 0 ) { if ( o_faketape ) WRMSG( HHC00075, "E", "fet_tapemark()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_tapemark()", het_error( rc ) ); break; } continue; } if( rc < 0 ) { if ( i_faketape ) WRMSG( HHC00075, "E", "fet_read()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_read()", het_error( rc ) ); break; } if ( o_faketape ) rc = fet_write( d_fetb, buf, rc ); else rc = het_write( d_hetb, buf, rc ); if( rc < 0 ) { if ( o_faketape ) WRMSG( HHC00075, "E", "fet_write()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_write()", het_error( rc ) ); break; } } return( rc ); }
/* || Standard main() function */ int main( int argc, char *argv[] ) { char *pgmname; /* prog name in host format */ char *pgm; /* less any extension (.ext) */ char msgbuf[512]; /* message build work area */ int rc; SLLABEL lab; HETB *hetb; /* used for aws and het tapes*/ FETB *fetb; /* used for faketapes */ int o_iehinitt; int o_nl; int o_compress; int o_faketape; char *o_filename; char *o_owner; char *o_volser; char *strtok_str = NULL; /* Set program name */ if ( argc > 0 ) { if ( strlen(argv[0]) == 0 ) { pgmname = strdup( UTILITY_NAME ); } else { char path[MAX_PATH]; #if defined( _MSVC_ ) GetModuleFileName( NULL, path, MAX_PATH ); #else strncpy( path, argv[0], sizeof( path ) ); #endif pgmname = strdup(basename(path)); #if !defined( _MSVC_ ) strncpy( path, argv[0], sizeof(path) ); #endif } } else { pgmname = strdup( UTILITY_NAME ); } pgm = strtok_r( strdup(pgmname), ".", &strtok_str); INITIALIZE_UTILITY( pgmname ); /* Display the program identification message */ MSGBUF( msgbuf, MSG_C( HHC02499, "I", pgm, "HET IEHINITT " ) ); display_version (stderr, msgbuf+10, FALSE); hetb = NULL; fetb = NULL; o_filename = NULL; o_faketape = FALSE; o_iehinitt = TRUE; o_nl = FALSE; o_compress = TRUE; o_owner = NULL; o_volser = NULL; while( TRUE ) { rc = getopt( argc, argv, "dhin" ); if( rc == -1 ) { break; } switch( rc ) { case 'd': o_compress = FALSE; break; case 'h': usage( pgm ); goto exit; break; case 'i': o_iehinitt = TRUE; o_nl = FALSE; break; case 'n': o_iehinitt = FALSE; o_nl = TRUE; break; default: usage( pgm ); goto exit; break; } } argc -= optind; if( argc < 1 ) { usage( pgm ); goto exit; } o_filename = argv[ optind ]; if ( ( rc = (int)strlen( o_filename ) ) > 4 && ( rc = strcasecmp( &o_filename[rc-4], ".fkt" ) ) == 0 ) { o_faketape = TRUE; } if( o_iehinitt ) { if( argc == 2 ) { o_volser = argv[ optind + 1 ]; } else if( argc == 3 ) { o_volser = argv[ optind + 1 ]; o_owner = argv[ optind + 2 ]; } else { usage( pgm ); goto exit; } } if( o_nl ) { if( argc != 1 ) { usage( pgm ); goto exit; } } if( o_volser ) het_string_to_upper( o_volser ); if( o_owner ) het_string_to_upper( o_owner ); if ( o_faketape ) { rc = fet_open( &fetb, o_filename, FETOPEN_CREATE ); if ( rc < 0 ) { WRMSG( HHC00075, "E", "fet_open()", fet_error( rc ) ); goto exit; } } else { rc = het_open( &hetb, o_filename, HETOPEN_CREATE ); if( rc < 0 ) { WRMSG( HHC00075, "E", "het_open()", het_error( rc ) ); goto exit; } rc = het_cntl( hetb, HETCNTL_SET | HETCNTL_COMPRESS, o_compress ); if( rc < 0 ) { WRMSG( HHC00075, "E", "het_cntl()", het_error( rc ) ); goto exit; } } if( o_iehinitt ) { rc = sl_vol1( &lab, o_volser, o_owner ); if( rc < 0 ) { WRMSG( HHC00075, "E", "sl_vol1()", sl_error( rc ) ); goto exit; } if ( o_faketape ) rc = fet_write( fetb, &lab, (U16)sizeof( lab ) ); else rc = het_write( hetb, &lab, sizeof( lab ) ); if( rc < 0 ) { if ( o_faketape ) WRMSG( HHC00075, "E", "fet_write() for VOL1", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_write() for VOL1", het_error( rc ) ); goto exit; } rc = sl_hdr1( &lab, SL_INITDSN, NULL, 0, 0, NULL, 0 ); if( rc < 0 ) { WRMSG( HHC00075, "E", "sl_hdr1()", sl_error( rc ) ); goto exit; } if ( o_faketape ) rc = fet_write( fetb, &lab, (U16)sizeof(lab) ); else rc = het_write( hetb, &lab, sizeof( lab ) ); if( rc < 0 ) { if ( o_faketape ) WRMSG( HHC00075, "E", "fet_write() for HDR1", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_write() for HDR1", het_error( rc ) ); goto exit; } } else if( o_nl ) { if ( o_faketape ) rc = fet_tapemark( fetb ); else rc = het_tapemark( hetb ); if( rc < 0 ) { if ( o_faketape ) WRMSG( HHC00075, "E", "fet_tapemark()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_tapemark()", het_error( rc ) ); goto exit; } } if ( o_faketape ) rc = fet_tapemark( fetb ); else rc = het_tapemark( hetb ); if( rc < 0 ) { if ( o_faketape ) WRMSG( HHC00075, "E", "fet_tapemark()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_tapemark()", het_error( rc ) ); goto exit; } exit: if ( o_faketape) fet_close( &fetb ); else het_close( &hetb ); return( rc < 0 ); }
/* || Standard main */ int main( int argc, char *argv[] ) { char *pgmname; /* prog name in host format */ char *pgm; /* less any extension (.ext) */ char msgbuf[512]; /* message build work area */ FILE *outf; int rc; int i; char pathname[MAX_PATH]; char *strtok_str = NULL; /* Set program name */ if ( argc > 0 ) { if ( strlen(argv[0]) == 0 ) { pgmname = strdup( UTILITY_NAME ); } else { char path[MAX_PATH]; #if defined( _MSVC_ ) GetModuleFileName( NULL, path, MAX_PATH ); #else strncpy( path, argv[0], sizeof( path ) ); #endif pgmname = strdup(basename(path)); #if !defined( _MSVC_ ) strncpy( path, argv[0], sizeof(path) ); #endif } } else { pgmname = strdup( UTILITY_NAME ); } pgm = strtok_r( strdup(pgmname), ".", &strtok_str); INITIALIZE_UTILITY( pgmname ); /* Display the program identification message */ MSGBUF( msgbuf, MSG_C( HHC02499, "I", pgm, "Extract Files from AWS, HET or FAKETAPE" ) ); display_version (stderr, msgbuf+10, FALSE); /* || Process option switches */ while( TRUE ) { rc = getopt( argc, argv, "abhnsu" ); if( rc == -1 ) { break; } switch( rc ) { case 'a': opts.flags |= O_ASCII; set_codepage(NULL); break; case 'h': usage( pgm ); exit( 1 ); break; case 'n': opts.flags |= O_NL; break; case 's': opts.flags |= O_STRIP; break; case 'u': opts.flags |= O_UNBLOCK; break; default: usage( pgm ); exit( 1 ); break; } } /* || Calc number of non-switch arguments */ argc -= optind; /* || We must have at least the first 3 parms */ if(argc < 3) { if ( argc > 1 ) WRMSG( HHC02446, "E" ); usage( pgm ); exit( 1 ); } hostpath( pathname, argv[ optind ], sizeof(pathname) ); opts.ifile = strdup( pathname ); if ( ( rc = (int)strlen( opts.ifile ) ) > 4 && ( rc = strcasecmp( &opts.ifile[rc-4], ".fkt" ) ) == 0 ) { opts.faketape = TRUE; } hostpath( pathname, argv[ optind + 1 ], sizeof(pathname) ); opts.ofile = strdup( pathname ); opts.fileno = atoi( argv[ optind + 2 ] ); if( opts.fileno == 0 || opts.fileno > 9999 ) { char msgbuf[20]; MSGBUF( msgbuf, "%d", opts.fileno ); WRMSG( HHC02205, "E", msgbuf, "; file number must be within the range of 1 to 9999" ); exit( 1 ); } /* || If NL tape, then we require the DCB attributes */ if( opts.flags & O_NL ) { if( argc != 6 ) { WRMSG( HHC02750, "E" ); exit( 1 ); } } /* || If specified, get the DCB attributes */ if( argc > 3 ) { /* || Must have only three */ if( argc != 6 ) { usage( pgm ); exit( 1 ); } /* || Lookup the specified RECFM in our table */ opts.recfm = 0; for( i = 0 ; i < (int)VALFMCNT ; i++ ) { if( strcasecmp( argv[ optind + 3 ], valfm[ i ].recfm ) == 0 ) { opts.recfm = valfm[ i ].fmt; break; } } /* || If we didn't find a match, show the user what the valid ones are */ if( opts.recfm == 0) { char msgbuf[512] = ""; char msgbuf2[64] = ""; char msgbuf3[16] = ""; char msgbuf4[128] = ""; /* || Dump out the valid RECFMs */ MSGBUF( msgbuf, MSG( HHC02751, "I" ) ); for( i = 0 ; i < (int)VALFMCNT ; i++ ) { MSGBUF( msgbuf3, " %-4.4s", valfm[ i ].recfm ); if( ( ( i + 1 ) % 3 ) == 0 ) { strcat( msgbuf2, msgbuf3 ); MSGBUF( msgbuf4, MSG( HHC02752, "I", msgbuf2 ) ); strcat( msgbuf, msgbuf4 ); msgbuf2[0] = 0; } else { strcat( msgbuf2, msgbuf3 ); } } printf( "%s", msgbuf ); exit( 1 ); } /* || Get the record length */ opts.lrecl = atoi( argv[ optind + 4 ] ); /* || Get and validate the blksize */ opts.blksize = atoi( argv[ optind + 5 ] ); if( opts.blksize == 0 ) { WRMSG( HHC02205, "E", "0", "; block size can't be zero" ); exit( 1 ); } } /* || Open the tape file */ if ( opts.faketape ) rc = fet_open( &opts.fetb, opts.ifile, FETOPEN_READONLY ); else rc = het_open( &opts.hetb, opts.ifile, HETOPEN_READONLY ); if( rc >= 0 ) { /* || Get memory for the tape buffer */ blkptr = malloc( HETMAX_BLOCKSIZE ); if( blkptr != NULL ) { /* || Open the output file */ outf = fopen( opts.ofile, "wb" ); if( outf != NULL ) { /* || Go extract the file from the tape */ rc = getfile( outf ); /* || Close the output file */ fclose( outf ); } /* || Free the buffer memory */ free( blkptr ); } } else { if ( opts.faketape ) WRMSG( HHC00075, "E", "fet_open()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_open()", het_error( rc ) ); } /* || Close the tape file */ if ( opts.faketape ) fet_close( &opts.fetb ); else het_close( &opts.hetb ); return 0; }
/* || Extract the file from the tape */ int getfile( FILE *outf ) { SLFMT fmt; SLLABEL lab; unsigned char *ptr; int fileno; int rc; /* || Skip to the desired file */ if( opts.flags & O_NL ) { /* || For NL tapes, just use the specified file number */ fileno = opts.fileno; /* || Start skipping */ while( --fileno ) { /* || Forward space to beginning of next file */ if ( opts.faketape ) rc = fet_fsf ( opts.fetb ); else rc = het_fsf( opts.hetb ); if( rc < 0 ) { char msgbuf[128]; MSGBUF( msgbuf, "%set_fsf() while positioning to file '%d'", opts.faketape ? "f" : "h", opts.fileno ); WRMSG( HHC00075, "E", msgbuf, het_error( rc ) ); return( rc ); } } } else { /* || First block should be a VOL1 record */ rc = get_sl( &lab ); if( rc < 0 || !sl_isvol( &lab, 1 ) ) { WRMSG( HHC02753, "E", "VOL1" ); return( -1 ); } /* || For SL, adjust the file # so we end up on the label before the data */ fileno = ( opts.fileno * 3 ) - 2; /* || Start skipping */ while( --fileno ) { /* || Forward space to beginning of next file */ if ( opts.faketape ) rc = fet_fsf ( opts.fetb ); else rc = het_fsf( opts.hetb ); if( rc < 0 ) { char msgbuf[128]; MSGBUF( msgbuf, "%set_fsf() while positioning to file '%d'", opts.faketape ? "f" : "h", opts.fileno ); WRMSG( HHC00075, "E", msgbuf, het_error( rc ) ); return( rc ); } } /* || Get the HDR1 label. */ rc = get_sl( &lab ); if( rc < 0 || !sl_ishdr( &lab, 1 ) ) { WRMSG( HHC02753, "E", "HDR1" ); return( -1 ); } /* || Make the label more managable */ sl_fmtlab( &fmt, &lab ); WRMSG( HHC02754, "E", fmt.slds1.dsid ); /* || Get the HDR2 label. */ rc = get_sl( &lab ); if( rc < 0 || !sl_ishdr( &lab, 2 ) ) { WRMSG( HHC02753, "E", "HDR2" ); return( -1 ); } /* || Merge the DCB information */ merge( &lab ); /* || Hop over the tapemark */ if ( opts.faketape ) rc = fet_fsf( opts.fetb ); else rc = het_fsf( opts.hetb ); if( rc < 0 ) { if ( opts.faketape ) WRMSG( HHC00075, "E", "fet_fsf()", fet_error( rc ) ); else WRMSG( HHC00075, "E", "het_fsf()", het_error( rc ) ); return( rc ); } } /* || Different processing when converting to ASCII */ if( opts.flags & ( O_ASCII | O_UNBLOCK ) ) { /* || Get a record */ while( ( rc = getrecord( ) ) >= 0 ) { #ifdef EXTERNALGUI if( extgui ) { off_t curpos; /* Report progress every nnnK */ if ( opts.faketape ) curpos = ftell( opts.fetb->fd ); else curpos = ftell( opts.hetb->fd ); if( ( curpos & PROGRESS_MASK ) != ( prevpos & PROGRESS_MASK ) ) { prevpos = curpos; fprintf( stderr, "IPOS=%" I64_FMT "d\n", (U64)curpos ); } } #endif /*EXTERNALGUI*/ /* || Get working copy of record ptr */ ptr = recptr; /* || Only want data portion for RECFM=V records */ if( opts.recfm & O_VARIABLE ) { ptr += 4; rc -= 4; } /* || Convert record to ASCII */ if( opts.flags & O_ASCII ) { sl_etoa( NULL, ptr, rc ); } /* || Strip trailing blanks */ if( opts.flags & O_STRIP ) { while( rc > 0 && ptr[ rc - 1 ] == ' ' ) { rc--; } } /* || Write the record out */ fwrite( ptr, rc, 1, outf ); /* || Put out a linefeed when converting */ if( opts.flags & O_ASCII ) { fwrite( "\n", 1, 1, outf ); } } } else { /* || Get a record */ while( ( rc = getblock( ) ) >= 0 ) { #ifdef EXTERNALGUI if( extgui ) { off_t curpos; /* Report progress every nnnK */ if ( opts.faketape ) curpos = ftell( opts.fetb->fd ); else curpos = ftell( opts.hetb->fd ); if( ( curpos & PROGRESS_MASK ) != ( prevpos & PROGRESS_MASK ) ) { prevpos = curpos; fprintf( stderr, "IPOS=%" I64_FMT "d\n", (U64)curpos ); } } #endif /*EXTERNALGUI*/ /* || Write the record out */ fwrite( blkptr, blklen, 1, outf ); } } return( rc ); }
/* || Standard main */ int main( int argc, char *argv[] ) { char *pgm; /* less any extension (.ext) */ FILE *outf; int rc; int i; char pathname[MAX_PATH]; INITIALIZE_UTILITY( UTILITY_NAME, "Extract Files from AWS, HET or FAKETAPE", &pgm ); /* || Process option switches */ while( TRUE ) { rc = getopt( argc, argv, "abhnsu" ); if( rc == -1 ) { break; } switch( rc ) { case 'a': opts.flags |= O_ASCII; set_codepage(NULL); break; case 'h': usage( pgm ); exit( 1 ); UNREACHABLE_CODE(); case 'n': opts.flags |= O_NL; break; case 's': opts.flags |= O_STRIP; break; case 'u': opts.flags |= O_UNBLOCK; break; default: usage( pgm ); exit( 1 ); UNREACHABLE_CODE(); } } /* || Calc number of non-switch arguments */ argc -= optind; /* || We must have at least the first 3 parms */ if(argc < 3) { if ( argc > 1 ) // "Invalid number of arguments" FWRMSG( stderr, HHC02446, "E" ); usage( pgm ); exit( 1 ); } hostpath( pathname, argv[ optind ], sizeof(pathname) ); opts.ifile = strdup( pathname ); if ( ( rc = (int)strlen( opts.ifile ) ) > 4 && ( rc = strcasecmp( &opts.ifile[rc-4], ".fkt" ) ) == 0 ) { opts.faketape = TRUE; } hostpath( pathname, argv[ optind + 1 ], sizeof(pathname) ); opts.ofile = strdup( pathname ); opts.fileno = atoi( argv[ optind + 2 ] ); if( opts.fileno == 0 || opts.fileno > 9999 ) { char msgbuf[20]; MSGBUF( msgbuf, "%d", opts.fileno ); // "Invalid argument %s%s" FWRMSG( stderr, HHC02205, "S", msgbuf, "; file number must be within the range of 1 to 9999" ); exit( 1 ); } /* || If NL tape, then we require the DCB attributes */ if( opts.flags & O_NL ) { if( argc != 6 ) { // "DCB attributes required for NL tape" FWRMSG( stderr, HHC02750, "S" ); exit( 1 ); } } /* || If specified, get the DCB attributes */ if( argc > 3 ) { /* || Must have only three */ if( argc != 6 ) { usage( pgm ); exit( 1 ); } /* || Lookup the specified RECFM in our table */ opts.recfm = 0; for( i = 0 ; i < (int)VALFMCNT ; i++ ) { if( strcasecmp( argv[ optind + 3 ], valfm[ i ].recfm ) == 0 ) { opts.recfm = valfm[ i ].fmt; break; } } /* || If we didn't find a match, show the user what the valid ones are */ if( opts.recfm == 0) { char msgbuf[512] = ""; char msgbuf2[64] = ""; char msgbuf3[16] = ""; char msgbuf4[128] = ""; /* || Dump out the valid RECFMs */ // "Valid record formats are:" MSGBUF( msgbuf, MSG( HHC02751, "I" ) ); for( i = 0 ; i < (int)VALFMCNT ; i++ ) { MSGBUF( msgbuf3, " %-4.4s", valfm[ i ].recfm ); if( ( ( i + 1 ) % 3 ) == 0 ) { strlcat( msgbuf2, msgbuf3, sizeof(msgbuf2) ); // "%s" MSGBUF( msgbuf4, MSG( HHC02752, "I", msgbuf2 ) ); strlcat( msgbuf, msgbuf4, sizeof(msgbuf) ); msgbuf2[0] = 0; } else { strlcat( msgbuf2, msgbuf3, sizeof(msgbuf2) ); } } printf( "%s", msgbuf ); exit( 1 ); } /* || Get the record length */ opts.lrecl = atoi( argv[ optind + 4 ] ); /* || Get and validate the blksize */ opts.blksize = atoi( argv[ optind + 5 ] ); if( opts.blksize == 0 ) { // "Invalid argument %s%s" FWRMSG( stderr, HHC02205, "S", "0", "; block size can't be zero" ); exit( 1 ); } } /* || Open the tape file */ if ( opts.faketape ) rc = fet_open( &opts.fetb, opts.ifile, FETOPEN_READONLY ); else rc = het_open( &opts.hetb, opts.ifile, HETOPEN_READONLY ); if( rc >= 0 ) { /* || Get memory for the tape buffer */ blkptr = malloc( HETMAX_BLOCKSIZE ); if( blkptr != NULL ) { /* || Open the output file */ outf = fopen( opts.ofile, "wb" ); if( outf != NULL ) { /* || Go extract the file from the tape */ rc = getfile( outf ); /* || Close the output file */ fclose( outf ); } /* || Free the buffer memory */ free( blkptr ); } } else { // "Error in function %s: %s" if ( opts.faketape ) FWRMSG( stderr, HHC00075, "E", "fet_open()", fet_error( rc ) ); else FWRMSG( stderr, HHC00075, "E", "het_open()", het_error( rc ) ); } /* || Close the tape file */ if ( opts.faketape ) fet_close( &opts.fetb ); else het_close( &opts.hetb ); return 0; }