summaryrefslogtreecommitdiff
path: root/vms/vmsmunch.c
blob: 81400364756835592d3b99b4a5d7bd260c5a6064 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
  Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 2007-Mar-4 or later
  (the contents of which are also included in zip.h) for terms of use.
  If, for some reason, all these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
*/

/* 2004-12-13 SMS.
 * Disabled the module name macro to accommodate old GNU C which didn't
 * obey the directive, and thus confused MMS/MMK where the object
 * library dependencies need to have the correct module name.
 */
#if 0
# define module_name     VMSMUNCH
# define module_version  "V1.3-4"
#endif /* 0 */

/*
 *  Modified by:
 *
 *    v1.3.1        O.v.d.Linden, C. Spieler            04-JUL-1998 14:35
 *            Modified check that decides on the type of definitions for
 *            FIB$W_FID etc. to support GNU C.
 *
 *    v1.3          Hunter Goatley                      14-SEP-1992 08:51
 *            Added definitions of FIB$W_FID, FIB$W_DID, and
 *            FIB$L_ACCTL to allow for the fact that fibdef
 *            contains variant_unions under Alpha.
 */
/*---------------------------------------------------------------------------

  vmsmunch.c                    version 1.2                     28 Apr 1992

  This routine is a blatant and unrepentent appropriation of all the nasty
  and difficult-to-do and complicated VMS shenanigans which Joe Meadows has
  so magnificently captured in his FILE utility.  Not only that, it's even
  allowed! (see below).  But let it be clear at the outset that Joe did all
  the work; yea, verily, he is truly a godlike unit.

  The appropriations and modifications herein were performed primarily by
  him known as "Cave Newt," although the Info-ZIP working group probably had
  their fingers in it somewhere along the line.  The idea is to put the raw
  power of Joe's original routine at the disposal of various routines used
  by UnZip (and Zip, possibly), not least among them the utime() function.
  Read on for details...

        01-SEP-1994     Richard Levitte <levitte@e.kth.se>
                        If one of the fields given to VMSmunch are NULL,
                        do not update the corresponding daytime.

        18-JUL-1994     Hunter Goatley <goathunter@WKU.EDU>
                        Fixed IO$_ACCESS call.

        18-Jul-1994     Richard Levitte levitte@e.kth.se
                        Changed VMSmunch() to deassign the channel before
                        returning when an error has occured.

        02-Apr-1994     Jamie Hanrahan  jeh@cmkrnl.com
                        Moved definition of VMStimbuf struct from here
                        to vmsmunch.h
  ---------------------------------------------------------------------------

  Usage (i.e., "interface," in geek-speak):

     int VMSmunch( char *filename, int action, char *ptr );

     filename   the name of the file on which to be operated, obviously
     action     an integer which specifies what action to take
     ptr        pointer to any extra item which may be needed (else NULL)

  The possible values for the action argument are as follows:

     GET_TIMES      get the creation and revision dates of filename; ptr
                    must point to an empty VMStimbuf struct, as defined
                    in vmsmunch.h
                    (with room for at least 24 characters, including term.)
     SET_TIMES      set the creation and revision dates of filename (utime
                    option); ptr must point to a valid VMStimbuf struct,
                    as defined in vmsmunch.h
     GET_RTYPE      get the record type of filename; ptr must point to an
                    integer which, on return, is set to the type (as defined
                    in vmsdefs.h:  FAT$C_* defines)
     CHANGE_RTYPE   change the record type to that specified by the integer
                    to which ptr points; save the old record type (later
                    saves overwrite earlier ones)
     RESTORE_RTYPE  restore the record type to the previously saved value;
                    or, if none, set it to "fixed-length, 512-byte" record
                    format (ptr not used)

  ---------------------------------------------------------------------------

  Comments from FILE.C, a utility to modify file characteristics:

     Written by Joe Meadows Jr, at the Fred Hutchinson Cancer Research Center
     BITNET: JOE@FHCRCVAX
     PHONE: (206) 467-4970

     There are no restrictions on this code, you may sell it, include it
     with any commercial package, or feed it to a whale.. However, I would
     appreciate it if you kept this comment in the source code so that anyone
     receiving this code knows who to contact in case of problems. Note that
     I do not demand this condition..

  ---------------------------------------------------------------------------*/


