From 6a73ffc9e08d05583abc7fa4e1088901503fc769 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Tue, 11 Nov 2008 06:56:47 +0000 Subject: [PATCH] Add -f and -v options to chflags. -f ignores failures if chflags can't modify the flags. -v prints file names as they are processed. -vv prints the flags changes. Obtained-from: FreeBSD --- usr.bin/chflags/chflags.1 | 22 +++++++++++++---- usr.bin/chflags/chflags.c | 50 ++++++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/usr.bin/chflags/chflags.1 b/usr.bin/chflags/chflags.1 index 819e57a2b5..e91f0431c7 100644 --- a/usr.bin/chflags/chflags.1 +++ b/usr.bin/chflags/chflags.1 @@ -34,10 +34,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)chflags.1 8.4 (Berkeley) 5/2/95 -.\" $FreeBSD: src/usr.bin/chflags/chflags.1,v 1.9.2.6 2003/04/08 19:16:16 johan Exp $ -.\" $DragonFly: src/usr.bin/chflags/chflags.1,v 1.6 2008/11/11 06:28:19 pavalos Exp $ +.\" $FreeBSD: src/bin/chflags/chflags.1,v 1.30 2008/03/09 12:10:24 rwatson Exp $ +.\" $DragonFly: src/usr.bin/chflags/chflags.1,v 1.7 2008/11/11 06:56:47 pavalos Exp $ .\" -.Dd June 24, 2008 +.Dd November 11, 2008 .Dt CHFLAGS 1 .Os .Sh NAME @@ -45,7 +45,7 @@ .Nd change file flags .Sh SYNOPSIS .Nm -.Op Fl h +.Op Fl fhv .Oo .Fl R .Op Fl H | Fl L | Fl P @@ -62,6 +62,12 @@ operand. .Pp The options are as follows: .Bl -tag -width indent +.It Fl f +Do not display a diagnostic message if +.Nm +could not modify the flags for +.Va file , +nor modify the exit status to reflect such failures. .It Fl H If the .Fl R @@ -84,6 +90,14 @@ This is the default. .It Fl R Change the file flags for the file hierarchies rooted in the files instead of just the files themselves. +.It Fl v +Cause +.Nm +to be verbose, showing filenames as the flags are modified. +If the +.Fl v +option is specified more than once, the old and new flags of the file +will also be printed, in octal notation. .El .Pp The flags are specified as an octal number or a comma separated list diff --git a/usr.bin/chflags/chflags.c b/usr.bin/chflags/chflags.c index 606980d7c0..1416ed9cdb 100644 --- a/usr.bin/chflags/chflags.c +++ b/usr.bin/chflags/chflags.c @@ -1,4 +1,4 @@ -/* +/*- * Copyright (c) 1992, 1993, 1994 * The Regents of the University of California. All rights reserved. * @@ -32,8 +32,8 @@ * * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved. * @(#)chflags.c 8.5 (Berkeley) 4/1/94 - * $FreeBSD: src/usr.bin/chflags/chflags.c,v 1.7.2.3 2001/08/01 23:09:18 obrien Exp $ - * $DragonFly: src/usr.bin/chflags/chflags.c,v 1.7 2008/11/11 05:53:07 pavalos Exp $ + * $FreeBSD: src/bin/chflags/chflags.c,v 1.24 2008/03/09 12:10:24 rwatson Exp $ + * $DragonFly: src/usr.bin/chflags/chflags.c,v 1.8 2008/11/11 06:56:47 pavalos Exp $ */ #include @@ -55,14 +55,15 @@ main(int argc, char **argv) { FTS *ftsp; FTSENT *p; - u_long clear, set; + u_long clear, newflags, set; long val; - int Hflag, Lflag, Rflag, hflag, ch, fts_options, oct, rval; + int Hflag, Lflag, Rflag, fflag, hflag, vflag; + int ch, fts_options, oct, rval; char *flags, *ep; int (*change_flags)(const char *, unsigned long); - Hflag = Lflag = Rflag = hflag = 0; - while ((ch = getopt(argc, argv, "HLPRh")) != -1) + Hflag = Lflag = Rflag = fflag = hflag = vflag = 0; + while ((ch = getopt(argc, argv, "HLPRfhv")) != -1) switch (ch) { case 'H': Hflag = 1; @@ -78,9 +79,15 @@ main(int argc, char **argv) case 'R': Rflag = 1; break; + case 'f': + fflag = 1; + break; case 'h': hflag = 1; break; + case 'v': + vflag++; + break; default: usage(); } @@ -162,18 +169,23 @@ main(int argc, char **argv) default: break; } - if (oct) { - if (!(*change_flags)(p->fts_accpath, set)) - continue; - } else { - p->fts_statp->st_flags |= set; - p->fts_statp->st_flags &= clear; - if (!(*change_flags)(p->fts_accpath, - (u_long)p->fts_statp->st_flags)) - continue; + if (oct) + newflags = set; + else + newflags = (p->fts_statp->st_flags | set) & clear; + if (newflags == p->fts_statp->st_flags) + continue; + if ((*change_flags)(p->fts_accpath, newflags) && !fflag) { + warn("%s", p->fts_path); + rval = 1; + } else if (vflag) { + printf("%s", p->fts_path); + if (vflag > 1) + printf(": 0%lo -> 0%lo", + (u_long)p->fts_statp->st_flags, + newflags); + printf("\n"); } - warn("%s", p->fts_path); - rval = 1; } if (errno) err(1, "fts_read"); @@ -184,6 +196,6 @@ static void usage(void) { fprintf(stderr, - "usage: chflags [-h] [-R [-H | -L | -P]] flags file ...\n"); + "usage: chflags [-fhv] [-R [-H | -L | -P]] flags file ...\n"); exit(1); } -- 2.41.0