da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1992-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * David Korn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Glenn Fowler
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * AT&T Research
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * chgrp+chown
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz"[-?@(#)$Id: chgrp (AT&T Research) 2009-07-02 $\n]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+NAME?chgrp - change the group ownership of files]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+DESCRIPTION?\bchgrp\b changes the group ownership of each file"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" to \agroup\a, which can be either a group name or a numeric"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" group id. The user ownership of each file may also be changed to"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" \auser\a by prepending \auser\a\b:\b to the group name.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+NAME?chown - change the ownership of files]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+DESCRIPTION?\bchown\b changes the ownership of each file"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" to \auser\a, which can be either a user name or a numeric"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" user id. The group ownership of each file may also be changed to"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" \auser\a by appending \b:\b\agroup\a to the user name.]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[b:before?Only change files with \bctime\b before (less than) the "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "\bmtime\b of \afile\a.]:[file]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[c:changes?Describe only files whose ownership actually changes.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[f:quiet|silent?Do not report files whose ownership fails to change.]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[l|h:symlink?Change the ownership of the symbolic links on systems that "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "support this.]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[m:map?The first operand is interpreted as a file that contains a map "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "of space separated \afrom_uid:from_gid to_uid:to_gid\a pairs. The "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "\auid\a or \agid\a part of each pair may be omitted to mean any \auid\a "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "or \agid\a. Ownership of files matching the \afrom\a part of any pair "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "is changed to the corresponding \ato\a part of the pair. The matching "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "for each file operand is in the order \auid\a:\agid\a, \auid\a:, "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin ":\agid\a. For a given file, once a \auid\a or \agid\a mapping is "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "determined it is not overridden by any subsequent match. Unmatched "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "files are silently ignored.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[n:show?Show actions but don't execute.]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[r:reference?Omit the explicit ownership operand and use the ownership "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "of \afile\a instead.]:[file]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[u:unmapped?Print a diagnostic for each file for which either the "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "\auid\a or \agid\a or both were not mapped.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[v:verbose?Describe changed permissions of all files.]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[H:metaphysical?Follow symbolic links for command arguments; otherwise "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "don't follow symbolic links when traversing directories.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[L:logical|follow?Follow symbolic links when traversing directories.]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[P:physical|nofollow?Don't follow symbolic links when traversing "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "directories.]"
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin"[R:recursive?Recursively change ownership of directories and their "
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin "contents.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[X:test?Canonicalize output for testing.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" file ...\n"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+EXIT STATUS?]{"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "[+0?All files changed successfully.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "[+>0?Unable to change ownership of one or more files.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+SEE ALSO?\bchmod\b(1), \btw\b(1), \bgetconf\b(1), \bls\b(1)]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if defined(__STDPP__directive) && defined(__STDPP__hide)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if defined(__STDPP__directive) && defined(__STDPP__hide)
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin#define OPT_UNMAPPED (1<<7) /* unmapped file diagnostic */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _lib_chown */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * parse uid and gid from s
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chingetids(register char* s, char** e, Key_t* key, int options)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* t;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register int n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while (isspace(*s))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (t = s; (n = *t) && n != ':' && n != '.' && !isspace(n); t++);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((n = t++ - s) >= sizeof(buf))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = (int)strtol(s, &z, 0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (s = t; (n = *t) && !isspace(n); t++);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((n = t++ - s) >= sizeof(buf))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = (int)strtol(s, &z, 0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* s;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register Map_t* m;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin register int i;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin flags = fts_flags() | FTS_TOP | FTS_NOPOSTORDER | FTS_NOSEEDOTDIR;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfputr(sp, ERROR_translate(0, 0, 0, "[owner[:group]]"), -1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfputr(sp, ERROR_translate(0, 0, 0, "[[owner:]group]"), -1);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin error(ERROR_exit(1), "%s: cannot stat", opt_info.arg);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin while (!sh_checksig(context) && (ent = fts_read(fts)))
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if ((unsigned long)ent->fts_statp->st_ctime >= before)
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin keys[0].uid = keys[1].uid = ent->fts_statp->st_uid;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin keys[0].gid = keys[2].gid = ent->fts_statp->st_gid;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin } while (++i < elementsof(keys) && (uid == NOID || gid == NOID));
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if ((options & OPT_UNMAPPED) && (uid == NOID || gid == NOID))
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin error(ERROR_warn(0), "%s: uid and gid not mapped", ent->fts_path);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin error(ERROR_warn(0), "%s: uid not mapped", ent->fts_path);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin error(ERROR_warn(0), "%s: gid not mapped", ent->fts_path);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if (uid != ent->fts_statp->st_uid && uid != NOID || gid != ent->fts_statp->st_gid && gid != NOID)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((ent->fts_info & FTS_SL) && (flags & FTS_PHYSICAL) && (options & OPT_LCHOWN))
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin sfprintf(sfstdout, "%s uid:%05d->%05d gid:%05d->%05d %s\n", op, ent->fts_statp->st_uid, uid, ent->fts_statp->st_gid, gid, ent->fts_path);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!(options & OPT_SHOW) && (*chownf)(ent->fts_accpath, uid, gid) && !(options & OPT_FORCE))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_system(0), "%s: cannot change%s", ent->fts_accpath, s);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_warn(0), "%s: directory causes cycle", ent->fts_accpath);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_system(0), "%s: cannot read directory", ent->fts_accpath);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_system(0), "%s: cannot search directory", ent->fts_accpath);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_system(0), "%s: not found", ent->fts_accpath);