home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / uptime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  2.9 KB  |  98 lines

  1. ;/* uptime.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfis -j73 uptime.c
  3. Blink FROM LIB:c.o,uptime.o TO uptime LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.     /* Uses SAS C getreg() */
  7.  
  8.     #include <exec/types.h>
  9.     #include <exec/memory.h>
  10.     #include <dos/dos.h>
  11.     #include <dos/dosextens.h>
  12.     #include <dos/datetime.h>
  13.     #include <utility/date.h>
  14.     #include <dos.h>
  15.  
  16.     #include <clib/exec_protos.h>
  17.     #include <clib/dos_protos.h>
  18.     #include <clib/utility_protos.h>
  19.  
  20.     #include <stdlib.h>
  21.  
  22.     struct Library *UtilityBase;
  23.  
  24.     LONG main(void);
  25.  
  26.     LONG main(void)
  27.     {
  28.       struct InfoData *infodata;
  29.       struct DeviceList *ramdevice;
  30.       struct DateStamp *now;
  31.       LONG currenttime, boottime;
  32.       BPTR lock;
  33.       LONG vargs[3];
  34.       LONG rc = RETURN_FAIL;
  35.  
  36.       /* Fails silently if < 37 */
  37.       if (UtilityBase = OpenLibrary("utility.library", 37))
  38.       {
  39.         if (infodata = AllocMem(sizeof(struct InfoData), MEMF_CLEAR))
  40.         {
  41.           if (now = AllocMem(sizeof(struct DateStamp), MEMF_CLEAR))
  42.           {
  43.             if (lock = Lock("RAM:", SHARED_LOCK))
  44.             {
  45.               if ((Info(lock, infodata)) == DOSTRUE)
  46.               {
  47.                 /* Make C pointer */
  48.  
  49.                 ramdevice = BADDR(infodata->id_VolumeNode);
  50.  
  51.                 boottime = SMult32(ramdevice->dl_VolumeDate.ds_Days, 86400) +
  52.                            SMult32(ramdevice->dl_VolumeDate.ds_Minute, 60) +
  53.                            SDivMod32(ramdevice->dl_VolumeDate.ds_Tick,
  54.                                      TICKS_PER_SECOND );
  55.  
  56.                 DateStamp(now);
  57.  
  58.                 currenttime = SMult32(now->ds_Days, 86400) +
  59.                               SMult32(now->ds_Minute, 60) +
  60.                               SDivMod32(now->ds_Tick, TICKS_PER_SECOND);
  61.  
  62.                 currenttime -= boottime;
  63.  
  64.                 if (currenttime > 0)
  65.                 {
  66.                   vargs[0] = UDivMod32(currenttime, 86400);
  67.  
  68.                   vargs[1] = getreg(1);
  69.                   vargs[1] = UDivMod32(vargs[1], 3600);
  70.  
  71.                   vargs[2] =getreg(1);
  72.                   vargs[2] = UDivMod32(vargs[2], 60);
  73.  
  74.                   /*
  75.                    * Passing the address of the array allows the VPrintf()
  76.                    * function to access the array contents.  Keep in mind
  77.                    * that VPrintf() does _NOT_ know how many elements are
  78.                    * really valid in the final parameter, and will gleefully
  79.                    * run past the valid arguments.
  80.                    */
  81.                   VFPrintf(Output(),
  82.                            "up for %ld days, %ld hours, %ld minutes\n",
  83.                            vargs );
  84.  
  85.                   rc = RETURN_OK;
  86.                 }
  87.               }
  88.               UnLock(lock);
  89.             }
  90.             FreeMem(now, sizeof(struct DateStamp));
  91.           }
  92.           FreeMem(infodata, sizeof(struct InfoData));
  93.         }
  94.         CloseLibrary(UtilityBase);
  95.       }
  96.       exit(rc);
  97.     }
  98.