home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / SOFTWARE / SOURCES / UNZIP52.ZIP / amiga / filedate.c (.txt) < prev    next >
C/C++ Source or Header  |  1996-04-25  |  16KB  |  469 lines

  1. /* Low-level Amiga routines shared between Zip and UnZip.
  2.  *
  3.  * Contains:  FileDate()
  4.  *            tzset()           [replaces bad C library versions]
  5.  *            gmtime()          [ditto]
  6.  *            localtime()       [ditto]
  7.  *            time()            [ditto]
  8.  *            sendpkt()
  9.  *            Agetch()
  10.  *
  11.  * The first two are used by all Info-Zip programs except fUnZip.  The last two
  12.  * are used by all except the non-CRYPT version of fUnZip.  Probably some of
  13.  * the stuff in here is unused by ZipNote and ZipSplit too...  sendpkt() is
  14.  * used by Agetch() and FileDate(), and by windowheight() in amiga/amiga.c
  15.  * (UnZip).
  16.  */
  17.  
  18.  
  19. /* HISTORY/CHANGES
  20.  *  2 Sep 92, Greg Roelofs, Original coding.
  21.  *  6 Sep 92, John Bush, Incorporated into UnZip 5.1
  22.  *  6 Sep 92, John Bush, Interlude "FileDate()" defined, which calls or
  23.  *            redefines SetFileDate() depending upon AMIGADOS2 definition.
  24.  * 11 Oct 92, John Bush, Eliminated AMIGADOS2 switch by determining
  25.  *            revision via OpenLibrary() call.  Now only one version of
  26.  *            the program runs on both platforms (1.3.x vs. 2.x)
  27.  * 11 Oct 92, John Bush, Merged with Zip version and changed arg passing
  28.  *            to take time_t input instead of struct DateStamp.
  29.  *            Arg passing made to conform with utime().
  30.  * 22 Nov 92, Paul Kienitz, fixed includes for Aztec and cleaned up some
  31.  *            lint-ish errors; simplified test for AmigaDOS version.
  32.  * 11 Nov 95, Paul Kienitz, added Agetch() for crypt password input and
  33.  *            UnZip's "More" prompt -- simplifies crypt.h and avoids
  34.  *            use of library code redundant with sendpkt().  Made it
  35.  *            available to FUnZip, which does not use FileDate().
  36.  * 22 Nov 95, Paul Kienitz, created a new tzset() that gets the current
  37.  *            timezone from the Locale preferences.  These exist only under
  38.  *            AmigaDOS 2.1 and up, but it is probably correctly set on more
  39.  *            Amigas than the TZ environment variable is.  We check that
  40.  *            only if TZ is not validly set.  We do not parse daylight
  41.  *            savings syntax except to check for presence vs. absence of a
  42.  *            DST part; United States rules are assumed.  This is better
  43.  *            than the tzset()s in the Amiga compilers' libraries do.
  44.  * 15 Jan 96, Chr. Spieler, corrected the logic when to select low level
  45.  *            sendpkt() (when FileDate(), Agetch() or windowheight() is used),
  46.  *            and AMIGA's Agetch() (CRYPT, and UnZip(SFX)'s UzpMorePause()).
  47.  * 10 Feb 96, Paul Kienitz, re-fiddled that selection logic again, moved
  48.  *            stuff around for clarity.
  49.  * 16 Mar 96, Paul Kienitz, created a replacement localtime() to go with the
  50.  *            new tzset(), because Aztec's is hopelessly broken.  Also
  51.  *            gmtime(), which localtime() calls.
  52.  * 12 Apr 96, Paul Kienitz, daylight savings was being handled incorrectly.
  53.  * 21 Apr 96, Paul Kienitz, had to replace time() as well, Aztec's returns
  54.  *            local time instead of GMT.  That's why their localtime() was bad,
  55.  *            because it assumed time_t was already local, and gmtime() was
  56.  *            the one that checked TZ.
  57.  * 23 Apr 96, Chr. Spieler, deactivated time() replacement for UnZip stuff.
  58.  *            Currently, the UnZip sources do not make use of time() (and do
  59.  *            not supply the working mktime() replacement, either!).
  60.  */
  61.  
  62. #include <ctype.h>
  63. #include <string.h>
  64. #include <time.h>
  65. #include <errno.h>
  66. #include <stdio.h>
  67.  
  68. #include <exec/types.h>
  69. #include <exec/memory.h>
  70. #include <exec/libraries.h>
  71. #include <libraries/dos.h>
  72. #include <libraries/dosextens.h>
  73. #include <clib/exec_protos.h>
  74. #include <clib/dos_protos.h>
  75. #include <clib/locale_protos.h>
  76.  
  77. #ifdef AZTEC_C
  78. #  include <pragmas/exec_lib.h>
  79. #  include <pragmas/dos_lib.h>
  80. #  include <pragmas/locale_lib.h>
  81. #  define ESRCH  ENOENT
  82. #  define EOSERR EIO
  83. #endif
  84.  
  85. #if defined(LATTICE) || defined(__SASC)
  86. #  include <proto/exec.h>
  87. #  include <proto/dos.h>
  88. #endif
  89.  
  90. #define OF(x) x               /* so crypt.h prototypes compile okay */
  91. #if defined(ZIP) || defined(FUNZIP)
  92. #  define near                /* likewise */
  93.    typedef unsigned long ulg; /* likewise */
  94.    typedef size_t extent;     /* likewise */
  95.    typedef void zvoid;        /* likewise */
  96. #endif
  97. #include "crypt.h"            /* just so we can tell if CRYPT is defined */
  98.  
  99.  
  100. #ifndef FUNZIP
  101.  
  102. /***********************/
  103. /* Function filedate() */
  104. /***********************/
  105.  
  106. /*  FileDate() (originally utime.c), by Paul Wells.  Modified by John Bush
  107.  *  and others (see also sendpkt() comments, below); NewtWare SetFileDate()
  108.  *  clone cheaply ripped off from utime().
  109.  */
  110.  
  111. /* DESCRIPTION
  112.  * This routine chooses between 2 methods to set the file date on AMIGA.
  113.  * Since AmigaDOS 2.x came out, SetFileDate() was available in ROM (v.36
  114.  * and higher).  Under AmigaDOS 1.3.x (less than v.36 ROM), SetFileDate()
  115.  * must be accomplished by constructing a message packet and sending it
  116.  * to the file system handler of the file to be stamped.
  117.  *
  118.  * The system's ROM version is extracted from the external system Library
  119.  * base.
  120.  *
  121.  * NOTE:  although argument passing conforms with utime(), note the
  122.  *        following differences:
  123.  *          - Return value is boolean success/failure.
  124.  *          - If a structure or array is passed, only the first value
  125.  *            is used, which *may* correspond to date accessed and not
  126.  *            date modified.
  127.  */
  128.  
  129. #  ifndef SUCCESS
  130. #    define SUCCESS (-1L)
  131. #    define FAILURE 0L
  132. #  endif
  133.  
  134. #  define ReqVers 36L  /* required library version for SetFileDate() */
  135. #  define TZSIZE  40   /* max space allowed for a timezone string    */
  136.  
  137. extern struct Library *SysBase;
  138.  
  139. long timezone = 0;     /* should be pretty safe for reentrancy */
  140. int daylight = 0;
  141.  
  142. LONG FileDate (char *filename, time_t u[]);
  143. void tzset(void);
  144.  
  145. /* =============================================================== */
  146.  
  147. LONG FileDate(filename, u)
  148.     char *filename;
  149.     time_t u[];
  150. {
  151.     LONG SetFileDate(UBYTE *filename, struct DateStamp *pDate);
  152.     LONG sendpkt(struct MsgPort *pid, LONG action, LONG *args, LONG nargs);
  153.     struct MsgPort *taskport;
  154.     BPTR dirlock, lock;
  155.     struct FileInfoBlock *fib;
  156.     LONG pktargs[4];
  157.     UBYTE *ptr;
  158.     long ret;
  159.  
  160.     struct DateStamp pDate;
  161.     time_t mtime;
  162.  
  163.     tzset();
  164.     mtime = u[0] - timezone;
  165.  
  166. /* magic number = 2922 = 8 years + 2 leaps between 1970 - 1978 */
  167.     pDate.ds_Days = (mtime / 86400L) - 2922L;
  168.     mtime = mtime % 86400L;
  169.     pDate.ds_Minute = mtime / 60L;
  170.     mtime = mtime % 60L;
  171.     pDate.ds_Tick = mtime * TICKS_PER_SECOND;
  172.  
  173.     if (SysBase->lib_Version >= ReqVers) {
  174.         return (SetFileDate(filename,&pDate));  /* native routine at 2.0+ */
  175.     }
  176.     else  /* !(SysBase->lib_Version >=ReqVers) */
  177.     {
  178.         if( !(taskport = (struct MsgPort *)DeviceProc(filename)) )
  179.         {
  180.             errno = ESRCH;          /* no such process */
  181.             return FAILURE;
  182.         }
  183.  
  184.         if( !(lock = Lock(filename,SHARED_LOCK)) )
  185.         {
  186.             errno = ENOENT;         /* no such file */
  187.             return FAILURE;
  188.         }
  189.  
  190.         if( !(fib = (struct FileInfoBlock *)AllocMem(
  191.             (long)sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)) )
  192.         {
  193.             errno = ENOMEM;         /* insufficient memory */
  194.             UnLock(lock);
  195.             return FAILURE;
  196.         }
  197.  
  198.         if( Examine(lock,fib)==FAILURE )
  199.         {
  200.             errno = EOSERR;         /* operating system error */
  201.             UnLock(lock);
  202.             FreeMem(fib,(long)sizeof(*fib));
  203.             return FAILURE;
  204.         }
  205.  
  206.         dirlock = ParentDir(lock);
  207.         ptr = (UBYTE *)AllocMem(64L,MEMF_PUBLIC);
  208.         strcpy((ptr+1),fib->fib_FileName);
  209.         *ptr = strlen(fib->fib_FileName);
  210.         FreeMem(fib,(long)sizeof(*fib));
  211.         UnLock(lock);
  212.  
  213.         /* now fill in argument array */
  214.  
  215.         pktargs[0] = 0;
  216.         pktargs[1] = (LONG)dirlock;
  217.         pktargs[2] = (LONG)&ptr[0] >> 2;
  218.         pktargs[3] = (LONG)&pDate;
  219.  
  220.         errno = ret = sendpkt(taskport,ACTION_SET_DATE,pktargs,4L);
  221.  
  222.         FreeMem(ptr,64L);
  223.         UnLock(dirlock);
  224.  
  225.         return SUCCESS;
  226.     }  /* ?(Sy