Module Name:    src
Committed By:   christos
Date:           Fri Sep  6 17:08:22 UTC 2019

Modified Files:
        src/usr.bin/fstat: fstat.1 fstat.c fstat.h misc.c

Log Message:
Add -O to print offsets; align columns properly.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/fstat/fstat.1
cvs rdiff -u -r1.112 -r1.113 src/usr.bin/fstat/fstat.c
cvs rdiff -u -r1.9 -r1.10 src/usr.bin/fstat/fstat.h
cvs rdiff -u -r1.21 -r1.22 src/usr.bin/fstat/misc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/fstat/fstat.1
diff -u src/usr.bin/fstat/fstat.1:1.34 src/usr.bin/fstat/fstat.1:1.35
--- src/usr.bin/fstat/fstat.1:1.34	Mon Jul  3 17:34:57 2017
+++ src/usr.bin/fstat/fstat.1	Fri Sep  6 13:08:22 2019
@@ -1,4 +1,4 @@
-.\"	$NetBSD: fstat.1,v 1.34 2017/07/03 21:34:57 wiz Exp $
+.\"	$NetBSD: fstat.1,v 1.35 2019/09/06 17:08:22 christos Exp $
 .\"
 .\" Copyright (c) 1987, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     from: @(#)fstat.1	8.3 (Berkeley) 2/25/94
 .\"
-.Dd December 15, 2013
+.Dd September 6, 2019
 .Dt FSTAT 1
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd display status of open files
 .Sh SYNOPSIS
 .Nm
-.Op Fl Afnv
+.Op Fl AfnOv
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Op Fl p Ar pid
@@ -54,7 +54,7 @@ If no options are specified,
 reports on all open files in the system.
 .Pp
 Options:
-.Bl -tag -width Ds
+.Bl -tag -width XXXXXXXXXX
 .It Fl A
 Add an output column with the address of the kernel object (vnode or file),
 that can be matched with
@@ -72,12 +72,15 @@ resides, type
 Please see the
 .Sx BUGS
 section for issues with this option.
-.It Fl M
-Extract values associated with the name list from the specified core
+.It Fl M Ar core
+Extract values associated with the name list from the specified
+.Ar core
 instead of the default
 .Pa /dev/kmem .
-.It Fl N
-Extract the name list from the specified system instead of the default
+.It Fl N Ar system
+Extract the name list from the specified
+.Ar system
+instead of the default
 .Pa /netbsd .
 .It Fl n
 Numerical format.
@@ -88,10 +91,14 @@ device number that the special device re
 in
 .Pa /dev ;
 and print the mode of the file in octal instead of symbolic form.
-.It Fl p
-Report all files open by the specified process.
-.It Fl u
-Report all files open by the specified user.
+.It Fl O
+Report file offsets instead of sizes.
+.It Fl p Ar process
+Report all files open by the specified
+.Ar process .
+.It Fl u Ar user
+Report all files open by the specified
+.Ar user .
 .It Fl v
 Verbose mode.
 Print error messages upon failures to locate particular
@@ -161,9 +168,11 @@ using a symbolic format (see
 .Xr strmode 3 ) ;
 otherwise, the mode is printed
 as an octal number.
-.It Li SZ\&|DV
+.It Li SZ\&|DV or OFFS
 If the file is not a character or block special file, prints the size of
-the file in bytes.
+the file in bytes (or the offset if the
+.Fl O
+is specified).
 Otherwise, if the
 .Fl n
 flag is not specified, prints

Index: src/usr.bin/fstat/fstat.c
diff -u src/usr.bin/fstat/fstat.c:1.112 src/usr.bin/fstat/fstat.c:1.113
--- src/usr.bin/fstat/fstat.c:1.112	Sat Feb  2 22:19:29 2019
+++ src/usr.bin/fstat/fstat.c	Fri Sep  6 13:08:22 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: fstat.c,v 1.112 2019/02/03 03:19:29 mrg Exp $	*/
+/*	$NetBSD: fstat.c,v 1.113 2019/09/06 17:08:22 christos Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
 #else
-__RCSID("$NetBSD: fstat.c,v 1.112 2019/02/03 03:19:29 mrg Exp $");
+__RCSID("$NetBSD: fstat.c,v 1.113 2019/09/06 17:08:22 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -134,6 +134,7 @@ static int 	fsflg,	/* show files on same
 static int 	checkfile; /* true if restricting to particular files or filesystems */
 static int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
 static int	Aflg;	/* prefix with address of file structure */
