From owner-svn-src-head@FreeBSD.ORG Tue Jan 29 18:19:41 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 42A67382; Tue, 29 Jan 2013 18:19:41 +0000 (UTC) (envelope-from brooks@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 2628BE80; Tue, 29 Jan 2013 18:19:41 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.5/8.14.5) with ESMTP id r0TIJflQ001178; Tue, 29 Jan 2013 18:19:41 GMT (envelope-from brooks@svn.freebsd.org) Received: (from brooks@localhost) by svn.freebsd.org (8.14.5/8.14.5/Submit) id r0TIJeYW001176; Tue, 29 Jan 2013 18:19:40 GMT (envelope-from brooks@svn.freebsd.org) Message-Id: <201301291819.r0TIJeYW001176@svn.freebsd.org> From: Brooks Davis Date: Tue, 29 Jan 2013 18:19:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r246083 - head/bin/cat X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Jan 2013 18:19:41 -0000 Author: brooks Date: Tue Jan 29 18:19:40 2013 New Revision: 246083 URL: http://svnweb.freebsd.org/changeset/base/246083 Log: Add -l option to cat(1). This option causes cat(1) to use fcntl(2) to set an exclusive advisory lock on stdout. This will be used to guarantee orderly writing to METALOG. Sponsored by: DARPA, AFRL Obtained from: NetBSD (mason) Modified: head/bin/cat/cat.1 head/bin/cat/cat.c Modified: head/bin/cat/cat.1 ============================================================================== --- head/bin/cat/cat.1 Tue Jan 29 17:54:26 2013 (r246082) +++ head/bin/cat/cat.1 Tue Jan 29 18:19:40 2013 (r246083) @@ -32,7 +32,7 @@ .\" @(#)cat.1 8.3 (Berkeley) 5/2/95 .\" $FreeBSD$ .\" -.Dd March 21, 2004 +.Dd Jaunary 29, 2013 .Dt CAT 1 .Os .Sh NAME @@ -40,7 +40,7 @@ .Nd concatenate and print files .Sh SYNOPSIS .Nm -.Op Fl benstuv +.Op Fl belnstuv .Op Ar .Sh DESCRIPTION The @@ -79,6 +79,16 @@ Display non-printing characters (see the option), and display a dollar sign .Pq Ql \&$ at the end of each line. +.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 @@ -160,6 +170,7 @@ operand. .Xr tail 1 , .Xr vis 1 , .Xr zcat 1 , +.Xr fcntl 2 , .Xr setbuf 3 .Rs .%A Rob Pike @@ -175,7 +186,7 @@ utility is compliant with the specification. .Pp The flags -.Op Fl benstv +.Op Fl belnstv are extensions to the specification. .Sh HISTORY A Modified: head/bin/cat/cat.c ============================================================================== --- head/bin/cat/cat.c Tue Jan 29 17:54:26 2013 (r246082) +++ head/bin/cat/cat.c Tue Jan 29 18:19:40 2013 (r246083) @@ -64,7 +64,7 @@ __FBSDID("$FreeBSD$"); #include #include -static int bflag, eflag, nflag, sflag, tflag, vflag; +static int bflag, eflag, lflag, nflag, sflag, tflag, vflag; static int rval; static const char *filename; @@ -96,10 +96,11 @@ int main(int argc, char *argv[]) { int ch; + struct flock stdout_lock; setlocale(LC_CTYPE, ""); - while ((ch = getopt(argc, argv, "benstuv")) != -1) + while ((ch = getopt(argc, argv, "belnstuv")) != -1) switch (ch) { case 'b': bflag = nflag = 1; /* -b implies -n */ @@ -107,6 +108,9 @@ main(int argc, char *argv[]) case 'e': eflag = vflag = 1; /* -e implies -v */ break; + case 'l': + lflag = 1; + break; case 'n': nflag = 1; break; @@ -127,6 +131,15 @@ main(int argc, char *argv[]) } argv += optind; + if (lflag) { + stdout_lock.l_len = 0; + stdout_lock.l_start = 0; + stdout_lock.l_type = F_WRLCK; + stdout_lock.l_whence = SEEK_SET; + if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1) + err(EXIT_FAILURE, "stdout"); + } + if (bflag || eflag || nflag || sflag || tflag || vflag) scanfiles(argv, 1); else @@ -140,7 +153,7 @@ main(int argc, char *argv[]) static void usage(void) { - fprintf(stderr, "usage: cat [-benstuv] [file ...]\n"); + fprintf(stderr, "usage: cat [-belnstuv] [file ...]\n"); exit(1); /* NOTREACHED */ }