micro-utils/coreutils/chown.c

152 lines
2.5 KiB
C
Raw Normal View History

2023-10-27 19:55:18 +03:00
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
2023-10-31 12:53:32 +03:00
#include "make_path.h"
#include "get_stat.h"
2023-10-28 09:11:02 +03:00
unsigned int r_flag;
2023-11-06 23:00:26 +03:00
unsigned int s_flag;
2023-11-01 12:25:51 +03:00
int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path);
2023-10-27 19:55:18 +03:00
int (*chown_func)(const char *pathname, uid_t owner, gid_t group);
long gid;
long uid;
2023-10-28 09:11:02 +03:00
int change(const char *file) {
2023-11-06 13:41:52 +03:00
if (chown_func(file, uid, gid)) {
2023-11-06 23:00:26 +03:00
if (!s_flag)
fprintf(stderr, "chown: unable to chown %s: %s\n", file, strerror(errno));
2023-10-28 09:11:02 +03:00
return 1;
}
2023-11-06 13:41:52 +03:00
return 0;
2023-10-28 09:11:02 +03:00
}
2023-10-27 19:55:18 +03:00
int cntree(const char *dst) {
2023-10-28 10:02:24 +03:00
int ret = change(dst);
2023-10-27 19:55:18 +03:00
2023-10-28 10:02:24 +03:00
struct stat stat_path;
2023-11-01 12:25:51 +03:00
if (get_stat("chown", dst, &stat_path))
2023-10-28 10:02:24 +03:00
return 1;
2023-10-28 10:11:00 +03:00
if (!S_ISDIR(stat_path.st_mode) || !r_flag)
2023-10-28 10:02:24 +03:00
return ret;
DIR *dir = opendir(dst);
if (dir == NULL) {
2023-11-06 23:00:26 +03:00
if (!s_flag)
fprintf(stderr, "chown: %s: Can`t open directory\n", dst);
2023-10-28 10:02:24 +03:00
return 1;
}
struct dirent *ep;
while ((ep = readdir(dir)) != NULL) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
2023-10-31 12:53:32 +03:00
char *full_path = mu_make_path("chown", dst, ep->d_name);
2023-10-28 10:02:24 +03:00
if (full_path == NULL)
continue;
if (cntree(full_path))
ret = 1;
free(full_path);
}
closedir(dir);
return ret;
2023-10-27 19:55:18 +03:00
}
void get_owner(const char *arg) {
char *group = strchr(arg, ':');
unsigned int g_flag = 1;
unsigned int u_flag = 1;
2023-11-01 12:25:51 +03:00
2023-10-27 19:55:18 +03:00
if (group == arg)
u_flag = 0;
else if (!group)
g_flag = 0;
if (g_flag) {
group[0] = '\0';
group++;
struct group *grp = getgrnam(group);
if (!grp) {
2023-11-06 23:00:26 +03:00
if (!s_flag)
fprintf(stderr, "chown: invalid group: %s\n", group);
2023-10-27 19:55:18 +03:00
exit(1);
}
gid = grp->gr_gid;
}
if (u_flag) {
struct passwd *pwd = getpwnam(arg);
if (!pwd) {
2023-11-06 23:00:26 +03:00
if (!s_flag)
fprintf(stderr, "chown: invalid user: %s\n", arg);
2023-10-27 19:55:18 +03:00
exit(1);
}
uid = pwd->pw_gid;
}
}
2023-11-06 23:52:48 +03:00
int main(int argc, char **argv) {
2023-11-01 12:25:51 +03:00
get_stat = mu_get_lstat;
2023-10-27 19:55:18 +03:00
chown_func = lchown;
2023-11-06 23:52:48 +03:00
int opt;
while ((opt = getopt(argc, argv, "RfH") != -1)) {
switch (opt) {
case 'R':
r_flag = 1;
break;
case 'f':
s_flag = 1;
break;
case 'H':
chown_func = chown;
get_stat = mu_get_stat;
break;
default:
printf("chown [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-r recursive]\n");
return 0;
2023-10-27 19:55:18 +03:00
}
}
2023-11-06 23:52:48 +03:00
if (argv[optind] == NULL) {
2023-10-27 19:55:18 +03:00
fprintf(stderr, "chown: missing operand\n");
return 1;
}
gid = -1;
uid = -1;
2023-11-06 23:52:48 +03:00
get_owner(argv[optind]);
2023-10-27 19:55:18 +03:00
2023-11-06 23:52:48 +03:00
argv += optind;
argc -= optind;
2023-10-27 19:55:18 +03:00
int ret = 0;
2023-11-06 23:52:48 +03:00
for (int i = 1; i < argc; i++)
2023-10-27 19:55:18 +03:00
if (cntree(argv[i]))
ret = 1;
return ret;
}