| 62 | /* ── FD count (platform-specific) ────────────────────────────────── */ |
| 63 | |
| 64 | static int count_open_fds(void) { |
| 65 | #ifdef __linux__ |
| 66 | struct dirent **entries = NULL; |
| 67 | int n = scandir("/proc/self/fd", &entries, NULL, NULL); |
| 68 | if (n < 0) { |
| 69 | return CBM_NOT_FOUND; |
| 70 | } |
| 71 | for (int i = 0; i < n; i++) { |
| 72 | free(entries[i]); |
| 73 | } |
| 74 | free(entries); |
| 75 | return n - PAIR_LEN; /* . and .. */ |
| 76 | #elif defined(__APPLE__) |
| 77 | /* Count via /dev/fd using scandir (MT-safe) */ |
| 78 | struct dirent **entries = NULL; |
| 79 | int n = scandir("/dev/fd", &entries, NULL, NULL); |
| 80 | if (n < 0) { |
| 81 | return CBM_NOT_FOUND; |
| 82 | } |
| 83 | for (int i = 0; i < n; i++) { |
| 84 | free(entries[i]); |
| 85 | } |
| 86 | free(entries); |
| 87 | return n - PAIR_LEN; /* . and .. */ |
| 88 | #else |
| 89 | return CBM_NOT_FOUND; /* Not available on Windows */ |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | /* ── Writer ──────────────────────────────────────────────────────── */ |
| 94 | |