1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
/* $Id$ */
#include "bbs.h"
#ifdef NEED_STRLCAT
#include <sys/types.h>
#include <string.h>
/* size_t
* strlcat(char *dst, const char *src, size_t size);
*/
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t
strlcat(dst, src, siz)
char *dst;
const char *src;
size_t siz;
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
#endif
#ifdef NEED_STRLCPY
/* ------------------------------------------------------------------------ */
/* size_t
* strlcpy(char *dst, const char *src, size_t size);
*/
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t strlcpy(dst, src, siz)
char *dst;
const char *src;
size_t siz;
{
char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
#endif
#ifdef NEED_STRCASESTR
char *
strcasestr(const char *big, const char *little)
{
char *ans = (char *)big;
int len = strlen(little);
char *endptr = (char *)big + strlen(big) - len;
while (ans <= endptr)
if (!strncasecmp(ans, little, len))
return ans;
else
ans++;
return 0;
}
#endif
#ifdef NEED_SCANDIR
/*
* Scan the directory dirname calling select to make a list of selected
* directory entries then sort using qsort and compare routine dcomp.
* Returns the number of entries and a pointer to a list of pointers to
* struct dirent (through namelist). Returns -1 if there were any errors.
*/
<<<<<<< .working
=======
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
/*
* The DIRSIZ macro is the minimum record length which will hold the directory
* entry. This requires the amount of space in struct dirent without the
* d_name field, plus enough space for the name and a terminating nul byte
* (dp->d_namlen + 1), rounded up to a 4 byte boundary.
*/
#undef DIRSIZ
#define DIRSIZ(dp) \
((sizeof(struct dirent) - sizeof(dp)->d_name) + \
((strlen((dp)->d_name) + 1 + 3) &~ 3))
#if 0
((sizeof(struct dirent) - sizeof(dp)->d_name) + \
(((dp)->d_namlen + 1 + 3) &~ 3))
#endif
int
scandir(dirname, namelist, select, dcomp)
const char *dirname;
struct dirent ***namelist;
int (*select) (struct dirent *);
int (*dcomp) (const void *, const void *);
{
register struct dirent *d, *p, **names;
register size_t nitems;
struct stat stb;
long arraysz;
DIR *dirp;
if ((dirp = opendir(dirname)) == NULL)
return(-1);
if (fstat(dirp->dd_fd, &stb) < 0)
return(-1);
/*
* estimate the array size by taking the size of thedirectory file
* and dividing it by a multiple of the minimum sizeentry.
*/
arraysz = (stb.st_size / 24);
names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
if (names == NULL)
return(-1);
nitems = 0;
while ((d = readdir(dirp)) != NULL) {
if (select != NULL && !(*select)(d))
continue; /* just selected names */
/*
* Make a minimum size copy of the data
*/
p = (struct dirent *)malloc(DIRSIZ(d));
if (p == NULL)
return(-1);
p->d_ino = d->d_ino;
p->d_off = d->d_off;
p->d_reclen = d->d_reclen;
memcpy(p->d_name, d->d_name, strlen(d->d_name) +1);
#if 0
p->d_fileno = d->d_fileno;
p->d_type = d->d_type;
p->d_reclen = d->d_reclen;
p->d_namlen = d->d_namlen;
bcopy(d->d_name, p->d_name, p->d_namlen + 1);
#endif
/*
* Check to make sure the array has space left and
* realloc the maximum size.
*/
if (++nitems >= arraysz) {
if (fstat(dirp->dd_fd, &stb) < 0)
return(-1); /* just might have grown */
arraysz = stb.st_size / 12;
names = (struct dirent **)realloc((char*)names,
arraysz * sizeof(struct dirent*));
if (names == NULL)
return(-1);
}
names[nitems-1] = p;
}
closedir(dirp);
if (nitems && dcomp != NULL)
qsort(names, nitems, sizeof(struct dirent *),dcomp);
*namelist = names;
return(nitems);
}
/*
* Alphabetic order comparison routine for those who want it.
*/
int
alphasort(d1, d2)
const void *d1;
const void *d2;
{
return(strcmp((*(struct dirent **)d1)->d_name,
(*(struct dirent **)d2)->d_name));
}
#endif
#ifdef NEED_FLOCK
int
flock (int fd, int f)
{
if( f == LOCK_EX )
return lockf(fd, F_LOCK, 0L);
if( f == LOCK_UN )
return lockf(fd, F_ULOCK, 0L);
return -1;
}
#endif
#ifdef NEED_UNSETENV
void
unsetenv(name)
char *name;
{
extern char **environ;
register char **pp;
int len = strlen(name);
for (pp = environ; *pp != NULL; pp++)
{
if (strncmp(name, *pp, len) == 0 &&
((*pp)[len] == '=' || (*pp)[len] == '\0'))
break;
}
for (; *pp != NULL; pp++)
*pp = pp[1];
}
#endif
#ifdef NEED_INET_PTON
#include <arpa/nameser.h>
/*
* Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
int
inet_pton(int af, const char *src, void *dst)
{
static const char digits[] = "0123456789";
int saw_digit, octets, ch;
u_char tmp[INADDRSZ], *tp;
saw_digit = 0;
octets = 0;
*(tp = tmp) = 0;
while ((ch = *src++) != '\0') {
const char *pch;
if ((pch = strchr(digits, ch)) != NULL) {
u_int new = *tp * 10 + (pch - digits);
if (new > 255)
return (0);
*tp = new;
if (! saw_digit) {
if (++octets > 4)
return (0);
saw_digit = 1;
}
} else if (ch == '.' && saw_digit) {
if (octets == 4)
return (0);
*++tp = 0;
saw_digit = 0;
} else
return (0);
}
if (octets < 4)
return (0);
memcpy(dst, tmp, INADDRSZ);
return (1);
}
#endif
#ifdef NEED_BSD_SIGNAL
void (*bsd_signal(int sig, void (*func)(int)))(int)
{
struct sigaction act, oact;
act.sa_handler = func;
act.sa_flags = SA_RESTART;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, sig);
if (sigaction(sig, &act, &oact) == -1)
return(SIG_ERR);
return(oact.sa_handler);
}
#endif
#ifdef Solaris
#include <sys/stat.h>
#include <sys/swap.h>
double swapused(int *total, int *used)
{
double percent = -1;
register int cnt, i;
register int free;
struct swaptable *swt;
struct swapent *ste;
static char path[256];
cnt = swapctl(SC_GETNSWP, 0);
swt = (struct swaptable *)malloc(sizeof(int) +
cnt * sizeof(struct swapent));
if (swt == NULL)
{
return 0;
}
swt->swt_n = cnt;
/* fill in ste_path pointers: we don't care about the paths, so we point
them all to the same buffer */
ste = &(swt->swt_ent[0]);
i = cnt;
while (--i >= 0)
{
ste++->ste_path = path;
}
/* grab all swap info */
swapctl(SC_LIST, swt);
/* walk thru the structs and sum up the fields */
*total = free = 0;
ste = &(swt->swt_ent[0]);
i = cnt;
while (--i >= 0)
{
/* dont count slots being deleted */
if (!(ste->ste_flags & ST_INDEL) &&
!(ste->ste_flags & ST_DOINGDEL))
{
*total += ste->ste_pages;
free += ste->ste_free;
}
ste++;
}
*used = *total - free;
if( total != 0)
percent = (double)*used / (double)*total;
else
percent = 0;
return percent;
}
double
swapused(int *total, int *used)
{
double percent = -1;
kvm_t *kd;
struct kvm_swap swapinfo;
int pagesize;
kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
if (kd) {
if (kvm_getswapinfo(kd, &swapinfo, 1, 0) == 0) {
pagesize = getpagesize();
*total = swapinfo.ksw_total * pagesize;
*used = swapinfo.ksw_used * pagesize;
if (*total != 0)
percent = (double)*used / (double)*total;
}
kvm_close(kd);
}
return percent;
}
#endif
#if __FreeBSD__
int
cpuload(char *str)
{
double l[3] = {-1, -1, -1};
if (getloadavg(l, 3) != 3)
l[0] = -1;
if (str) {
if (l[0] != -1)
sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
else
strcpy(str, " (unknown) ");
}
return (int)l[0];
}
#endif
#ifdef Solaris
#include <kstat.h>
#include <sys/param.h>
#define loaddouble(la) ((double)(la) / FSCALE)
int
cpuload(char *str)
{
kstat_ctl_t *kc;
kstat_t *ks;
kstat_named_t *kn;
double l[3] = {-1, -1, -1};
kc = kstat_open();
if( !kc ){
strcpy(str, "(unknown) ");
return -1;
}
ks = kstat_lookup( kc, "unix", 0, "system_misc");
if( kstat_read( kc, ks, 0) == -1){
strcpy( str, "( unknown ");
return -1;
}
kn = kstat_data_lookup( ks, "avenrun_1min" );
if( kn ) {
l[0] = loaddouble(kn->value.ui32);
}
kn = kstat_data_lookup( ks, "avenrun_5min" );
if( kn ) {
l[1] = loaddouble(kn->value.ui32);
}
kn = kstat_data_lookup( ks, "avenrun_15min" );
if( kn ) {
l[2] = loaddouble(kn->value.ui32);
}
if (str) {
if (l[0] != -1)
sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
else
strcpy(str, " (unknown) ");
}
kstat_close(kc);
return (int)l[0];
}
#endif
#ifdef __linux__
int
cpuload(char *str)
{
double l[3] = {-1, -1, -1};
FILE *fp;
if ((fp = fopen("/proc/loadavg", "r"))) {
if (fscanf(fp, "%lf %lf %lf", &l[0], &l[1], &l[2]) != 3)
l[0] = -1;
fclose(fp);
}
if (str) {
if (l[0] != -1)
snprintf(str, sizeof(str), " %.2f %.2f %.2f", l[0], l[1], l[2]);
else
strcpy(str, " (unknown) ");
}
return (int)l[0];
}
double
swapused(int *total, int *used)
{
double percent = -1;
char buf[101];
FILE *fp;
if ((fp = fopen("/proc/meminfo", "r"))) {
while (fgets(buf, 100, fp) && strstr(buf, "SwapTotal:") == NULL);
sscanf(buf, "%*s %d", total);
fgets(buf, 100, fp);
sscanf(buf, "%*s %d", used);
*used = *total - *used;
if (*total != 0)
percent = (double)*used / (double)*total;
fclose(fp);
}
return percent;
}
#endif
|