chown.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, -L, and -P. */
  10. /* BB_AUDIT GNU defects - unsupported long options. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
  12. #include "busybox.h"
  13. static uid_t uid = -1;
  14. static gid_t gid = -1;
  15. static int (*chown_func)(const char *, uid_t, gid_t) = chown;
  16. #define OPT_RECURSE (option_mask32 & 1)
  17. #define OPT_NODEREF (option_mask32 & 2)
  18. #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
  19. #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
  20. #define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
  21. #define OPT_STR ("Rh" USE_DESKTOP("vcf"))
  22. /* TODO:
  23. * -H if a command line argument is a symbolic link to a directory, traverse it
  24. * -L traverse every symbolic link to a directory encountered
  25. * -P do not traverse any symbolic links (default)
  26. */
  27. static int fileAction(const char *fileName, struct stat *statbuf,
  28. void ATTRIBUTE_UNUSED *junk, int depth)
  29. {
  30. // TODO: -H/-L/-P
  31. // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
  32. if (!chown_func(fileName,
  33. (uid == (uid_t)-1) ? statbuf->st_uid : uid,
  34. (gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
  35. if (OPT_VERBOSE
  36. || (OPT_CHANGED && (statbuf->st_uid != uid || statbuf->st_gid != gid))
  37. ) {
  38. printf("changed ownership of '%s' to %u:%u\n", fileName, uid, gid);
  39. }
  40. return TRUE;
  41. }
  42. if (!OPT_QUIET)
  43. bb_perror_msg("%s", fileName); /* A filename can have % in it... */
  44. return FALSE;
  45. }
  46. int chown_main(int argc, char **argv)
  47. {
  48. int retval = EXIT_SUCCESS;
  49. char *groupName;
  50. opt_complementary = "-2";
  51. getopt32(argc, argv, OPT_STR);
  52. if (OPT_NODEREF) chown_func = lchown;
  53. argv += optind;
  54. /* First, check if there is a group name here */
  55. groupName = strchr(*argv, '.');
  56. if (!groupName) {
  57. groupName = strchr(*argv, ':');
  58. }
  59. /* Check for the username and groupname */
  60. if (groupName) {
  61. *groupName++ = '\0';
  62. gid = get_ug_id(groupName, bb_xgetgrnam);
  63. }
  64. if (--groupName != *argv)
  65. uid = get_ug_id(*argv, bb_xgetpwnam);
  66. ++argv;
  67. /* Ok, ready to do the deed now */
  68. do {
  69. if (!recursive_action(*argv,
  70. OPT_RECURSE, // recurse
  71. FALSE, // follow links: TODO: -H/-L/-P
  72. FALSE, // depth first
  73. fileAction, // file action
  74. fileAction, // dir action
  75. NULL, // user data
  76. 0) // depth
  77. ) {
  78. retval = EXIT_FAILURE;
  79. }
  80. } while (*++argv);
  81. return retval;
  82. }