home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / prog / source / cp.lha / setdate.c < prev    next >
C/C++ Source or Header  |  1988-03-03  |  5KB  |  156 lines

  1. ===============
  2. cat <setDate.c>
  3. ===============
  4. #include "exec/types.h"
  5. #include "exec/ports.h"
  6. #include "exec/io.h"
  7. #include "exec/memory.h"
  8. #include "libraries/dos.h"
  9. #include "libraries/dosextens.h"
  10. #include <stdio.h>
  11. #define AZTEC 1
  12. #ifdef AZTEC
  13. #include "functions.h"         /* aztec C include */
  14. #endif
  15.  
  16. #define ACTION_SETDATE_MODE 34L  /* The packet type we will be playing with */
  17. #define DOSTRUE             -1L  /* AmigaDos TRUE */
  18. #define MAXARGS              7L  /* limit in packet structure (dosextens.h) */
  19. #define NARGS                4L  /* Number of args for setdate */
  20.  
  21. /*---------------------------------------------------------------------*/
  22. /*  sendpkt: generalized send a dos packet.                            */
  23. /*---------------------------------------------------------------------*/
  24.  
  25. static long sendpkt(pid,action,args,nargs)
  26.  
  27. struct MsgPort *pid;    /* process indentifier ... (handlers message port ) */
  28. long  action,          /* number of arguments in list */
  29.       nargs;           /* number of arguments in list  */
  30. ULONG args[];          /* a pointer to a argument list */
  31. {
  32.  
  33.    struct MsgPort        *replyport;
  34.    struct StandardPacket *packet;
  35.  
  36.    long   count, res1;
  37.    ULONG  *pargs;
  38.  
  39.    if(nargs > MAXARGS) return NULL;
  40.  
  41.    replyport = (struct MsgPort *) CreatePort(NULL,NULL); /* make reply port */
  42.    if(!replyport) return NULL;
  43.  
  44.    packet = (struct StandardPacket *)
  45.       AllocMem((long)sizeof(struct StandardPacket),MEMF_PUBLIC | MEMF_CLEAR);
  46.    if(!packet)
  47.      {
  48.        FreeMem((void *)packet,(long)sizeof(struct StandardPacket));
  49.        return(NULL);
  50.      }
  51.  
  52.    packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt); /* link packet- */
  53.    packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);  /* to message    */
  54.    packet->sp_Pkt.dp_Port         = replyport;         /* set-up reply port */
  55.    packet->sp_Pkt.dp_Type         = action;           /* what to do... */
  56.  
  57.    /* move all the arguments to the packet */
  58.    pargs = (ULONG *)&(packet->sp_Pkt.dp_Arg1);   /* address of first argument */
  59.    for(count=NULL;count < nargs && count < MAXARGS; ++count)
  60.      pargs[count]=args[count];
  61.  
  62.    PutMsg(pid,packet); /* send packet */
  63.    (void)WaitPort(replyport); /* wait for packet to come back */
  64.    (void)GetMsg(replyport);   /* pull message */
  65.  
  66.    res1 = packet->sp_Pkt.dp_Res1; /* get result */
  67.  
  68.    /* all done clean up */
  69.    FreeMem((void *)packet,(long)sizeof(*packet));
  70.    DeletePort(replyport);
  71.  
  72.    return(res1);
  73.  
  74. }
  75.  
  76. /*---------------------------------------------------------------------*/
  77. /*  setDate: datestamp the given file with the given date.             */
  78. /*---------------------------------------------------------------------*/
  79.  
  80. BOOL setDate( name, date )
  81. char             *name;
  82. struct DateStamp *date;
  83. {
  84.    struct MsgPort *task;            /* for process id handler */
  85.    ULONG arg[4];                    /* array of arguments     */
  86.    char *bstr, strcpy();            /* of file to be set      */
  87.    long rc;
  88.    char *strchr();
  89.    int strlen();
  90.  
  91.    rc = 0;
  92.  
  93.    if ( !(bstr = (char *)AllocMem(68L, (long)(MEMF_PUBLIC)))) goto exit2;
  94.    if ( !(task = (struct MsgPort *)DeviceProc( name )))       goto exit1;
  95.  
  96.    /* Dos Packet needs the filename in Bstring format */
  97.  
  98.    (void)strcpy( bstr+1, name );
  99.    *bstr = strlen( name );
  100.  
  101.    arg[0] = (ULONG)NULL;
  102.    arg[1] = (ULONG)IoErr(); /* lock on parent director set by DeviceProc() */
  103.    arg[2] = (ULONG) bstr >> 2;
  104.    arg[3] = (ULONG) date;
  105.    rc = sendpkt( task, ACTION_SETDATE_MODE, arg, 4L );
  106.  
  107. exit1: if ( bstr ) FreeMem( (void *)bstr, 68L );
  108. exit2: if ( rc == DOSTRUE )
  109.           return TRUE;
  110.        else
  111.           return FALSE;
  112. }
  113.  
  114. /*---------------------------------------------------------------------*/
  115. /*  getDate: get the datestamp the given file.                         */
  116. /*---------------------------------------------------------------------*/
  117.  
  118. BOOL getDate(name, date )
  119. char *name;
  120. register struct DateStamp *date;
  121. {
  122.  
  123.    struct FileInfoBlock *Fib;
  124.    ULONG                FLock;
  125.    int                  result;
  126.    register struct DateStamp    *d;
  127.  
  128.    if ( (FLock = (ULONG) Lock(name, (long)(ACCESS_READ) )) == NULL)
  129.       return FALSE;
  130.  
  131.    Fib = (struct FileInfoBlock * )
  132.         AllocMem( (long)sizeof(struct FileInfoBlock), (long)(MEMF_CHIP));
  133.  
  134.    if (Fib == NULL )
  135.      result = FALSE;
  136.    else
  137.      {
  138.         if ( !Examine( FLock, Fib ))
  139.            result = FALSE;
  140.         else if ( Fib->fib_DirEntryType > 0 )
  141.            result = FALSE;  /* It's a directory */
  142.         else
  143.            {
  144.                 d = &Fib->fib_Date;
  145.                 date->ds_Days   = d->ds_Days;
  146.                 date->ds_Minute = d->ds_Minute;
  147.                 date->ds_Tick   = d->ds_Tick;
  148.                 result = TRUE;
  149.            }
  150.         FreeMem( (void *)Fib, (long)sizeof(struct FileInfoBlock) );
  151.      }
  152.  
  153.    UnLock( FLock );
  154.    return result;
  155. }
  156.