2011-08-29 18:05:08 +04:00
|
|
|
/* $NetBSD: chflags.c,v 1.16 2011/08/29 14:05:08 joerg Exp $ */
|
1994-11-14 23:27:21 +03:00
|
|
|
|
1994-11-14 23:11:58 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-11-14 23:11:58 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-18 16:39:50 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-11-14 23:11:58 +03:00
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1994-11-14 23:11:58 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-26 12:49:20 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "from: @(#)chflags.c 8.5 (Berkeley) 4/1/94";
|
|
|
|
#else
|
2011-08-29 18:05:08 +04:00
|
|
|
__RCSID("$NetBSD: chflags.c,v 1.16 2011/08/29 14:05:08 joerg Exp $");
|
1995-03-26 12:49:20 +04:00
|
|
|
#endif
|
1994-11-14 23:11:58 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fts.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2006-12-14 23:57:00 +03:00
|
|
|
#include <util.h>
|
1998-10-10 11:38:22 +04:00
|
|
|
|
2011-08-29 18:05:08 +04:00
|
|
|
__dead static void usage(void);
|
1994-11-14 23:11:58 +03:00
|
|
|
|
|
|
|
int
|
2005-01-12 20:19:23 +03:00
|
|
|
main(int argc, char *argv[])
|
1994-11-14 23:11:58 +03:00
|
|
|
{
|
|
|
|
FTS *ftsp;
|
|
|
|
FTSENT *p;
|
2000-08-04 12:06:57 +04:00
|
|
|
u_long clear, set, newflags;
|
1994-11-14 23:11:58 +03:00
|
|
|
long val;
|
2000-08-04 12:06:57 +04:00
|
|
|
int Hflag, Lflag, Rflag, ch, fts_options, hflag, oct, rval;
|
1994-11-14 23:11:58 +03:00
|
|
|
char *flags, *ep;
|
2005-02-17 19:53:45 +03:00
|
|
|
int (*change_flags)(const char *, u_long);
|
1994-11-14 23:11:58 +03:00
|
|
|
|
2000-08-04 12:06:57 +04:00
|
|
|
Hflag = Lflag = Rflag = hflag = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "HLPRh")) != -1)
|
1994-11-14 23:11:58 +03:00
|
|
|
switch (ch) {
|
|
|
|
case 'H':
|
|
|
|
Hflag = 1;
|
2000-07-08 07:14:50 +04:00
|
|
|
Lflag = 0;
|
1994-11-14 23:11:58 +03:00
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
Lflag = 1;
|
2000-07-08 07:14:50 +04:00
|
|
|
Hflag = 0;
|
1994-11-14 23:11:58 +03:00
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
Hflag = Lflag = 0;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
Rflag = 1;
|
|
|
|
break;
|
2000-08-04 12:06:57 +04:00
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
1994-11-14 23:11:58 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
fts_options = FTS_PHYSICAL;
|
|
|
|
if (Rflag) {
|
|
|
|
if (Hflag)
|
|
|
|
fts_options |= FTS_COMFOLLOW;
|
|
|
|
if (Lflag) {
|
|
|
|
fts_options &= ~FTS_PHYSICAL;
|
|
|
|
fts_options |= FTS_LOGICAL;
|
|
|
|
}
|
2002-07-07 15:44:02 +04:00
|
|
|
} else if (!hflag)
|
|
|
|
fts_options |= FTS_COMFOLLOW;
|
1994-11-14 23:11:58 +03:00
|
|
|
|
|
|
|
flags = *argv;
|
|
|
|
if (*flags >= '0' && *flags <= '7') {
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(flags, &ep, 8);
|
|
|
|
if (val < 0)
|
|
|
|
errno = ERANGE;
|
|
|
|
if (errno)
|
2000-07-08 07:14:50 +04:00
|
|
|
err(1, "invalid flags: %s", flags);
|
|
|
|
if (*ep)
|
|
|
|
errx(1, "invalid flags: %s", flags);
|
1994-11-14 23:11:58 +03:00
|
|
|
set = val;
|
2000-07-08 07:14:50 +04:00
|
|
|
oct = 1;
|
1994-11-14 23:11:58 +03:00
|
|
|
} else {
|
|
|
|
if (string_to_flags(&flags, &set, &clear))
|
2000-07-08 07:14:50 +04:00
|
|
|
errx(1, "invalid flag: %s", flags);
|
1994-11-14 23:11:58 +03:00
|
|
|
clear = ~clear;
|
|
|
|
oct = 0;
|
|
|
|
}
|
|
|
|
|
2000-07-08 07:14:50 +04:00
|
|
|
if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
|
2000-08-04 12:06:57 +04:00
|
|
|
err(1, "fts_open");
|
1994-11-14 23:11:58 +03:00
|
|
|
|
|
|
|
for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
|
2000-08-04 12:06:57 +04:00
|
|
|
change_flags = chflags;
|
1994-11-14 23:11:58 +03:00
|
|
|
switch (p->fts_info) {
|
|
|
|
case FTS_D:
|
|
|
|
if (Rflag) /* Change it at FTS_DP. */
|
|
|
|
continue;
|
|
|
|
fts_set(ftsp, p, FTS_SKIP);
|
|
|
|
break;
|
|
|
|
case FTS_DNR: /* Warn, chflag, continue. */
|
|
|
|
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
|
|
|
|
rval = 1;
|
|
|
|
break;
|
|
|
|
case FTS_ERR: /* Warn, continue. */
|
|
|
|
case FTS_NS:
|
|
|
|
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
|
|
|
|
rval = 1;
|
|
|
|
continue;
|
2000-08-04 12:06:57 +04:00
|
|
|
case FTS_SL: /* Ignore unless -h. */
|
|
|
|
/*
|
|
|
|
* All symlinks we found while doing a physical
|
|
|
|
* walk end up here.
|
|
|
|
*/
|
|
|
|
if (!hflag)
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Note that if we follow a symlink, fts_info is
|
|
|
|
* not FTS_SL but FTS_F or whatever. And we should
|
|
|
|
* use lchflags only for FTS_SL and should use chflags
|
|
|
|
* for others.
|
|
|
|
*/
|
|
|
|
change_flags = lchflags;
|
|
|
|
break;
|
|
|
|
case FTS_SLNONE: /* Ignore. */
|
1994-11-14 23:11:58 +03:00
|
|
|
/*
|
|
|
|
* The only symlinks that end up here are ones that
|
2000-08-04 12:06:57 +04:00
|
|
|
* don't point to anything. Note that if we are
|
|
|
|
* doing a phisycal walk, we never reach here unless
|
|
|
|
* we asked to follow explicitly.
|
1994-11-14 23:11:58 +03:00
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2000-08-04 12:06:57 +04:00
|
|
|
if (oct)
|
|
|
|
newflags = set;
|
|
|
|
else {
|
|
|
|
newflags = p->fts_statp->st_flags;
|
|
|
|
newflags |= set;
|
|
|
|
newflags &= clear;
|
|
|
|
}
|
|
|
|
if ((*change_flags)(p->fts_accpath, newflags)) {
|
|
|
|
warn("%s", p->fts_path);
|
|
|
|
rval = 1;
|
1994-11-14 23:11:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (errno)
|
|
|
|
err(1, "fts_read");
|
|
|
|
exit(rval);
|
|
|
|
}
|
|
|
|
|
2011-08-29 18:05:08 +04:00
|
|
|
static void
|
2005-01-12 20:19:23 +03:00
|
|
|
usage(void)
|
1994-11-14 23:11:58 +03:00
|
|
|
{
|
2000-08-04 12:06:57 +04:00
|
|
|
|
1994-11-14 23:11:58 +03:00
|
|
|
(void)fprintf(stderr,
|
2000-08-04 12:06:57 +04:00
|
|
|
"usage: chflags [-R [-H | -L | -P]] [-h] flags file ...\n");
|
1994-11-14 23:11:58 +03:00
|
|
|
exit(1);
|
|
|
|
}
|