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).
This commit is contained in:
Ed Schouten 2011-09-28 18:53:36 +00:00
parent 7d6060bd9c
commit 9f365aa1d6
12 changed files with 36 additions and 61 deletions

View File

@ -32,7 +32,7 @@
.\" @(#)ls.1 8.7 (Berkeley) 7/29/94 .\" @(#)ls.1 8.7 (Berkeley) 7/29/94
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd April 4, 2008 .Dd September 28, 2011
.Dt LS 1 .Dt LS 1
.Os .Os
.Sh NAME .Sh NAME
@ -357,8 +357,7 @@ option is given,
the numeric ID's are displayed. the numeric ID's are displayed.
.Pp .Pp
If the file is a character special or block special file, If the file is a character special or block special file,
the major and minor device numbers for the file are displayed the device number for the file is displayed in the size field.
in the size field.
If the file is a symbolic link the pathname of the If the file is a symbolic link the pathname of the
linked-to file is preceded by linked-to file is preceded by
.Dq Li -> . .Dq Li -> .

View File

@ -563,7 +563,7 @@ display(const FTSENT *p, FTSENT *list, int options)
long maxblock; long maxblock;
u_long btotal, labelstrlen, maxinode, maxlen, maxnlink; u_long btotal, labelstrlen, maxinode, maxlen, maxnlink;
u_long maxlabelstr; u_long maxlabelstr;
u_int devstrlen; u_int sizelen;
int maxflags; int maxflags;
gid_t maxgroup; gid_t maxgroup;
uid_t maxuser; uid_t maxuser;
@ -572,7 +572,6 @@ display(const FTSENT *p, FTSENT *list, int options)
int entries, needstats; int entries, needstats;
const char *user, *group; const char *user, *group;
char *flags, *labelstr = NULL; char *flags, *labelstr = NULL;
char buf[STRBUF_SIZEOF(u_quad_t) + 1];
char ngroup[STRBUF_SIZEOF(uid_t) + 1]; char ngroup[STRBUF_SIZEOF(uid_t) + 1];
char nuser[STRBUF_SIZEOF(gid_t) + 1]; char nuser[STRBUF_SIZEOF(gid_t) + 1];
@ -656,7 +655,8 @@ display(const FTSENT *p, FTSENT *list, int options)
MAKENINES(maxsize); MAKENINES(maxsize);
free(jinitmax); free(jinitmax);
} }
devstrlen = 0; d.s_size = 0;
sizelen = 0;
flags = NULL; flags = NULL;
for (cur = list, entries = 0; cur; cur = cur->fts_link) { for (cur = list, entries = 0; cur; cur = cur->fts_link) {
if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 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]; np->group = &np->data[ulen + 1];
(void)strcpy(np->group, group); (void)strcpy(np->group, group);
if ((S_ISCHR(sp->st_mode) || if (S_ISCHR(sp->st_mode) ||
S_ISBLK(sp->st_mode)) && S_ISBLK(sp->st_mode)) {
devstrlen < DEVSTR_HEX_LEN) { sizelen = snprintf(NULL, 0,
if (minor(sp->st_rdev) > 255 || "%#jx", (uintmax_t)sp->st_rdev);
minor(sp->st_rdev) < 0) if (d.s_size < sizelen)
devstrlen = DEVSTR_HEX_LEN; d.s_size = sizelen;
else
devstrlen = DEVSTR_LEN;
} }
if (f_flags) { if (f_flags) {
@ -837,23 +835,16 @@ display(const FTSENT *p, FTSENT *list, int options)
d.maxlen = maxlen; d.maxlen = maxlen;
if (needstats) { if (needstats) {
d.btotal = btotal; d.btotal = btotal;
(void)snprintf(buf, sizeof(buf), "%lu", maxblock); d.s_block = snprintf(NULL, 0, "%lu", maxblock);
d.s_block = strlen(buf);
d.s_flags = maxflags; d.s_flags = maxflags;
d.s_label = maxlabelstr; d.s_label = maxlabelstr;
d.s_group = maxgroup; d.s_group = maxgroup;
(void)snprintf(buf, sizeof(buf), "%lu", maxinode); d.s_inode = snprintf(NULL, 0, "%lu", maxinode);
d.s_inode = strlen(buf); d.s_nlink = snprintf(NULL, 0, "%lu", maxnlink);
(void)snprintf(buf, sizeof(buf), "%lu", maxnlink); sizelen = f_humanval ? HUMANVALSTR_LEN :
d.s_nlink = strlen(buf); snprintf(NULL, 0, "%ju", maxsize);
if (f_humanval) if (d.s_size < sizelen)
d.s_size = HUMANVALSTR_LEN; d.s_size = sizelen;
else {
(void)snprintf(buf, sizeof(buf), "%ju", maxsize);
d.s_size = strlen(buf);
}
if (d.s_size < devstrlen)
d.s_size = devstrlen;
d.s_user = maxuser; d.s_user = maxuser;
} }
printfcn(&d); printfcn(&d);

View File

@ -36,8 +36,6 @@
#define NO_PRINT 1 #define NO_PRINT 1
#define HUMANVALSTR_LEN 5 #define HUMANVALSTR_LEN 5
#define DEVSTR_LEN 8
#define DEVSTR_HEX_LEN 15
extern long blocksize; /* block size units */ extern long blocksize; /* block size units */

View File

@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$");
#include <langinfo.h> #include <langinfo.h>
#include <libutil.h> #include <libutil.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@ -351,16 +352,8 @@ printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
static void static void
printdev(size_t width, dev_t dev) printdev(size_t width, dev_t dev)
{ {
char buf[DEVSTR_HEX_LEN + 1];
if (minor(dev) > 255 || minor(dev) < 0) (void)printf("%#*jx ", (u_int)width, (uintmax_t)dev);
(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);
} }
static void static void

View File

@ -392,17 +392,13 @@ tdev(KINFO *k, VARENT *ve)
{ {
VAR *v; VAR *v;
dev_t dev; dev_t dev;
char buff[16];
v = ve->var; v = ve->var;
dev = k->ki_p->ki_tdev; dev = k->ki_p->ki_tdev;
if (dev == NODEV) if (dev == NODEV)
(void)printf("%*s", v->width, "??"); (void)printf("%*s", v->width, "??");
else { else
(void)snprintf(buff, sizeof(buff), (void)printf("%#*jx", v->width, (uintmax_t)dev);
"%d/%d", major(dev), minor(dev));
(void)printf("%*s", v->width, buff);
}
} }
void void

View File

@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -60,8 +61,8 @@ devname_r(dev_t dev, mode_t type, char *buf, int len)
} }
/* Finally just format it */ /* Finally just format it */
snprintf(buf, len, "#%c:%d:0x%x", snprintf(buf, len, "#%c:%#jx",
S_ISCHR(type) ? 'C' : 'B', major(dev), minor(dev)); S_ISCHR(type) ? 'C' : 'B', (uintmax_t)dev);
return (buf); return (buf);
} }

