home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / SOFTWARE / SOURCES / UNZIP52.ZIP / atari / atari.c (.txt) next >
C/C++ Source or Header  |  1996-04-20  |  26KB  |  711 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   atari.c
  4.  
  5.   Atari-specific routines for use with Info-ZIP's UnZip 5.1 and later.
  6.  
  7.   Contains:  readdir()
  8.              do_wild()           <-- generic enough to put in fileio.c?
  9.              mapattr()
  10.              mapname()
  11.              checkdir()
  12.              mkdir()
  13.              close_outfile()
  14.              version()
  15.  
  16.   Due to the amazing MiNT library being very, very close to BSD unix's
  17.   library, I'm using the unix.c as a base for this.  Note:  If you're not
  18.   going to compile this with the MiNT libraries (for GNU C, Turbo C, Pure C,
  19.   Lattice C, or Heat & Serve C), you're going to be in for some nasty work.
  20.   Most of the modifications in this file were made by Chris Herborth
  21.   (cherborth@semprini.waterloo-rdp.on.ca) and /should/ be marked with [cjh].
  22.  
  23.   ---------------------------------------------------------------------------*/
  24.  
  25.  
  26. #define UNZIP_INTERNAL
  27. #include "unzip.h"
  28. #include <dirent.h>            /* MiNTlibs has dirent [cjh] */
  29.  
  30. static int created_dir;        /* used in mapname(), checkdir() */
  31. static int renamed_fullpath;   /* ditto */
  32.  
  33.  
  34. #ifndef SFX
  35.  
  36. /**********************/
  37. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  38. /**********************/
  39.  
  40. char *do_wild(__G__ wildspec)
  41.     __GDEF
  42.     char *wildspec;         /* only used first time on a given dir */
  43. {
  44.     static DIR *dir = (DIR *)NULL;
  45.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  46.     static int firstcall=TRUE, have_dirname, dirnamelen;
  47.     struct dirent *file;
  48.  
  49.  
  50.     /* Even when we're just returning wildspec, we *always* do so in
  51.      * matchname[]--calling routine is allowed to append four characters
  52.      * to the returned string, and wildspec may be a pointer to argv[].
  53.      */
  54.     if (firstcall) {        /* first call:  must initialize everything */
  55.         firstcall = FALSE;
  56.  
  57.         /* break the wildspec into a directory part and a wildcard filename */
  58.         if ((wildname = strrchr(wildspec, '/')) == (char *)NULL) {
  59.             dirname = ".";
  60.             dirnamelen = 1;
  61.             have_dirname = FALSE;
  62.             wildname = wildspec;
  63.         } else {
  64.             ++wildname;     /* point at character after '/' */
  65.             dirnamelen = wildname - wildspec;
  66.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  67.                 Info(slide, 0x201, ((char *)slide,
  68.                   "warning:  can't allocate wildcard buffers\n"));
  69.                 strcpy(matchname, wildspec);
  70.                 return matchname;   /* but maybe filespec was not a wildcard */
  71.             }
  72.             strncpy(dirname, wildspec, dirnamelen);
  73.             dirname[dirnamelen] = '\0';   /* terminate for strcpy below */
  74.             have_dirname = TRUE;
  75.         }
  76.  
  77.         if ((dir = opendir(dirname)) != (DIR *)NULL) {
  78.             while ((file = readdir(dir)) != (struct dirent *)NULL) {
  79.                 if (file->d_name[0] == '.' && wildname[0] != '.')
  80.                     continue;  /* Unix:  '*' and '?' do not match leading dot */
  81.                     /* Need something here for TOS filesystem? [cjh] */
  82.                 if (match(file->d_name, wildname, 0)) {  /* 0 == case sens. */
  83.                     if (have_dirname) {
  84.                         strcpy(matchname, dirname);
  85.                         strcpy(matchname+dirnamelen, file->d_name);
  86.                     } else
  87.                         strcpy(matchname, file->d_name);
  88.                     return matchname;
  89.                 }
  90.             }
  91.             /* if we get to here directory is exhausted, so close it */
  92.             closedir(dir);
  93.             dir = (DIR *)NULL;
  94.         }
  95.  
  96.         /* return the raw wildspec in case that works (e.g., directory not
  97.          * searchable, but filespec was not wild and file is readable) */
  98.         strcpy(matchname, wildspec);
  99.         return matchname;
  100.     }
  101.  
  102.     /* last time through, might have failed opendir but returned raw wildspec */
  103.     if (dir == (DIR *)NULL) {
  104.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  105.         if (have_dirname)
  106.             free(dirname);
  107.         return (char *)NULL;
  108.     }
  109.  
  110.     /* If we've gotten this far, we've read and matched at least one entry
  111.      * successfully (in a previous call), so dirname has been copied into
  112.      * matchname already.
  113.      */
  114.     while ((file = readdir(dir)) != (struct dirent *)NULL)
  115.         /* May need special TOS handling here. [cjh] */
  116.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  117.             if (have_dirname) {
  118.                 /* strcpy(matchname, dirname); */
  119.                 strcpy(matchname+dirnamelen, file->d_name);
  120.             } else
  121.                 strcpy(matchname, file->d_name);
  122.             return matchname;
  123.         }
  124.  
  125.     closedir(dir);     /* have read at least one dir entry; nothing left */
  126.     dir = (DIR *)NULL;
  127.     firstcall = TRUE;  /* reset for new wildspec */
  128.     if (have_dirname)
  129.         free(dirname);
  130.     return (char *)NULL;
  131.  
  132. } /* end function do_wild() */
  133.  
  134. #endif /* !SFX */
  135.  
  136.  
  137.  
  138.  
  139.  
  140. /**********************/
  141. /* Function mapattr() */
  142. /**********************/
  143.  
  144. int mapattr(__G)
  145.     __GDEF
  146. {
  147.     ulg tmp = G.crec.external_file_attributes;
  148.  
  149.     switch (G.pInfo->hostnum) {
  150.         case UNIX_:
  151.         case ATARI_:
  152.             /* minix filesystem under MiNT on Atari [cjh] */
  153.         case VMS_:
  154.             G.pInfo->file_attr = (unsigned)(tmp >> 16);
  155.             return 0;
  156.         case AMIGA_:
  157.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  158.             G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  159.             break;
  160.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  161.         case FS_FAT_:
  162.         case FS_HPFS_:
  163.         case FS_NTFS_:
  164.         case MAC_:
  165.         case TOPS20_:
  166.         default:
  167.             tmp = !(tmp & 1) << 1;   /* read-only bit --> write perms bits */
  168.             G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  169.             break;
  170.     } /* end switch (host-OS-created-by) */
  171.  
  172.     /* for originating systems with no concept of "group," "other," "system": */
  173.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  174.     G.pInfo->file_attr &= ~tmp;
  175.  
  176.     return 0;
  177.  
  178. } /* end function mapattr() */
  179.  
  180.  
  181.  
  182.  
  183.  
  184. /************************/
  185. /*  Function mapname()  */
  186. /************************/
  187.  
  188. int mapname(__G__ renamed)   /* return 0 if no error, 1 if caution (filename */
  189.     __GDEF                   /* truncated), 2 if warning (skip file because  */
  190.     int renamed;             /* dir doesn't exist), 3 if error (skip file),  */
  191.                              /* 10 if no memory (skip file)                  */
  192. {
  193.     char pathcomp[FILNAMSIZ];    /* path-component buffer */
  194.     char *pp, *cp=(char *)NULL;  /* character pointers */
  195.     char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  196.     int quote = FALSE;           /* flags */
  197.     int error = 0;
  198.     register unsigned workch;    /* hold the character being tested */
  199.  
  200.  
  201. /*---------------------------------------------------------------------------
  202.     Initialize various pointers and counters and stuff.
  203.   ---------------------------------------------------------------------------*/
  204.  
  205.     if (G.pInfo->vollabel)
  206.         return IZ_VOL_LABEL;    /* can't set disk volume labels in Unix */
  207.  
  208.     /* can create path as long as not just freshening, or if user told us */
  209.     G.create_dirs = (!G.fflag || renamed);
  210.  
  211.     created_dir = FALSE;        /* not yet */
  212.  
  213.     /* user gave full pathname:  don't prepend rootpath */
  214.     renamed_fullpath = (renamed && (*G.filename == '/'));
  215.  
  216.     if (checkdir(__G__ (char *)NULL, INIT) == 10)
  217.         return 10;              /* initialize path buffer, unless no memory */
  218.  
  219.     *pathcomp = '\0';           /* initialize translation buffer */
  220.     pp = pathcomp;              /* point to translation buffer */
  221.     if (G.jflag)                /* junking directories */
  222.         cp = (char *)strrchr(G.filename, '/');
  223.     if (cp == (char *)NULL)     /* no '/' or not junking dirs */
  224.         cp = G.filename;        /* point to internal zipfile-member pathname */
  225.     else
  226.         ++cp;