source: branches/v2.9/common_functions/uptime.c@ 7

Last change on this file since 7 was 2, checked in by stevenhl, 8 years ago

Import sources from cwmm-full.zip dated 2005-03-21

File size: 8.9 KB
Line 
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
29static 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
83static 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
112static 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
140unsigned 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
160unsigned 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 */
239unsigned 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
251void 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
259ULONG SysGetUptimeSecs(BOOL fUseTimer)
260{
261 if(fUseTimer)
262 return get_uptime_from_os2_timer();
263 else
264 return get_uptime_from_swapfile();
265}
266
267char* SysGetUptimeString(BOOL fUseTimer, char * chrBuffer, ULONG ulSize)
268{
269 unsigned long uptime, days, hours, minutes, seconds;
270 char chrTemp[30];
271
272 if(!ulSize)
273 return NULLHANDLE;
274
275 uptime=SysGetUptimeSecs(fUseTimer);
276
277 days = uptime / (60L * 60L * 24L);
278 uptime = uptime % (60L * 60L * 24L);
279 hours = uptime / (60L * 60L);
280 uptime = uptime % (60L * 60L);
281 minutes = uptime / (60L);
282 seconds = uptime % (60L);
283
284 sprintf (chrTemp, "%ld %02ld:%02ld:%02ld",
285 days, hours,
286 minutes, seconds);
287 strncpy(chrBuffer, chrTemp, ulSize);
288 chrBuffer[ulSize-1]=0;
289 return chrBuffer;
290}
291
292int main__(int argc, char **argv)
293{
294 int i, verbose = 0, use_os2_timer = 0;
295 char *s;
296 unsigned long uptime, days, hours, minutes, seconds;
297
298 for (i = 1; i < argc; i++)
299 {
300 if (argv[i][0] == '-' || argv[i][0] == '/')
301 {
302 switch(argv[i][1])
303 {
304 case 'h':
305 case '?':
306 print_help();
307 return(0);
308 case 'v':
309 verbose = 1;
310 break;
311 case '2':
312 use_os2_timer = 1;
313 break;
314 }
315 }
316 }
317
318 if (use_os2_timer)
319 uptime = get_uptime_from_os2_timer();
320 else
321 uptime = get_uptime_from_swapfile();
322
323 days = uptime / (60L * 60L * 24L);
324 uptime = uptime % (60L * 60L * 24L);
325 hours = uptime / (60L * 60L);
326 uptime = uptime % (60L * 60L);
327 minutes = uptime / (60L);
328 seconds = uptime % (60L);
329
330#if 0
331 printf ("%s: uptime is %ld day%s, %02ld:%02ld hours and %02ld second%s\n",
332 hostname, days, "s" + (days == 1), hours,
333 minutes, seconds, "s" + (seconds == 1));
334#endif
335
336 return days;
337}
338
Note: See TracBrowser for help on using the repository browser.