Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 1 Aug 1999 22:59:11 -0400 (EDT)
From:      Jamie Howard <howardjp@wam.umd.edu>
To:        Keith Stevenson <k.stevenson@louisville.edu>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: Was someone looking for a BSD licensed stat(1)?
Message-ID:  <Pine.LNX.4.10.9908012252550.9366-100000@dragon.ham.muohio.edu>
In-Reply-To: <19990801192728.B20953@homer.louisville.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 1 Aug 1999, Keith Stevenson wrote:

> the features that were requested.  I honestly haven't figured out how to
> implement the display of selected fields yet.  I realize that this isn't a

Attached is a diff that takes care of that.  Basically, the user can
specify a format that looks like "%f\t%U\t%A" to get the files name, the
username of the owner, and the last access time seperated by tabs
followed by a new line. All of the supported format specifiers are
documented in the man page. I looked around for another version of stat
that does this but could not locate one; therefore the format specifiers
are what made sense to me.  There is also limited support for C-style
escape sequences (no \nnn).

Have a nice day!

Jamie




diff -c stat/stat.1 stat-jph/stat.1
*** stat/stat.1	Sun Aug  1 23:18:48 1999
--- stat-jph/stat.1	Sun Aug  1 23:50:31 1999
***************
*** 42,71 ****
  The options are as follows:
  .Bl -tag -width Fl
  .It Fl d
! dereference symlinks
  .It Fl f
! read filenames to be stat-ed from file ( - means stdin)
  .It Fl g
! show time elements in GMT time zone.  (implies -s)
  .It Fl h
! print usage summary
  .It Fl i
! ignore errors
  .It Fl l
! display one element per line
  .It Fl s
! print mode, uid, gid, and times as strings
  .It Fl t
! terse output
  .It Fl o Ar opts
! output only selected elements of struct stat
  .It Fl f Ar file
! read filenames to be stat-ed from file ( - means stdin)
  .El
  .Sh EXAMPLES
  An example or two will go here
  .Pp
  .Sh SEE ALSO
  .Xr stat 2
  .Rs
  .Sh AUTHORS
--- 42,107 ----
  The options are as follows:
  .Bl -tag -width Fl
  .It Fl d
! Follow symbolic links.
  .It Fl f
! Read filenames to be stat-ed from file ( - means stdin).
  .It Fl g
! Show time elements in GMT time zone.  (implies -s)
  .It Fl h
! Print usage summary.
  .It Fl i
! Ignore errors.
  .It Fl l
! Display one element per line.
  .It Fl s
! Print mode, uid, gid, and times as strings.
  .It Fl t
! Terse output.
  .It Fl o Ar opts
! Output only selected elements of struct stat.  The following
! formatting tags are permitted:
! .Bl -tag -width Fl
! .It Ar %A
! last access time
! .It Ar %C
! file creation time
! .It Ar %G
! file group (group name)
! .It Ar %M
! last modify time
! .It Ar %P
! file permission mode (symbolic)
! .It Ar %U
! file owner (username)
! .It Ar %b
! blocks allocated for file
! .It Ar %d
! device file is on
! .It Ar %f 
! file name
! .It Ar %g
! file group (gid)
! .It Ar %i
! inode
! .It Ar %l
! number of hard links to file
! .It Ar %p
! file permission mode (octal)
! .It Ar %s
! file size in blocks 
! .It Ar %t 
! type of file
! .It Ar %u
! file owner (uid)
! .El
  .It Fl f Ar file
! Read filenames to be stat-ed from file ( - means stdin).
  .El
  .Sh EXAMPLES
  An example or two will go here
  .Pp
  .Sh SEE ALSO
+ .Xr ls 1 ,
  .Xr stat 2
  .Rs
  .Sh AUTHORS
***************
*** 73,76 ****
  .Sh DIAGNOSTICS
  Most errors are the result of bad filenames.
  .Sh BUGS
! Some features (-f, -i, & -o) are currently unimplemented
--- 109,112 ----
  .Sh DIAGNOSTICS
  Most errors are the result of bad filenames.
  .Sh BUGS
! Some features (-f and -i) are currently unimplemented
diff -c stat/stat.c stat-jph/stat.c
*** stat/stat.c	Sun Aug  1 23:17:14 1999
--- stat-jph/stat.c	Sun Aug  1 23:34:29 1999
***************
*** 57,62 ****
--- 57,65 ----
  void terseprint(const char*, const char*, const char*, const char*,
  		struct stat*, struct group*, struct passwd*, struct tm*,
  		struct tm*, struct tm*, int);
+ void customprint(const char *, const char*, const char*, const char*, 
+ 		 const char*, struct stat*, struct group*, struct passwd*, 
+ 		 struct tm*, struct tm*, struct tm*, int);
  int vstat(const char*, struct stat*, int);
  
  
***************
*** 78,85 ****
  	struct stat	inode;
  	struct tm	*atime, *mtime, *ctime;
  
! 	extern char 	*optarg;
! 	extern int	optind;
  
  
  	/* flags */
--- 81,87 ----
  	struct stat	inode;
  	struct tm	*atime, *mtime, *ctime;
  
! 	char *oformat;
  
  
  	/* flags */
***************
*** 113,122 ****
  				f_listout = 1;
  				break;
  			case 'o':
