[comp.sys.apollo] Fix for corrupt headers

zeleznik%cs.utah.edu@wasatch.utah.edu (Mike Zeleznik) (06/02/89)

In article <8906021332.AA01229@richter.mit.edu> krowitz@RICHTER.MIT.EDU (David Krowitz) writes:
>
>Talking about corrupted stream headers ... is their anyway to
>retrieve the contents of a file that has that error message
>(file not closed properly due to system crash ...)? Is there
>some way just to patch up the header of a UASC or REC file?

I think this may do what you want.  I got it from an OLD Apollo
Technical Forum publication.   It just creates a new file with a good
header, maps the old file AFTER the bum header, and just copies the
bytes into the new one.  It used to work fine, but I haven't run it
in a long time.

---------------------------------------------------------------------------
/* From Apollo Tech Forum; pgm to fix files with bad headers */
/* (translated from Pascal) */

/* mpz */

#include "/sys/ins/base.ins.c"
#include "/sys/ins/streams.ins.c"
#include "/sys/ins/ms.ins.c"
#include "/sys/ins/pfm.ins.c"
#include "/sys/ins/error.ins.c"

char            *address;
name_$pname_t   file1, file2;
boolean         extend;
linteger        length, mapln, mapst;
status_$t       status;
stream_$id_t    stid;
stream_$sk_t    seek_key;
stream_$inquire_mask_t  inq_mask, error;

main(argc, argv)
 int  argc;
 char *argv[];
{
    if (argc < 3)
     {
        printf("Usage: %s badfile newfile [-max bytes]\n", argv[0]);
        exit(1);
     }
    else
     {
        strcpy(&file1, argv[1]);
        strcpy(&file2, argv[2]);

        mapln = 5000000;    /* Default max file of 5Meg */
        if (argc == 5)
            if ( strcmp( argv[3], "-max") == 0 )
                sscanf(argv[4], "%d", &mapln);
            else
                printf("Unknown option (%s). Ignoring.\n", argv[3]);
     }

 /* First, map the source file AFTER the header bytes */

    extend = false;
    mapst  = 32;

    address = ms_$mapl(file1, (short)strlen(file1), mapst, mapln,
                       ms_$nr_xor_1w, ms_$r, extend, length, status);
     check("ms_$mapl call");

 /* Open the destination file, so you get good header */

    stream_$create(file2, (short)strlen(file2), stream_$write,
                   stream_$no_conc_write, stid, status);
     check("stream_$create call");

 /* And copy over the data */

    stream_$put_rec(stid, address, length, seek_key, status);
     check("stream_$put_rec call");

    stream_$close(stid, status);
     check("stream_$close call");

}

check(string)
  char  string[];
{
    if (status.all != 0)
     {  printf(" %s: <s.code %d>\n",string, status.s.code);
        error_$print(status);
        exit(1);
     }
}
---------------------------------------------------------------------------

  Michael Zeleznik              Computer Science Dept.
                                University of Utah
  zeleznik@cs.utah.edu          Salt Lake City, UT  84112
                                (801) 581-5617