/* 2004-12-13 SMS.
 * Disabled the module name macro to accommodate old GNU C which didn't
 * obey the directive, and thus confused MMS/MMK where the object
 * library dependencies need to have the correct module name.
 */
#if 0
# if defined(__DECC) || defined(__GNUC__)
#  pragma module module_name module_version
# else
#  module module_name module_version
# endif
#endif /* 0 */

/*****************************/
/*  Includes, Defines, etc.  */
/*****************************/

/* Accomodation for /NAMES = AS_IS with old header files. */

#define sys$asctim SYS$ASCTIM
#define sys$assign SYS$ASSIGN
#define sys$bintim SYS$BINTIM
#define sys$dassgn SYS$DASSGN
#define sys$parse SYS$PARSE
#define sys$qiow SYS$QIOW
#define sys$search SYS$SEARCH

#include "zip.h"

#include <stdio.h>
#include <string.h>
#include <iodef.h>
#include <starlet.h>
#include <fibdef.h>   /* this gets created with the c3.0 compiler */

/*
 *  Under Alpha (DEC C in VAXC mode) and under `good old' VAXC, the FIB unions
 *  are declared as variant_unions.  DEC C (Alpha) in ANSI modes and third
 *  party compilers which do not support `variant_union' define preprocessor
 *  symbols to `hide' the "intermediate union/struct" names from the
 *  programmer's API.
 *  We check the presence of these defines and for DEC's FIBDEF.H defining
 *  __union as variant_union to make sure we access the structure correctly.
 */
#if defined(fib$w_did) || (defined(__union) && (__union == variant_union))
#  define FIB$W_DID     fib$w_did
#  define FIB$W_FID     fib$w_fid
#  define FIB$L_ACCTL   fib$l_acctl
#else
#  define FIB$W_DID     fib$r_did_overlay.fib$w_did
#  define FIB$W_FID     fib$r_fid_overlay.fib$w_fid
#  define FIB$L_ACCTL   fib$r_acctl_overlay.fib$l_acctl
#endif

#include "vms.h"
#include "vmsmunch.h"  /* GET/SET_TIMES, RTYPE, etc. */
#include "vmsdefs.h"   /* fatdef.h, etc. */

static void asctim(char *time, long int binval[2]);
static void bintim(char *time, long int binval[2]);

/* from <ssdef.h> */
#ifndef SS$_NORMAL
#  define SS$_NORMAL    1
#  define SS$_BADPARAM  20
#endif


/* On VAX, define Goofy VAX Type-Cast to obviate /standard = vaxc.
   Otherwise, lame system headers on VAX cause compiler warnings.
   (GNU C may define vax but not __VAX.)
*/
#ifdef vax
# define __VAX 1
#endif /* def vax */

#ifdef __VAX
# define GVTC (unsigned int)
#else /* def __VAX */
# define GVTC
#endif /* def __VAX */


/*************************/
/*  Function VMSmunch()  */
/*************************/

