/* * UNG's Not GNU * * Copyright (c) 2011-2019, Jakob Kaivo * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include #include #include #include #include static enum { NOFOLLOW, FOLLOWARGV, FOLLOWALL } links = FOLLOWARGV; static int recursive = 0; static uid_t newowner = -1; static gid_t newgroup = -1; static int retval = 0; static int nftw_chown(const char *p, const struct stat *st, int typeflag, struct FTW *f) { (void)typeflag; (void)f; if (st == NULL) { fprintf(stderr, "chown: %s: unknown error\n", p); retval = 1; } else if (S_ISLNK(st->st_mode)) { /* handle specially */ } else { if (chown(p, newowner, newgroup) != 0) { fprintf(stderr, "chown: %s: %s\n", p, strerror(errno)); retval = 1; } } return 0; } static void parse_owner(char *owner) { char *colon = strchr(owner, ':'); if (colon) { *colon = '\0'; char *grpname = colon + 1; struct group *grp = getgrnam(grpname); if (!grp) { char *end; newgroup = (gid_t)strtoumax(grpname, &end, 0); if (end && *end != '\0') { printf("chown: couldn't find group '%s'\n", grpname); exit(0); } } else { newgroup = grp->gr_gid; } } struct passwd *pwd = getpwnam(owner); if (!pwd) { char *end; newowner = (uid_t)strtoumax(owner, &end, 0); if (end && *end != '\0') { printf("chown: couldn't find user '%s'\n", owner); exit(0); } } else { newowner = pwd->pw_uid; } } int do_chown(char *path, int recurse, int howlinks, uid_t uid, gid_t gid) { recursive = recurse; links = howlinks; newowner = uid; newgroup = gid; // FIXME: This is where to recurse and handle the difference betwixt links types return nftw_chown(path, NULL, 0, NULL); } int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); int c; while ((c = getopt(argc, argv, "hHLPR")) != -1) { switch (c) { case 'h': if (recursive) { return 1; } recursive = -1; links = NOFOLLOW; break; case 'H': links = FOLLOWARGV; break; case 'L': links = FOLLOWALL; break; case 'P': links = NOFOLLOW; break; case 'R': if (recursive == -1) { return 1; } recursive = 1; break; default: return 1; } } if (optind >= argc - 1) { return 1; } parse_owner(argv[optind++]); while (optind < argc) { do_chown(argv[optind], recursive, links, newowner, newgroup); optind++; } return 0; }