+static int	Oflg;	/* print offset instead of size */
 int	vflg;	/* display errors in locating kernel data objects etc... */
 
 static fdfile_t **ofiles; /* buffer of pointers to file structures */
@@ -171,12 +172,12 @@ static const char *inet6_addrstr(char *,
     uint16_t, bool);
 #endif
 static const char *at_addrstr(char *, size_t, const struct sockaddr_at *);
-static void	socktrans(struct socket *, int);
+static void	socktrans(struct file *, struct socket *, int);
 static void	misctrans(struct file *, int);
 static int	ufs_filestat(struct vnode *, struct filestat *);
 static void	usage(void) __dead;
 static const char   *vfilestat(struct vnode *, struct filestat *);
-static void	vtrans(struct vnode *, int, int, long);
+static void	vtrans(struct file *, struct vnode *, int, int, long);
 static void	ftrans(fdfile_t *, int);
 static void	ptrans(struct file *, struct pipe *, int);
 static void	kdriver_init(void);
@@ -196,7 +197,7 @@ main(int argc, char **argv)
 	arg = 0;
 	what = KERN_PROC_ALL;
 	nlistf = memf = NULL;
-	while ((ch = getopt(argc, argv, "fnAp:u:vN:M:")) != -1)
+	while ((ch = getopt(argc, argv, "fnAOp:u:vN:M:")) != -1)
 		switch((char)ch) {
 		case 'f':
 			fsflg = 1;
@@ -213,6 +214,9 @@ main(int argc, char **argv)
 		case 'A':
 			Aflg = 1;
 			break;
+		case 'O':
+			Oflg = 1;
+			break;
 		case 'p':
 			if (pflg++)
 				usage();
@@ -284,11 +288,14 @@ main(int argc, char **argv)
 	if (Aflg)
 		(void)printf("%-*s ", 2*(int)(sizeof(void*)), "ADDR");
 	if (nflg)
-		(void)printf("%s",
-"USER     CMD          PID   FD  DEV     INUM  MODE  SZ|DV R/W");
+		(void)printf(
+"USER     CMD          PID   FD    DEV       INUM   MODE  %s R/W",
+    Oflg ? " OFFS" : "SZ|DV" );
 	else
-		(void)printf("%s",
-"USER     CMD          PID   FD MOUNT       INUM MODE         SZ|DV R/W");
+		(void)printf(
+"USER     CMD          PID   FD  MOUNT         INUM MODE         %s R/W",
+    Oflg ? " OFFS" : "SZ|DV" );
+
 	if (checkfile && fsflg == 0)
 		(void)printf(" NAME\n");
 	else
@@ -450,11 +457,11 @@ dofiles(struct kinfo_proc2 *p)
 	 * root directory vnode, if one
 	 */
 	if (cwdi.cwdi_rdir)
-		vtrans(cwdi.cwdi_rdir, RDIR, FREAD, (long)cwdi.cwdi_rdir);
+		vtrans(NULL, cwdi.cwdi_rdir, RDIR, FREAD, (long)cwdi.cwdi_rdir);
 	/*
 	 * current working directory vnode
 	 */
-	vtrans(cwdi.cwdi_cdir, CDIR, FREAD, (long)cwdi.cwdi_cdir);
+	vtrans(NULL, cwdi.cwdi_cdir, CDIR, FREAD, (long)cwdi.cwdi_cdir);
 #if 0
 	/*
 	 * Disable for now, since p->p_tracep appears to point to a ktr_desc *
@@ -507,10 +514,10 @@ ftrans(fdfile_t *fp, int i)
 			2*(int)(sizeof(void*)), (long)fdfile.ff_file);
 	switch (file.f_type) {
 	case DTYPE_VNODE:
-		vtrans(file.f_data, i, file.f_flag, (long)fdfile.ff_file);
+		vtrans(&file, file.f_data, i, file.f_flag, (long)fdfile.ff_file);
 		break;
 	case DTYPE_SOCKET:
-		socktrans(file.f_data, i);
+		socktrans(&file, file.f_data, i);
 		break;
 	case DTYPE_PIPE:
 		if (checkfile == 0)
@@ -634,7 +641,37 @@ checkfs(struct vnode *vp, struct vnode *
 }
 
 static void
-vtrans(struct vnode *vp, int i, int flag, long addr)
+vprint(struct vnode *vn, struct filestat *fst)
+{
+	switch (vn->v_type) {
+	case VBLK:
+	case VCHR: {
+		const char *name;
+
+		if (nflg || ((name = devname(fst->rdev, vn->v_type == VCHR ? 
+		    S_IFCHR : S_IFBLK)) == NULL))
+			(void)printf("  %s,%-2llu",
+			    kdriver_search(vn->v_type, major(fst->rdev)),
+			    (unsigned long long)minor(fst->rdev));
+		else
+			(void)printf(" %6s", name);
+		break;
+	}
+	default:
+		(void)printf(" %6lld", (long long)fst->size);
+	}
+}
+
+void
+oprint(struct file *fp, const char *str)
+{
+	if (Oflg)
+		(void)printf(" %6lld", (long long)(fp ? fp->f_offset : 0));
+	fputs(str, stdout);
+}
+
+static void
+vtrans(struct file *fp, struct vnode *vp, int i, int flag, long addr)
 {
 	struct vnode vn;
 	char mode[15], rw[3];
@@ -658,32 +695,20 @@ vtrans(struct vnode *vp, int i, int flag
 		return;
 	}
 	if (nflg)
-		(void)printf("  %2llu,%-2llu",
+		(void)printf("  %3llu,%-2llu",
 		    (unsigned long long)major(fst.fsid),
 		    (unsigned long long)minor(fst.fsid));
 	else
 		(void)printf("  %-8s", getmnton(vn.v_mount));
 	if (nflg)
-		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
+		(void)snprintf(mode, sizeof mode, "%6o", fst.mode);
 	else
 		strmode(fst.mode, mode);
-	(void)printf("  %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
-	switch (vn.v_type) {
-	case VBLK:
-	case VCHR: {
-		char *name;
-
-		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 
-		    S_IFCHR : S_IFBLK)) == NULL))
-			(void)printf("  %s,%-2llu",
-			    kdriver_search(vn.v_type, major(fst.rdev)),
-			    (unsigned long long)minor(fst.rdev));
-		else
-			(void)printf(" %6s", name);
-		break;
-	}
-	default:
-		(void)printf(" %6lld", (long long)fst.size);
+	(void)printf("  %8"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
+	if (Oflg) {
+		oprint(fp, "");
+	} else {
+		vprint(&vn, &fst);
 	}
 	rw[0] = '\0';
 	if (flag & FREAD)
@@ -996,7 +1021,7 @@ at_addrstr(char *buf, size_t len, const 
 }
 
 static void
-socktrans(struct socket *sock, int i)
+socktrans(struct file *f, struct socket *sock, int i)
 {
 	static const char *stypename[] = {
 		"unused",	/* 0 */
@@ -1209,7 +1234,7 @@ again:
 		    lbuf);
 	else if (so.so_pcb)
 		printf(" %jx", (uintmax_t)(uintptr_t)so.so_pcb);