- 				printf("Unimplemented.\n");
- 				exit(EX_OK);
  				f_customout = 1;
  				f_listout = 1;
  				break;
  			case 's':
  				f_stringout = 1;
--- 115,123 ----
  				f_listout = 1;
  				break;
  			case 'o':
  				f_customout = 1;
  				f_listout = 1;
+ 				oformat = optarg;
  				break;
  			case 's':
  				f_stringout = 1;
***************
*** 253,264 ****
  				strcat(fflags, " sunlnk");
  
  			/* Generate output */
! 			if ((!f_customout) && (!f_listout) && (!f_terseout)) {
  				prettyprint(argv[count], ftype, fmode, fflags,
  				    &inode, gp, pw, atime, mtime, ctime);
  			} 
! 			if (f_listout && (!f_terseout)) {
  				listprint(argv[count], ftype, fmode, fflags,
  				    &inode, gp, pw, atime, mtime, ctime,
  				    f_stringout);
  			}
--- 254,270 ----
  				strcat(fflags, " sunlnk");
  
  			/* Generate output */
! 			if ((!f_customout) && (!f_listout) && (!f_terseout) && (!f_customout)) {
  				prettyprint(argv[count], ftype, fmode, fflags,
  				    &inode, gp, pw, atime, mtime, ctime);
  			} 
! 			if (f_listout && (!f_terseout) && (!f_customout)) {
  				listprint(argv[count], ftype, fmode, fflags,
+ 				    &inode, gp, pw, atime, mtime, ctime,
+ 				    f_stringout);
+ 			}
+ 			if (f_customout && (!f_terseout)) {
+ 				customprint(oformat, argv[count], ftype, fmode, fflags,
  				    &inode, gp, pw, atime, mtime, ctime,
  				    f_stringout);
  			}
diff -c stat/util.c stat-jph/util.c
*** stat/util.c	Sun Aug  1 23:18:11 1999
--- stat-jph/util.c	Sun Aug  1 23:41:04 1999
***************
*** 205,210 ****
--- 205,326 ----
          }
  }
  
+ /* customprint: Print in a user specified format */
+ void
+ customprint(const char *fmt, const char *path, const char *ftype, 
+ 	    const char *fmode, const char *fflags, struct stat *inode, 
+ 	    struct group *gp, struct passwd *pw, struct tm *atime, 
+ 	    struct tm *mtime, struct tm *ctime, int f_stringout)
+ {
+     int i, l, newline;
+     char c;
+ 
+     newline = 1;
+     l = strlen(fmt);
+     for (i = 0; i < l; i++) {
+ 	c = fmt[i];
+ 	if (c == '%') {
+ 	    i++;
+ 	    c = fmt[i];
+ 	    switch (c) {
+ 	    case 'A':
+ 		printf("%s", asctime(atime));
+ 		break;
+ 	    case 'C':
+ 		printf("%s\n", asctime(ctime));
+ 		break;
+ 	    case 'M':
+ 		printf("%s", asctime(mtime));
+ 		break;
+ 	    case 'G':
+ 		if (gp != NULL)
+ 		    printf("%s", gp->gr_name);
+ 		break;
+ 	    case 'P':
+ 		printf("%s", fmode);
+ 		break;
+ 	    case 'U':
+ 		if (pw != NULL)
+                         printf("%s", pw->pw_name);
+ 		break;
+ 	    case 'b':
+ 		printf("%qd", inode->st_blocks);
+ 		break;
+ 	    case 'd':
+ 		printf("%u", inode->st_dev);
+ 		break;
+ 	    case 'f':
+ 		printf("%s", path);
+ 		break;
+ 	    case 'g':
+ 		printf("%hu", inode->st_gid);
+ 		break;
+ 	    case 'i':
+ 	        printf("%u", inode->st_ino);
+ 		break;
+ 	    case 'l':
+ 		printf("%hu", inode->st_nlink);
+ 		break;
+ 	    case 'p':
+                 printf("%.4o", inode->st_mode & 07777);
+ 		break;
+ 	    case 's':
+ 	        printf("%qu", inode->st_size);
+ 		break;
+ 	    case 't':
+ 		printf("%s", ftype);
+ 		break;
+ 	    case 'u':
+ 		printf("%hu", inode->st_uid);
+ 		break;
+ 	    default:
+ 		printf("%c", c);
+ 	    }
+ 	    continue;
+ 	}
+ 
+ 	if (c == '\\') {
+ 	    i++;
+ 	    c = fmt[i];
+ 	    switch (c) {
+ 	    case 'a':
+ 		putchar('\a');
+ 		break;
+ 	    case 'b':
+ 		putchar('\b');
+ 		break;
+ 	    case 'c':
+ 		newline = 0;
+ 		break;
+ 	    case 'f':
+ 		putchar('\f');
+ 		break;
+ 	    case 'n':
+ 		putchar('\n');
+ 		break;
+ 	    case 'r':
+ 		putchar('\r');
+ 		break;
+ 	    case 't':
+ 		putchar('\t');
+ 		break;
+ 	    case 'v':
+ 		putchar('\v');
+ 		break;
+ 	    default:
+ 		putchar(c);
+ 	    }
+ 	    continue;
+ 	}
+ 	putchar(c);
+     }
+ 
+     if (newline)
+ 	putchar('\n');
+ 
+     return;
+ }
+ 
  
  /* vstat: Do either a stat(2) or lstat(2) depending on the value of deref */
  int



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




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