aboutsummaryrefslogtreecommitdiff
path: root/chown.c
blob: 8eb2c1ba5681604a69a14336e332c7ab16cdcbab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* SPDX-License-Identifier: BSD-3-Clause */
/*
 * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
 */
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>

#define REQ_PRINT_USAGE /* Require print_usage() from common.h */
#define REQ_ERRPRINT /* Require errprint() from common.h */
#define DESCRIPTION "Change file ownership."
#define OPERANDS    "owner file"
#include "common.h"

#ifdef FASESBOX
int chown_main(int argc, char *argv[]) {
#else
int main(int argc, char *argv[]) {
#endif
	struct passwd *user;
	if (argc == 1) {
		print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
		return 1;
	}
	if ((user = getpwnam(argv[1])) == NULL && (user = getpwuid(strtol(argv[1], NULL, 10))) == NULL)
		return errprint(argv[0], argv[1], errno); /* User doesn't exist */
	/* User found! */
	else if ((user = getpwnam(argv[1])) != NULL)
		chown(argv[2], user->pw_uid, user->pw_gid);
	else if ((user = getpwuid(strtol(argv[1], NULL, 10))) != NULL)
		chown(argv[2], user->pw_uid, user->pw_gid);
	if (errno) return errprint(argv[0], argv[2], errno);

	return 0;
}