-	(void)printf("\n");
+	oprint(f, "\n");
 	return;
 bad:
 	(void)printf("* error\n");
@@ -1235,7 +1260,7 @@ ptrans(struct file *fp, struct pipe *cpi
 		(fp->f_flag & FWRITE) ? "w" : "r",
 		(fp->f_flag & FNONBLOCK) ? "n" : "",
 		(cp.pipe_state & PIPE_ASYNC) ? "a" : "");
-	(void)printf("\n");
+	oprint(fp, "\n");
 	return;
 bad:
 	(void)printf("* error\n");

Index: src/usr.bin/fstat/fstat.h
diff -u src/usr.bin/fstat/fstat.h:1.9 src/usr.bin/fstat/fstat.h:1.10
--- src/usr.bin/fstat/fstat.h:1.9	Sun Apr 12 02:36:12 2009
+++ src/usr.bin/fstat/fstat.h	Fri Sep  6 13:08:22 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: fstat.h,v 1.9 2009/04/12 06:36:12 lukem Exp $	*/
+/*	$NetBSD: fstat.h,v 1.10 2019/09/06 17:08:22 christos Exp $	*/
 /*-
  * Copyright (c) 1988, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -60,3 +60,4 @@ int	isofs_filestat(struct vnode *, struc
 int	ntfs_filestat(struct vnode *, struct filestat *);
 int	ptyfs_filestat(struct vnode *, struct filestat *);
 int	tmpfs_filestat(struct vnode *, struct filestat *);
+void	oprint(struct file *, const char *);

Index: src/usr.bin/fstat/misc.c
diff -u src/usr.bin/fstat/misc.c:1.21 src/usr.bin/fstat/misc.c:1.22
--- src/usr.bin/fstat/misc.c:1.21	Sat Feb  2 22:20:24 2019
+++ src/usr.bin/fstat/misc.c	Fri Sep  6 13:08:22 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: misc.c,v 1.21 2019/02/03 03:20:24 thorpej Exp $	*/
+/*	$NetBSD: misc.c,v 1.22 2019/09/06 17:08:22 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: misc.c,v 1.21 2019/02/03 03:20:24 thorpej Exp $");
+__RCSID("$NetBSD: misc.c,v 1.22 2019/09/06 17:08:22 christos Exp $");
 
 #include <stdbool.h>
 #include <sys/param.h>
@@ -159,7 +159,7 @@ p_bpf(struct file *f)
 		(void)printf(", waiting");
 	else if (bpf.bd_state == BPF_TIMED_OUT)
 		(void)printf(", timeout");
-	(void)printf("\n");
+	oprint(f, "\n");
 	return 0;
 }
 
@@ -183,11 +183,12 @@ p_sem(struct file *f)
 			    ks.ks_name, Pid);
 		} else {
 			buf[ks.ks_namelen] = '\0';
-			(void)printf(", name=%s\n", buf);
+			(void)printf(", name=%s", buf);
+			oprint(f, "\n");
 			return 0;
 		}
 	}
-	(void)printf("\n");
+	oprint(f, "\n");
 	return 0;
 }
 
@@ -200,7 +201,8 @@ p_mqueue(struct file *f)
 		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
 		return 0;
 	}
-	(void)printf("* mqueue \"%s\"\n", mq.mq_name);
+	(void)printf("* mqueue \"%s\"", mq.mq_name);
+	oprint(f, "\n");
 	return 0;
 }
 
@@ -228,7 +230,8 @@ p_rnd(struct file *f)
 		return 0;
 	}
 	snprintb(buf, sizeof(buf), CPRNG_FMT, str.cs_flags);
-	(void)printf("* rnd \"%s\" flags %s\n", str.cs_name, buf);
+	(void)printf("* rnd \"%s\" flags %s", str.cs_name, buf);
+	oprint(f, "\n");
 	return 0;
 }
 
@@ -241,7 +244,8 @@ p_kqueue(struct file *f)
 		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
 		return 0;
 	}
-	(void)printf("* kqueue pending %d\n", kq.kq_count);
+	(void)printf("* kqueue pending %d", kq.kq_count);
+	oprint(f, "\n");
 	return 0;
 }
 
@@ -281,22 +285,24 @@ pmisc(struct file *f, const char *name)
 	case NL_SEM:
 		return p_sem(f);
 	case NL_TAP:
-		printf("* tap %lu\n", (unsigned long)(intptr_t)f->f_data);
-		return 0;
+		printf("* tap %lu", (unsigned long)(intptr_t)f->f_data);
+		break;
 	case NL_CRYPTO:
-		printf("* crypto %p\n", f->f_data);
-		return 0;
+		printf("* crypto %p", f->f_data);
+		break;
 	case NL_AUDIO:
-		printf("* audio %p\n", f->f_data);
-		return 0;
+		printf("* audio %p", f->f_data);
+		break;
 	case NL_PAD:
-		printf("* pad %p\n", f->f_data);
-		return 0;
+		printf("* pad %p", f->f_data);
+		break;
 	case NL_MAX:
-		printf("* %s ops=%p %p\n", name, f->f_ops, f->f_data);
-		return 0;
+		printf("* %s ops=%p %p", name, f->f_ops, f->f_data);
+		break;
 	default:
-		printf("* %s %p\n", nl[i].n_name, f->f_data);
-		return 0;
+		printf("* %s %p", nl[i].n_name, f->f_data);
+		break;
 	}
+	oprint(f, "\n");
+	return 0;
 }

Reply via email to