home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #3 / amigamamagazinepolishissue1998.iso / szachy / gnu / amyboard-3.2.pl2 / amiga / gettimeofday.c < prev    next >
C/C++ Source or Header  |  1995-05-23  |  2KB  |  113 lines

  1. /**
  2. ***  simple gettimeofday() replacement; no timezone handling
  3. ***
  4. ***  Jochen Wiedmann, 14-Feb-95
  5. ***
  6. ***  This is in the public domain, use it as you want.
  7. **/
  8.  
  9.  
  10. /****************************************************************
  11.     This file uses the auto initialization possibilities of
  12.     Dice, gcc and SAS/C, respectively.
  13.  
  14.     Dice does this by using the keywords __autoinit and
  15.     __autoexit, SAS uses names beginning with _STI or
  16.     _STD, respectively. gcc uses the asm() instruction,
  17.     to emulate C++ constructors and destructors.
  18. ****************************************************************/
  19.  
  20.  
  21. #if defined(__SASC)
  22. #define __autoinit
  23. #define __autoexec
  24. extern void _XCEXIT(ULONG);
  25. #elif defined(__GNUC__)
  26. #define __autoinit
  27. #define __autoexit
  28. __asm ("  .text;  .stabs \"___CTOR_LIST__\",22,0,0,__STIInitGetTimeOfDay");
  29. __asm ("  .text;  .stabs \"___DTOR_LIST__\",22,0,0,__STDTermGetTimeOfDay");
  30. #elif defined(_DCC)
  31. extern void _AutoFail0(void);
  32. #else
  33. #error "Don't know how to handle your compiler."
  34. #endif
  35.  
  36. #include <stdlib.h>
  37. #include <errno.h>
  38.  
  39. #include <exec/types.h>
  40. #include <exec/io.h>
  41. #include <devices/timer.h>
  42. #include <proto/exec.h>
  43.  
  44.  
  45. STATIC struct MsgPort *port     = NULL;
  46. STATIC struct timerequest *req  = NULL;
  47.  
  48. __autoinit LONG _STIInitGetTimeOfDay(VOID)
  49.  
  50. { if ((port = CreateMsgPort()))
  51.   { if ((req = CreateIORequest(port, sizeof(*req))))
  52.     { if (!(OpenDevice((STRPTR) "timer.device", UNIT_MICROHZ,
  53.                (struct IORequest *) req, 0)))
  54.       { return(FALSE);
  55.       }
  56.       DeleteIORequest(req);
  57.       req = NULL;
  58.     }
  59.     DeleteMsgPort(port);
  60.     port = NULL;
  61.   }
  62. #if defined(__GNUC__)
  63.   abort();
  64. #elif defined(_DCC)
  65.   _AutoFail0();
  66. #endif
  67.   return(TRUE);
  68. }
  69.  
  70.  
  71. __autoexit VOID _STDTermGetTimeOfDay(VOID)
  72.  
  73. { if (req)
  74.   { CloseDevice((struct IORequest *) req);
  75.     DeleteIORequest(req);
  76.     req = NULL;
  77.   }
  78.   if (port)
  79.   { DeleteMsgPort(port);
  80.     port = NULL;
  81.   }
  82. }
  83.  
  84.  
  85. struct timezone;
  86. int gettimeofday(struct timeval *tv, struct timezone *tz)
  87.  
  88. { int error;
  89.  
  90.   req->tr_node.io_Command = TR_GETSYSTIME;
  91.   error = DoIO((struct IORequest *) req);
  92.   *tv = req->tr_time;
  93.   if (error)
  94.   { errno = ENOMEM;
  95.     return(-1);
  96.   }
  97.   return(0);
  98. }
  99.  
  100. int setsystime(struct timeval *tv, struct timezone *tz)
  101.  
  102. { int error;
  103.  
  104.   req->tr_time = *tv;
  105.   req->tr_node.io_Command = TR_SETSYSTIME;
  106.   error = DoIO((struct IORequest *) req);
  107.   if (error)
  108.   { errno = ENOMEM;
  109.     return(-1);
  110.   }
  111.   return(0);
  112. }
  113.