RFC: Port of NetBSD cat(1)'s -f option.

Giorgos Keramidas <[email protected]>
Newsgroups gmane.os.freebsd.devel.audit
Message-ID <[email protected]>
Here's a patch that adds to our cat(1) the same functionality as
NetBSD cat(1) when passed -f on the command line.  The original
commit log from NetBSD's CVS tree is:

	----------------------------
	revision 1.18
	date: 2000/01/15 01:13:15;  author: christos;  state: Exp;  lines: +4 -2
	branches:  1.18.4;
	Add a -f fflag that makes sure that we only try to read from plain files
	so that there is no chance to block.

What do you all think of this?

%%%
Index: cat.1
===================================================================
RCS file: /home/ncvs/src/bin/cat/cat.1,v
retrieving revision 1.19
diff -u -r1.19 cat.1
--- cat.1	25 Sep 2001 18:10:18 -0000	1.19
+++ cat.1	15 May 2002 05:57:28 -0000
@@ -82,6 +82,8 @@
 option), and display a dollar sign
 .Pq Ql \&$
 at the end of each line.
+.It Fl f
+Only attempt to display regular files.
 .It Fl n
 Number the output lines, starting at 1.
 .It Fl s
Index: cat.c
===================================================================
RCS file: /home/ncvs/src/bin/cat/cat.c,v
retrieving revision 1.21
diff -u -r1.21 cat.c
--- cat.c	22 Feb 2002 20:42:57 -0000	1.21
+++ cat.c	15 May 2002 05:56:26 -0000
@@ -66,7 +66,7 @@
 #include <unistd.h>
 #include <stddef.h>
 
-int bflag, eflag, nflag, sflag, tflag, vflag;
+int bflag, eflag, fflag, nflag, sflag, tflag, vflag;
 int rval;
 const char *filename;
 
@@ -85,7 +85,7 @@
 
 	setlocale(LC_CTYPE, "");
 
-	while ((ch = getopt(argc, argv, "benstuv")) != -1)
+	while ((ch = getopt(argc, argv, "befnstuv")) != -1)
 		switch (ch) {
 		case 'b':
 			bflag = nflag = 1;	/* -b implies -n */
@@ -93,6 +93,9 @@
 		case 'e':
 			eflag = vflag = 1;	/* -e implies -v */
 			break;
+		case 'f':
+			fflag = 1;
+			break;
 		case 'n':
 			nflag = 1;
 			break;
@@ -110,12 +113,12 @@
 			break;
 		default:
 			fprintf(stderr,
-			    "usage: cat [-benstuv] [-] [file ...]\n");
+			    "usage: cat [-befnstuv] [-] [file ...]\n");
 			exit(1);
 		}
 	argv += optind;
 
-	if (bflag || eflag || nflag || sflag || tflag || vflag)
+	if (bflag || eflag || fflag || nflag || sflag || tflag || vflag)
 		scanfiles(argv, 1);
 	else
 		scanfiles(argv, 0);
@@ -138,7 +141,15 @@
 			filename = "stdin";
 			fd = STDIN_FILENO;
 		} else {
+			struct stat st;
+
 			filename = path;
+			if (stat(path, &st) < 0 ||
+			    S_ISREG(st.st_mode) == 0) {
+				i++;		/* Skip to next file. */
+				continue;
+			}
+
 			fd = open(path, O_RDONLY);
 #ifndef NO_UDOM_SUPPORT
 			if (fd < 0 && errno == EOPNOTSUPP)
%%%

-- 
Giorgos Keramidas    - http://www.FreeBSD.org
[email protected] - The Power to Serve

To Unsubscribe: send mail to [email protected]
with "unsubscribe freebsd-audit" in the body of the message
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.