Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 19 May 2002 00:01:42 -0700 (PDT)
From:      Paul Herman <pherman@frenchfries.net>
To:        Dima Dorfman <dima@trit.org>
Cc:        Giorgos Keramidas <keramida@FreeBSD.ORG>, <current@FreeBSD.ORG>
Subject:   new fstat(1) feature (was Re: mergemaster(8) broken -- uses Perl)
Message-ID:  <20020518235204.O2117-100000@mammoth.eat.frenchfries.net>
In-Reply-To: <20020519062613.04B153E1A@turbine.trit.org>

next in thread | previous in thread | raw e-mail | index | archive | help

OK, here's a patch to fstat(1) which adds an "-s" option to stat(2)
a list of files on the command line.  It's against -STABLE but
should still apply to -CURRENT.  Comments are appreciated.

The only other addition I would like to have is have "-n" option
display everything numericaly as it does now, whereas no "-n"
would display everything in human readable format (users, groups,
modes, dates, etc.)

If someone else would like to do that, please patch away! :-)
Otherwise, I'll see if I can't get around to it some other time.

-Paul.

Index: fstat.1
===================================================================
RCS file: /u02/ncvs/src/usr.bin/fstat/fstat.1,v
retrieving revision 1.9.2.6
diff -u -r1.9.2.6 fstat.1
--- fstat.1	16 Apr 2002 19:53:35 -0000	1.9.2.6
+++ fstat.1	19 May 2002 06:35:41 -0000
@@ -87,6 +87,10 @@
 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 s
+Print
+.Xr stat 2
+contents of files given on the command line.
 .It Fl u
 Report all files open by the specified user.
 .It Fl v
@@ -181,6 +185,40 @@
 .Xr ln 1 ) ,
 the name printed may not be the actual
 name that the process originally used to open that file.
+.El
+.Pp
+unless the
+.Fl s
+option is given in which case the following is printed:
+.Bl -tag -width BLOCKS
+.It Li INODE
+The inode number of the file.
+.It Li DEV
+The device number the file resides on.
+.It Li SIZE
+The size in bytes of the file.
+.It Li BLOCKS
+The number of blocks used by the file.
+.It Li MODE
+The file's protection mode.
+.It Li FLAGS
+The file's
+.Xr chflags 2
+flags.
+.It Li LNK
+The number of hard links.
+.It Li UID
+The user ID of the file's owner.
+.It Li GID
+The group ID of the file's group.
+.It Li ATIME
+The time of last access.
+.It Li MTIME
+The time of last data modification.
+.It Li CTIME
+The time of last file status change.
+.It Li NAME
+The file name.
 .El
 .Sh SOCKETS
 The formating of open sockets depends on the protocol domain.
Index: fstat.c
===================================================================
RCS file: /u02/ncvs/src/usr.bin/fstat/fstat.c,v
retrieving revision 1.21.2.7
diff -u -r1.21.2.7 fstat.c
--- fstat.c	21 Nov 2001 10:49:37 -0000	1.21.2.7
+++ fstat.c	19 May 2002 06:10:58 -0000
@@ -121,6 +121,7 @@
 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
 int	vflg;	/* display errors in locating kernel data objects etc... */
 int	mflg;	/* include memory-mapped files */
+int	sflg;	/* display inode information */


 struct file **ofiles;	/* buffer of pointers to file structures */
@@ -137,6 +138,7 @@

 kvm_t *kd;

+void dostats __P((void));
 void dofiles __P((struct kinfo_proc *kp));
 void dommap __P((struct kinfo_proc *kp));
 void vtrans __P((struct vnode *vp, int i, int flag));
@@ -165,7 +167,7 @@
 	arg = 0;
 	what = KERN_PROC_ALL;
 	nlistf = memf = NULL;
-	while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
+	while ((ch = getopt(argc, argv, "fmnp:su:vN:M:")) != -1)
 		switch((char)ch) {
 		case 'f':
 			fsflg = 1;
@@ -192,6 +194,9 @@
 			what = KERN_PROC_PID;
 			arg = atoi(optarg);
 			break;
+		case 's':
+			sflg = 1;
+			break;
 		case 'u':
 			if (uflg++)
 				usage();
@@ -241,12 +246,24 @@
 #endif
 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
 		errx(1, "%s", kvm_geterr(kd));
-	if (nflg)
+	if (sflg) {
+		if (!checkfile) {
+			warnx("must provide a filename");
+			usage();
+		}
 		printf("%s",
+"INODE  DEV    SIZE     BLOCKS MODE   FLAGS  LNK UID GID ATIME      MTIME      CTIME      NAME\n");
+		dostats();
+		exit(0);
+	}
+	else {
+		if (nflg)
+			printf("%s",
 "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
-	else
-		printf("%s",
+		else
+			printf("%s",
 "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
+	}
 	if (checkfile && fsflg == 0)
 		printf(" NAME\n");
 	else
@@ -288,6 +305,31 @@
 	}

 /*
+ * print inode information for all files in devs
+ */
+void
+dostats() {
+	struct stat s;
+	register DEVS *d;
+	for (d = devs; d != NULL; d = d->next) {
+		if (d->name == NULL)	/* does this ever happen? */
+			errx(1, "invalid filename");
+		if (stat(d->name, &s) == -1) {
+			warnx("couldn't stat file %s", d->name);
+			continue;
+		}
+		(void)printf("%-6u %-6d %-8qd %-6qd %-6.6o %-6.6o %-3d %-3d %-3d ",
+			s.st_ino, s.st_dev, s.st_size, s.st_blocks,
+			(unsigned int)s.st_mode,
+			(unsigned int)s.st_flags,
+			s.st_nlink, s.st_uid, s.st_gid);
+		(void)printf("%-10lu %-10lu %-10lu %s",
+			s.st_atime, s.st_mtime, s.st_ctime, d->name);
+		(void)putchar('\n');
+	}
+}
+
+/*
  * print open files attributed to this process
  */
 void
@@ -874,6 +916,6 @@
 usage()
 {
 	(void)fprintf(stderr,
- "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
+ "usage: fstat [-fmnsv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
 	exit(1);
 }



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020518235204.O2117-100000>