/* * "human-readable" output: use 3 digits max.--put unit suffixes at * the end. Makes output compact and easy-to-read esp. on huge disks. * Code moved into libutil; this is now just a wrapper. */ static void prthumanval(long long bytes) { char ret[FMT_SCALED_STRSIZE]; if (fmt_scaled(bytes, ret) == -1) { (void)printf(" %lld", bytes); return; } (void)printf(" %7s", ret); }
static void printsize(size_t width, off_t bytes) { char ret[FMT_SCALED_STRSIZE]; if ((f_humanval) && (fmt_scaled(bytes, ret) != -1)) { (void)printf("%*s ", (u_int)width, ret); return; } (void)printf("%*qd ", (u_int)width, bytes); }
static void printsize(int width, off_t bytes) { char ret[FMT_SCALED_STRSIZE]; if ((f_humanval) && (fmt_scaled(bytes, ret) != -1)) { (void)printf("%*s ", width, ret); return; } (void)printf("%*lld ", width, (long long)bytes); }
void format_and_print(long long v) { if (humanchar) { char result[FMT_SCALED_STRSIZE]; (void)fmt_scaled(v, result); (void)printf("%7s", result); } else { (void)printf(" %7lld", v); } }
void ct_print_scaled_stat(FILE *outfh, const char *label, int64_t val, int64_t sec, int newline) { char rslt[FMT_SCALED_STRSIZE]; bzero(rslt, sizeof(rslt)); rslt[0] = '?'; fmt_scaled(val, rslt); fprintf(outfh, "%s\t%s", label, rslt); if (val == 0 || sec == 0) { if (newline) fprintf(outfh, "\n"); return; } bzero(rslt, sizeof(rslt)); rslt[0] = '?'; fmt_scaled(val / sec, rslt); fprintf(outfh, "\t(%s/sec)%s", rslt, newline ? "\n": ""); }
void prtout(quad_t size, char *path, int hflag) { if (!hflag) (void)printf("%lld\t%s\n", (long long)size, path); else { char buf[FMT_SCALED_STRSIZE]; if (fmt_scaled(size * 512, buf) == 0) (void)printf("%s\t%s\n", buf, path); else (void)printf("%lld\t%s\n", (long long)size, path); } }
/* * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh */ char * ls_file(const char *name, const struct stat *st, int remote, int si_units) { int ulen, glen, sz = 0; struct tm *ltime = localtime(&st->st_mtime); char *user, *group; char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; char sbuf[FMT_SCALED_STRSIZE]; time_t now; strmode(st->st_mode, mode); if (!remote) { user = user_from_uid(st->st_uid, 0); } else { snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); user = ubuf; } if (!remote) { group = group_from_gid(st->st_gid, 0); } else { snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); group = gbuf; } if (ltime != NULL) { now = time(NULL); if (now - (365*24*60*60)/2 < st->st_mtime && now >= st->st_mtime) sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); else sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); } if (sz == 0) tbuf[0] = '\0'; ulen = MAX(strlen(user), 8); glen = MAX(strlen(group), 8); if (si_units) { fmt_scaled((long long)st->st_size, sbuf); snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode, (u_int)st->st_nlink, ulen, user, glen, group, sbuf, tbuf, name); } else { snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, (u_int)st->st_nlink, ulen, user, glen, group, (unsigned long long)st->st_size, tbuf, name); } return xstrdup(buf); }
/* * This is the original version of the program in the man page. * Copy-and-paste whatever you need from it. */ int main(int argc, char **argv) { char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE]; long long ninput = 10483892, result; if (scan_scaled(cinput, &result) == 0) printf("\"%s\" -> %lld\n", cinput, result); else perror(cinput); if (fmt_scaled(ninput, buf) == 0) printf("%lld -> \"%s\"\n", ninput, buf); else fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno)); return 0; }
char * win32_ls_file(const char *name, LPWIN32_FIND_DATA file, int remote, int si_units) { int ulen, glen, sz = 0; //struct tm *ltime = localtime(&st->st_mtime); char *user, *group; char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; char sbuf[FMT_SCALED_STRSIZE]; SYSTEMTIME now; SYSTEMTIME ftime; time_t mtime = filetime_to_time_t( file->ftLastWriteTime ); BOOL time_conv_ok = FileTimeToSystemTime( &file->ftLastWriteTime, &ftime); struct tm *ltime = localtime( &mtime ); if (!time_conv_ok) { error("Failed to convert file time to localtime"); } strmode(0644, mode); if (!remote) { user = user_from_uid(0, 0); } else { snprintf(ubuf, sizeof ubuf, "%u", 0); user = ubuf; } if (!remote) { group = group_from_gid(0, 0); } else { snprintf(gbuf, sizeof gbuf, "%u", 0); group = gbuf; } if (time_conv_ok) { //now = time(NULL); GetSystemTime(&now); if ( (time_diff(now, ftime) / 10000000ULL) < (365*24*60*60) ) { //if (now - (365*24*60*60)/2 < st->st_mtime && // now >= st->st_mtime) sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); } else { sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); } } if (sz == 0) tbuf[0] = '\0'; ulen = MAX(strlen(user), 8); glen = MAX(strlen(group), 8); long long size = (file->nFileSizeHigh * (MAXDWORD+1)) + file->nFileSizeLow; if (si_units) { fmt_scaled(size, sbuf); snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode, 1 /*nlink -- FIXME */, ulen, user, glen, group, sbuf, tbuf, name); } else { snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, 1 /*nlink -- FIXME */, ulen, user, glen, group, size, tbuf, name); } return xstrdup(buf); }
/* * Print a running summary of interface statistics. * Repeat display every interval seconds, showing statistics * collected over that interval. Assumes that interval is non-zero. * First line printed at top of screen is always cumulative. */ static void sidewaysintpr(unsigned int interval, int repeatcount) { sigset_t emptyset; int line; char ibytes[FMT_SCALED_STRSIZE]; char obytes[FMT_SCALED_STRSIZE]; fetchifs(); if (ip_cur.ift_name[0] == '\0') { fprintf(stderr, "%s: %s: unknown interface\n", __progname, interface); exit(1); } (void)signal(SIGALRM, catchalarm); signalled = 0; (void)alarm(interval); banner: if (bflag) printf("%7.7s in %8.8s %6.6s out %5.5s", ip_cur.ift_name, " ", ip_cur.ift_name, " "); else printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s", ip_cur.ift_name, " ", ip_cur.ift_name, " ", " "); if (dflag) printf(" %5.5s", " "); if (bflag) printf(" %7.7s in %8.8s %6.6s out %5.5s", "total", " ", "total", " "); else printf(" %5.5s in %5.5s%5.5s out %5.5s %5.5s", "total", " ", "total", " ", " "); if (dflag) printf(" %5.5s", " "); putchar('\n'); if (bflag) printf("%10.10s %8.8s %10.10s %5.5s", "bytes", " ", "bytes", " "); else printf("%8.8s %5.5s %8.8s %5.5s %5.5s", "packets", "errs", "packets", "errs", "colls"); if (dflag) printf(" %5.5s", "drops"); if (bflag) printf("%10.10s %8.8s %10.10s %5.5s", "bytes", " ", "bytes", " "); else printf(" %8.8s %5.5s %8.8s %5.5s %5.5s", "packets", "errs", "packets", "errs", "colls"); if (dflag) printf(" %5.5s", "drops"); putchar('\n'); fflush(stdout); line = 0; bzero(&ip_old, sizeof(ip_old)); bzero(&sum_old, sizeof(sum_old)); loop: bzero(&sum_cur, sizeof(sum_cur)); fetchifs(); if (bflag) { if (hflag) { fmt_scaled(ip_cur.ift_ib - ip_old.ift_ib, ibytes); fmt_scaled(ip_cur.ift_ob - ip_old.ift_ob, obytes); printf("%10s %8.8s %10s %5.5s", ibytes, " ", obytes, " "); } else printf("%10llu %8.8s %10llu %5.5s", ip_cur.ift_ib - ip_old.ift_ib, " ", ip_cur.ift_ob - ip_old.ift_ob, " "); } else printf("%8llu %5llu %8llu %5llu %5llu", ip_cur.ift_ip - ip_old.ift_ip, ip_cur.ift_ie - ip_old.ift_ie, ip_cur.ift_op - ip_old.ift_op, ip_cur.ift_oe - ip_old.ift_oe, ip_cur.ift_co - ip_old.ift_co); if (dflag) printf(" %5llu", /* XXX ifnet.if_snd.ifq_drops - ip->ift_dr); */ 0LL); ip_old = ip_cur; if (bflag) { if (hflag) { fmt_scaled(sum_cur.ift_ib - sum_old.ift_ib, ibytes); fmt_scaled(sum_cur.ift_ob - sum_old.ift_ob, obytes); printf(" %10s %8.8s %10s %5.5s", ibytes, " ", obytes, " "); } else printf(" %10llu %8.8s %10llu %5.5s", sum_cur.ift_ib - sum_old.ift_ib, " ", sum_cur.ift_ob - sum_old.ift_ob, " "); } else printf(" %8llu %5llu %8llu %5llu %5llu", sum_cur.ift_ip - sum_old.ift_ip, sum_cur.ift_ie - sum_old.ift_ie, sum_cur.ift_op - sum_old.ift_op, sum_cur.ift_oe - sum_old.ift_oe, sum_cur.ift_co - sum_old.ift_co); if (dflag) printf(" %5llu", sum_cur.ift_dr - sum_old.ift_dr); sum_old = sum_cur; putchar('\n'); fflush(stdout); if (repeatcount && --repeatcount == 0) return; line++; sigemptyset(&emptyset); if (!signalled) sigsuspend(&emptyset); signalled = 0; (void)alarm(interval); if (line == 21 && isatty(STDOUT_FILENO)) goto banner; goto loop; }
static void print_addr(struct sockaddr *sa, struct sockaddr **rtinfo, struct if_data *ifd) { struct sockaddr_dl *sdl; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; char *cp; int m, n; switch (sa->sa_family) { case AF_UNSPEC: printf("%-11.11s ", "none"); printf("%-17.17s ", "none"); break; case AF_INET: sin = (struct sockaddr_in *)sa; cp = netname4(sin->sin_addr.s_addr, ((struct sockaddr_in *)rtinfo[RTAX_NETMASK])->sin_addr.s_addr); if (vflag) n = strlen(cp) < 11 ? 11 : strlen(cp); else n = 11; printf("%-*.*s ", n, n, cp); cp = routename4(sin->sin_addr.s_addr); if (vflag) n = strlen(cp) < 17 ? 17 : strlen(cp); else n = 17; printf("%-*.*s ", n, n, cp); #if 0 if (aflag) { u_long multiaddr; struct in_multi inm; multiaddr = (u_long)LIST_FIRST(&ifaddr.in.ia_multiaddrs); while (multiaddr != 0) { kread(multiaddr, &inm, sizeof inm); printf("\n%25s %-17.17s ", "", routename4(inm.inm_addr.s_addr)); multiaddr = (u_long)LIST_NEXT(&inm, inm_list); } } #endif break; case AF_INET6: sin6 = (struct sockaddr_in6 *)sa; #ifdef __KAME__ if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { sin6->sin6_scope_id = ntohs(*(u_int16_t *) &sin6->sin6_addr.s6_addr[2]); sin6->sin6_addr.s6_addr[2] = 0; sin6->sin6_addr.s6_addr[3] = 0; } #endif cp = netname6(sin6, (struct sockaddr_in6 *)rtinfo[RTAX_NETMASK]); if (vflag) n = strlen(cp) < 11 ? 11 : strlen(cp); else n = 11; printf("%-*.*s ", n, n, cp); cp = routename6(sin6); if (vflag) n = strlen(cp) < 17 ? 17 : strlen(cp); else n = 17; printf("%-*.*s ", n, n, cp); #if 0 if (aflag) { u_long multiaddr; struct in6_multi inm; struct sockaddr_in6 m6; multiaddr = (u_long)LIST_FIRST(&ifaddr.in6.ia6_multiaddrs); while (multiaddr != 0) { kread(multiaddr, &inm, sizeof inm); memset(&m6, 0, sizeof(m6)); m6.sin6_len = sizeof(struct sockaddr_in6); m6.sin6_family = AF_INET6; m6.sin6_addr = inm.in6m_addr; #ifdef __KAME__ if (IN6_IS_ADDR_MC_LINKLOCAL(&m6.sin6_addr) || IN6_IS_ADDR_MC_INTFACELOCAL(&m6.sin6_addr)) { m6.sin6_scope_id = ntohs(*(u_int16_t *) &m6.sin6_addr.s6_addr[2]); m6.sin6_addr.s6_addr[2] = 0; m6.sin6_addr.s6_addr[3] = 0; } #endif cp = routename6(&m6); if (vflag) n = strlen(cp) < 17 ? 17 : strlen(cp); else n = 17; printf("\n%25s %-*.*s ", "", n, n, cp); multiaddr = (u_long)LIST_NEXT(&inm, in6m_entry); } } #endif break; case AF_LINK: sdl = (struct sockaddr_dl *)sa; m = printf("%-11.11s ", "<Link>"); if (sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_CARP || sdl->sdl_type == IFT_FDDI || sdl->sdl_type == IFT_ISO88025) printf("%-17.17s ", ether_ntoa((struct ether_addr *)LLADDR(sdl))); else { cp = (char *)LLADDR(sdl); n = sdl->sdl_alen; goto hexprint; } break; default: m = printf("(%d)", sa->sa_family); for (cp = sa->sa_len + (char *)sa; --cp > sa->sa_data && (*cp == 0);) {} n = cp - sa->sa_data + 1; cp = sa->sa_data; hexprint: while (--n >= 0) m += printf("%x%c", *cp++ & 0xff, n > 0 ? '.' : ' '); m = 30 - m; while (m-- > 0) putchar(' '); break; } if (bflag) { if (hflag) { char ibytes[FMT_SCALED_STRSIZE]; char obytes[FMT_SCALED_STRSIZE]; fmt_scaled(ifd->ifi_ibytes, ibytes); fmt_scaled(ifd->ifi_obytes, obytes); printf("%10s %10s", ibytes, obytes); } else printf("%10llu %10llu", ifd->ifi_ibytes, ifd->ifi_obytes); } else printf("%8llu %5llu %8llu %5llu %5llu", ifd->ifi_ipackets, ifd->ifi_ierrors, ifd->ifi_opackets, ifd->ifi_oerrors, ifd->ifi_collisions); if (tflag) printf(" %4d", 0 /* XXX ifnet.if_timer */); if (dflag) printf(" %4d", 0 /* XXX ifnet.if_snd.ifq_drops */); putchar('\n'); }
void bio_inq(char *name) { char *status, *cache; char size[64], scsiname[16], volname[32]; char percent[10], seconds[20]; int i, d, volheader, hotspare, unused; char encname[16], serial[32]; struct bioc_inq bi; struct bioc_vol bv; struct bioc_disk bd; memset(&bi, 0, sizeof(bi)); bi.bi_bio.bio_cookie = bio_cookie; if (ioctl(devh, BIOCINQ, &bi)) { if (errno == ENOTTY) bio_diskinq(name); else err(1, "BIOCINQ"); return; } bio_status(&bi.bi_bio.bio_status); volheader = 0; for (i = 0; i < bi.bi_novol; i++) { memset(&bv, 0, sizeof(bv)); bv.bv_bio.bio_cookie = bio_cookie; bv.bv_volid = i; bv.bv_percent = -1; bv.bv_seconds = 0; if (ioctl(devh, BIOCVOL, &bv)) err(1, "BIOCVOL"); bio_status(&bv.bv_bio.bio_status); if (name && strcmp(name, bv.bv_dev) != 0) continue; if (!volheader) { volheader = 1; printf("%-11s %-10s %14s %-8s\n", "Volume", "Status", "Size", "Device"); } percent[0] = '\0'; seconds[0] = '\0'; if (bv.bv_percent != -1) snprintf(percent, sizeof percent, " %d%% done", bv.bv_percent); if (bv.bv_seconds) snprintf(seconds, sizeof seconds, " %u seconds", bv.bv_seconds); switch (bv.bv_status) { case BIOC_SVONLINE: status = BIOC_SVONLINE_S; break; case BIOC_SVOFFLINE: status = BIOC_SVOFFLINE_S; break; case BIOC_SVDEGRADED: status = BIOC_SVDEGRADED_S; break; case BIOC_SVBUILDING: status = BIOC_SVBUILDING_S; break; case BIOC_SVREBUILD: status = BIOC_SVREBUILD_S; break; case BIOC_SVSCRUB: status = BIOC_SVSCRUB_S; break; case BIOC_SVINVALID: default: status = BIOC_SVINVALID_S; } switch (bv.bv_cache) { case BIOC_CVWRITEBACK: cache = BIOC_CVWRITEBACK_S; break; case BIOC_CVWRITETHROUGH: cache = BIOC_CVWRITETHROUGH_S; break; case BIOC_CVUNKNOWN: default: cache = BIOC_CVUNKNOWN_S; } snprintf(volname, sizeof volname, "%s %u", bi.bi_dev, bv.bv_volid); unused = 0; hotspare = 0; if (bv.bv_level == -1 && bv.bv_nodisk == 1) hotspare = 1; else if (bv.bv_level == -2 && bv.bv_nodisk == 1) unused = 1; else { if (human) fmt_scaled(bv.bv_size, size); else snprintf(size, sizeof size, "%14llu", bv.bv_size); switch (bv.bv_level) { case 'C': printf("%11s %-10s %14s %-7s CRYPTO%s%s\n", volname, status, size, bv.bv_dev, percent, seconds); break; case 'c': printf("%11s %-10s %14s %-7s CONCAT%s%s\n", volname, status, size, bv.bv_dev, percent, seconds); break; default: printf("%11s %-10s %14s %-7s RAID%u%s%s %s\n", volname, status, size, bv.bv_dev, bv.bv_level, percent, seconds, cache); break; } } for (d = 0; d < bv.bv_nodisk; d++) { memset(&bd, 0, sizeof(bd)); bd.bd_bio.bio_cookie = bio_cookie; bd.bd_diskid = d; bd.bd_volid = i; if (ioctl(devh, BIOCDISK, &bd)) err(1, "BIOCDISK"); bio_status(&bd.bd_bio.bio_status); switch (bd.bd_status) { case BIOC_SDONLINE: status = BIOC_SDONLINE_S; break; case BIOC_SDOFFLINE: status = BIOC_SDOFFLINE_S; break; case BIOC_SDFAILED: status = BIOC_SDFAILED_S; break; case BIOC_SDREBUILD: status = BIOC_SDREBUILD_S; break; case BIOC_SDHOTSPARE: status = BIOC_SDHOTSPARE_S; break; case BIOC_SDUNUSED: status = BIOC_SDUNUSED_S; break; case BIOC_SDSCRUB: status = BIOC_SDSCRUB_S; break; case BIOC_SDINVALID: default: status = BIOC_SDINVALID_S; } if (hotspare || unused) ; /* use volname from parent volume */ else snprintf(volname, sizeof volname, " %3u", bd.bd_diskid); if (bv.bv_level == 'C' && bd.bd_size == 0) snprintf(size, sizeof size, "%14s", "key disk"); else if (human) fmt_scaled(bd.bd_size, size); else snprintf(size, sizeof size, "%14llu", bd.bd_size); snprintf(scsiname, sizeof scsiname, "%u:%u.%u", bd.bd_channel, bd.bd_target, bd.bd_lun); if (bd.bd_procdev[0]) strlcpy(encname, bd.bd_procdev, sizeof encname); else strlcpy(encname, "noencl", sizeof encname); if (bd.bd_serial[0]) strlcpy(serial, bd.bd_serial, sizeof serial); else strlcpy(serial, "unknown serial", sizeof serial); printf("%11s %-10s %14s %-7s %-6s <%s>\n", volname, status, size, scsiname, encname, bd.bd_vendor); if (verbose) printf("%11s %-10s %14s %-7s %-6s '%s'\n", "", "", "", "", "", serial); } } }