]>
git.saurik.com Git - apple/file_cmds.git/blob - chmod/chmod.c
c38286d1aaa698beab5281e54b05129742967f77
   2  * Copyright (c) 1989, 1993, 1994 
   3  *      The Regents of the University of California.  All rights reserved. 
   5  * Redistribution and use in source and binary forms, with or without 
   6  * modification, are permitted provided that the following conditions 
   8  * 1. Redistributions of source code must retain the above copyright 
   9  *    notice, this list of conditions and the following disclaimer. 
  10  * 2. Redistributions in binary form must reproduce the above copyright 
  11  *    notice, this list of conditions and the following disclaimer in the 
  12  *    documentation and/or other materials provided with the distribution. 
  13  * 3. All advertising materials mentioning features or use of this software 
  14  *    must display the following acknowledgement: 
  15  *      This product includes software developed by the University of 
  16  *      California, Berkeley and its contributors. 
  17  * 4. Neither the name of the University nor the names of its contributors 
  18  *    may be used to endorse or promote products derived from this software 
  19  *    without specific prior written permission. 
  21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
  22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
  25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  35 static char const copyright
[] = 
  36 "@(#) Copyright (c) 1989, 1993, 1994\n\ 
  37         The Regents of the University of California.  All rights reserved.\n"; 
  42 static char sccsid
[] = "@(#)chmod.c     8.8 (Berkeley) 4/1/94"; 
  45 #include <sys/cdefs.h> 
  46 __RCSID("$FreeBSD: src/bin/chmod/chmod.c,v 1.27 2002/08/04 05:29:13 obrien Exp $"); 
  48 #include <sys/types.h> 
  60 int main(int, char *[]); 
  64 main(int argc
, char *argv
[]) 
  71         int Hflag
, Lflag
, Rflag
, ch
, fflag
, fts_options
, hflag
, rval
; 
  74         mode_t newmode
, omode
; 
  75         int (*change_mode
)(const char *, mode_t
); 
  79         Hflag 
= Lflag 
= Rflag 
= fflag 
= hflag 
= vflag 
= 0; 
  80         while ((ch 
= getopt(argc
, argv
, "HLPRXfghorstuvwx")) != -1) 
 102                          * In System V (and probably POSIX.2) the -h option 
 103                          * causes chmod to change the mode of the symbolic 
 104                          * link.  4.4BSD's symbolic links didn't have modes, 
 105                          * so it was an undocumented noop.  In FreeBSD 3.0, 
 106                          * lchmod(2) is introduced and this option does real 
 111 #endif /* __APPLE__ */ 
 114                  * "-[rwx]" are valid mode commands.  If they are the entire 
 115                  * argument, getopt has moved past them, so decrement optind. 
 116                  * Regardless, we're done argument processing. 
 118                 case 'g': case 'o': case 'r': case 's': 
 119                 case 't': case 'u': case 'w': case 'X': case 'x': 
 120                         if (argv
[optind 
- 1][0] == '-' && 
 121                             argv
[optind 
- 1][1] == ch 
&& 
 122                             argv
[optind 
- 1][2] == '\0') 
 132 done
:   argv 
+= optind
; 
 139                 fts_options 
= FTS_PHYSICAL
; 
 142                 "the -R and -h options may not be specified together."); 
 144                         fts_options 
|= FTS_COMFOLLOW
; 
 146                         fts_options 
&= ~FTS_PHYSICAL
; 
 147                         fts_options 
|= FTS_LOGICAL
; 
 150                 fts_options 
= hflag 
? FTS_PHYSICAL 
: FTS_LOGICAL
; 
 154                 change_mode 
= lchmod
; 
 159 #endif /* __APPLE__ */ 
 162         if (*mode 
>= '0' && *mode 
<= '7') { 
 164                 val 
= strtol(mode
, &ep
, 8); 
 165                 if (val 
> USHRT_MAX 
|| val 
< 0) 
 168                         err(1, "invalid file mode: %s", mode
); 
 170                         errx(1, "invalid file mode: %s", mode
); 
 174                 if ((set 
= setmode(mode
)) == NULL
) 
 175                         errx(1, "invalid file mode: %s", mode
); 
 179         if ((ftsp 
= fts_open(++argv
, fts_options
, 0)) == NULL
) 
 181         for (rval 
= 0; (p 
= fts_read(ftsp
)) != NULL
;) { 
 182                 switch (p
->fts_info
) { 
 183                 case FTS_D
:                     /* Change it at FTS_DP. */ 
 185                                 fts_set(ftsp
, p
, FTS_SKIP
); 
 187                 case FTS_DNR
:                   /* Warn, chmod, continue. */ 
 188                         warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
)); 
 191                 case FTS_ERR
:                   /* Warn, continue. */ 
 193                         warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
)); 
 196                 case FTS_SL
:                    /* Ignore. */ 
 199                          * The only symlinks that end up here are ones that 
 200                          * don't point to anything and ones that we found 
 201                          * doing a physical walk. 
 210                 newmode 
= oct 
? omode 
: getmode(set
, p
->fts_statp
->st_mode
); 
 211                 if ((newmode 
& ALLPERMS
) == (p
->fts_statp
->st_mode 
& ALLPERMS
)) 
 213                 if ((*change_mode
)(p
->fts_accpath
, newmode
) && !fflag
) { 
 214                         warn("%s", p
->fts_path
); 
 218                                 (void)printf("%s", p
->fts_accpath
); 
 223                                         strmode(p
->fts_statp
->st_mode
, m1
); 
 224                                         strmode((p
->fts_statp
->st_mode 
& 
 225                                             S_IFMT
) | newmode
, m2
); 
 227                                         (void)printf(": 0%o [%s] -> 0%o [%s]", 
 228                                             p
->fts_statp
->st_mode
, m1
, 
 229                                             (p
->fts_statp
->st_mode 
& S_IFMT
) | 
 247         (void)fprintf(stderr
, 
 248             "usage: chmod [-fv] [-R [-H | -L | -P]] mode file ...\n"); 
 250         (void)fprintf(stderr
, 
 251             "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n"); 
 252 #endif /* __APPLE__ */