/* Program: XUPTIME OS/2 1.1 Platform: OS/2 Compilers: Borland C, EMX GNU C, IBM C-Set Compile: $(CC) uptime.c Purpose: Print the uptime of the machine based on the creation date of the swap file. In contrast too tools querying the uptime timer of OS/2, this can also cover uptimes > ~ 50 days. Author: Tobias Ernst, tobi@bland.fido.de License: Public Domain */ #define INCL_DOSFILEMGR #define INCL_DOSMISC #define INCL_DOSDATETIME #define INCL_DOSERRORS #include #include #include #include /* get_boot_drive: get the drive letter of this OS/2 system's boot drive */ #include #define eatwhite(cp) { while (*(cp) == ' ' || *(cp) == '\n' || *(cp) == '\t') { (cp)++; } } /* get_swap_file: get the name of the swap file of this OS/2 system by parsing it's config.sys file. */ #define buffersize 256+50 static BOOL get_swap_file(char *chrSwapFile, ULONG ulSize) { char configsys[] = "X:\\CONFIG.SYS"; char buffer[buffersize], *cp; FILE *f; int i; configsys[0] = SysQueryBootDriveLetter();//get_boot_drive(); if ((f = fopen(configsys, "r")) == NULL) return FALSE; while ( (cp = fgets(buffer, buffersize, f)) != NULL) { eatwhite(cp); if (!strnicmp(cp, "SWAPPATH", 8)) { cp += 8; eatwhite(cp); if (*cp == '=') { cp++; eatwhite(cp); for (i = 0; cp[i] != ' ' && cp[i] != '\t' && cp[i] != '\n' && cp[i] != '\0'; i++); strncpy(chrSwapFile, cp, i); if (chrSwapFile[i - 1] != '\\') { chrSwapFile[i++] = '\\'; } strcpy(chrSwapFile + i, "SWAPPER.DAT"); fclose(f); return (TRUE); } } /* eat rest of line if it was > 256 chars */ while (*buffer && buffer[strlen(buffer) - 1] != '\n') { if (fgets(buffer, buffersize, f) == NULL) { break; } } } // fprintf (stderr, "%s does not contain a SWAPPATH statement.\n", configsys); fclose(f); return 0; } /* is_leap_year: tests if the specified year (4 digit) is a leap year */ static int is_leap_year(unsigned year) { if (!((year % 100) % 4)) { if (!(year % 100)) { if (!(year % 400)) { return 1; } else { return 0; } } else { return 1; } } else { return 0; } } /* n_days_in_month: returns the number of days in a certain month */ static int n_days_in_month(unsigned month, unsigned year) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return (is_leap_year(year)) ? 29 : 28; } return 0; } /* n_days_since_1900: returns the number of days that have passed since 1/1/1900 */ unsigned long n_days_since_1900(int dd, int md, int yd) { int d=1,m,y; unsigned long ds=0; for (y = 1900; y= 50 days.\n"); } /*!***********************************************************/ /* */ /* @@DESC */ /* */ /* To be written... */ /* */ /*!!**********************************************************/ ULONG SysGetUptimeSecs(BOOL fUseTimer) { if(fUseTimer) return get_uptime_from_os2_timer(); else return get_uptime_from_swapfile(); } /*!***********************************************************/ /* */ /* @@DESC */ /* */ /* To be written... */ /* */ /*!!**********************************************************/ char* SysGetUptimeString(BOOL fUseTimer, char * chrBuffer, ULONG ulSize) { unsigned long uptime, days, hours, minutes, seconds; char chrTemp[30]; if(!ulSize) return NULLHANDLE; uptime=SysGetUptimeSecs(fUseTimer); days = uptime / (60L * 60L * 24L); uptime = uptime % (60L * 60L * 24L); hours = uptime / (60L * 60L); uptime = uptime % (60L * 60L); minutes = uptime / (60L); seconds = uptime % (60L); sprintf (chrTemp, "%lu %02lu:%02lu:%02lu", days, hours, minutes, seconds); strncpy(chrBuffer, chrTemp, ulSize); chrBuffer[ulSize-1]=0; return chrBuffer; } int main__(int argc, char **argv) { int i, verbose = 0, use_os2_timer = 0; char *s; unsigned long uptime, days, hours, minutes, seconds; for (i = 1; i < argc; i++) { if (argv[i][0] == '-' || argv[i][0] == '/') { switch(argv[i][1]) { case 'h': case '?': print_help(); return(0); case 'v': verbose = 1; break; case '2': use_os2_timer = 1; break; } } } if (use_os2_timer) uptime = get_uptime_from_os2_timer(); else uptime = get_uptime_from_swapfile(); days = uptime / (60L * 60L * 24L); uptime = uptime % (60L * 60L * 24L); hours = uptime / (60L * 60L); uptime = uptime % (60L * 60L); minutes = uptime / (60L); seconds = uptime % (60L); #if 0 printf ("%s: uptime is %ld day%s, %02ld:%02ld hours and %02ld second%s\n", hostname, days, "s" + (days == 1), hours, minutes, seconds, "s" + (seconds == 1)); #endif return days; }