aboutsummaryrefslogtreecommitdiff
path: root/coreutils/chown.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/chown.c')
-rw-r--r--coreutils/chown.c104
1 files changed, 55 insertions, 49 deletions
diff --git a/coreutils/chown.c b/coreutils/chown.c
index 4e766a90b..3e983cfa6 100644
--- a/coreutils/chown.c
+++ b/coreutils/chown.c
@@ -21,10 +21,14 @@
21 * 21 *
22 */ 22 */
23 23
24#include <stdio.h> 24/* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
25/* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
26/* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */
27/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
28
25#include <stdlib.h> 29#include <stdlib.h>
26#include <string.h>
27#include <unistd.h> 30#include <unistd.h>
31#include <string.h>
28#include "busybox.h" 32#include "busybox.h"
29 33
30/* Don't use lchown for libc5 or glibc older then 2.1.x */ 34/* Don't use lchown for libc5 or glibc older then 2.1.x */
@@ -42,65 +46,67 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
42 if (chown_func(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) { 46 if (chown_func(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
43 return (TRUE); 47 return (TRUE);
44 } 48 }
45 perror(fileName); 49 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
46 return (FALSE); 50 return (FALSE);
47} 51}
48 52
53#define FLAG_R 1
54#define FLAG_h 2
55
56static unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *))
57{
58 unsigned long r;
59 char *p;
60
61 r = strtoul(s, &p, 10);
62 if (*p || (s == p)) {
63 r = my_getxxnam(s);
64 }
65
66 return r;
67}
68
49int chown_main(int argc, char **argv) 69int chown_main(int argc, char **argv)
50{ 70{
51 int opt; 71 int flags;
52 int recursiveFlag = FALSE, 72 int retval = EXIT_SUCCESS;
53 noderefFlag = FALSE; 73 char *groupName;
54 char *groupName=NULL; 74
55 char *p=NULL; 75 flags = bb_getopt_ulflags(argc, argv, "Rh");
56 76
57 /* do normal option parsing */ 77 if (flags & FLAG_h) chown_func = lchown;
58 while ((opt = getopt(argc, argv, "Rh")) > 0) { 78
59 switch (opt) { 79 if (argc - optind < 2) {
60 case 'R': 80 bb_show_usage();
61 recursiveFlag = TRUE;
62 break;
63 case 'h':
64 noderefFlag = TRUE;
65 break;
66 default:
67 show_usage();
68 }
69 } 81 }
70 82
71 if (noderefFlag) chown_func = lchown; 83 argv += optind;
72 84
73 if (argc > optind && argc > 2 && argv[optind]) { 85 /* First, check if there is a group name here */
74 /* First, check if there is a group name here */ 86 if ((groupName = strchr(*argv, '.')) == NULL) {
75 groupName = strchr(argv[optind], '.'); 87 groupName = strchr(*argv, ':');
76 if (groupName == NULL) 88 }
77 groupName = strchr(argv[optind], ':'); 89
78 if (groupName) { 90 gid = -1;
79 *groupName++ = '\0'; 91 if (groupName) {
80 gid = strtoul(groupName, &p, 10); 92 *groupName++ = '\0';
81 if (groupName == p) 93 gid = get_ug_id(groupName, my_getgrnam);
82 gid = my_getgrnam(groupName);
83 } else {
84 gid = -1;
85 }
86 /* Now check for the username */
87 uid = strtoul(argv[optind], &p, 10); /* Is is numeric? */
88 if (argv[optind] == p) {
89 uid = my_getpwnam(argv[optind]);
90 }
91 } else {
92 error_msg_and_die(too_few_args);
93 } 94 }
94 95
96 /* Now check for the username */
97 uid = get_ug_id(*argv, my_getpwnam);
98
99 ++argv;
100
95 /* Ok, ready to do the deed now */ 101 /* Ok, ready to do the deed now */
96 while (++optind < argc) { 102 do {
97 if (! recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 103 if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
98 fileAction, fileAction, NULL)) { 104 fileAction, fileAction, NULL)) {
99 return EXIT_FAILURE; 105 retval = EXIT_FAILURE;
100 } 106 }
101 } 107 } while (*++argv);
102 return EXIT_SUCCESS;
103 108
109 return retval;
104} 110}
105 111
106/* 112/*