- if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
- fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
- fclose(fp);
- return NULL;
+#elif defined(__OpenBSD__)
+ #include <stdlib.h>
+ #include <sys/param.h> /* dbtob */
+ #include <sys/swap.h>
+ #include <sys/types.h>
+ #include <unistd.h>
+
+ static void
+ getstats(int *total, int *used)
+ {
+ struct swapent *sep, *fsep;
+ int rnswap, nswap, i;
+
+ nswap = swapctl(SWAP_NSWAP, 0, 0);
+ if (nswap < 1) {
+ fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno));
+ }
+
+ fsep = sep = calloc(nswap, sizeof(*sep));
+ if (!sep) {
+ fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno));
+ }
+
+ rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
+ if (rnswap < 0) {
+ fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno));
+ }
+
+ if (nswap != rnswap) {
+ fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n");
+ }
+
+ *total = 0;
+ *used = 0;
+
+ for (i = 0; i < rnswap; i++) {
+ *total += sep->se_nblks >> 1;
+ *used += sep->se_inuse >> 1;
+ }
+
+ free(fsep);