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

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden 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.  * NT specific functions for ZIP.
  13.  *
  14.  * The NT version of ZIP heavily relies on the MSDOS and OS2 versions,
  15.  * since we have to do similar things to switch between NTFS, HPFS and FAT.
  16.  */
  17.  
  18.  
  19. #include "zip.h"
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <time.h>
  24. #include <ctype.h>
  25. #include <windows.h>
  26. #include "win32/win32zip.h"
  27.  
  28. #define A_RONLY    0x01
  29. #define A_HIDDEN   0x02
  30. #define A_SYSTEM   0x04
  31. #define A_LABEL    0x08
  32. #define A_DIR      0x10
  33. #define A_ARCHIVE  0x20
  34.  
  35.  
  36. #define EAID     0x0009
  37.  
  38.  
  39. #ifndef UTIL
  40.  
  41. extern int noisy;
  42.  
  43.  
  44. /* FAT / HPFS detection */
  45.  
  46. int IsFileSystemOldFAT(char *dir)
  47. {
  48.   char root[4];
  49.   char vname[128];
  50.   DWORD vnamesize = sizeof(vname);
  51.   DWORD vserial;
  52.   DWORD vfnsize;
  53.   DWORD vfsflags;
  54.   char vfsname[128];
  55.   DWORD vfsnamesize = sizeof(vfsname);
  56.   
  57.     /*
  58.      * We separate FAT and HPFS+other file systems here.
  59.      * I consider other systems to be similar to HPFS/NTFS, i.e.
  60.      * support for long file names and being case sensitive to some extent.
  61.      */
  62.  
  63.     strncpy(root, dir, 3);
  64.     if ( isalpha(root[0]) && (root[1] == ':') ) {
  65.       root[0] = to_up(dir[0]);
  66.       root[2] = '\\';
  67.       root[3] = 0;
  68.     }
  69.     else {
  70.       root[0] = '\\';
  71.       root[1] = 0;
  72.     }
  73.  
  74.     if ( !GetVolumeInformation(root, vname, vnamesize,
  75.                          &vserial, &vfnsize, &vfsflags,
  76.                          vfsname, vfsnamesize)) {
  77.         fprintf(mesg, "zip diagnostic: GetVolumeInformation failed\n");
  78.         return(FALSE);
  79.     }
  80.  
  81.     return vfnsize <= 12;
  82. }
  83.  
  84. /* access mode bits and time stamp */
  85.  
  86. int GetFileMode(char *name)
  87. {
  88. DWORD dwAttr;
  89.  
  90.   dwAttr = GetFileAttributes(name);
  91.   if ( dwAttr == -1 ) {
  92.     fprintf(mesg, "zip diagnostic: GetFileAttributes failed\n");
  93.     return(0x20); /* the most likely, though why the error? security? */
  94.   }
  95.   return(
  96.           (dwAttr&FILE_ATTRIBUTE_READONLY  ? A_RONLY   :0)
  97.         | (dwAttr&FILE_ATTRIBUTE_HIDDEN    ? A_HIDDEN  :0)
  98.         | (dwAttr&FILE_ATTRIBUTE_SYSTEM    ? A_SYSTEM  :0)
  99.         | (dwAttr&FILE_ATTRIBUTE_DIRECTORY ? A_DIR     :0)
  100.         | (dwAttr&FILE_ATTRIBUTE_ARCHIVE   ? A_ARCHIVE :0));
  101. }
  102.  
  103. long GetTheFileTime(char *name)
  104. {
  105. HANDLE h;
  106. FILETIME ft, lft;
  107. WORD dh, dl;
  108.  
  109.   h = CreateFile(name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  110.                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  111.   if ( h != INVALID_HANDLE_VALUE ) {
  112.     GetFileTime(h, NULL, NULL, &ft);
  113.     FileTimeToLocalFileTime( &ft, &lft);
  114.     FileTimeToDosDateTime( &lft, &dh, &dl);
  115.     CloseHandle(h);
  116.     return(dh<<16) | dl;
  117.   }
  118.   else
  119.     return 0L;
  120. }
  121.  
  122. void ChangeNameForFAT(char *name)
  123. {
  124.   char *src, *dst, *next, *ptr, *dot, *start;
  125.   static char invalid[] = ":;,=+\"[]<>| \t";
  126.  
  127.   if ( isalpha(name[0]) && (name[1] == ':') )
  128.     start = name + 2;
  129.   else
  130.     start = name;
  131.  
  132.   src = dst = start;
  133.   if ( (*src == '/') || (*src == '\\') )
  134.     src++, dst++;
  135.  
  136.   while ( *src )
  137.   {
  138.     for ( next = src; *next && (*next != '/') && (*next != '\\'); next++ );
  139.  
  140.     for ( ptr = src, dot = NULL; ptr < next; ptr++ )
  141.       if ( *ptr == '.' )
  142.       {
  143.         dot = ptr; /* remember last dot */
  144.         *ptr = '_';
  145.       }
  146.  
  147.     if ( dot == NULL )
  148.       for ( ptr = src; ptr < next; ptr++ )
  149.         if ( *ptr == '_' )
  150.           dot = ptr; /* remember last _ as if it were a dot */
  151.  
  152.     if ( dot && (dot > src) &&
  153.          ((next - dot <= 4) ||
  154.           ((next - src > 8) && (dot - src > 3))) )
  155.     {
  156.       if ( dot )
  157.         *dot = '.';
  158.  
  159.       for ( ptr = src; (ptr < dot) && ((ptr - src) < 8); ptr++ )
  160.         *dst++ = *ptr;
  161.  
  162.       for ( ptr = dot; (ptr < next) && ((ptr - dot) < 4); ptr++ )
  163.         *dst++ = *ptr;
  164.     }
  165.     else
  166.     {
  167.       if ( dot && (next - src == 1) )
  168.         *dot = '.';           /* special case: "." as a path component */
  169.  
  170.       for ( ptr = src; (ptr < next) && ((ptr - src) < 8); ptr++ )
  171.         *dst++ = *ptr;
  172.     }
  173.  
  174.     *dst++ = *next; /* either '/' or 0 */
  175.  
  176.     if ( *next )
  177.     {
  178.       src = next + 1;
  179.  
  180.       if ( *src == 0 ) /* handle trailing '/' on dirs ! */
  181.         *dst = 0;
  182.     }
  183.     else
  184.       break;
  185.   }
  186.  
  187.   for ( src = start; *src != 0; ++src )
  188.     if ( (strchr(invalid, *src) != NULL) || (*src == ' ') )
  189.       *src = '_';
  190. }
  191.  
  192. char *GetLongPathEA(char *name)
  193. {
  194.         return(NULL); /* volunteers ? */
  195. }
  196.  
  197. int IsFileNameValid(x)
  198. char *x;
  199. {
  200.         WIN32_FIND_DATA fd;
  201.         HANDLE h = FindFirstFile(x, &fd);
  202.  
  203.         if (h == INVALID_HANDLE_VALUE) return FALSE;
  204.         FindClose(h);
  205.         return TRUE;
  206. }
  207.  
  208. #endif /* UTIL */
  209.  
  210.  
  211. char *StringLower(char *szArg)
  212. {
  213.   unsigned char *szPtr;
  214.   for ( szPtr = szArg; *szPtr; szPtr++ )
  215.     *szPtr = lower[*szPtr];
  216.   return szArg;
  217. }
  218.