chown.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini chown implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
  10. /* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
  11. /* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */
  12. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include "busybox.h"
  17. static uid_t uid = -1;
  18. static gid_t gid = -1;
  19. static int (*chown_func)(const char *, uid_t, gid_t) = chown;
  20. static int fileAction(const char *fileName, struct stat *statbuf,
  21. void ATTRIBUTE_UNUSED *junk)
  22. {
  23. if (!chown_func(fileName,
  24. (uid == (uid_t)-1) ? statbuf->st_uid : uid,
  25. (gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
  26. return TRUE;
  27. }
  28. bb_perror_msg("%s", fileName); /* A filename could have % in it... */
  29. return FALSE;
  30. }
  31. #define FLAG_R 1
  32. #define FLAG_h 2
  33. int chown_main(int argc, char **argv)
  34. {
  35. int flags;
  36. int retval = EXIT_SUCCESS;
  37. char *groupName;
  38. flags = bb_getopt_ulflags(argc, argv, "Rh");
  39. if (flags & FLAG_h) chown_func = lchown;
  40. if (argc - optind < 2) {
  41. bb_show_usage();
  42. }
  43. argv += optind;
  44. /* First, check if there is a group name here */
  45. if ((groupName = strchr(*argv, '.')) == NULL) {
  46. groupName = strchr(*argv, ':');
  47. }
  48. /* Check for the username and groupname */
  49. if (groupName) {
  50. *groupName++ = '\0';
  51. gid = get_ug_id(groupName, bb_xgetgrnam);
  52. }
  53. if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam);
  54. ++argv;
  55. /* Ok, ready to do the deed now */
  56. do {
  57. if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
  58. fileAction, fileAction, NULL)) {
  59. retval = EXIT_FAILURE;
  60. }
  61. } while (*++argv);
  62. return retval;
  63. }
  64. /*
  65. Local Variables:
  66. c-file-style: "linux"
  67. c-basic-offset: 4
  68. tab-width: 4
  69. End:
  70. */