[2] | 1 | /* Program: XUPTIME OS/2 1.1
|
---|
| 2 | Platform: OS/2
|
---|
| 3 | Compilers: Borland C, EMX GNU C, IBM C-Set
|
---|
| 4 | Compile: $(CC) uptime.c
|
---|
| 5 | Purpose: Print the uptime of the machine based on the creation date
|
---|
| 6 | of the swap file. In contrast too tools querying the uptime
|
---|
| 7 | timer of OS/2, this can also cover uptimes > ~ 50 days.
|
---|
| 8 | Author: Tobias Ernst, tobi@bland.fido.de
|
---|
| 9 | License: Public Domain
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #define INCL_DOSFILEMGR
|
---|
| 13 | #define INCL_DOSMISC
|
---|
| 14 | #define INCL_DOSDATETIME
|
---|
| 15 | #define INCL_DOSERRORS
|
---|
| 16 | #include <os2.h>
|
---|
| 17 | #include <stdio.h>
|
---|
| 18 | #include <stdlib.h>
|
---|
| 19 | #include <string.h>
|
---|
| 20 | /* get_boot_drive: get the drive letter of this OS/2 system's boot drive */
|
---|
| 21 | #include <sys_funcs.h>
|
---|
| 22 |
|
---|
| 23 | #define eatwhite(cp) { while (*(cp) == ' ' || *(cp) == '\n' || *(cp) == '\t') { (cp)++; } }
|
---|
| 24 |
|
---|
| 25 | /* get_swap_file: get the name of the swap file of this OS/2 system by
|
---|
| 26 | parsing it's config.sys file. */
|
---|
| 27 |
|
---|
| 28 | #define buffersize 256+50
|
---|
| 29 | static BOOL get_swap_file(char *chrSwapFile, ULONG ulSize)
|
---|
| 30 | {
|
---|
| 31 | char configsys[] = "X:\\CONFIG.SYS";
|
---|
| 32 | char buffer[buffersize], *cp;
|
---|
| 33 | FILE *f;
|
---|
| 34 | int i;
|
---|
| 35 |
|
---|
| 36 | configsys[0] = SysQueryBootDriveLetter();//get_boot_drive();
|
---|
| 37 | if ((f = fopen(configsys, "r")) == NULL)
|
---|
| 38 | return FALSE;
|
---|
| 39 |
|
---|
| 40 | while ( (cp = fgets(buffer, buffersize, f)) != NULL)
|
---|
| 41 | {
|
---|
| 42 | eatwhite(cp);
|
---|
| 43 | if (!strnicmp(cp, "SWAPPATH", 8))
|
---|
| 44 | {
|
---|
| 45 | cp += 8;
|
---|
| 46 | eatwhite(cp);
|
---|
| 47 | if (*cp == '=')
|
---|
| 48 | {
|
---|
| 49 | cp++;
|
---|
| 50 | eatwhite(cp);
|
---|
| 51 | for (i = 0;
|
---|
| 52 | cp[i] != ' ' && cp[i] != '\t'
|
---|
| 53 | && cp[i] != '\n' && cp[i] != '\0';
|
---|
| 54 | i++);
|
---|
| 55 | strncpy(chrSwapFile, cp, i);
|
---|
| 56 | if (chrSwapFile[i - 1] != '\\')
|
---|
| 57 | {
|
---|
| 58 | chrSwapFile[i++] = '\\';
|
---|
| 59 | }
|
---|
| 60 | strcpy(chrSwapFile + i, "SWAPPER.DAT");
|
---|
| 61 | fclose(f);
|
---|
| 62 | return (TRUE);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /* eat rest of line if it was > 256 chars */
|
---|
| 67 | while (*buffer && buffer[strlen(buffer) - 1] != '\n')
|
---|
| 68 | {
|
---|
| 69 | if (fgets(buffer, buffersize, f) == NULL)
|
---|
| 70 | {
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | // fprintf (stderr, "%s does not contain a SWAPPATH statement.\n", configsys);
|
---|
| 76 | fclose(f);
|
---|
| 77 | return 0;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | /* is_leap_year: tests if the specified year (4 digit) is a leap year */
|
---|
| 82 |
|
---|
| 83 | static int is_leap_year(unsigned year)
|
---|
| 84 | {
|
---|
| 85 | if (!((year % 100) % 4))
|
---|
| 86 | {
|
---|
| 87 | if (!(year % 100))
|
---|
| 88 | {
|
---|
| 89 | if (!(year % 400))
|
---|
| 90 | {
|
---|
| 91 | return 1;
|
---|
| 92 | }
|
---|
| 93 | else
|
---|
| 94 | {
|
---|
| 95 | return 0;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | else
|
---|
| 99 | {
|
---|
| 100 | return 1;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | else
|
---|
| 104 | {
|
---|
| 105 | return 0;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | /* n_days_in_month: returns the number of days in a certain month */
|
---|
| 111 |
|
---|
| 112 | static int n_days_in_month(unsigned month, unsigned year)
|
---|
| 113 | {
|
---|
| 114 | switch (month)
|
---|
| 115 | {
|
---|
| 116 | case 1:
|
---|
| 117 | case 3:
|
---|
| 118 | case 5:
|
---|
| 119 | case 7:
|
---|
| 120 | case 8:
|
---|
| 121 | case 10:
|
---|
| 122 | case 12:
|
---|
| 123 | return 31;
|
---|
| 124 | case 4:
|
---|
| 125 | case 6:
|
---|
| 126 | case 9:
|
---|
| 127 | case 11:
|
---|
| 128 | return 30;
|
---|
| 129 | case 2:
|
---|
| 130 | return (is_leap_year(year)) ? 29 : 28;
|
---|
| 131 | }
|
---|
| 132 | return 0;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | /* n_days_since_1900: returns the number of days that have passed since
|
---|
| 138 | 1/1/1900 */
|
---|
| 139 |
|
---|
| 140 | unsigned long n_days_since_1900(int dd, int md, int yd)
|
---|
| 141 | {
|
---|
| 142 | int d=1,m,y;
|
---|
| 143 | unsigned long ds=0;
|
---|
| 144 |
|
---|
| 145 | for (y = 1900; y<yd; y++)
|
---|
| 146 | {
|
---|
| 147 | ds += is_leap_year(y) ? 366 : 365;
|
---|
| 148 | }
|
---|
| 149 | for (m=1; m<md; m++)
|
---|
| 150 | {
|
---|
| 151 | ds += n_days_in_month(m, y);
|
---|
| 152 | }
|
---|
| 153 | ds += dd - d;
|
---|
| 154 | return ds;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | /* get_uptime: returns the age of the swap file in seconds */
|
---|
| 159 |
|
---|
| 160 | unsigned long get_uptime_from_swapfile(void)
|
---|
| 161 | {
|
---|
| 162 | static char chrSwapFile[CCHMAXPATH]={0};
|
---|
| 163 | FILEFINDBUF3 InfoBuf;
|
---|
| 164 | HDIR hDir = 0x0001;
|
---|
| 165 | ULONG cSearch = 1;
|
---|
| 166 | ULONG usAttrib;
|
---|
| 167 | APIRET rc;
|
---|
| 168 | DATETIME DateTime;
|
---|
| 169 | unsigned long uptime;
|
---|
| 170 |
|
---|
| 171 | if(chrSwapFile[0]==0)
|
---|
| 172 | get_swap_file(chrSwapFile, sizeof(chrSwapFile));
|
---|
| 173 |
|
---|
| 174 |
|
---|
| 175 | rc = DosFindFirst((UCHAR *)chrSwapFile,
|
---|
| 176 | &hDir,
|
---|
| 177 | FILE_SYSTEM | FILE_HIDDEN | FILE_READONLY,
|
---|
| 178 | &InfoBuf,
|
---|
| 179 | sizeof InfoBuf,
|
---|
| 180 | &cSearch,
|
---|
| 181 | FIL_STANDARD);
|
---|
| 182 |
|
---|
| 183 | if (rc != NO_ERROR)
|
---|
| 184 | {
|
---|
| 185 | fprintf(stderr, "DosFindFirst returned %ld\n", rc);
|
---|
| 186 | return(0);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | DosFindClose(hDir);
|
---|
| 190 |
|
---|
| 191 | rc = DosGetDateTime(&DateTime);
|
---|
| 192 |
|
---|
| 193 | if (rc != 0)
|
---|
| 194 | {
|
---|
| 195 | fprintf(stderr, "DosGetDateTime returned %ld\n", rc);
|
---|
| 196 | return(0);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | uptime = n_days_since_1900(DateTime.day, DateTime.month, DateTime.year);
|
---|
| 200 | uptime -= n_days_since_1900(InfoBuf.fdateCreation.day,
|
---|
| 201 | InfoBuf.fdateCreation.month,
|
---|
| 202 | InfoBuf.fdateCreation.year + 1980);
|
---|
| 203 | uptime *= (60L * 60L * 24L);
|
---|
| 204 |
|
---|
| 205 | uptime += (ULONG)DateTime.hours * 60L * 60L
|
---|
| 206 | + (ULONG)DateTime.minutes * 60L
|
---|
| 207 | + (ULONG)DateTime.seconds;
|
---|
| 208 | uptime -= (ULONG)InfoBuf.ftimeCreation.hours * 60L * 60L
|
---|
| 209 | + (ULONG)InfoBuf.ftimeCreation.minutes * 60L
|
---|
| 210 | + (ULONG)InfoBuf.ftimeCreation.twosecs * 2L;
|
---|
| 211 |
|
---|
| 212 | #if 0
|
---|
| 213 | if (verbose)
|
---|
| 214 | {
|
---|
| 215 | printf ("OS/2 swap file used: %s\n", swapfile);
|
---|
| 216 | printf ("Swapfile creation date: %02d/%02d/%04d, %02d:%02d\n",
|
---|
| 217 | InfoBuf.fdateCreation.month, InfoBuf.fdateCreation.day,
|
---|
| 218 | InfoBuf.fdateCreation.year + 1980,
|
---|
| 219 | InfoBuf.ftimeCreation.hours, InfoBuf.ftimeCreation.minutes);
|
---|
| 220 | printf ("Last modification: %02d/%02d/%04d, %02d:%02d\n",
|
---|
| 221 | InfoBuf.fdateLastWrite.month, InfoBuf.fdateLastWrite.day,
|
---|
| 222 | InfoBuf.fdateLastWrite.year + 1980,
|
---|
| 223 | InfoBuf.ftimeLastWrite.hours, InfoBuf.ftimeLastWrite.minutes);
|
---|
| 224 | printf ("Last read access: %02d/%02d/%04d, %02d:%02d\n",
|
---|
| 225 | InfoBuf.fdateLastAccess.month, InfoBuf.fdateLastAccess.day,
|
---|
| 226 | InfoBuf.fdateLastAccess.year + 1980,
|
---|
| 227 | InfoBuf.ftimeLastAccess.hours, InfoBuf.ftimeLastAccess.minutes);
|
---|
| 228 | printf ("Current date is: %02d/%02d/%04d, %02d:%02d\n",
|
---|
| 229 | DateTime.month, DateTime.day, DateTime.year,
|
---|
| 230 | DateTime.hours, DateTime.minutes);
|
---|
| 231 | printf ("-------------------------------------------------\n");
|
---|
| 232 | }
|
---|
| 233 | #endif
|
---|
| 234 |
|
---|
| 235 | return uptime;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /* get_uptime_from_os2_timer: get the "uptime" from QSV_MS_COUNT */
|
---|
| 239 | unsigned long get_uptime_from_os2_timer(void)
|
---|
| 240 | {
|
---|
| 241 | ULONG ms_count;
|
---|
| 242 | ULONG DataBufLen = 4;
|
---|
| 243 |
|
---|
| 244 | if (DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, (UCHAR *)(&ms_count), DataBufLen) != NO_ERROR)
|
---|
| 245 | return(0);
|
---|
| 246 |
|
---|
| 247 | return ms_count / (ULONG)1000; /* Return seconds */
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 |
|
---|
| 251 | void print_help(void)
|
---|
| 252 | {
|
---|
| 253 | printf (" -2 Query uptime from OS/2 system timer. This does only work if the\n");
|
---|
| 254 | printf (" uptime is < 50 days. If you do NOT specify this parameter, the\n");
|
---|
| 255 | printf (" uptime is calculated from the creation date of the swap file,\n");
|
---|
| 256 | printf (" which will also work for uptimes >= 50 days.\n");
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[4] | 259 | /*!***********************************************************/
|
---|
| 260 | /* */
|
---|
| 261 | /* @@DESC */
|
---|
| 262 | /* */
|
---|
| 263 | /* To be written... */
|
---|
| 264 | /* */
|
---|
| 265 | /*!!**********************************************************/
|
---|
[2] | 266 | ULONG SysGetUptimeSecs(BOOL fUseTimer)
|
---|
| 267 | {
|
---|
| 268 | if(fUseTimer)
|
---|
| 269 | return get_uptime_from_os2_timer();
|
---|
| 270 | else
|
---|
| 271 | return get_uptime_from_swapfile();
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[4] | 274 | /*!***********************************************************/
|
---|
| 275 | /* */
|
---|
| 276 | /* @@DESC */
|
---|
| 277 | /* */
|
---|
| 278 | /* To be written... */
|
---|
| 279 | /* */
|
---|
| 280 | /*!!**********************************************************/
|
---|
[2] | 281 | char* SysGetUptimeString(BOOL fUseTimer, char * chrBuffer, ULONG ulSize)
|
---|
| 282 | {
|
---|
| 283 | unsigned long uptime, days, hours, minutes, seconds;
|
---|
| 284 | char chrTemp[30];
|
---|
| 285 |
|
---|
| 286 | if(!ulSize)
|
---|
| 287 | return NULLHANDLE;
|
---|
| 288 |
|
---|
| 289 | uptime=SysGetUptimeSecs(fUseTimer);
|
---|
| 290 |
|
---|
| 291 | days = uptime / (60L * 60L * 24L);
|
---|
| 292 | uptime = uptime % (60L * 60L * 24L);
|
---|
| 293 | hours = uptime / (60L * 60L);
|
---|
| 294 | uptime = uptime % (60L * 60L);
|
---|
| 295 | minutes = uptime / (60L);
|
---|
| 296 | seconds = uptime % (60L);
|
---|
| 297 |
|
---|
[108] | 298 | sprintf (chrTemp, "%lu %02lu:%02lu:%02lu",
|
---|
[2] | 299 | days, hours,
|
---|
| 300 | minutes, seconds);
|
---|
| 301 | strncpy(chrBuffer, chrTemp, ulSize);
|
---|
| 302 | chrBuffer[ulSize-1]=0;
|
---|
| 303 | return chrBuffer;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | int main__(int argc, char **argv)
|
---|
| 307 | {
|
---|
| 308 | int i, verbose = 0, use_os2_timer = 0;
|
---|
| 309 | char *s;
|
---|
| 310 | unsigned long uptime, days, hours, minutes, seconds;
|
---|
| 311 |
|
---|
| 312 | for (i = 1; i < argc; i++)
|
---|
| 313 | {
|
---|
| 314 | if (argv[i][0] == '-' || argv[i][0] == '/')
|
---|
| 315 | {
|
---|
| 316 | switch(argv[i][1])
|
---|
| 317 | {
|
---|
| 318 | case 'h':
|
---|
| 319 | case '?':
|
---|
| 320 | print_help();
|
---|
| 321 | return(0);
|
---|
| 322 | case 'v':
|
---|
| 323 | verbose = 1;
|
---|
| 324 | break;
|
---|
| 325 | case '2':
|
---|
| 326 | use_os2_timer = 1;
|
---|
| 327 | break;
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | if (use_os2_timer)
|
---|
| 333 | uptime = get_uptime_from_os2_timer();
|
---|
| 334 | else
|
---|
| 335 | uptime = get_uptime_from_swapfile();
|
---|
| 336 |
|
---|
| 337 | days = uptime / (60L * 60L * 24L);
|
---|
| 338 | uptime = uptime % (60L * 60L * 24L);
|
---|
| 339 | hours = uptime / (60L * 60L);
|
---|
| 340 | uptime = uptime % (60L * 60L);
|
---|
| 341 | minutes = uptime / (60L);
|
---|
| 342 | seconds = uptime % (60L);
|
---|
| 343 |
|
---|
| 344 | #if 0
|
---|
| 345 | printf ("%s: uptime is %ld day%s, %02ld:%02ld hours and %02ld second%s\n",
|
---|
| 346 | hostname, days, "s" + (days == 1), hours,
|
---|
| 347 | minutes, seconds, "s" + (seconds == 1));
|
---|
| 348 | #endif
|
---|
| 349 |
|
---|
| 350 | return days;
|
---|
| 351 | }
|
---|
| 352 |
|
---|