home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip201.zip / vms / VMSmunch.c < prev    next >
C/C++ Source or Header  |  1993-09-10  |  12KB  |  348 lines

  1. /*
  2.  *  Modified by:
  3.  *
  4.  *    v1.3            Hunter Goatley          14-SEP-1992 08:51
  5.  *            Added definitions of FIB$W_FID, FIB$W_DID, and
  6.  *            FIB$L_ACCTL to allow for the fact that fibdef
  7.  *            contains variant_unions under Alpha.
  8.  */
  9. /*---------------------------------------------------------------------------
  10.  
  11.   VMSmunch.c                    version 1.2                     28 Apr 1992
  12.  
  13.   This routine is a blatant and unrepentent appropriation of all the nasty
  14.   and difficult-to-do and complicated VMS shenanigans which Joe Meadows has
  15.   so magnificently captured in his FILE utility.  Not only that, it's even
  16.   allowed! (see below).  But let it be clear at the outset that Joe did all
  17.   the work; yea, verily, he is truly a godlike unit.
  18.  
  19.   The appropriations and modifications herein were performed primarily by
  20.   him known as "Cave Newt," although the Info-ZIP working group probably had
  21.   their fingers in it somewhere along the line.  The idea is to put the raw
  22.   power of Joe's original routine at the disposal of various routines used
  23.   by UnZip (and Zip, possibly), not least among them the utime() function.
  24.   Read on for details...
  25.  
  26.   ---------------------------------------------------------------------------
  27.  
  28.   Usage (i.e., "interface," in geek-speak):
  29.  
  30.      int VMSmunch( char *filename, int action, char *ptr );
  31.  
  32.      filename   the name of the file on which to be operated, obviously
  33.      action     an integer which specifies what action to take
  34.      ptr        pointer to any extra item which may be needed (else NULL)
  35.  
  36.   The possible values for the action argument are as follows:
  37.  
  38.      GET_TIMES      get the creation and revision dates of filename; ptr
  39.                     must point to an empty VMStimbuf struct, as defined below
  40.                     (with room for at least 24 characters, including term.)
  41.      SET_TIMES      set the creation and revision dates of filename (utime
  42.                     option); ptr must point to a valid VMStimbuf struct,
  43.                     as defined below
  44.      GET_RTYPE      get the record type of filename; ptr must point to an
  45.                     integer which, on return, is set to the type (as defined
  46.                     in VMSmunch.h:  FAT$C_* defines)
  47.      CHANGE_RTYPE   change the record type to that specified by the integer
  48.                     to which ptr points; save the old record type (later
  49.                     saves overwrite earlier ones)
  50.      RESTORE_RTYPE  restore the record type to the previously saved value;
  51.                     or, if none, set it to "fixed-length, 512-byte" record
  52.                     format (ptr not used)
  53.  
  54.   ---------------------------------------------------------------------------
  55.  
  56.   Comments from FILE.C, a utility to modify file characteristics:
  57.  
  58.      Written by Joe Meadows Jr, at the Fred Hutchinson Cancer Research Center
  59.      BITNET: JOE@FHCRCVAX
  60.      PHONE: (206) 467-4970
  61.  
  62.      There are no restrictions on this code, you may sell it, include it 
  63.      with any commercial package, or feed it to a whale.. However, I would 
  64.      appreciate it if you kept this comment in the source code so that anyone
  65.      receiving this code knows who to contact in case of problems. Note that 
  66.      I do not demand this condition..
  67.  
  68.   ---------------------------------------------------------------------------*/
  69.  
  70.  
  71.  
  72.  
  73. /*****************************/
  74. /*  Includes, Defines, etc.  */
  75. /*****************************/
  76.  
  77. #include <descrip.h>
  78. #include <rms.h>
  79. #include <stdio.h>
  80. #include <iodef.h>
  81. #include <atrdef.h>   /* this gets created with the c3.0 compiler */
  82. #include <fibdef.h>   /* this gets created with the c3.0 compiler */
  83.  
  84. #include "VMSmunch.h"  /* GET/SET_TIMES, RTYPE, fatdef.h, etc. */
  85.  
  86. #define RTYPE     fat$r_rtype_overlay.fat$r_rtype_bits
  87. #define RATTRIB   fat$r_rattrib_overlay.fat$r_rattrib_bits
  88.  
  89. /*
  90.  *  Under Alpha, the FIB unions are declared as variant_unions.
  91.  *  FIBDEF.H includes the definition of __union, which we check
  92.  *  below to make sure we access the structure correctly.
  93.  */
  94. #if defined(__union) && (__union == variant_union)
  95. #define FIB$W_FID     fib$w_fid
  96. #define FIB$W_DID     fib$w_did
  97. #define FIB$L_ACCTL   fib$l_acctl
  98. #else
  99. #define FIB$W_FID     fib$r_fid_overlay.fib$w_fid
  100. #define FIB$W_DID     fib$r_did_overlay.fib$w_did
  101. #define FIB$L_ACCTL   fib$r_acctl_overlay.fib$l_acctl
  102. #endif        /* #if __union == variant_union */
  103. static void asctim();
  104. static void bintim();
  105.  
  106. struct VMStimbuf {      /* VMSmunch */
  107.     char *actime;       /* VMS revision date, ASCII format */
  108.     char *modtime;      /* VMS creation date, ASCII format */
  109. };
  110.  
  111. /* from <ssdef.h> */
  112. #ifndef SS$_NORMAL
  113. #  define SS$_NORMAL    1
  114. #  define SS$_BADPARAM  20
  115. #endif
  116.  
  117.  
  118.  
  119.  
  120.  
  121. /*************************/
  122. /*  Function VMSmunch()  */
  123. /*************************/
  124.  
  125. int VMSmunch( filename, action, ptr )
  126.     char  *filename, *ptr;
  127.     int   action;
  128. {
  129.  
  130.     /* original file.c variables */
  131.  
  132.     static struct FAB Fab;
  133.     static struct NAM Nam;
  134.     static struct fibdef Fib; /* short fib */
  135.  
  136.     static struct dsc$descriptor FibDesc =
  137.       {sizeof(Fib),DSC$K_DTYPE_Z,DSC$K_CLASS_S,&Fib};
  138.     static struct dsc$descriptor_s DevDesc =
  139.       {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,&Nam.nam$t_dvi[1]};
  140.     static struct fatdef Fat;
  141.     static union {
  142.       struct fchdef fch;
  143.       long int dummy;
  144.     } uchar;
  145.     static struct fjndef jnl;
  146.     static long int Cdate[2],Rdate[2],Edate[2],Bdate[2];
  147.     static short int revisions;
  148.     static unsigned long uic;
  149.     static union {
  150.       unsigned short int value;
  151.       struct {
  152.         unsigned system : 4;
  153.         unsigned owner : 4;
  154.         unsigned group : 4;
  155.         unsigned world : 4;
  156.       } bits;
  157.     } prot;
  158.  
  159.     static struct atrdef Atr[] = {
  160.       {sizeof(Fat),ATR$C_RECATTR,&Fat},        /* record attributes */
  161.       {sizeof(uchar),ATR$C_UCHAR,&uchar},      /* File characteristics */
  162.       {sizeof(Cdate),ATR$C_CREDATE,&Cdate[0]}, /* Creation date */
  163.       {sizeof(Rdate),ATR$C_REVDATE,&Rdate[0]}, /* Revision date */
  164.       {sizeof(Edate),ATR$C_EXPDATE,&Edate[0]}, /* Expiration date */
  165.       {sizeof(Bdate),ATR$C_BAKDATE,&Bdate[0]}, /* Backup date */
  166.       {sizeof(revisions),ATR$C_ASCDATES,&revisions}, /* number of revisions */
  167.       {sizeof(prot),ATR$C_FPRO,&prot},         /* file protection  */
  168.       {sizeof(uic),ATR$C_UIC,&uic},            /* file owner */
  169.       {sizeof(jnl),ATR$C_JOURNAL,&jnl},        /* journal flags */
  170.       {0,0,0}
  171.     } ;
  172.  
  173.     static char EName[NAM$C_MAXRSS];
  174.     static char RName[NAM$C_MAXRSS];
  175.     static struct dsc$descriptor_s FileName =
  176.       {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  177.     static struct dsc$descriptor_s string = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  178.     static short int DevChan;
  179.     static short int iosb[4];
  180.  
  181.     static long int i,status;
  182. /*  static char *retval;  */
  183.  
  184.  
  185.     /* new VMSmunch variables */
  186.  
  187.     static int  old_rtype=FAT$C_FIXED;   /* storage for record type */
  188.  
  189.  
  190.  
  191. /*---------------------------------------------------------------------------
  192.     Initialize attribute blocks, parse filename, resolve any wildcards, and
  193.     get the file info.
  194.   ---------------------------------------------------------------------------*/
  195.  
  196.     /* initialize RMS structures, we need a NAM to retrieve the FID */
  197.     Fab = cc$rms_fab;
  198.     Fab.fab$l_fna = filename;
  199.     Fab.fab$b_fns = strlen(filename);
  200.     Fab.fab$l_nam = &Nam; /* FAB has an associated NAM */
  201.     Nam = cc$rms_nam;
  202.     Nam.nam$l_esa = &EName; /* expanded filename */
  203.     Nam.nam$b_ess = sizeof(EName);
  204.     Nam.nam$l_rsa = &RName; /* resultant filename */
  205.     Nam.nam$b_rss = sizeof(RName);
  206.  
  207.     /* do $PARSE and $SEARCH here */
  208.     status = sys$parse(&Fab);
  209.     if (!(status & 1)) return(status);
  210.  
  211.     /* search for the first file.. If none signal error */
  212.     status = sys$search(&Fab);
  213.     if (!(status & 1)) return(status);
  214.  
  215.     while (status & 1) {
  216.         /* initialize Device name length, note that this points into the NAM
  217.            to get the device name filled in by the $PARSE, $SEARCH services */
  218.         DevDesc.dsc$w_length = Nam.nam$t_dvi[0];
  219.  
  220.         status = sys$assign(&DevDesc,&DevChan,0,0);
  221.         if (!(status & 1)) return(status);
  222.  
  223.         FileName.dsc$a_pointer = Nam.nam$l_name;
  224.         FileName.dsc$w_length = Nam.nam$b_name+Nam.nam$b_type+Nam.nam$b_ver;
  225.  
  226.         /* Initialize the FIB */
  227.         for (i=0;i<3;i++)
  228.             Fib.FIB$W_FID[i]=Nam.nam$w_fid[i];
  229.         for (i=0;i<3;i++)
  230.             Fib.FIB$W_DID[i]=Nam.nam$w_did[i];
  231.  
  232.         /* Use the IO$_ACCESS function to return info about the file */
  233.         /* Note, used this way, the file is not opened, and the expiration */
  234.         /* and revision dates are not modified */
  235.         status = sys$qiow(0,DevChan,IO$_ACCESS,&iosb,0,0,
  236.                           &FibDesc,&FileName,0,0,&Atr,0);
  237.         if (!(status & 1)) return(status);
  238.         status = iosb[0];
  239.         if (!(status & 1)) return(status);
  240.  
  241.     /*-----------------------------------------------------------------------
  242.         We have the current information from the file:  now see what user
  243.         wants done with it.
  244.       -----------------------------------------------------------------------*/
  245.  
  246.         switch (action) {
  247.  
  248.           case GET_TIMES:
  249.               asctim(((struct VMStimbuf *)ptr)->modtime, Cdate);
  250.               asctim(((struct VMStimbuf *)ptr)->actime, Rdate);
  251.               break;
  252.  
  253.           case SET_TIMES:
  254.               bintim(((struct VMStimbuf *)ptr)->modtime, Cdate);
  255.               bintim(((struct VMStimbuf *)ptr)->actime, Rdate);
  256.               break;
  257.  
  258.           case GET_RTYPE:   /* non-modifying */
  259.               *(int *)ptr = Fat.RTYPE.fat$v_rtype;
  260.               return RMS$_NORMAL;     /* return to user */
  261.               break;
  262.  
  263.           case CHANGE_RTYPE:
  264.               old_rtype = Fat.RTYPE.fat$v_rtype;         /* save current one */
  265.               if ((*(int *)ptr < FAT$C_UNDEFINED) || 
  266.                   (*(int *)ptr > FAT$C_STREAMCR))
  267.                   Fat.RTYPE.fat$v_rtype = FAT$C_STREAMLF;  /* Unix I/O happy */
  268.               else
  269.                   Fat.RTYPE.fat$v_rtype = *(int *)ptr;
  270.               break;
  271.  
  272.           case RESTORE_RTYPE:
  273.               Fat.RTYPE.fat$v_rtype = old_rtype;
  274.               break;
  275.  
  276.           default:
  277.               return SS$_BADPARAM;   /* anything better? */
  278.         }
  279.  
  280.     /*-----------------------------------------------------------------------
  281.         Go back and write modified data to the file header.
  282.       -----------------------------------------------------------------------*/
  283.  
  284.         /* note, part of the FIB was cleared by earlier QIOW, so reset it */
  285.         Fib.FIB$L_ACCTL = FIB$M_NORECORD;
  286.         for (i=0;i<3;i++)
  287.             Fib.FIB$W_FID[i]=Nam.nam$w_fid[i];
  288.         for (i=0;i<3;i++)
  289.             Fib.FIB$W_DID[i]=Nam.nam$w_did[i];
  290.  
  291.         /* Use the IO$_MODIFY function to change info about the file */
  292.         /* Note, used this way, the file is not opened, however this would */
  293.         /* normally cause the expiration and revision dates to be modified. */
  294.         /* Using FIB$M_NORECORD prohibits this from happening. */
  295.         status = sys$qiow(0,DevChan,IO$_MODIFY,&iosb,0,0,
  296.                           &FibDesc,&FileName,0,0,&Atr,0);
  297.         if (!(status & 1)) return(status);
  298.  
  299.         status = iosb[0];
  300.         if (!(status & 1)) return(status);
  301.  
  302.         status = sys$dassgn(DevChan);
  303.         if (!(status & 1)) return(status);
  304.  
  305.         /* look for next file, if none, no big deal.. */
  306.         status = sys$search(&Fab);
  307.     }
  308. } /* end function VMSmunch() */
  309.  
  310.  
  311.  
  312.  
  313.  
  314. /***********************/
  315. /*  Function bintim()  */
  316. /***********************/
  317.  
  318. void asctim(time,binval)   /* convert 64-bit binval to string, put in time */
  319.     char *time;
  320.     long int binval[2];
  321. {
  322.     static struct dsc$descriptor date_str={23,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  323.       /* dsc$w_length, dsc$b_dtype, dsc$b_class, dsc$a_pointer */
  324.  
  325.     date_str.dsc$a_pointer = time;
  326.     sys$asctim(0, &date_str, binval, 0);
  327.     time[23] = '\0';
  328. }
  329.  
  330.  
  331.  
  332.  
  333.  
  334. /***********************/
  335. /*  Function bintim()  */
  336. /***********************/
  337.  
  338. void bintim(time,binval)   /* convert time string to 64 bits, put in binval */
  339.     char *time;
  340.     long int binval[2];
  341. {
  342.     static struct dsc$descriptor date_str={0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  343.  
  344.     date_str.dsc$w_length = strlen(time);
  345.     date_str.dsc$a_pointer = time;
  346.     sys$bintim(&date_str, binval);
  347. }
  348.