Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/cat Add a -f fflag that makes sure that we only try to r...



details:   https://anonhg.NetBSD.org/src/rev/d0762d26354b
branches:  trunk
changeset: 480539:d0762d26354b
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jan 15 01:13:15 2000 +0000

description:
Add a -f fflag that makes sure that we only try to read from plain files
so that there is no chance to block.

diffstat:

 bin/cat/cat.1 |   6 ++++--
 bin/cat/cat.c |  30 +++++++++++++++++++++++++-----
 2 files changed, 29 insertions(+), 7 deletions(-)

diffs (107 lines):

diff -r 389a517c9db9 -r d0762d26354b bin/cat/cat.1
--- a/bin/cat/cat.1     Sat Jan 15 01:11:45 2000 +0000
+++ b/bin/cat/cat.1     Sat Jan 15 01:13:15 2000 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: cat.1,v 1.17 2000/01/09 15:56:37 abs Exp $
+.\"    $NetBSD: cat.1,v 1.18 2000/01/15 01:13:15 christos Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -44,7 +44,7 @@
 .Nd concatenate and print files
 .Sh SYNOPSIS
 .Nm
-.Op Fl benstuv
+.Op Fl befnstuv
 .Op Fl
 .Op Ar
 .Sh DESCRIPTION
@@ -77,6 +77,8 @@
 .Pq Ql \&$
 at the end of each line
 as well.
+.It Fl f
+Only attempt to display plain files.
 .It Fl n
 Number the output lines, starting at 1.
 .It Fl s
diff -r 389a517c9db9 -r d0762d26354b bin/cat/cat.c
--- a/bin/cat/cat.c     Sat Jan 15 01:11:45 2000 +0000
+++ b/bin/cat/cat.c     Sat Jan 15 01:13:15 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cat.c,v 1.21 1999/07/08 01:56:09 christos Exp $        */
+/*     $NetBSD: cat.c,v 1.22 2000/01/15 01:13:15 christos Exp $        */
 
 /*
  * Copyright (c) 1989, 1993
@@ -47,7 +47,7 @@
 #if 0
 static char sccsid[] = "@(#)cat.c      8.2 (Berkeley) 4/27/95";
 #else
-__RCSID("$NetBSD: cat.c,v 1.21 1999/07/08 01:56:09 christos Exp $");
+__RCSID("$NetBSD: cat.c,v 1.22 2000/01/15 01:13:15 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -64,7 +64,7 @@
 #include <string.h>
 #include <unistd.h>
 
-int bflag, eflag, nflag, sflag, tflag, vflag;
+int bflag, eflag, fflag, nflag, sflag, tflag, vflag;
 int rval;
 const char *filename;
 
@@ -84,7 +84,7 @@
 
        (void)setlocale(LC_ALL, "");
 
-       while ((ch = getopt(argc, argv, "benstuv")) != -1)
+       while ((ch = getopt(argc, argv, "befnstuv")) != -1)
                switch (ch) {
                case 'b':
                        bflag = nflag = 1;      /* -b implies -n */
@@ -98,6 +98,9 @@
                case 's':
                        sflag = 1;
                        break;
+               case 'f':
+                       fflag = 1;
+                       break;
                case 't':
                        tflag = vflag = 1;      /* -t implies -v */
                        break;
@@ -138,7 +141,7 @@
                if (*argv) {
                        if (!strcmp(*argv, "-"))
                                fp = stdin;
-                       else if ((fp = fopen(*argv, "r")) == NULL) {
+                       else if ((fp = fopen(*argv, "rf")) == NULL) {
                                warn("%s", *argv);
                                rval = 1;
                                ++argv;
@@ -236,7 +239,24 @@
                if (*argv) {
                        if (!strcmp(*argv, "-"))
                                fd = fileno(stdin);
+                       else if (fflag) {
+                               struct stat st;
+                               fd = open(*argv, O_RDONLY|O_NONBLOCK, 0);
+                               if (fd < 0)
+                                       goto skip;
+
+                               if (fstat(fd, &st) == -1) {
+                                       close(fd);
+                                       goto skip;
+                               }
+                               if (!S_ISREG(st.st_mode)) {
+                                       close(fd);
+                                       errno = EFTYPE;
+                                       goto skip;
+                               }
+                       }
                        else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
+skip:
                                warn("%s", *argv);
                                rval = 1;
                                ++argv;



Home | Main Index | Thread Index | Old Index