home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / uptime.zip / uptime.c < prev    next >
C/C++ Source or Header  |  1998-03-16  |  4KB  |  127 lines

  1. /*
  2.  
  3.   Tue Jun 11 11:54:38 GMT 1996
  4.   
  5.   uptime.exe by Cheng-Yang Tan, cytan@tristan.tn.cornell.edu
  6.   
  7.   Description:
  8.       a UNIX like uptime application.
  9.   
  10.   To make:
  11.       gcc -Zomf -o uptime.exe uptime.c uptime.def
  12.       
  13.   dependencies:
  14.       dqps.h    
  15.       
  16.   The source below demonstrates usage of:
  17.       (1) the undocumented API DosQProcStatus    
  18.       (2) the thunking macros of emx-gcc version 2.7.2
  19.       (3) the documented API DosQuerySysInfo
  20.       
  21.   Acknowledgements:
  22.       Tomas Ögren, stric@arbornet.org who wrote cstart from which I copied
  23.                    the code for getting the "uptime".
  24.                    
  25.     Kai Uwe Rommel, for his excellent documentation on DosQProcStatus
  26.         available at ftp.leo.org in directory /pub/comp/os/os2/doc as proc.zip
  27.         
  28.     Eberhard Mattes, for his documentation on the thunking macros of emx-gcc
  29.         available as emxdev.inf in the emx09b distribution.
  30.         
  31.     IBM for making OS/2.        
  32.         
  33.   Copyright:
  34.       I release this code into the public domain *except* where the copyrights
  35.       are held by the individual authors above.  I hold no responsibility for the
  36.       usage of this code.
  37.         
  38. */
  39.  
  40.  
  41. #define INCL_DOSFILEMGR
  42. #define INCL_DOSMISC
  43. #include <os2.h>
  44. #include <time.h>
  45. #include <malloc.h>
  46.  
  47. #include "dqps.h"
  48.  
  49. unsigned short dosQProcStatus(ULONG* DQPS_Buffer, USHORT buffer_size)
  50. {
  51.    return((unsigned short)(
  52.    _THUNK_PASCAL_PROLOG (sizeof(ULONG*)+sizeof(USHORT));
  53.    _THUNK_PASCAL_FLAT (DQPS_Buffer);
  54.    _THUNK_PASCAL_SHORT (buffer_size);
  55.    _THUNK_CALL (DosQProcStatus)));
  56.   
  57. }
  58.  
  59. void get_num_processes_num_threads(ULONG* num_processes, ULONG*num_threads)
  60. {
  61.    int count = 0;
  62.    int size;
  63.    PULONG DQPS_Buffer = _tmalloc(0xFFFF);
  64.    USHORT buffer_size = 0xFFFF;
  65.    struct qsPrec_s *pProcRecord;
  66.    
  67.    if(dosQProcStatus(DQPS_Buffer, buffer_size)){
  68.       /*we have an error!*/
  69.       *num_threads = *num_processes =  0;
  70.       return;
  71.    }   
  72.    
  73.    *num_threads = ((struct qsPtrRec_s*)DQPS_Buffer)->pGlobalRec->cThrds;
  74.    
  75.    pProcRecord = ((struct qsPtrRec_s*)DQPS_Buffer)->pProcRec;
  76.    while(pProcRecord->RecType == 1){
  77.       size = sizeof(struct qsPrec_s);
  78.       size += pProcRecord->cTCB * sizeof(struct qsTrec_s) ;
  79.       size += pProcRecord->c16Sem * sizeof(short) ;
  80.       size += pProcRecord->cLib * sizeof(short) ;
  81.       size += pProcRecord->cShrMem * sizeof(short) ;
  82.       pProcRecord = (struct qsPrec_s*) ( (char*)pProcRecord + size ) ;
  83.       count++ ;
  84.    }
  85.    *num_processes = count;
  86. }
  87.  
  88.  
  89. ULONG getdata(ULONG whichone)
  90. {
  91.     ULONG temp;
  92.     DosQuerySysInfo ( whichone,  whichone,  &temp,  4);
  93.     return temp;
  94. }
  95.  
  96. int main(void)
  97. {
  98.     struct tm *tim;
  99.     ULONG currtime, uptime;
  100.     ULONG num_processes, num_threads;
  101.     DATETIME datetime;
  102.  
  103.     /* The current time */
  104.  
  105.         DosGetDateTime(&datetime);
  106.     printf("  %u:%02u%s  ", 
  107.         datetime.hours-12 >0? datetime.hours-12:datetime.hours,
  108.         datetime.minutes,
  109.         datetime.hours-12 >0? "pm":"am");
  110.     
  111.     /*currtime=getdata(QSV_TIME_LOW);
  112.     tim = localtime(&currtime);
  113.     printf("  %u:%02u%s  ", 
  114.         tim->tm_hour-12 >0? tim->tm_hour-12:tim->tm_hour,
  115.         tim->tm_min,
  116.         tim->tm_hour-12 >0? "pm":"am");*/
  117.  
  118.     /* up time */        
  119.     uptime=getdata(QSV_MS_COUNT);
  120.     printf("up %lu days, %lu:%02lu:%02lu, ",(uptime/(1000*3600*24))%365,(uptime/(1000*3600))%24,(uptime/(1000*60))%60,(uptime/(1000))%60); 
  121.  
  122.     /* the load of the system */        
  123.     get_num_processes_num_threads(&num_processes, &num_threads);
  124.     printf("load: %lu processes, %lu threads.\n", num_processes, num_threads);
  125.     return 0;
  126. }
  127.