home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / cmsmvs / mvs.c < prev    next >
C/C++ Source or Header  |  1996-04-05  |  5KB  |  201 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden, George Petrov and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  * MVS specific things
  13.  */
  14. #include "mvs.h"
  15.  
  16. static int gen_node( DIR *dirp, RECORD *recptr )
  17. {
  18.    char *ptr, *name, ttr[TTRLEN];
  19.    int skip, count = 2;
  20.    unsigned int info_byte, alias, ttrn;
  21.    struct dirent *new;
  22.  
  23.    ptr = recptr->rest;
  24.    while (count < recptr->count) {
  25.       if (!memcmp( ptr, endmark, NAMELEN ))
  26.          return 1;
  27.       name = ptr;                    /* member name */
  28.       ptr += NAMELEN;
  29.       memcpy( ttr, ptr, TTRLEN );    /* ttr name    */
  30.       ptr += TTRLEN;
  31.       info_byte = (unsigned int) (*ptr);   /* info byte */
  32.       if ( !(info_byte & ALIAS_MASK) ) {   /* no alias  */
  33.          new = malloc( sizeof(struct dirent) );
  34.          if (dirp->D_list == NULL)
  35.             dirp->D_list = dirp->D_curpos = new;
  36.          else
  37.             dirp->D_curpos = (dirp->D_curpos->d_next = new);
  38.          new->d_next = NULL;
  39.          memcpy( new->d_name, name, NAMELEN );
  40.          new->d_name[NAMELEN] = '\0';
  41.          if ((name = strchr( new->d_name, ' ' )) != NULL)
  42.             *name = '\0';      /* skip trailing blanks */
  43.       }
  44.       skip = (info_byte & SKIP_MASK) * 2 + 1;
  45.       ptr += skip;
  46.       count += (TTRLEN + NAMELEN + skip);
  47.    }
  48.    return 0;
  49. }
  50.  
  51. DIR *opendir(const char *dirname)
  52. {
  53.    int bytes, list_end = 0;
  54.    DIR *dirp;
  55.    FILE *fp;
  56.    RECORD rec;
  57.  
  58.    fp = fopen( dirname, "rb" );
  59.    if (fp != NULL) {
  60.       dirp = malloc( sizeof(DIR) );
  61.       if (dirp != NULL) {
  62.          dirp->D_list = dirp->D_curpos = NULL;
  63.          strcpy( dirp->D_path, dirname );
  64.          do {
  65.             bytes = fread( &rec, 1, sizeof(rec), fp );
  66.             if (bytes == sizeof(rec))
  67.                list_end = gen_node( dirp, &rec );
  68.          } while (!feof(fp) && !list_end);
  69.          fclose( fp );
  70.          dirp->D_curpos = dirp->D_list;
  71.          return dirp;
  72.       }
  73.       fclose( fp );
  74.    }
  75.    return NULL;
  76. }
  77.  
  78. struct dirent *readdir(DIR *dirp)
  79. {
  80.    struct dirent *cur;
  81.  
  82.    cur = dirp->D_curpos;
  83.    dirp->D_curpos = dirp->D_curpos->d_next;
  84.    return cur;
  85. }
  86.  
  87. void rewinddir(DIR *dirp)
  88. {
  89.    dirp->D_curpos = dirp->D_list;
  90. }
  91.  
  92. int closedir(DIR *dirp)
  93. {
  94.    struct dirent *node;
  95.  
  96.    while (dirp->D_list != NULL) {
  97.       node = dirp->D_list;
  98.       dirp->D_list = dirp->D_list->d_next;
  99.       free( node );
  100.    }
  101.    free( dirp );
  102.    return 0;
  103. }
  104.  
  105. local char *readd(d)
  106. DIR *d;                 /* directory stream to read from */
  107. /* Return a pointer to the next name in the directory stream d, or NULL if
  108.    no more entries or an error occurs. */
  109. {
  110.   struct dirent *e;
  111.  
  112.   e = readdir(d);
  113.   return e == NULL ? (char *) NULL : e->d_name;
  114. }
  115.  
  116. int procname(n)
  117. char *n;                /* name to process */
  118. /* Process a name or sh expression to operate on (or exclude).  Return
  119.    an error code in the ZE_ class. */
  120. {
  121.   char *a;              /* path and name for recursion */
  122.   DIR *d;               /* directory stream from opendir() */
  123.   char *e;              /* pointer to name from readd() */
  124.   int m;                /* matched flag */
  125.   char *p;              /* path for recursion */
  126.   struct stat s;        /* result of stat() */
  127.   struct zlist far *z;  /* steps through zfiles list */
  128.  
  129.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  130.     return newname(n, 0);
  131.   else if (LSSTAT(n, &s))
  132.   {
  133.     /* Not a file or directory--search for shell expression in zip file */
  134.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  135.     m = 1;
  136.     for (z = zfiles; z != NULL; z = z->nxt) {
  137.       if (MATCH(p, z->zname))
  138.       {
  139.         z->mark = pcount ? filter(z->zname) : 1;
  140.         if (verbose)
  141.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  142.                z->mark ? "in" : "ex", z->name);
  143.         m = 0;
  144.       }
  145.     }
  146.     free((zvoid *)p);
  147.     return m ? ZE_MISS : ZE_OK;
  148.   }
  149.  
  150.   /* Live name--use if file, recurse if directory */
  151.   if ((s.st_mode & S_IFDIR) == 0)
  152.   {
  153.     /* add or remove name of file */
  154.     if ((m = newname(n, 0)) != ZE_OK)
  155.       return m;
  156.   } else {
  157.     /* Add trailing / to the directory name */
  158.     if ((p = malloc(strlen(n)+2)) == NULL)
  159.       return ZE_MEM;
  160.     if (strcmp(n, ".") == 0) {
  161.       *p = '\0';  /* avoid "./" prefix and do not create zip entry */
  162.     } else {
  163.       strcpy(p, n);
  164.       a = p + strlen(p);
  165.       if (a[-1] != '/')
  166.         strcpy(a, "/");
  167.       if (dirnames && (m = newname(p, 1)) != ZE_OK) {
  168.         free((zvoid *)p);
  169.         return m;
  170.       }
  171.     }
  172.     /* recurse into directory */
  173.     if (recurse && (d = opendir(n)) != NULL)
  174.     {
  175.       while ((e = readd(d)) != NULL) {
  176.         if (strcmp(e, ".") && strcmp(e, ".."))
  177.         {
  178.           if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  179.           {
  180.             closedir(d);
  181.             free((zvoid *)p);
  182.             return ZE_MEM;
  183.           }
  184.           strcat(strcpy(a, p), e);
  185.           if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  186.           {
  187.             if (m == ZE_MISS)
  188.               zipwarn("name not matched: ", a);
  189.             else
  190.               ziperr(m, a);
  191.           }
  192.           free((zvoid *)a);
  193.         }
  194.       }
  195.       closedir(d);
  196.     }
  197.     free((zvoid *)p);
  198.   } /* (s.st_mode & S_IFDIR) == 0) */
  199.   return ZE_OK;
  200. }
  201.