chown.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
  23. /* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
  24. /* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */
  25. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include "busybox.h"
  30. /* Don't use lchown for glibc older then 2.1.x */
  31. #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
  32. #define lchown chown
  33. #endif
  34. static long uid;
  35. static long gid;
  36. static int (*chown_func)(const char *, uid_t, gid_t) = chown;
  37. static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
  38. {
  39. if (chown_func(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
  40. chmod(fileName, statbuf->st_mode);
  41. return (TRUE);
  42. }
  43. bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
  44. return (FALSE);
  45. }
  46. #define FLAG_R 1
  47. #define FLAG_h 2
  48. int chown_main(int argc, char **argv)
  49. {
  50. int flags;
  51. int retval = EXIT_SUCCESS;
  52. char *groupName;
  53. flags = bb_getopt_ulflags(argc, argv, "Rh");
  54. if (flags & FLAG_h) chown_func = lchown;
  55. if (argc - optind < 2) {
  56. bb_show_usage();
  57. }
  58. argv += optind;
  59. /* First, check if there is a group name here */
  60. if ((groupName = strchr(*argv, '.')) == NULL) {
  61. groupName = strchr(*argv, ':');
  62. }
  63. gid = -1;
  64. if (groupName) {
  65. *groupName++ = '\0';
  66. gid = get_ug_id(groupName, my_getgrnam);
  67. }
  68. /* Now check for the username */
  69. uid = get_ug_id(*argv, my_getpwnam);
  70. ++argv;
  71. /* Ok, ready to do the deed now */
  72. do {
  73. if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
  74. fileAction, fileAction, NULL)) {
  75. retval = EXIT_FAILURE;
  76. }
  77. } while (*++argv);
  78. return retval;
  79. }
  80. /*
  81. Local Variables:
  82. c-file-style: "linux"
  83. c-basic-offset: 4
  84. tab-width: 4
  85. End:
  86. */