home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit5a190 / ckvcvt.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  14KB  |  565 lines

  1. /*
  2.  * ckvcvt - assorted outboard processing for C-Kermit/VMS labeled files
  3.  */
  4.  
  5. /* Revision History:
  6.  * T1.0-00 - 08-Apr-91 - tmk - Initial coding, sync w/ 5A(167) and ckvfio.c 
  7.  *                   2.0-066.
  8.  * T1.0-01 - 14-Apr-91 - tmk - Fix errors in help output, show actual system
  9.  *                   message on RMS error.
  10.  * T1.0-02 - 15-Apr-91 - tmk - Redefine fab$b_journal as fab$b_rfm+1.
  11.  * T1.0-03 - 15-Apr-91 - tmk - ACL support, sync w/ 5A(169) and ckvfio.c 2.0-
  12.  *                   069.
  13.  * T1.0-04 - 16-Apr-91 - tmk - Fix _another_ journaling bug (whimper), handle
  14.  *                   missing semicolon in filespec gracefully.
  15.  * T1.0-05 - 04-Sep-92 - tmk - Implement ckvfio.c 087 fix.
  16.  * T1.0-06 - 08-Apr-93 - tmk - Implement ckvfio.c 097 fix.
  17.  */
  18.  
  19. #ifdef WINTCP
  20. #include stdio
  21. #include stdlib
  22. #include ctype
  23. #include string
  24. #include rms
  25. #include ssdef
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <rms.h>
  32. #include <ssdef.h>
  33. #endif /* WINTCP */
  34. #define    VERSION    "T1.0-06"
  35.  
  36. #define    R_MODE    "rb"
  37. #define    IO_ERROR    0
  38. #define IO_SUCCESS    1
  39.  
  40. /*
  41.  * Definitions for output file
  42.  */
  43.  
  44. static struct FAB fab_ofile;
  45. static struct RAB rab_ofile;
  46. static struct XABDAT xabdat_ofile;
  47. static struct XABFHC xabfhc_ofile;
  48. static struct XABPRO xabpro_ofile;
  49. static struct XABALL xaball_ofile;
  50. static struct XABRDT xabrdt_ofile;
  51. static short ofile_ffb;
  52. static int ofile_rec;
  53.  
  54. /*
  55.  * Common RMS items
  56.  */
  57.  
  58. static unsigned long int rms_sts;
  59.  
  60. /*
  61.  * Global varables
  62.  */
  63.  
  64. int    keepacl = 0;                /* Preserve ACL data? */
  65. int    backup    = 0;                /* Preserve file backup date? */
  66. int    debug    = 0;                /* Debug output? */
  67. int    notrim    = 0;                /* Don't trim names? */
  68. int    inquire = 0;                /* Testing if labeled? */
  69. int    owner    = 0;                /* Preserve file ownership? */
  70. int    strip    = 0;                /* Just stripping? */
  71. FILE    *infd    = NULL;                /* Input file descriptor */
  72. char    *infn    = NULL;                /* Input file name */
  73. char    *outfn    = NULL;                /* Special output file name */
  74. char    buffer[512];                /* Work buffer */
  75. char    label[99];                /* Label name */
  76. int    lblen    = 0;                /* Length of label */
  77. char    vmsname[255];                /* Stored name */
  78. char    vmsfile[70];                /* Stored file info */
  79. char    vmsacl[512];                /* Stored ACL data */
  80. int    acllen = 0;                /* Size of same */
  81. char    *filptr = vmsfile;            /* Attribute pointer */
  82. int    gotname    = 0;                /* Found name? */
  83. int    gotfile = 0;                /* Found file info? */
  84. int    gotacl = 0;                /* Found ACL info? */
  85. int    bail    = 0;                /* If we should bail out */
  86. char    revdat[8];                /* Revision date */
  87. unsigned short revnum;                /* Revision number */
  88. unsigned short jnlflg;                /* Journaling flags */
  89.  
  90. /*
  91.  * Function prototypes
  92.  */
  93.  
  94. extern int main(int, char **);
  95. void do_help();
  96. void barf(char *);
  97. void strip_file();
  98.  
  99. /*
  100.  * Ok, let's do it
  101.  */
  102.  
  103. int main(argc, argv)
  104. int argc;
  105. char *argv[];
  106. {
  107.     register char *ap;
  108.     int i, j;                    /* How original */
  109.  
  110.     if (argc < 2)                /* User say anything? */
  111.     do_help();                /* If not... */
  112.  
  113.     if (*argv[1] != '-') {
  114.     infn = argv[1];
  115.     if ((infd = fopen(argv[1], R_MODE)) == NULL) {
  116.         perror(argv[1]);
  117.         exit(IO_ERROR);
  118.     }
  119.     argc--;
  120.     argv++;
  121.     }
  122.     else
  123.     bail++;
  124.  
  125.     while (argc > 1) {
  126.     ap = argv[1];
  127.     if (*ap != '-') {
  128.         fprintf(stderr, "Unknown option '%s',", ap);
  129.         fprintf(stderr, " do CKVCVT -? for help.\n");
  130.     }
  131.     else for (ap++; *ap; ap++) {
  132.         switch (tolower(*ap)) {
  133.  
  134.         case 'a':                /* Preserve ACL's */
  135.         keepacl++;
  136.         break;
  137.  
  138.         case 'b':                /* Preserve backup date */
  139.         backup++;
  140.         break;
  141.  
  142.         case 'd':                /* Debug mode? */
  143.         debug++;
  144.         break;
  145.  
  146.         case 'f':                /* Name output file */
  147.         if (isgraph(ap[1]) != 0)
  148.             outfn = &ap[1];
  149.         else if (argc > 2) {
  150.             outfn = argv[2];
  151.             argc--;
  152.             argv++;
  153.         } 
  154.         else {
  155.             break;
  156.         }
  157.         goto next_arg;
  158.  
  159.         case 'i':                /* Inquire if labeled */
  160.         inquire++;
  161.         strip++;
  162.         break;
  163.  
  164.         case 'o':                /* Preserve file ownership */
  165.         owner++;
  166.         break;
  167.  
  168.         case 's':                /* Just strip it */
  169.         strip++;
  170.         break;
  171.  
  172.         case 't':                /* Don't trim filenames */
  173.         notrim++;
  174.         break;
  175.  
  176.         case '?':                /* Emit help text */
  177.         case 'h':
  178.         do_help();
  179.         break;
  180.  
  181.         default:
  182.         fprintf(stderr, "?Unknown option '%c',", *ap);
  183.         fprintf(stderr, " do CKVCVT -? for help\n");
  184.         }
  185.     }
  186.     next_arg:
  187.     argc--;
  188.     argv++;
  189.     }
  190.  
  191.     if (bail != 0)
  192.     exit(IO_ERROR);
  193.  
  194.     fread(buffer, 20, 1, infd);
  195.     if (strncmp(buffer, "KERMIT LABELED FILE:", 20) != 0)
  196.     barf("not a Kermit labeled file");
  197.  
  198.     fread(buffer, 2, 1, infd);
  199.     buffer[2] = '\0';
  200.     lblen = atoi(buffer);
  201.     if (lblen != 2)
  202.     barf("");
  203.  
  204.     fread(buffer, lblen, 1, infd);
  205.  
  206.     if (strip != 0)                /* Stripping headers? */
  207.     strip_file();
  208.  
  209.     if (strncmp(buffer, "D7", 2) != 0)
  210.     barf("not from a VAX/VMS system");
  211.  
  212.     fread(buffer, 6, 1, infd);
  213.     if (strncmp(buffer, "04VERS", 6) != 0)
  214.     barf("");
  215.  
  216.     fread(buffer, 8, 1, infd);
  217.     buffer[8] = '\0';
  218.     lblen = atoi(buffer);
  219.  
  220.     fread(buffer, lblen, 1, infd);
  221.     buffer[lblen] = '\0';
  222.     if (debug)
  223.     fprintf(stderr, "File created under VAX/VMS %s\n", buffer);
  224.  
  225.     fread(buffer, 7, 1, infd);
  226.     if (strncmp(buffer, "05KVERS", 7) != 0)
  227.     barf("");
  228.  
  229.     fread(buffer, 8, 1, infd);
  230.     buffer[8] = '\0';
  231.     lblen = atoi(buffer);
  232.  
  233.     fread(buffer, lblen, 1, infd);
  234.     buffer[lblen] = '\0';
  235.     if (debug)
  236.     fprintf(stderr, "File created with C-Kermit/VMS %s\n", buffer);
  237.  
  238.     next_label:
  239.     fread(buffer, 2, 1, infd);
  240.     buffer[2] = '\0';
  241.     lblen = atoi(buffer);
  242.     if (lblen == 0)
  243.     barf("lost sync");
  244.  
  245.     fread(buffer, lblen, 1, infd);
  246.     buffer[lblen] = '\0';
  247.     if (strcmp(buffer, "VMSNAME") == 0) {
  248.     fread(buffer, 8, 1, infd);
  249.     buffer[8] = '\0';
  250.     lblen = atoi(buffer);
  251.     fread(vmsname, lblen, 1, infd);
  252.     vmsname[lblen] = '\0';
  253.     gotname++;
  254.     if (debug)
  255.         fprintf(stderr, "Loaded file name block as %s\n", vmsname);
  256.     i = strstr(vmsname, "::");
  257.     if (i != NULL) {
  258.         i += 2;
  259.         memmove(vmsname, i, strlen(vmsname));
  260.     }
  261.         if (!notrim) {
  262.         i = strrchr(vmsname, ':');
  263.         j = strrchr(vmsname, ']');
  264.         if (j == NULL)
  265.         j = strrchr(vmsname, '>');
  266.         if (j > i)
  267.         i = j;
  268.         i++;
  269.         memmove(vmsname, i, strlen(vmsname));
  270.     }
  271.     if (strchr(vmsname, ';') != NULL) {
  272.         for (j = strlen(vmsname); vmsname[j] != ';'; j--)
  273.         ;
  274.         vmsname[j] = '\0';
  275.     }
  276.     if (debug)
  277.         fprintf(stderr, "Resultant filespec: %s\n", vmsname);
  278.     goto next_label;
  279.     }
  280.     else if (strcmp(buffer, "VMSFILE") == 0) {
  281.     fread(buffer, 8, 1, infd);
  282.     buffer[8] = '\0';
  283.     lblen = atoi(buffer);
  284.     fread(vmsfile, lblen, 1, infd);
  285.     vmsfile[lblen] = '\0';
  286.     gotfile++;
  287.     if (debug)
  288.         fprintf(stderr, "Loaded file attribute block\n");
  289.     goto next_label;
  290.     }
  291.     else if (strcmp(buffer, "VMSACL") == 0) {
  292.     fread(buffer, 8, 1, infd);
  293.     buffer[8] = '\0';
  294.     acllen = atoi(buffer);
  295.     fread(vmsacl, acllen, 1, infd);
  296.     vmsacl[acllen] = '\0';
  297.     gotacl++;
  298.     if (debug)
  299.         fprintf(stderr, "Loaded file ACL block\n");
  300.     goto next_label;
  301.     }
  302.     else if (strcmp(buffer, "DATA") == 0) {
  303.     fread(buffer, 8, 1, infd);
  304.     buffer[8] = '\0';
  305.     lblen = atoi(buffer);
  306.     if (lblen != 0)
  307.         barf("");
  308.     if (debug)
  309.         fprintf(stderr, "Positioned at start of file data\n");
  310.     goto all_set;
  311.     }
  312.     else {
  313.     fprintf(stderr, "%s: unrecognized label '%s'\n", infn, buffer);
  314.     fread(buffer, 8, 1, infd);
  315.     buffer[8] = '\0';
  316.     lblen = atoi(buffer);
  317.     if (lblen > 512)
  318.         barf("unrecognized label too long to skip");
  319.     fread(vmsfile, lblen, 1, infd);
  320.     goto next_label;
  321.     }
  322.  
  323.     all_set:
  324.     if (gotfile != 1 || gotname != 1)
  325.     barf("missing required labels");
  326.  
  327. /*
  328.  * Prep the characteristics
  329.  */
  330.  
  331.     fab_ofile = cc$rms_fab;
  332.     fab_ofile.fab$b_fac = FAB$M_BIO | FAB$M_PUT;
  333.     fab_ofile.fab$l_fop = FAB$M_MXV;
  334.     if (outfn == NULL) {
  335.     fab_ofile.fab$l_fna = vmsname;
  336.     fab_ofile.fab$b_fns = strlen(vmsname);
  337.     }
  338.     else {
  339.     fab_ofile.fab$l_fna = outfn;
  340.     fab_ofile.fab$b_fns = strlen(outfn);
  341.     }
  342.     fab_ofile.fab$l_xab = &xabdat_ofile;
  343.     rab_ofile = cc$rms_rab;
  344.     rab_ofile.rab$l_fab = &fab_ofile;
  345.     xabdat_ofile = cc$rms_xabdat;
  346.     xabdat_ofile.xab$l_nxt = &xabrdt_ofile;
  347.     xabrdt_ofile = cc$rms_xabrdt;
  348.     xabrdt_ofile.xab$l_nxt = &xabfhc_ofile;
  349.     xabfhc_ofile = cc$rms_xabfhc;
  350.     xabfhc_ofile.xab$l_nxt = &xabpro_ofile;
  351.     xabpro_ofile = cc$rms_xabpro;
  352.     xabpro_ofile.xab$l_nxt = &xaball_ofile;
  353.     xaball_ofile = cc$rms_xaball;
  354.  
  355. /*
  356.  * Load 'em up
  357.  */
  358.  
  359.     memmove(&xabpro_ofile.xab$w_pro, filptr, 2);
  360.     filptr += 2;
  361.     if (owner != 0)
  362.     memmove(&xabpro_ofile.xab$l_uic, filptr, 4);
  363.     filptr += 4;
  364.     memmove(&fab_ofile.fab$b_rfm, filptr, 1);
  365.     filptr += 1;
  366.     memmove(&fab_ofile.fab$b_org, filptr, 1);
  367.     filptr += 1;
  368.     memmove(&fab_ofile.fab$b_rat, filptr, 1);
  369.     filptr += 5;                /* 4 bytes reserved for char */
  370.     memmove(&fab_ofile.fab$b_fsz, filptr, 1);
  371.     filptr += 1;
  372.     memmove(&xabfhc_ofile.xab$w_lrl, filptr, 2);
  373.     filptr += 2;
  374.     memmove(&fab_ofile.fab$w_mrs, filptr, 2);
  375.     filptr += 2;
  376.     memmove(&xabfhc_ofile.xab$l_ebk, filptr, 4);
  377.     filptr += 4;
  378. /* preserve this as RMS won't remember it for us */
  379.     memmove(&ofile_ffb, filptr, 2);
  380.     filptr += 2;
  381.     memmove(&xaball_ofile.xab$l_alq, filptr, 4);
  382.     filptr += 4;
  383.     memmove(&xaball_ofile.xab$w_deq, filptr, 2);
  384.     filptr += 2;
  385.     memmove(&xaball_ofile.xab$b_bkz, filptr, 1);
  386.     filptr += 1;
  387.     memmove(&fab_ofile.fab$w_gbc, filptr, 2);
  388.     filptr += 2;
  389.     memmove(&xabfhc_ofile.xab$w_verlimit, filptr, 2);
  390.     filptr += 2;
  391.     memmove(&jnlflg, filptr, 1);
  392.     if (jnlflg !=0)
  393.     printf("Original file was marked for RMS Journaling, this copy is not.\n");
  394.     filptr += 1;
  395.     memmove(&xabdat_ofile.xab$q_cdt, filptr, 8);
  396.     filptr += 8;
  397.     memmove(&revdat, filptr, 8);
  398.     filptr += 8;
  399.     memmove(&revnum, filptr, 2);
  400.     filptr += 2;
  401.     memmove(&xabdat_ofile.xab$q_edt, filptr, 8);
  402.     filptr += 8;
  403.     if (backup != 0)
  404.     memmove(&xabdat_ofile.xab$q_bdt, filptr, 8);
  405.     filptr += 8;
  406.  
  407. /*
  408.  * ACL's?
  409.  */
  410.  
  411.     if(keepacl != 0 && gotacl != 0) {
  412.     xabpro_ofile.xab$l_aclbuf = &vmsacl;
  413.     xabpro_ofile.xab$w_aclsiz = acllen;
  414.     }
  415.  
  416. /*
  417.  * Give it a quick whirl around the dance floor
  418.  */
  419.  
  420.     printf("Creating %s...\n", fab_ofile.fab$l_fna);
  421.     rms_sts = sys$create(&fab_ofile);
  422.     if (!(rms_sts & 1))
  423.     exit(rms_sts);
  424.  
  425.     if(keepacl != 0 && gotacl != 0) {
  426.     if (!(xabpro_ofile.xab$l_aclsts & 1))
  427.         exit(xabpro_ofile.xab$l_aclsts);
  428.     }
  429.  
  430.     rms_sts = sys$connect(&rab_ofile);
  431.     if (!(rms_sts & 1))
  432.     exit(rms_sts);
  433.  
  434.     ofile_rec = 1;
  435.     fread(buffer, 512, 1, infd);
  436.     while (!feof(infd)) {
  437.     rab_ofile.rab$l_rbf = buffer;
  438.     rab_ofile.rab$w_rsz = 512;
  439.     if (ofile_rec == xabfhc_ofile.xab$l_ebk) {
  440.         xabfhc_ofile.xab$w_ffb = ofile_ffb;
  441.         if (ofile_ffb)
  442.         rab_ofile.rab$w_rsz -= (512 - ofile_ffb);
  443.         if (debug) {
  444.         fprintf(stderr, "FFB (first free byte) = %d\n", ofile_ffb);
  445.         fprintf(stderr, "Last record size = %d\n", rab_ofile.rab$w_rsz);
  446.         }
  447.     }
  448.     rms_sts = sys$write(&rab_ofile);
  449.     if (!(rms_sts & 1))
  450.         exit(rms_sts);
  451.     fread(buffer, 512, 1, infd);
  452.     ofile_rec++;
  453.     }
  454.  
  455. /*
  456.  * Update the revision information
  457.  */
  458.  
  459.     memmove(&xabrdt_ofile.xab$q_rdt, revdat, 8);
  460.     memmove(&xabrdt_ofile.xab$w_rvn, &revnum, 2);
  461.     
  462.     rms_sts = sys$close(&fab_ofile);
  463.     if (!(rms_sts & 1))
  464.     exit(rms_sts);
  465.     printf("...done.\n");
  466.     exit(IO_SUCCESS);
  467. }
  468.  
  469. void do_help()
  470. {
  471.     printf("This is CKVCVT %s\n\n", VERSION);
  472.     printf("Usage: CKVCVT infile options\n\n");
  473.     printf("Options:\n");
  474.     printf("         -a     Preserve file ACL data (may require privs)\n");
  475.     printf("         -b     Preserve file backup date\n");
  476.     printf("         -d     Print debugging information\n");
  477.     printf("         -f fn  Name output file fn instead of default\n");
  478.     printf("         -i     Inquire if a file is labeled\n");
  479.     printf("         -o     Preserve file ownership (requires privs)\n");
  480.     printf("         -s     Strip one level of label information\n");
  481.     printf("         -t     Don't trim paths from output file name\n");
  482.     printf("         -?     Display this message\n\n");
  483.     exit(IO_SUCCESS);
  484. }
  485.  
  486. void barf(text)
  487. char *text;
  488. {
  489.     if (text == "") 
  490.     fprintf(stderr, "%s: corrupted Kermit labeled file\n", infn);
  491.     else
  492.     fprintf(stderr, "%s: %s\n", infn, text);
  493.     exit(IO_ERROR);
  494. }
  495.  
  496. void strip_file()
  497. {
  498.     next_label:
  499.     fread(buffer, 2, 1, infd);            /* Get label length */
  500.     buffer[2] = '\0';
  501.     lblen = atoi(buffer);
  502.     if (lblen == 0)                /* Better not be zero! */
  503.     barf("lost sync");
  504.  
  505.     fread(label, lblen, 1, infd);        /* Get the label name */
  506.     label[lblen] = '\0';
  507.     fread(buffer, 8, 1, infd);            /* Get the contents length */
  508.     buffer[8] = '\0';
  509.     lblen = atoi(buffer);
  510.     if (strcmp(label, "DATA") == 0) {        /* If done... */
  511.     if (lblen != 0)
  512.         barf("");
  513.     if (debug)
  514.         fprintf(stderr, "Positioned at start of file data\n");
  515.     goto all_set;
  516.     }
  517.     fread(buffer, lblen, 1, infd);        /* Else skip contents */
  518.     goto next_label;                /* And loop */
  519.  
  520. /*
  521.  * We've skipped the first header, now do whatever task is needed
  522.  */
  523.  
  524.     all_set:
  525.     if (inquire != 0) {
  526.     printf("%s is a properly formatted Kermit labeled file\n", infn);
  527.     exit(IO_SUCCESS);
  528.     }
  529.  
  530.     fab_ofile = cc$rms_fab;
  531.     fab_ofile.fab$b_fac = FAB$M_BIO | FAB$M_PUT;
  532.     fab_ofile.fab$l_fop = FAB$M_MXV;
  533.     fab_ofile.fab$l_fna = infn;
  534.     fab_ofile.fab$b_fns = strlen(infn);
  535.     fab_ofile.fab$b_rfm = FAB$C_FIX;
  536.     fab_ofile.fab$w_mrs = 512;
  537.     rab_ofile = cc$rms_rab;
  538.     rab_ofile.rab$l_fab = &fab_ofile;
  539.  
  540.     printf("Stripping %s...\n", fab_ofile.fab$l_fna);
  541.     rms_sts = sys$create(&fab_ofile);
  542.     if (!(rms_sts & 1))
  543.     exit(rms_sts);
  544.  
  545.     rms_sts = sys$connect(&rab_ofile);
  546.     if (!(rms_sts & 1))
  547.     exit(rms_sts);
  548.  
  549.     fread(buffer, 512, 1, infd);
  550.     while (!feof(infd)) {
  551.     rab_ofile.rab$l_rbf = buffer;
  552.     rab_ofile.rab$w_rsz = 512;
  553.     rms_sts = sys$write(&rab_ofile);
  554.     if (!(rms_sts & 1))
  555.         exit(rms_sts);
  556.     fread(buffer, 512, 1, infd);
  557.     }
  558.     
  559.     rms_sts = sys$close(&fab_ofile);
  560.     if (!(rms_sts & 1))
  561.     exit(rms_sts);
  562.     printf("...done.\n");
  563.     exit(IO_SUCCESS);
  564. }
  565.