C 66
Backup.c Guest on 30th May 2020 08:22:17 PM
  1.  
  2. /* Dump contents of Tops-10 BACKUP tapes, which have been read
  3.    into a disk file. The "known good" way to do this is to use the
  4.    unix utility "dd", and a command line something like this:
  5.  
  6.    dd if=/dev/rmt0 of=data ibs=2720 obs=2720 conv=block
  7.  
  8.    the key thing is that this program expects a fixed block size of
  9.    2720 bytes.  If the tape actually has some other format, this
  10.    program probably won't succeed.  You can use the unix utility "tcopy"
  11.    to inspect the contents of the tape.
  12.  
  13.    Here's the tcopy output from a good tape:
  14.  
  15.    tcopy /dev/rmt0
  16.    file 0: block size 2720: 9917 records
  17.    file 0: eof after 9917 records: 26974240 bytes
  18.    eot
  19.    total length: 26974240 bytes
  20.  
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <string.h>
  28. #include "backup.h"
  29.  
  30. #define bool long
  31. #define false 0
  32. #define true 1
  33.  
  34. #define RAWSIZE (5*(32+512))
  35.  
  36. #define endof(s) (strchr(s, (char) 0))
  37.  
  38. FILE* source;                   /* Source "tape". */
  39.  
  40. bool eightbit = false;          /* Status of -8 (eight-bit) flag. */
  41. bool copytape = false;          /* Status of -c (copytape fmt) flag. */
  42. bool buildtree = false;         /* Status of -d (build trees) flag. */
  43. bool interchange = false;       /* Status of -i (interchange) flag. */
  44. bool binary = false;            /* Status of -b (binary) flag. */
  45. bool timfmt = false;            /* Status of -m (mts format) flag. */
  46. long verbose = 0;               /* Status of -v (verbose) flag. */
  47.  
  48. char** argfiles;                /* File spec's to extract. */
  49. long argcount;                  /* Number of them. */
  50.  
  51. unsigned char rawdata[RAWSIZE]; /* Raw data for a tape block. */
  52.  
  53. long headlh[32], headrh[32];    /* Header block from tape. */
  54. long datalh[512], datarh[512];  /* Data block from tape. */
  55.  
  56. long prevSEQ;                   /* SEQ number of previous block. */
  57. long currentfilenumber;
  58.  
  59. char deferbyte;                 /* Defered byte for output. */
  60. long defercount;                        /* Count of defered output bytes. */
  61.  
  62. bool extracting;
  63. FILE* destination;
  64.  
  65. /* Tape information: */
  66.  
  67. char systemname[100];
  68. char savesetname[100];
  69.  
  70. /* File information: */
  71.  
  72. long a_bsiz;                            /* For now. */
  73. long a_alls;
  74. long a_mode;
  75. long a_leng;
  76.  
  77. char filedev[100];              /* Device: */
  78. char filedir[100];              /* [ufd] */
  79. char filename[100];             /* file name. */
  80. char fileext[100];              /* extension. */
  81.  
  82. char filespec[7][100];          /* [0]: device:ufd. */
  83.                                 /* [1-5]: sfd's, stored directly here. */
  84.                                 /* [6]: file.ext */
  85.  
  86. char cname[100];                /* Canonical name. */
  87.  
  88. /* unpackheader unpacks the header block from the raw stream. */
  89.  
  90. void unpackheader() {
  91.   unsigned char* rawptr;
  92.   long i, left, right;
  93.   unsigned char c;
  94.  
  95.   rawptr = &rawdata[0];
  96.  
  97.   for (i = 0; i < 32; i++) {
  98.     left = *(rawptr++) << 10;
  99.     left |= *(rawptr++) << 2;
  100.     left |= (c = *(rawptr++)) >> 6;
  101.     right = (c & 077) << 12;
  102.     right |= *(rawptr++) << 4;
  103.     right |= *(rawptr++) & 017;
  104.     headlh[i] = left;
  105.     headrh[i] = right;
  106.         if(verbose>1) {printf("\n%i l=%d, r=%d",i,left,right);}
  107.   }
  108. }
  109.  
  110. /* unpackdata unpacks the data block from the raw stream. */
  111.  
  112. void unpackdata() {
  113.   unsigned char* rawptr;
  114.   long i, left, right;
  115.   unsigned char c;
  116.  
  117.   rawptr = &rawdata[32*5];
  118.  
  119.   for (i = 0; i < 512; i++) {
  120.     left = *(rawptr++) << 10;
  121.     left |= *(rawptr++) << 2;
  122.     left |= (c = *(rawptr++)) >> 6;
  123.     right = (c & 077) << 12;
  124.     right |= *(rawptr++) << 4;
  125.     right |= *(rawptr++) & 017;
  126.     datalh[i] = left;
  127.     datarh[i] = right;
  128.   }
  129. }
  130.  
  131. /* pars_36bits reads 36 bits from a machine word. */
  132.  
  133. void pars_36bits(index, store)
  134. long index;
  135. char *store;
  136. {
  137.   long l, r;
  138.  
  139.   l = datalh[index];
  140.   r = datarh[index];
  141.  
  142.   store[0] = r & 0377;
  143.   store[1] = (r >> 8) & 0377;
  144.   store[2] = ((r >> 16) & 03) | ((l << 2) & 0374);
  145.   store[3] = (l >> 6) & 0377;
  146.   store[4] = (l >> 14) & 017;
  147.   store[5] = store[6] = store[7] = 0;
  148. }
  149.  
  150. /* pars_5chars reads five ASCII chars from a machine word. */
  151.  
  152. void pars_5chars(index, store)
  153. long index;
  154. char* store;
  155. {
  156.   long l, r;
  157.  
  158.   l = datalh[index];
  159.   r = datarh[index];
  160.  
  161.   store[0] = (0177 & (l >> 11));
  162.   store[1] = (0177 & (l >> 4));
  163.   store[2] = (0177 & ((l << 3) | ((r >> 15) & 017)));
  164.   store[3] = (0177 & (r >> 8));
  165.   store[4] = (0177 & (r >> 1));
  166. }
  167.  
  168. /* pars_asciz stores asciz text from data */
  169.  
  170. void pars_asciz(index, store)
  171. long index;
  172. char* store;
  173. {
  174.   long words;
  175.  
  176.   words = datarh[index++];
  177.   while ((words--) > 0) {
  178.     pars_5chars(index++, store);
  179.     store += 5;
  180.   }
  181.   *store = (char) 0;
  182. }
  183.  
  184. /* pars_o_name parses an o$name block from data. */
  185.  
  186. void pars_o_name(index)
  187. long index;
  188. {
  189.   long lastw;
  190.  
  191.   lastw = index + datarh[index];
  192.   ++index;
  193.   while (index < lastw) {
  194.     switch (datalh[index]) {
  195.     case 0:  index = lastw; break;
  196.     case 1:  pars_asciz(index, filedev);  break;
  197.     case 2:  pars_asciz(index, filename); break;
  198.     case 3:  pars_asciz(index, fileext);  break;
  199.     case 32: pars_asciz(index, filedir);  break;
  200.     case 33: pars_asciz(index, filespec[1]); break;
  201.     case 34: pars_asciz(index, filespec[2]); break;
  202.     case 35: pars_asciz(index, filespec[3]); break;
  203.     case 36: pars_asciz(index, filespec[4]); break;
  204.     case 37: pars_asciz(index, filespec[5]); break;
  205.     }
  206.     index += datarh[index];
  207.   }
  208. }
  209.  
  210. void pars_o_attr(index)
  211. long index;
  212. {
  213.   /* parse off file attribute block */
  214.   ++index;
  215.   a_bsiz = datarh[index + A_BSIZ];      /* for now... */
  216.   a_alls = datarh[index + A_ALLS];      /* for now... */
  217.   a_mode = datarh[index + A_MODE];      /* for now... */
  218.   a_leng = datarh[index + A_LENG];      /* for now... */
  219. }
  220.  
  221. void pars_o_dirt(index)
  222. long index;
  223. {
  224.   /* parse off directory attribute block */
  225. }
  226.  
  227. void pars_o_sysn(index)
  228. long index;
  229. {
  230.   pars_asciz(index, systemname);
  231. }
  232.  
  233. void pars_o_ssnm(index)
  234. long index;
  235. {
  236.   pars_asciz(index, savesetname);
  237. }
  238.  
  239. void zerotapeinfo() {
  240.   systemname[0] = (char) 0;
  241.   savesetname[0] = (char) 0;
  242. }
  243.  
  244. void zerofileinfo() {
  245.  
  246.   filedev[0]  = (char) 0;
  247.   filedir[0]  = (char) 0;
  248.   filename[0] = (char) 0;
  249.   fileext[0]  = (char) 0;
  250.  
  251.   filespec[0][0] = (char) 0;
  252.   filespec[1][0] = (char) 0;
  253.   filespec[2][0] = (char) 0;
  254.   filespec[3][0] = (char) 0;
  255.   filespec[4][0] = (char) 0;
  256.   filespec[5][0] = (char) 0;
  257.   filespec[6][0] = (char) 0;
  258.  
  259.   cname[0] = (char) 0;
  260. }
  261.  
  262. /* unpackinfo picks non-data information from data block. */
  263.  
  264. void unpackinfo() {
  265.   long index;
  266.  
  267.   unpackdata();
  268.  
  269.   index = 0;
  270.   while (index < headrh[G_LND]) {
  271.     switch (datalh[index]) {
  272.     case 1: pars_o_name(index); break;
  273.     case 2: pars_o_attr(index); break;
  274.     case 3: pars_o_dirt(index); break;
  275.     case 4: pars_o_sysn(index); break;
  276.     case 5: pars_o_ssnm(index); break;
  277.     }
  278.     index += datarh[index];
  279.   }
  280. }
  281.  
  282. void printtapeinfo() {
  283.   if (verbose) {
  284.     if (*savesetname != (char) 0) printf("Saveset name: %s\n", savesetname);
  285.     if (*systemname != (char) 0) printf("Written on: %s\n", systemname);
  286.   }
  287. }
  288.  
  289. void downcase(s)
  290. char* s;
  291. {
  292.   while (*s != (char) 0) {
  293.     if (isupper(*s)) *s = tolower(*s);
  294.     s++;
  295.   }
  296. }
  297.  
  298. void buildfilenames() {
  299.   long i;
  300.  
  301.   if (*filedev != (char) 0)
  302.     sprintf(filespec[0], "%s:%s", filedev, filedir);
  303.   else
  304.     sprintf(filespec[0], "%s", filedir);
  305.  
  306.   sprintf(filespec[6], "%s.%s", filename, fileext);
  307.  
  308.   for(i = 0; i < 7; i++)
  309.     downcase(filespec[i]);
  310.  
  311.   sprintf(cname, "%s", filespec[0]);
  312.   for(i = 1; i < 6; i++) {
  313.     if (*filespec[i] != (char) 0) sprintf(endof(cname), ".%s", filespec[i]);
  314.   }
  315.   if (*cname != (char) 0)
  316.     sprintf(endof(cname), "..%s", filespec[6]);
  317.   else
  318.     sprintf(cname, "%s", filespec[6]);
  319.  
  320. }
  321.  
  322. void printfileinfo() {
  323.  
  324.   buildfilenames();
  325.   printf("%3d  %s", currentfilenumber, cname);
  326.   if (verbose) {
  327.      printf(" (%d) alloc:%d, mode:%o, len:%d", a_bsiz, a_alls, a_mode, a_leng);
  328.   }
  329.   printf("\n");
  330. }
  331.  
  332. /* readblock reads one logical block from the input stream. */
  333. /* The header is unpacked into head{l,r}; the data is not. */
  334.  
  335. long blockn=0;
  336.  
  337. void readblock() {
  338.   long i, bytes;
  339.   unsigned char bc[4];
  340.  
  341.   i = fread(bc, sizeof(char), 4, source);
  342.   if (i == 0) return;
  343.   bytes = ((long) bc[1] << 8) | (bc[0]);
  344.   if (bytes == 0) return;
  345.   if (bytes != RAWSIZE)
  346.           fprintf(stderr, "backup: incorrect block size = %d\n", bytes);
  347.   i = fread(rawdata, sizeof(char), RAWSIZE, source);
  348.   blockn++;
  349.   while (i++ < RAWSIZE) rawdata[i] = (char) 0;
  350.   fread(bc, sizeof(char), 4, source);
  351.   unpackheader();
  352. }
  353.  
  354. /* Disk file output routines: */
  355.  
  356. void WriteBlock() {
  357.   char buffer[5*512];
  358.   char binbuf[8*512];
  359.   long bufpos, index;
  360.  
  361.   for (index = 0; index < 5*512; index++) buffer[index] = 0;
  362.   for (index = 0; index < 8*512; index++) binbuf[index] = 0;
  363.  
  364.   unpackdata();
  365.   if (binary) {
  366.     for (index = headrh[G_LND], bufpos = 0;
  367.        index < (headrh[G_LND] + headrh[G_SIZE]); index++) {
  368.           pars_36bits(index, &binbuf[bufpos]);
  369.           bufpos += 8;
  370.         }
  371.     (void) fwrite(binbuf, sizeof(char), bufpos, destination);
  372.   }
  373.   else {
  374.     for (index = headrh[G_LND], bufpos = 0;
  375.        index < (headrh[G_LND] + headrh[G_SIZE]); index++) {
  376.       pars_5chars(index, &buffer[bufpos]);
  377.       bufpos += 5;
  378.     }
  379.  
  380.     if (headlh[G_FLAGS] & GF_EOF) {
  381.       for (index = 1; (index < (eightbit ? 4 : 5)) && (bufpos > 0); index++) {
  382.         if (buffer[bufpos - 1] == (char) 0) bufpos--;
  383.       }
  384.     }
  385.     (void) fwrite(buffer, sizeof(char), bufpos, destination);
  386.   }
  387. }
  388.  
  389. /* OpenOutput opens the output file, according to -d and -i flags. */
  390.  
  391. bool OpenOutput() {
  392.  
  393.   struct stat statbuf;
  394.   char oname[100];
  395.   long i;
  396.  
  397.   defercount = 0;
  398.  
  399.   if (interchange) {
  400.         destination = fopen(filespec[6], (binary? "wb": "w"));
  401.   } else if (!buildtree) {
  402.         for (i = 0; (i < sizeof (cname)) && cname[i]; i++)
  403.                 if (cname[i] == ':') cname[i] = '.';
  404.     destination = fopen(cname, (binary? "wb": "w"));
  405.   } else {
  406. /*    for(i = 0, oname[0] = (char) 0; i < 6; i++) {
  407.       if (*filespec[i] == (char) 0) break;
  408.       sprintf(endof(oname), "%s", filespec[i]);
  409.       if (stat(oname, &statbuf) != 0) {
  410.         if (mkdir(oname, 0777) != 0) {
  411.           fprintf(stderr, "backup: cannot create %s/\n", oname);
  412.           return(false);
  413.         }
  414.       }
  415.       sprintf(endof(oname), "/");
  416.     }
  417.     sprintf(endof(oname), "%s", filespec[6]);
  418.     destination = fopen(oname, (binary? "wb": "w")); */
  419.           fprintf(stderr, "backup: tree mode not supported\n");
  420.           return(false);
  421.   }
  422.  
  423.   return(destination != NULL);
  424. }
  425.  
  426. void CloseOutput() {
  427.   /* Close output file after us. */
  428. }
  429.  
  430. /* Argmatch checks if the current file matches the given argument: */
  431.  
  432. bool argmatch(arg)
  433. char* arg;
  434. {
  435.   long target;
  436.   char* f;
  437.   char* p;
  438.   char* s;
  439.  
  440.   if (*arg == '#') {
  441.     (void) sscanf(arg, "#%d", &target);
  442.     return(target == currentfilenumber);
  443.   }
  444.  
  445.   if (*arg == '*') return(1);
  446.  
  447.   for (f = cname; *f != (char) 0; f++) {
  448.     for (p = f, s = arg; (*s != (char) 0) && (*p == *s); p++, s++);
  449.     if (*s == (char) 0) return (true);
  450.   }
  451.   return (false);
  452. }
  453.  
  454. /* doextract performs the job of "backup -x ..." */
  455.  
  456. void doextract() {
  457.   long i;
  458.  
  459.   currentfilenumber = 0;
  460.   extracting = false;
  461.   while (!feof(source)) {
  462.     readblock();
  463.     if (headrh[G_SEQ] == prevSEQ) continue;
  464.  
  465.     if (headrh[G_TYPE] == T_FILE) {
  466.       if (headlh[G_FLAGS] & GF_SOF) {
  467.         currentfilenumber++;
  468.         zerofileinfo();
  469.         unpackinfo();
  470.         buildfilenames();
  471.         for (i = 0; i < argcount; i++) {
  472.           if (argmatch(argfiles[i])) {
  473.             if (*argfiles[i] == '#') {
  474.               /* Maybe do a pure shift here? */
  475.               argfiles[i] = argfiles[--argcount];
  476.             }
  477.             extracting = true;
  478.             break;
  479.           }
  480.         }
  481.         if (extracting) {
  482.           if (OpenOutput()) {
  483.             if (verbose) {
  484.               printf("Extracting %s", cname);
  485.               fflush(stdout);
  486.             }
  487.           } else {
  488.             fprintf(stderr, "backup: can't open %s for output\n", cname);
  489.             extracting = false;
  490.           }
  491.         }
  492.       }
  493.       if (extracting) {
  494.         WriteBlock();
  495.         if (headlh[G_FLAGS] & GF_EOF) {
  496.           (void) fclose(destination);
  497.           extracting = false;
  498.           if (verbose) printf("\n");
  499.           if (argcount == 0)
  500.             break;
  501.         }
  502.       }
  503.     }
  504.     prevSEQ = headrh[G_SEQ];
  505.   }
  506. }
  507.  
  508. /* dodirectory performs the job of "backup -t ..." */
  509.  
  510. void dodirectory() {
  511.  
  512.   currentfilenumber = 0;
  513.  
  514.   while (!feof(source)) {
  515.     readblock();
  516.     if (headrh[G_SEQ] == prevSEQ) continue;
  517.  
  518.     if (headrh[G_TYPE] == T_BEGIN) {
  519.       zerotapeinfo();
  520.       unpackinfo();
  521.       printtapeinfo();
  522.     }
  523.     if (headrh[G_TYPE] == T_FILE) {
  524.       if (headlh[G_FLAGS] & GF_SOF) {
  525.         ++currentfilenumber;
  526.         zerofileinfo();
  527.         unpackinfo();
  528.         printfileinfo();
  529.       }
  530.     }
  531.     prevSEQ = headrh[G_SEQ];
  532.   }
  533. }
  534.  
  535. /* command decoder and dispatcher */
  536.  
  537. bool checkarg(arg)
  538. char* arg;
  539. {
  540.   long i;
  541.   char c;
  542.  
  543.   if (*arg == '#') {
  544.     if (sscanf(arg, "#%d%c", &i, &c) != 1) {
  545.       fprintf(stderr, "backup: bad argument: %s\n", arg);
  546.       return(true);
  547.     }
  548.   }
  549.   return(false);
  550. }
  551.  
  552.  
  553. void main(argc, argv)
  554. long argc;
  555. char* argv[];
  556. {
  557.   long i;
  558.   char* s, tapetype[4];
  559.   bool namenext = false;
  560.   bool actgiven = false;
  561.   char action;
  562.   char* inputname = NULL;
  563.  
  564.   if (--argc > 0) {
  565.     for (s = *(++argv); *s != (char) 0; s++)
  566.       switch(*s) {
  567.       case '-':
  568.         break;
  569.       case '8':
  570.         eightbit = true;  break;
  571.           case 'b':
  572.         binary = true; break;
  573.       case 'c':
  574.         copytape = true; break;
  575.       case 'd':
  576.         buildtree = true;  break;
  577.       case 'f':
  578.         namenext = true;  break;
  579.       case 'i':
  580.         interchange = true;  break;
  581.       case 'm':
  582.         timfmt = true; break;
  583.       case 't':
  584.       case 'x':
  585.         action = *s;  actgiven = true;  break;
  586.       case 'v':
  587.           verbose++;  break;
  588.       default:
  589.         fprintf(stderr, "backup: bad option %c\n", *s);
  590.         return;
  591.       }
  592.   }
  593.   if (namenext) {
  594.     if (--argc > 0)
  595.       inputname = *(++argv);
  596.     else {
  597.       fprintf(stderr, "backup: input file name missing\n");
  598.       return;
  599.     }
  600.   }
  601.  
  602.   argfiles = ++argv;            /* Keep rest of arguments. */
  603.   argcount = --argc;            /* ... and count 'em. */
  604.  
  605.   for (i = 0; i < argcount; i++) {
  606.     if (checkarg(argfiles[i])) {
  607.                 fprintf(stderr, "backup: error in argument %d = %s\n", i, argfiles[i]);
  608.                 return;  }  }
  609.  
  610.   if (inputname == NULL) {
  611.     /* Use environment var. TAPE here? */
  612.     fprintf(stderr, "backup: no input file given\n");
  613.     return;
  614.   }
  615.  
  616.   if (strcmp(inputname, "-") != 0) {
  617.     if ((source = fopen(inputname, "rb")) == NULL) {
  618.       fprintf(stderr, "backup: can't open %s for input\n", inputname);
  619.       return;
  620.     }
  621.         fprintf (stderr, "backup: opening %s for input\n", inputname);
  622.         if (timfmt) fread (tapetype, sizeof(char), 4, source);
  623.   } else {
  624.     source = stdin;
  625.   }
  626.  
  627.   switch (action) {
  628.   case 't': dodirectory(); break;
  629.   case 'x': doextract(); break;
  630.   default:
  631.     fprintf(stderr, "backup: internal error in program\n");
  632.     return;
  633.   }
  634.  
  635. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.