1
0
mirror of git://git.suckless.org/sbase synced 2024-12-22 15:04:53 +00:00
sbase/chgrp.c
FRIGN 8dc92fbd6c Refactor enmasse() and recurse() to reflect depth
The HLP-changes to sbase have been a great addition of functionality,
but they kind of "polluted" the enmasse() and recurse() prototypes.
As this will come in handy in the future, knowing at which "depth"
you are inside a recursing function is an important functionality.

Instead of having a special HLP-flag passed to enmasse, each sub-
function needs to provide it on its own and can calculate results
based on the current depth (for instance, 'H' implies 'P' at
depth > 0).
A special case is recurse(), because it actually depends on the
follow-type. A new flag "recurse_follow" brings consistency into
what used to be spread across different naming conventions (fflag,
HLP_flag, ...).

This also fixes numerous bugs with the behaviour of HLP in the
tools using it.
2015-03-02 22:50:38 +01:00

83 lines
1.3 KiB
C

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <errno.h>
#include <grp.h>
#include <unistd.h>
#include "util.h"
static int gid;
static int status;
static int Rflag;
static struct stat st;
static char *chownf_name = "chown";
static int (*chownf)(const char *, uid_t, gid_t) = chown;
static void
chgrp(const char *path, int depth)
{
if (chownf(path, st.st_uid, gid) < 0) {
weprintf("%s %s:", chownf_name, path);
status = 1;
}
if (Rflag)
recurse(path, chgrp, depth);
}
static void
usage(void)
{
eprintf("usage: chgrp [-h] [-R [-H | -L | -P]] group file ...\n");
}
int
main(int argc, char *argv[])
{
struct group *gr;
ARGBEGIN {
case 'h':
chownf_name = "lchown";
chownf = lchown;
break;
case 'R':
Rflag = 1;
break;
case 'H':
case 'L':
case 'P':
recurse_follow = ARGC();
break;
default:
usage();
} ARGEND;
if (argc < 2)
usage();
if (recurse_follow == 'P') {
chownf_name = "lchown";
chownf = lchown;
}
errno = 0;
gr = getgrnam(argv[0]);
if (!gr) {
if (errno)
eprintf("getgrnam %s:", argv[0]);
else
eprintf("getgrnam %s: no such group\n", argv[0]);
}
gid = gr->gr_gid;
while (*++argv) {
if (stat(*argv, &st) < 0) {
weprintf("stat %s:", *argv);
status = 1;
continue;
}
chgrp(*argv, 0);
}
return status;
}