X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=chown.c;h=a656072041b9b2bc58dfee138fb6ca69debaad64;hb=774d135b66f4ba914ee8649ce6e8c3f7d3e36c35;hp=bcaeea38e5daf9482dfd595ff57cc235073938a2;hpb=2ce1edcf544ac675e6762c9861a6b918401ea716;p=oweals%2Fbusybox.git diff --git a/chown.c b/chown.c index bcaeea38e..a65607204 100644 --- a/chown.c +++ b/chown.c @@ -1,7 +1,10 @@ +/* vi: set sw=4 ts=4: */ /* - * Mini chown/chgrp implementation for busybox + * Mini chown/chmod/chgrp implementation for busybox * - * Copyright (C) 1998 by Erik Andersen + * + * Copyright (C) 1999,2000,2001 by Lineo, inc. + * Written by Erik Andersen , * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,103 +23,91 @@ */ #include -#include -#include -#include "internal.h" - - -static int uid=-1; -static int gid=0; -static int chownApp; -static char* invocationName=NULL; +#include +#include +#include +#include "busybox.h" +/* Don't use lchown for libc5 or glibc older then 2.1.x */ +#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1) +#define lchown chown +#endif -static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n" - "Change the group membership of each FILE to GROUP.\n" - "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n"; -static const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n" - "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n" - "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n"; +static long uid; +static long gid; +static int (*chown_func)() = chown; - -static int fileAction(const char *fileName) +static int fileAction(const char *fileName, struct stat *statbuf, void* junk) { - struct stat statBuf; - if ((stat(fileName, &statBuf) < 0) || - (chown(fileName, - ((chownApp==TRUE)? uid: statBuf.st_uid), - gid) < 0)) { + if (chown_func(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) { + return (TRUE); + } perror(fileName); - return( TRUE); - } - return( FALSE); + return (FALSE); } int chown_main(int argc, char **argv) { - struct group *grp; - struct passwd *pwd; - int recursiveFlag=FALSE; - char *groupName; + int opt; + int recursiveFlag = FALSE, + noderefFlag = FALSE; + char *groupName=NULL; + char *p=NULL; + /* do normal option parsing */ + while ((opt = getopt(argc, argv, "Rh")) > 0) { + switch (opt) { + case 'R': + recursiveFlag = TRUE; + break; + case 'h': + noderefFlag = TRUE; + break; + default: + show_usage(); + } + } - chownApp = (strcmp(*argv, "chown")==0)? TRUE : FALSE; - - if (argc < 2) { - fprintf(stderr, "Usage: %s %s", *argv, - (chownApp==TRUE)? chown_usage : chgrp_usage); - exit( FALSE); - } - invocationName=*argv; - argc--; - argv++; + if (noderefFlag) chown_func = lchown; - /* Parse options */ - while (**argv == '-') { - while (*++(*argv)) switch (**argv) { - case 'R': - recursiveFlag = TRUE; - break; - default: - fprintf(stderr, "Unknown option: %c\n", **argv); - exit( FALSE); + if (argc > optind && argc > 2 && argv[optind]) { + /* First, check if there is a group name here */ + groupName = strchr(argv[optind], '.'); + if (groupName == NULL) + groupName = strchr(argv[optind], ':'); + if (groupName) { + *groupName++ = '\0'; + gid = strtoul(groupName, &p, 10); + if (groupName == p) + gid = my_getgrnam(groupName); + } else { + gid = -1; + } + /* Now check for the username */ + uid = strtoul(argv[optind], &p, 10); /* Is is numeric? */ + if (argv[optind] == p) { + uid = my_getpwnam(argv[optind]); + } + } else { + error_msg_and_die(too_few_args); } - argc--; - argv++; - } - - /* Find the selected group */ - groupName = strchr(*argv, '.'); - if ( chownApp==TRUE && groupName ) - *groupName++ = '\0'; - else - groupName = *argv; - grp = getgrnam(groupName); - if (grp == NULL) { - fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName); - exit( FALSE); - } - gid = grp->gr_gid; - /* Find the selected user (if appropriate) */ - if (chownApp==TRUE) { - pwd = getpwnam(*argv); - if (pwd == NULL) { - fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv); - exit( FALSE); + /* Ok, ready to do the deed now */ + while (++optind < argc) { + if (recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, + fileAction, fileAction, NULL) == FALSE) { + return EXIT_FAILURE; + } } - uid = pwd->pw_uid; - } + return EXIT_SUCCESS; - /* Ok, ready to do the deed now */ - if (argc <= 1) { - fprintf(stderr, "%s: too few arguments", invocationName); - exit( FALSE); - } - while (argc-- > 1) { - argv++; - recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction); - } - exit(TRUE); } + +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/