View File

@ -126,12 +126,10 @@ printstat(const char *cp, ino_t inum, union dinode *dp)
puts("regular file"); puts("regular file");
break; break;
case IFBLK: case IFBLK:
printf("block special (%d,%d)", printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev));
major(DIP(dp, di_rdev)), minor(DIP(dp, di_rdev)));
break; break;
case IFCHR: case IFCHR:
printf("character special (%d,%d)", printf("character special (%#jx)", DIP(dp, di_rdev));
major(DIP(dp, di_rdev)), minor(DIP(dp, di_rdev)));
break; break;
case IFLNK: case IFLNK:
fputs("symlink",stdout); fputs("symlink",stdout);

View File

@ -31,7 +31,7 @@
.\" @(#)find.1 8.7 (Berkeley) 5/9/95 .\" @(#)find.1 8.7 (Berkeley) 5/9/95
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd March 17, 2010 .Dd September 28, 2011
.Dt FIND 1 .Dt FIND 1
.Os .Os
.Sh NAME .Sh NAME
@ -507,7 +507,7 @@ This primary always evaluates to true.
The following information for the current file is written to standard output: 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 its inode number, size in 512-byte blocks, file permissions, number of hard
links, owner, group, size in bytes, last modification time, and pathname. 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. 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 If the file is a symbolic link, the pathname of the linked-to file will be
displayed preceded by displayed preceded by

View File

@ -70,8 +70,7 @@ printlong(char *name, char *accpath, struct stat *sb)
group_from_gid(sb->st_gid, 0)); group_from_gid(sb->st_gid, 0));
if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
(void)printf("%3d, %3d ", major(sb->st_rdev), (void)printf("%#8jx ", (uintmax_t)sb->st_rdev);
minor(sb->st_rdev));
else else
(void)printf("%8"PRId64" ", sb->st_size); (void)printf("%8"PRId64" ", sb->st_size);
printtime(sb->st_mtime); printtime(sb->st_mtime);

View File

@ -28,7 +28,7 @@
.\" @(#)fstat.1 8.3 (Berkeley) 2/25/94 .\" @(#)fstat.1 8.3 (Berkeley) 2/25/94
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd July 9, 2009 .Dd September 28, 2011
.Dt FSTAT 1 .Dt FSTAT 1
.Os .Os
.Sh NAME .Sh NAME
@ -142,7 +142,7 @@ pathname that the file system the file resides in is mounted on.
If the If the
.Fl n .Fl n
flag is specified, this header is present and is the 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 .It Li INUM
The inode number of the file. The inode number of the file.
.It Li MODE .It Li MODE

View File

@ -411,7 +411,7 @@ print_pts_info(struct procstat *procstat, struct filestat *fst)
} }
printf("* pseudo-terminal master "); printf("* pseudo-terminal master ");
if (nflg || !*pts.devname) { if (nflg || !*pts.devname) {
printf("%10d,%-2d", major(pts.dev), minor(pts.dev)); printf("%#10jx", (uintmax_t)pts.dev);
} else { } else {
printf("%10s", pts.devname); printf("%10s", pts.devname);
} }
@ -441,7 +441,7 @@ print_vnode_info(struct procstat *procstat, struct filestat *fst)
} }
if (nflg) 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) else if (vn.vn_mntdir != NULL)
(void)printf(" %-8s", vn.vn_mntdir); (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 (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) {
if (nflg || !*vn.vn_devname) if (nflg || !*vn.vn_devname)
printf(" %2d,%-2d", major(vn.vn_dev), minor(vn.vn_dev)); printf(" %#6jx", (uintmax_t)vn.vn_dev);
else { else {
printf(" %6s", vn.vn_devname); printf(" %6s", vn.vn_devname);
} }

View File

@ -345,7 +345,7 @@ ttyprt(struct xtty *xt)
errx(1, "struct xtty size mismatch"); errx(1, "struct xtty size mismatch");
if (usenumflag || xt->xt_dev == 0 || if (usenumflag || xt->xt_dev == 0 ||
(name = devname(xt->xt_dev, S_IFCHR)) == NULL) (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 else
printf("%10s ", name); printf("%10s ", name);
printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ",