home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / UNZP50P1.ZIP / AMIGA / utime.c < prev   
C/C++ Source or Header  |  1993-01-23  |  4KB  |  159 lines

  1. /* utime.c */
  2.  
  3. #include <string.h>
  4. #include <time.h>
  5. #include <errno.h>
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13.  
  14. extern LONG sendpkt(struct MsgPort *,LONG,LONG[],LONG);
  15.  
  16. extern int _OSERR;
  17.  
  18. #ifndef SUCCESS
  19. #define SUCCESS (-1L)
  20. #define FAILURE 0L
  21. #endif
  22.  
  23. int utime(char *file, time_t timep[]);
  24.  
  25. int utime(file,timep)
  26. char *file;
  27. time_t timep[];
  28. {
  29.  
  30.     struct DateStamp date;
  31.     struct MsgPort *taskport;
  32.     struct FileLock *dirlock, *lock;
  33.     struct FileInfoBlock *fib;
  34.  
  35.     LONG argv[4];
  36.     UBYTE *ptr;
  37.     long ret;
  38.  
  39. /*  timep[1] -= timezone;   */
  40.  
  41.     date.ds_Days = timep[1] / 86400;
  42.     date.ds_Minute = (timep[1] - (date.ds_Days * 86400))/60;
  43.     date.ds_Tick = ( timep[1] - (date.ds_Days * 86400) -
  44.                                 (date.ds_Minute * 60)
  45.                    ) * TICKS_PER_SECOND;
  46.     date.ds_Days -= ((8*365+2));
  47.  
  48.     if( !(taskport = (struct MsgPort *)DeviceProc(file)) )
  49.     {
  50.         errno = ESRCH;          /* no such process */
  51.         _OSERR = IoErr();
  52.         return(-1);
  53.     }
  54.  
  55.     if( !(lock = (struct FileLock *)Lock(file,SHARED_LOCK)) )
  56.     {
  57.         errno = ENOENT;         /* no such file */
  58.         _OSERR = IoErr();
  59.         return(-1);
  60.     }
  61.  
  62.     if( !(fib = (struct FileInfoBlock *)AllocMem(
  63.         (long)sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)) )
  64.     {
  65.         errno = ENOMEM;         /* insufficient memory */
  66.         UnLock((BPTR)lock);
  67.         return(-1);
  68.     }
  69.  
  70.     if( Examine((BPTR)lock,fib)==FAILURE )
  71.     {
  72.         errno = EOSERR;         /* operating system error */
  73.         _OSERR = IoErr();
  74.         UnLock((BPTR)lock);
  75.         FreeMem((char *)fib,(long)sizeof(*fib));
  76.         return(-1);
  77.     }
  78.  
  79.     dirlock = (struct FileLock *)ParentDir((BPTR)lock);
  80.     ptr = (UBYTE *)AllocMem(64L,MEMF_PUBLIC);
  81.     strcpy((ptr+1),fib->fib_FileName);
  82.     *ptr = strlen(fib->fib_FileName);
  83.     FreeMem((char *)fib,(long)sizeof(*fib));
  84.     UnLock((BPTR)lock);
  85.  
  86.     /* now fill in argument array */
  87.  
  88.     argv[0] = NULL;
  89.     argv[1] = (LONG)dirlock;
  90.     argv[2] = (LONG)&ptr[0] >> 2;
  91.     argv[3] = (LONG)&date;
  92.  
  93.     errno = ret = sendpkt(taskport,34L,argv,4L);
  94.  
  95.     FreeMem(ptr,64L);
  96.     UnLock((BPTR)dirlock);
  97.  
  98.     return(0);
  99.  
  100. } /* utime() */
  101. /*  sendpkt.c
  102.  *  by A. Finkel, P. Lindsay, C. Sheppner
  103.  *  returns Res1 of the reply packet
  104.  */
  105. /*
  106. #include <exec/types.h>
  107. #include <exec/memory.h>
  108. #include <libraries/dos.h>
  109. #include <libraries/dosextens.h>
  110. #include <proto/exec.h>
  111. #include <proto/dos.h>
  112. */
  113.  
  114. LONG sendpkt(pid,action,args,nargs)
  115. struct MsgPort *pid;            /* process identifier (handler message port) */
  116. LONG action,                    /* packet type (desired action)              */
  117.      *args,                     /* a pointer to argument list                */
  118.      nargs;                     /* number of arguments in list               */
  119. {
  120.  
  121.     struct MsgPort *replyport;
  122.     struct StandardPacket *packet;
  123.     LONG count, *pargs, res1;
  124.  
  125.     replyport = (struct MsgPort *)CreatePort(0L,0L);
  126.     if( !replyport ) return(NULL);
  127.  
  128.     packet = (struct StandardPacket *)AllocMem(
  129.             (long)sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR);
  130.     if( !packet )
  131.     {
  132.         DeletePort(replyport);
  133.         return(NULL);
  134.     }
  135.  
  136.     packet->sp_Msg.mn_Node.ln_Name  = (char *)&(packet->sp_Pkt);
  137.     packet->sp_Pkt.dp_Link          = &(packet->sp_Msg);
  138.     packet->sp_Pkt.dp_Port          = replyport;
  139.     packet->sp_Pkt.dp_Type          = action;
  140.  
  141.     /* copy the args into the packet */
  142.     pargs = &(packet->sp_Pkt.dp_Arg1);      /* address of 1st argument */
  143.     for( count=0; count<nargs; count++ )
  144.         pargs[count] = args[count];
  145.  
  146.     PutMsg(pid,(struct Message *)packet);   /* send packet */
  147.  
  148.     WaitPort(replyport);
  149.     GetMsg(replyport);
  150.  
  151.     res1 = packet->sp_Pkt.dp_Res1;
  152.  
  153.     FreeMem((char *)packet,(long)sizeof(*packet));
  154.     DeletePort(replyport);
  155.  
  156.     return(res1);
  157.  
  158. } /* sendpkt() */
  159.