From 9f365aa1d6aab0aca269b54fb5d216ac0e31a06f Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Wed, 28 Sep 2011 18:53:36 +0000 Subject: [PATCH] Get rid of major/minor number distinction. As of FreeBSD 6, devices can only be opened through devfs. These device nodes don't have major and minor numbers anymore. The st_rdev field in struct stat is simply based a copy of st_ino. Simply display device numbers as hexadecimal, using "%#jx". This is allowed by POSIX, since it explicitly states things like the following (example taken from ls(1)): "If the file is a character special or block special file, the size of the file may be replaced with implementation-defined information associated with the device in question." This makes the output of these commands more compact. For example, ls(1) now uses approximately four columns less. While there, simplify the column length calculation from ls(1) by calling snprintf() with a NULL buffer. Don't be afraid; if needed one can still obtain individual major/minor numbers using stat(1). --- bin/ls/ls.1 | 5 ++--- bin/ls/ls.c | 41 ++++++++++++++++------------------------- bin/ls/ls.h | 2 -- bin/ls/print.c | 11 ++--------- bin/ps/print.c | 8 ++------ lib/libc/gen/devname.c | 5 +++-- sbin/fsdb/fsdbutil.c | 6 ++---- usr.bin/find/find.1 | 4 ++-- usr.bin/find/ls.c | 3 +-- usr.bin/fstat/fstat.1 | 4 ++-- usr.bin/fstat/fstat.c | 6 +++--- usr.sbin/pstat/pstat.c | 2 +- 12 files changed, 36 insertions(+), 61 deletions(-) diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 3b23df45b88e..cc5ff482187b 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -32,7 +32,7 @@ .\" @(#)ls.1 8.7 (Berkeley) 7/29/94 .\" $FreeBSD$ .\" -.Dd April 4, 2008 +.Dd September 28, 2011 .Dt LS 1 .Os .Sh NAME @@ -357,8 +357,7 @@ option is given, the numeric ID's are displayed. .Pp If the file is a character special or block special file, -the major and minor device numbers for the file are displayed -in the size field. +the device number for the file is displayed in the size field. If the file is a symbolic link the pathname of the linked-to file is preceded by .Dq Li -> . diff --git a/bin/ls/ls.c b/bin/ls/ls.c index e482e2252f04..569f3d97f0af 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -563,7 +563,7 @@ display(const FTSENT *p, FTSENT *list, int options) long maxblock; u_long btotal, labelstrlen, maxinode, maxlen, maxnlink; u_long maxlabelstr; - u_int devstrlen; + u_int sizelen; int maxflags; gid_t maxgroup; uid_t maxuser; @@ -572,7 +572,6 @@ display(const FTSENT *p, FTSENT *list, int options) int entries, needstats; const char *user, *group; char *flags, *labelstr = NULL; - char buf[STRBUF_SIZEOF(u_quad_t) + 1]; char ngroup[STRBUF_SIZEOF(uid_t) + 1]; char nuser[STRBUF_SIZEOF(gid_t) + 1]; @@ -656,7 +655,8 @@ display(const FTSENT *p, FTSENT *list, int options) MAKENINES(maxsize); free(jinitmax); } - devstrlen = 0; + d.s_size = 0; + sizelen = 0; flags = NULL; for (cur = list, entries = 0; cur; cur = cur->fts_link) { if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { @@ -796,14 +796,12 @@ display(const FTSENT *p, FTSENT *list, int options) np->group = &np->data[ulen + 1]; (void)strcpy(np->group, group); - if ((S_ISCHR(sp->st_mode) || - S_ISBLK(sp->st_mode)) && - devstrlen < DEVSTR_HEX_LEN) { - if (minor(sp->st_rdev) > 255 || - minor(sp->st_rdev) < 0) - devstrlen = DEVSTR_HEX_LEN; - else - devstrlen = DEVSTR_LEN; + if (S_ISCHR(sp->st_mode) || + S_ISBLK(sp->st_mode)) { + sizelen = snprintf(NULL, 0, + "%#jx", (uintmax_t)sp->st_rdev); + if (d.s_size < sizelen) + d.s_size = sizelen; } if (f_flags) { @@ -837,23 +835,16 @@ display(const FTSENT *p, FTSENT *list, int options) d.maxlen = maxlen; if (needstats) { d.btotal = btotal; - (void)snprintf(buf, sizeof(buf), "%lu", maxblock); - d.s_block = strlen(buf); + d.s_block = snprintf(NULL, 0, "%lu", maxblock); d.s_flags = maxflags; d.s_label = maxlabelstr; d.s_group = maxgroup; - (void)snprintf(buf, sizeof(buf), "%lu", maxinode); - d.s_inode = strlen(buf); - (void)snprintf(buf, sizeof(buf), "%lu", maxnlink); - d.s_nlink = strlen(buf); - if (f_humanval) - d.s_size = HUMANVALSTR_LEN; - else { - (void)snprintf(buf, sizeof(buf), "%ju", maxsize); - d.s_size = strlen(buf); - } - if (d.s_size < devstrlen) - d.s_size = devstrlen; + d.s_inode = snprintf(NULL, 0, "%lu", maxinode); + d.s_nlink = snprintf(NULL, 0, "%lu", maxnlink); + sizelen = f_humanval ? HUMANVALSTR_LEN : + snprintf(NULL, 0, "%ju", maxsize); + if (d.s_size < sizelen) + d.s_size = sizelen; d.s_user = maxuser; } printfcn(&d); diff --git a/bin/ls/ls.h b/bin/ls/ls.h index a74abf0c9e5b..ee2a7a5004bb 100644 --- a/bin/ls/ls.h +++ b/bin/ls/ls.h @@ -36,8 +36,6 @@ #define NO_PRINT 1 #define HUMANVALSTR_LEN 5 -#define DEVSTR_LEN 8 -#define DEVSTR_HEX_LEN 15 extern long blocksize; /* block size units */ diff --git a/bin/ls/print.c b/bin/ls/print.c index 3f2033c404db..a788042144bb 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -351,16 +352,8 @@ printaname(const FTSENT *p, u_long inodefield, u_long sizefield) static void printdev(size_t width, dev_t dev) { - char buf[DEVSTR_HEX_LEN + 1]; - if (minor(dev) > 255 || minor(dev) < 0) - (void)snprintf(buf, sizeof(buf), "%3d, 0x%08x", - major(dev), (u_int)minor(dev)); - else - (void)snprintf(buf, sizeof(buf), "%3d, %3d", - major(dev), minor(dev)); - - (void)printf("%*s ", (u_int)width, buf); + (void)printf("%#*jx ", (u_int)width, (uintmax_t)dev); } static void diff --git a/bin/ps/print.c b/bin/ps/print.c index 7d1d1902b9b3..beb6f5205735 100644 --- a/bin/ps/print.c +++ b/bin/ps/print.c @@ -392,17 +392,13 @@ tdev(KINFO *k, VARENT *ve) { VAR *v; dev_t dev; - char buff[16]; v = ve->var; dev = k->ki_p->ki_tdev; if (dev == NODEV) (void)printf("%*s", v->width, "??"); - else { - (void)snprintf(buff, sizeof(buff), - "%d/%d", major(dev), minor(dev)); - (void)printf("%*s", v->width, buff); - } + else + (void)printf("%#*jx", v->width, (uintmax_t)dev); } void diff --git a/lib/libc/gen/devname.c b/lib/libc/gen/devname.c index 65a690fcdcc3..da0b923573ad 100644 --- a/lib/libc/gen/devname.c +++ b/lib/libc/gen/devname.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -60,8 +61,8 @@ devname_r(dev_t dev, mode_t type, char *buf, int len) } /* Finally just format it */ - snprintf(buf, len, "#%c:%d:0x%x", - S_ISCHR(type) ? 'C' : 'B', major(dev), minor(dev)); + snprintf(buf, len, "#%c:%#jx", + S_ISCHR(type) ? 'C' : 'B', (uintmax_t)dev); return (buf); } diff --git a/sbin/fsdb/fsdbutil.c b/sbin/fsdb/fsdbutil.c index 2c5710a80aa8..7ed78a7f39ee 100644 --- a/sbin/fsdb/fsdbutil.c +++ b/sbin/fsdb/fsdbutil.c @@ -126,12 +126,10 @@ printstat(const char *cp, ino_t inum, union dinode *dp) puts("regular file"); break; case IFBLK: - printf("block special (%d,%d)", - major(DIP(dp, di_rdev)), minor(DIP(dp, di_rdev))); + printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev)); break; case IFCHR: - printf("character special (%d,%d)", - major(DIP(dp, di_rdev)), minor(DIP(dp, di_rdev))); + printf("character special (%#jx)", DIP(dp, di_rdev)); break; case IFLNK: fputs("symlink",stdout); diff --git a/usr.bin/find/find.1 b/usr.bin/find/find.1 index 04fe50cccc70..10479f60fddb 100644 --- a/usr.bin/find/find.1 +++ b/usr.bin/find/find.1 @@ -31,7 +31,7 @@ .\" @(#)find.1 8.7 (Berkeley) 5/9/95 .\" $FreeBSD$ .\" -.Dd March 17, 2010 +.Dd September 28, 2011 .Dt FIND 1 .Os .Sh NAME @@ -507,7 +507,7 @@ This primary always evaluates to true. The following information for the current file is written to standard output: its inode number, size in 512-byte blocks, file permissions, number of hard links, owner, group, size in bytes, last modification time, and pathname. -If the file is a block or character special file, the major and minor numbers +If the file is a block or character special file, the device number will be displayed instead of the size in bytes. If the file is a symbolic link, the pathname of the linked-to file will be displayed preceded by diff --git a/usr.bin/find/ls.c b/usr.bin/find/ls.c index 8e1b8d3b9e65..44d1852430e1 100644 --- a/usr.bin/find/ls.c +++ b/usr.bin/find/ls.c @@ -70,8 +70,7 @@ printlong(char *name, char *accpath, struct stat *sb) group_from_gid(sb->st_gid, 0)); if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) - (void)printf("%3d, %3d ", major(sb->st_rdev), - minor(sb->st_rdev)); + (void)printf("%#8jx ", (uintmax_t)sb->st_rdev); else (void)printf("%8"PRId64" ", sb->st_size); printtime(sb->st_mtime); diff --git a/usr.bin/fstat/fstat.1 b/usr.bin/fstat/fstat.1 index e1f1c1bed168..7403a8e725ee 100644 --- a/usr.bin/fstat/fstat.1 +++ b/usr.bin/fstat/fstat.1 @@ -28,7 +28,7 @@ .\" @(#)fstat.1 8.3 (Berkeley) 2/25/94 .\" $FreeBSD$ .\" -.Dd July 9, 2009 +.Dd September 28, 2011 .Dt FSTAT 1 .Os .Sh NAME @@ -142,7 +142,7 @@ pathname that the file system the file resides in is mounted on. If the .Fl n flag is specified, this header is present and is the -major/minor number of the device that this file resides in. +number of the device that this file resides in. .It Li INUM The inode number of the file. .It Li MODE diff --git a/usr.bin/fstat/fstat.c b/usr.bin/fstat/fstat.c index 531eef23c445..c513a4627e91 100644 --- a/usr.bin/fstat/fstat.c +++ b/usr.bin/fstat/fstat.c @@ -411,7 +411,7 @@ print_pts_info(struct procstat *procstat, struct filestat *fst) } printf("* pseudo-terminal master "); if (nflg || !*pts.devname) { - printf("%10d,%-2d", major(pts.dev), minor(pts.dev)); + printf("%#10jx", (uintmax_t)pts.dev); } else { printf("%10s", pts.devname); } @@ -441,7 +441,7 @@ print_vnode_info(struct procstat *procstat, struct filestat *fst) } if (nflg) - printf(" %2d,%-2d", major(vn.vn_fsid), minor(vn.vn_fsid)); + printf(" %#8jx", (uintmax_t)vn.vn_fsid); else if (vn.vn_mntdir != NULL) (void)printf(" %-8s", vn.vn_mntdir); @@ -457,7 +457,7 @@ print_vnode_info(struct procstat *procstat, struct filestat *fst) if (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) { if (nflg || !*vn.vn_devname) - printf(" %2d,%-2d", major(vn.vn_dev), minor(vn.vn_dev)); + printf(" %#6jx", (uintmax_t)vn.vn_dev); else { printf(" %6s", vn.vn_devname); } diff --git a/usr.sbin/pstat/pstat.c b/usr.sbin/pstat/pstat.c index 54351667b27f..201784157329 100644 --- a/usr.sbin/pstat/pstat.c +++ b/usr.sbin/pstat/pstat.c @@ -345,7 +345,7 @@ ttyprt(struct xtty *xt) errx(1, "struct xtty size mismatch"); if (usenumflag || xt->xt_dev == 0 || (name = devname(xt->xt_dev, S_IFCHR)) == NULL) - printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev)); + printf("%#10jx ", (uintmax_t)xt->xt_dev); else printf("%10s ", name); printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ",