chown.c (a0e610c43975ca0ec0bfc7d1df88d8b7a3cb871c) chown.c (bb577bb6998be6b5a4853c203c8308bc9b84be60)
1/*
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 38 unchanged lines hidden (view full) ---

47
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <fts.h>
52#include <grp.h>
53#include <libgen.h>
54#include <pwd.h>
1/*
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 38 unchanged lines hidden (view full) ---

47
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <fts.h>
52#include <grp.h>
53#include <libgen.h>
54#include <pwd.h>
55#include <signal.h>
55#include <stdint.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
61static void a_gid(const char *);
62static void a_uid(const char *);
63static void chownerr(const char *);
64static uid_t id(const char *, const char *);
65static void usage(void);
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62static void a_gid(const char *);
63static void a_uid(const char *);
64static void chownerr(const char *);
65static uid_t id(const char *, const char *);
66static void usage(void);
67static void print_info(const FTSENT *, int);
66
67static uid_t uid;
68static gid_t gid;
69static int ischown;
70static const char *gname;
68
69static uid_t uid;
70static gid_t gid;
71static int ischown;
72static const char *gname;
73static volatile sig_atomic_t siginfo;
71
74
75static void
76siginfo_handler(int sig __unused)
77{
78
79 siginfo = 1;
80}
81
72int
73main(int argc, char **argv)
74{
75 FTS *ftsp;
76 FTSENT *p;
77 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
78 int ch, fts_options, rval;
79 char *cp;

--- 34 unchanged lines hidden (view full) ---

114 usage();
115 }
116 argv += optind;
117 argc -= optind;
118
119 if (argc < 2)
120 usage();
121
82int
83main(int argc, char **argv)
84{
85 FTS *ftsp;
86 FTSENT *p;
87 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
88 int ch, fts_options, rval;
89 char *cp;

--- 34 unchanged lines hidden (view full) ---

124 usage();
125 }
126 argv += optind;
127 argc -= optind;
128
129 if (argc < 2)
130 usage();
131
132 (void)signal(SIGINFO, siginfo_handler);
133
122 if (Rflag) {
123 if (hflag && (Hflag || Lflag))
124 errx(1, "the -R%c and -h options may not be "
125 "specified together", Hflag ? 'H' : 'L');
126 if (Lflag) {
127 fts_options = FTS_LOGICAL;
128 } else {
129 fts_options = FTS_PHYSICAL;

--- 54 unchanged lines hidden (view full) ---

184 case FTS_ERR: /* Warn, continue. */
185 case FTS_NS:
186 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
187 rval = 1;
188 continue;
189 default:
190 break;
191 }
134 if (Rflag) {
135 if (hflag && (Hflag || Lflag))
136 errx(1, "the -R%c and -h options may not be "
137 "specified together", Hflag ? 'H' : 'L');
138 if (Lflag) {
139 fts_options = FTS_LOGICAL;
140 } else {
141 fts_options = FTS_PHYSICAL;

--- 54 unchanged lines hidden (view full) ---

196 case FTS_ERR: /* Warn, continue. */
197 case FTS_NS:
198 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
199 rval = 1;
200 continue;
201 default:
202 break;
203 }
204 if (siginfo) {
205 print_info(p, 2);
206 siginfo = 0;
207 }
192 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
193 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
194 continue;
195 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
196 == -1 && !fflag) {
197 chownerr(p->fts_path);
198 rval = 1;
208 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
209 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
210 continue;
211 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
212 == -1 && !fflag) {
213 chownerr(p->fts_path);
214 rval = 1;
199 } else if (vflag) {
200 printf("%s", p->fts_path);
201 if (vflag > 1) {
202 if (ischown) {
203 printf(": %ju:%ju -> %ju:%ju",
204 (uintmax_t)
205 p->fts_statp->st_uid,
206 (uintmax_t)
207 p->fts_statp->st_gid,
208 (uid == (uid_t)-1) ?
209 (uintmax_t)
210 p->fts_statp->st_uid :
211 (uintmax_t)uid,
212 (gid == (gid_t)-1) ?
213 (uintmax_t)
214 p->fts_statp->st_gid :
215 (uintmax_t)gid);
216 } else {
217 printf(": %ju -> %ju",
218 (uintmax_t)
219 p->fts_statp->st_gid,
220 (gid == (gid_t)-1) ?
221 (uintmax_t)
222 p->fts_statp->st_gid :
223 (uintmax_t)gid);
224 }
225 }
226 printf("\n");
227 }
215 } else if (vflag)
216 print_info(p, vflag);
228 }
229 if (errno)
230 err(1, "fts_read");
231 exit(rval);
232}
233
234static void
235a_gid(const char *s)

--- 74 unchanged lines hidden (view full) ---

310 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
311 " file ...",
312 " chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
313 else
314 (void)fprintf(stderr, "%s\n",
315 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
316 exit(1);
317}
217 }
218 if (errno)
219 err(1, "fts_read");
220 exit(rval);
221}
222
223static void
224a_gid(const char *s)

--- 74 unchanged lines hidden (view full) ---

299 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
300 " file ...",
301 " chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
302 else
303 (void)fprintf(stderr, "%s\n",
304 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
305 exit(1);
306}
307
308static void
309print_info(const FTSENT *p, int vflag)
310{
311
312 printf("%s", p->fts_path);
313 if (vflag > 1) {
314 if (ischown) {
315 printf(": %ju:%ju -> %ju:%ju",
316 (uintmax_t)p->fts_statp->st_uid,
317 (uintmax_t)p->fts_statp->st_gid,
318 (uid == (uid_t)-1) ?
319 (uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid,
320 (gid == (gid_t)-1) ?
321 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
322 } else {
323 printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid,
324 (gid == (gid_t)-1) ?
325 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
326 }
327 }
328 printf("\n");
329}