int VMSmunch(
    char  *filename,
    int   action,
    char  *ptr )
{

    /* original file.c variables */

    static struct FAB Fab;
    static struct NAM_STRUCT Nam;
    static struct fibdef Fib; /* short fib */

    static struct dsc$descriptor FibDesc =
      {sizeof(Fib),DSC$K_DTYPE_Z,DSC$K_CLASS_S,(char *)&Fib};
    static struct dsc$descriptor_s DevDesc =
      {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,&Nam.NAM_DVI[1]};
    static struct fatdef Fat;
    static union {
      struct fchdef fch;
      long int dummy;
    } uchar;
    static struct fjndef jnl;
    static long int Cdate[2],Rdate[2],Edate[2],Bdate[2];
    static short int revisions;
    static unsigned long uic;
#if defined(__DECC) || defined(__DECCXX)
#pragma __member_alignment __save
#pragma __nomember_alignment
#endif /* __DECC || __DECCXX */
    static union {
      unsigned short int value;
      struct {
        unsigned system : 4;
        unsigned owner : 4;
        unsigned group : 4;
        unsigned world : 4;
      } bits;
    } prot;
#if defined(__DECC) || defined(__DECCXX)
#pragma __member_alignment __restore
#endif /* __DECC || __DECCXX */

    static struct atrdef Atr[] = {
      {sizeof(Fat),ATR$C_RECATTR, GVTC &Fat},          /* record attributes */
      {sizeof(uchar),ATR$C_UCHAR, GVTC &uchar},    /* File characteristics */
      {sizeof(Cdate),ATR$C_CREDATE, GVTC &Cdate[0]},   /* Creation date */
      {sizeof(Rdate),ATR$C_REVDATE, GVTC &Rdate[0]},   /* Revision date */
      {sizeof(Edate),ATR$C_EXPDATE, GVTC &Edate[0]},   /* Expiration date */
      {sizeof(Bdate),ATR$C_BAKDATE, GVTC &Bdate[0]},   /* Backup date */
      {sizeof(revisions),ATR$C_ASCDATES, GVTC &revisions}, /* number of revs */
      {sizeof(prot),ATR$C_FPRO, GVTC &prot},           /* file protection  */
      {sizeof(uic),ATR$C_UIC, GVTC &uic},              /* file owner */
      {sizeof(jnl),ATR$C_JOURNAL, GVTC &jnl},          /* journal flags */
      {0,0,0}
    } ;

    static char EName[NAM_MAXRSS];
    static char RName[NAM_MAXRSS];
    static struct dsc$descriptor_s FileName =
      {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
    static struct dsc$descriptor_s string = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
    static short int DevChan;
    static short int iosb[4];

    static long int i,status;
/*  static char *retval;  */


    /* new VMSmunch variables */

    static int  old_rtype=FAT$C_FIXED;   /* storage for record type */



/*---------------------------------------------------------------------------
    Initialize attribute blocks, parse filename, resolve any wildcards, and
    get the file info.
  ---------------------------------------------------------------------------*/

    /* Initialize RMS structures.  We need a NAM[L] to retrieve the FID. */
    Fab = cc$rms_fab;
    Fab.fab$l_fna = filename;
    Fab.fab$b_fns = strlen(filename);
    Fab.FAB_NAM = &Nam; /* FAB has an associated NAM[L]. */
    Nam = CC_RMS_NAM;
    Nam.NAM_ESA = EName; /* expanded filename */
    Nam.NAM_ESS = sizeof(EName);
    Nam.NAM_RSA = RName; /* resultant filename */
    Nam.NAM_RSS = sizeof(RName);

    /* do $PARSE and $SEARCH here */
    status = sys$parse(&Fab);
    if (!(status & 1)) return(status);

    /* search for the first file.. If none signal error */
    status = sys$search(&Fab);
    if (!(status & 1)) return(status);

    while (status & 1) {
        /* initialize Device name length, note that this points into the NAM[L]
           to get the device name filled in by the $PARSE, $SEARCH services */
        DevDesc.dsc$w_length = Nam.NAM_DVI[0];

        status = sys$assign(&DevDesc,&DevChan,0,0);
        if (!(status & 1)) return(status);

        FileName.dsc$a_pointer = Nam.NAM_L_NAME;
        FileName.dsc$w_length = Nam.NAM_B_NAME+Nam.NAM_B_TYPE+Nam.NAM_B_VER;

        /* Initialize the FIB */
        for (i=0;i<3;i++) {
            Fib.FIB$W_FID[i]=Nam.NAM_FID[i];
            Fib.FIB$W_DID[i]=Nam.NAM_DID[i];
        }

        /* Use the IO$_ACCESS function to return info about the file */
        /* Note, used this way, the file is not opened, and the expiration */
        /* and revision dates are not modified */
        status = sys$qiow(0,DevChan,IO$_ACCESS,&iosb,0,0,
                          &FibDesc,&FileName,0,0,&Atr,0);
        if (!(status & 1) || !((status = iosb[0]) & 1)) {
            sys$dassgn(DevChan);
            return(status);
        }

    /*-----------------------------------------------------------------------
        We have the current information from the file:  now see what user
        wants done with it.
      -----------------------------------------------------------------------*/

        switch (action) {

          case GET_TIMES:   /* non-modifying */
              asctim(((struct VMStimbuf *)ptr)->modtime, Cdate);
              asctim(((struct VMStimbuf *)ptr)->actime, Rdate);
              sys$dassgn(DevChan);
              return RMS$_NORMAL;     /* return to user */
              break;

          case SET_TIMES:
              if (((struct VMStimbuf *)ptr)->modtime != (char *)NULL)
                  bintim(((struct VMStimbuf *)ptr)->modtime, Cdate);
              if (((struct VMStimbuf *)ptr)->actime != (char *)NULL)
                  bintim(((struct VMStimbuf *)ptr)->actime, Rdate);
              break;

          case GET_RTYPE:   /* non-modifying */
              *(int *)ptr = Fat.fat$v_rtype;
              sys$dassgn(DevChan);
              return RMS$_NORMAL;     /* return to user */
              break;

          case CHANGE_RTYPE:
              old_rtype = Fat.fat$v_rtype;              /* save current one */
              if ((*(int *)ptr < FAT$C_UNDEFINED) ||
                  (*(int *)ptr > FAT$C_STREAMCR))
                  Fat.fat$v_rtype = FAT$C_STREAMLF;       /* Unix I/O happy */
              else
                  Fat.fat$v_rtype = *(int *)ptr;
              break;

          case RESTORE_RTYPE:
              Fat.fat$v_rtype = old_rtype;
              break;

          default:
              sys$dassgn(DevChan);
              return SS$_BADPARAM;   /* anything better? */
        }

    /*-----------------------------------------------------------------------
        Go back and write modified data to the file header.
      -----------------------------------------------------------------------*/

        /* note, part of the FIB was cleared by earlier QIOW, so reset it */
        Fib.FIB$L_ACCTL = FIB$M_NORECORD;
        for (i=0;i<3;i++) {
            Fib.FIB$W_FID[i]=Nam.NAM_FID[i];
            Fib.FIB$W_DID[i]=Nam.NAM_DID[i];
        }

        /* Use the IO$_MODIFY function to change info about the file */
        /* Note, used this way, the file is not opened, however this would */
        /* normally cause the expiration and revision dates to be modified. */
        /* Using FIB$M_NORECORD prohibits this from happening. */
        status = sys$qiow(0,DevChan,IO$_MODIFY,&iosb,0,0,
                          &FibDesc,&FileName,0,0,&Atr,0);
        if (!(status & 1) || !((status = iosb[0]) & 1)) {
            sys$dassgn(DevChan);
            return(status);
        }

        status = sys$dassgn(DevChan);
        if (!(status & 1)) return(status);

        /* look for next file, if none, no big deal.. */
        status = sys$search(&Fab);
    }
    return(status);
} /* end function VMSmunch() */





/***********************/
/*  Function asctim()  */
/***********************/

static void asctim(        /* convert 64-bit binval to string, put in time */
    char *time,
    long int binval[2] )
{
    static struct dsc$descriptor date_str={23,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
      /* dsc$w_length, dsc$b_dtype, dsc$b_class, dsc$a_pointer */

    date_str.dsc$a_pointer = time;
    sys$asctim(0, &date_str, binval, 0);
    time[23] = '\0';
}





/***********************/
/*  Function bintim()  */
/***********************/

static void bintim(        /* convert time string to 64 bits, put in binval */
    char *time,
    long int binval[2] )
{
    static struct dsc$descriptor date_str={0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};

    date_str.dsc$w_length = strlen(time);
    date_str.dsc$a_pointer = time;
    sys$bintim(&date_str, binval);
}