summaryrefslogtreecommitdiff
path: root/cmsmvs/mvs.c
blob: d7f74378693940a2092a28cc31afdeb1124c6bd1 (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
/*
  Copyright (c) 1990-1999 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 1999-Oct-05 or later
  (the contents of which are also included in zip.h) for terms of use.
  If, for some reason, both of these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.cdrom.com/pub/infozip/license.html
*/
/*
 * MVS specific things
 */
#include "zip.h"
#include "mvs.h"
#include <errno.h>

static int gen_node( DIR *dirp, RECORD *recptr )
{
   char *ptr, *name, ttr[TTRLEN];
   int skip, count = 2;
   unsigned int info_byte, alias, ttrn;
   struct dirent *new;

   ptr = recptr->rest;
   while (count < recptr->count) {
      if (!memcmp( ptr, endmark, NAMELEN ))
         return 1;
      name = ptr;                    /* member name */
      ptr += NAMELEN;
      memcpy( ttr, ptr, TTRLEN );    /* ttr name    */
      ptr += TTRLEN;
      info_byte = (unsigned int) (*ptr);   /* info byte */
      if ( !(info_byte & ALIAS_MASK) ) {   /* no alias  */
         new = malloc( sizeof(struct dirent) );
         if (dirp->D_list == NULL)
            dirp->D_list = dirp->D_curpos = new;
         else
            dirp->D_curpos = (dirp->D_curpos->d_next = new);
         new->d_next = NULL;
         memcpy( new->d_name, name, NAMELEN );
         new->d_name[NAMELEN] = '\0';
         if ((name = strchr( new->d_name, ' ' )) != NULL)
            *name = '\0';      /* skip trailing blanks */
      }
      skip = (info_byte & SKIP_MASK) * 2 + 1;
      ptr += skip;
      count += (TTRLEN + NAMELEN + skip);
   }
   return 0;
}

DIR *opendir(const char *dirname)
{
   int bytes, list_end = 0;
   DIR *dirp;
   FILE *fp;
   RECORD rec;

   fp = fopen( dirname, "rb" );
   if (fp != NULL) {
      dirp = malloc( sizeof(DIR) );
      if (dirp != NULL) {
         dirp->D_list = dirp->D_curpos = NULL;
         strcpy( dirp->D_path, dirname );
         do {
            bytes = fread( &rec, 1, sizeof(rec), fp );
            if (bytes == sizeof(rec))
               list_end = gen_node( dirp, &rec );
         } while (!feof(fp) && !list_end);
         fclose( fp );
         dirp->D_curpos = dirp->D_list;
         return dirp;
      }
      fclose( fp );
   }
   return NULL;
}

struct dirent *readdir(DIR *dirp)
{
   struct dirent *cur;

   cur = dirp->D_curpos;
   dirp->D_curpos = dirp->D_curpos->d_next;
   return cur;
}

void rewinddir(DIR *dirp)
{
   dirp->D_curpos = dirp->D_list;
}

int closedir(DIR *dirp)
{
   struct dirent *node;

   while (dirp->D_list != NULL) {
      node = dirp->D_list;
      dirp->D_list = dirp->D_list->d_next;
      free( node );
   }
   free( dirp );
   return 0;
}

local char *readd(d)
DIR *d;                 /* directory stream to read from */
/* Return a pointer to the next name in the directory stream d, or NULL if
   no more entries or an error occurs. */
{
  struct dirent *e;

  e = readdir(d);
  return e == NULL ? (char *) NULL : e->d_name;
}

int procname(n, caseflag)
char *n;                /* name to process */
int caseflag;           /* true to force case-sensitive match */
/* Process a name or sh expression to operate on (or exclude).  Return
   an error code in the ZE_ class. */
{
  char *a;              /* path and name for recursion */
  DIR *d;               /* directory stream from opendir() */
  char *e;              /* pointer to name from readd() */
  int m;                /* matched flag */
  char *p;              /* path for recursion */
  struct stat s;        /* result of stat() */
  struct zlist far *z;  /* steps through zfiles list */
  int exists;           /* 1 if file exists */

  if (strcmp(n, "-") == 0)   /* if compressing stdin */
    return newname(n, 0, caseflag);
  else if (!(exists = (LSSTAT(n, &s) == 0)))
  {
#ifdef MVS
    /* special case for MVS.  stat does not work on non-HFS files so if
     * stat fails with ENOENT, try to open the file for reading anyway.
     * If the user has no OMVS segment, stat gets an initialization error,
     * even on external files.
     */
    if (errno == ENOENT || errno == EMVSINITIAL) {
      FILE *f = fopen(n, "r");
      if (f) {
        /* stat got ENOENT but fopen worked, external file */
        fclose(f);
        exists = 1;
        memset(&s, '\0', sizeof(s));   /* stat data is unreliable for externals */
        s.st_mode = S_IFREG;           /* fudge it */
      }
    }
#endif /* MVS */
  }
  if (! exists) {
    /* Not a file or directory--search for shell expression in zip file */
    p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
    m = 1;
    for (z = zfiles; z != NULL; z = z->nxt) {
      if (MATCH(p, z->iname, caseflag))
      {
        z->mark = pcount ? filter(z->zname, caseflag) : 1;
        if (verbose)
            fprintf(mesg, "zip diagnostic: %scluding %s\n",
               z->mark ? "in" : "ex", z->name);
        m = 0;
      }
    }
    free((zvoid *)p);
    return m ? ZE_MISS : ZE_OK;
  }

  /* Live name--use if file, recurse if directory */
  if (!S_ISDIR(s.st_mode))
  {
    /* add or remove name of file */
    if ((m = newname(n, 0, caseflag)) != ZE_OK)
      return m;
  } else {
    /* Add trailing / to the directory name */
    if ((p = malloc(strlen(n)+2)) == NULL)
      return ZE_MEM;
    if (strcmp(n, ".") == 0) {
      *p = '\0';  /* avoid "./" prefix and do not create zip entry */
    } else {
      strcpy(p, n);
      a = p + strlen(p);
      if (a[-1] != '/')
        strcpy(a, "/");
      if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
        free((zvoid *)p);
        return m;
      }
    }
    /* recurse into directory */
    if (recurse && (d = opendir(n)) != NULL)
    {
      while ((e = readd(d)) != NULL) {
        if (strcmp(e, ".") && strcmp(e, ".."))
        {
          if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
          {
            closedir(d);
            free((zvoid *)p);
            return ZE_MEM;
          }
          strcat(strcpy(a, p), e);
          if ((m = procname(a, caseflag)) != ZE_OK)   /* recurse on name */
          {
            if (m == ZE_MISS)
              zipwarn("name not matched: ", a);
            else
              ziperr(m, a);
          }
          free((zvoid *)a);
        }
      }
      closedir(d);
    }
    free((zvoid *)p);
  } /* (s.st_mode & S_IFDIR) == 0) */
  return ZE_OK;
}