Subject: Cleaner cat(1) patch.
To: None <tech-userlevel@netbsd.org>
From: Mason Loring Bliss <mason@acheron.middleboro.ma.us>
List: tech-userlevel
Date: 06/08/2002 14:32:34
--fd5uyaI9j6xoeUBo
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

This one incorporates the requests for more verbosity in cat.1, for moving
locking out of the getopt(3) block, and for using err(3) rather than
fprintf(3)/exit(3) for dealing with errors.


Index: cat.1
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/basesrc/bin/cat/cat.1,v
retrieving revision 1.25
diff -u -w -r1.25 cat.1
--- cat.1	2002/05/15 06:52:54	1.25
+++ cat.1	2002/06/08 18:26:31
@@ -44,7 +44,7 @@
 .Nd concatenate and print files
 .Sh SYNOPSIS
 .Nm
-.Op Fl befnstuv
+.Op Fl beflnstuv
 .Op Fl
 .Op Ar
 .Sh DESCRIPTION
@@ -79,6 +79,15 @@
 as well.
 .It Fl f
 Only attempt to display regular files.
+.It Fl l
+Set an exclusive advisory lock on the standard output file descriptor.
+This lock is set using
+.Xr fcntl 2
+with the
+.Dv F_SETLKW
+command. If the output file is already locked,
+.Nm
+will block until the lock is acquired.
 .It Fl n
 Number the output lines, starting at 1.
 .It Fl s
@@ -117,7 +126,8 @@
 .Xr pr 1 ,
 .Xr tail 1 ,
 .Xr view 1 ,
-.Xr vis 1
+.Xr vis 1 ,
+.Xr fcntl 2
 .Rs
 .%A Rob Pike
 .%T "UNIX Style, or cat -v Considered Harmful"
Index: cat.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/basesrc/bin/cat/cat.c,v
retrieving revision 1.31
diff -u -w -r1.31 cat.c
--- cat.c	2002/05/09 02:19:42	1.31
+++ cat.c	2002/06/08 18:26:31
@@ -64,7 +64,7 @@
 #include <string.h>
 #include <unistd.h>
=20
-int bflag, eflag, fflag, nflag, sflag, tflag, vflag;
+int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
 int rval;
 const char *filename;
=20
@@ -78,11 +78,12 @@
 main(int argc, char *argv[])
 {
 	int ch;
+	struct flock stdout_lock;
=20
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, "");
=20
-	while ((ch =3D getopt(argc, argv, "befnstuv")) !=3D -1)
+	while ((ch =3D getopt(argc, argv, "beflnstuv")) !=3D -1)
 		switch (ch) {
 		case 'b':
 			bflag =3D nflag =3D 1;	/* -b implies -n */
@@ -93,6 +94,9 @@
 		case 'f':
 			fflag =3D 1;
 			break;
+		case 'l':
+			lflag =3D 1;
+			break;
 		case 'n':
 			nflag =3D 1;
 			break;
@@ -111,11 +115,20 @@
 		default:
 		case '?':
 			(void)fprintf(stderr,
-			    "usage: cat [-befnstuv] [-] [file ...]\n");
+			    "usage: cat [-beflnstuv] [-] [file ...]\n");
 			exit(1);
 			/* NOTREACHED */
 		}
 	argv +=3D optind;
+
+	if (lflag) {
+		stdout_lock.l_len =3D 0;
+		stdout_lock.l_start =3D 0;
+		stdout_lock.l_type =3D F_WRLCK;
+		stdout_lock.l_whence =3D SEEK_SET;
+		if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) =3D=3D -1)
+			err(EXIT_FAILURE, "stdout");
+	}
=20
 	if (bflag || eflag || nflag || sflag || tflag || vflag)
 		cook_args(argv);

--=20
Mason Loring Bliss   mason@acheron.middleboro.ma.us   Ewige Blumenkraft!
https://www.deadsexy.org/  awake ? sleep : random() & 2 ? dream : sleep;

--fd5uyaI9j6xoeUBo
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (NetBSD)
Comment: For info see http://www.gnupg.org

iD8DBQE9Ak3CykMMY715wXIRArIKAJ0d9bPpdyN8oEe6IyuNkoB701JDQwCdEO96
jy1cuItPofSq/OtUEmMVm3A=
=QqRO
-----END PGP SIGNATURE-----

--fd5uyaI9j6xoeUBo--