Subject: This should be the final 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 01:41:00
--+QahgC5+KEYLbs62
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment
Content-Transfer-Encoding: quoted-printable

While it's somewhat unlikely that fcntl will return any of the possible
errors it notes for file locking, it's not impossible. To that end, error
checking has been wrapped around the fcntl call.

The patch:

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 05:31:47
@@ -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,8 @@
 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.
 .It Fl n
 Number the output lines, starting at 1.
 .It Fl s
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 05:31:47
@@ -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,18 @@
 		case 'f':
 			fflag =3D 1;
 			break;
+		case 'l':
+			stdout_lock.l_start =3D 0;
+			stdout_lock.l_len =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) {
+				fprintf(stderr, "%s: %s\n", getprogname(), \
+				    strerror(errno));
+				exit(1);
+			}
+			break;
 		case 'n':
 			nflag =3D 1;
 			break;
@@ -111,7 +124,7 @@
 		default:
 		case '?':
 			(void)fprintf(stderr,
-			    "usage: cat [-befnstuv] [-] [file ...]\n");
+			    "usage: cat [-beflnstuv] [-] [file ...]\n");
 			exit(1);
 			/* NOTREACHED */
 		}


Sorry for the multiple posts! This should be it. The line-wrapping here is
somewhat ugly, but the code is functional.

As noted before, I'll commit this on Monday if there are no major problems,
and I'll present other changes needed to make use of this in fixing the
"build.sh -U -j {2,3,4,...}" problem.

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

--+QahgC5+KEYLbs62
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

iD8DBQE9AZjsykMMY715wXIRAhsuAJ4jCxfPZI9KCrdEPsBPHbjJPSk/fQCgnkrF
pMupPADoB44KRtqInw2jNQo=
=R79Z
-----END PGP SIGNATURE-----

--+QahgC5+KEYLbs62--