* Display a formatted value, or a '-' in the same space. */
| 192 | * Display a formatted value, or a '-' in the same space. |
| 193 | */ |
| 194 | static void |
| 195 | show_stat(const char *fmt, int width, const char *name, |
| 196 | u_long value, short showvalue, int div1000) |
| 197 | { |
| 198 | const char *lsep, *rsep; |
| 199 | char newfmt[64]; |
| 200 | |
| 201 | lsep = ""; |
| 202 | if (strncmp(fmt, "LS", 2) == 0) { |
| 203 | lsep = " "; |
| 204 | fmt += 2; |
| 205 | } |
| 206 | rsep = " "; |
| 207 | if (strncmp(fmt, "NRS", 3) == 0) { |
| 208 | rsep = ""; |
| 209 | fmt += 3; |
| 210 | } |
| 211 | if (showvalue == 0) { |
| 212 | /* Print just dash. */ |
| 213 | xo_emit("{P:/%s}{D:/%*s}{P:/%s}", lsep, width, "-", rsep); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * XXX: workaround {P:} modifier can't be empty and doesn't seem to |
| 219 | * take args... so we need to conditionally include it in the format. |
| 220 | */ |
| 221 | #define maybe_pad(pad) do { \ |
| 222 | if (strlen(pad)) { \ |
| 223 | snprintf(newfmt, sizeof(newfmt), "{P:%s}", pad); \ |
| 224 | xo_emit(newfmt); \ |
| 225 | } \ |
| 226 | } while (0) |
| 227 | |
| 228 | if (hflag) { |
| 229 | char buf[5]; |
| 230 | |
| 231 | /* Format in human readable form. */ |
| 232 | humanize_number(buf, sizeof(buf), (int64_t)value, "", |
| 233 | HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL | \ |
| 234 | ((div1000) ? HN_DIVISOR_1000 : 0)); |
| 235 | maybe_pad(lsep); |
| 236 | snprintf(newfmt, sizeof(newfmt), "{:%s/%%%ds}", name, width); |
| 237 | xo_emit(newfmt, buf); |
| 238 | maybe_pad(rsep); |
| 239 | } else { |
| 240 | /* Construct the format string. */ |
| 241 | maybe_pad(lsep); |
| 242 | snprintf(newfmt, sizeof(newfmt), "{:%s/%%%d%s}", |
| 243 | name, width, fmt); |
| 244 | xo_emit(newfmt, value); |
| 245 | maybe_pad(rsep); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Find next multiaddr for a given interface name. |
no test coverage detected