chgrp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini chgrp 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 chgrp does not support -H, -L, or -P. */
  25. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chgrp.html */
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include "busybox.h"
  29. /* Don't use lchown glibc older then 2.1.x */
  30. #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
  31. #define lchown chown
  32. #endif
  33. static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
  34. {
  35. if (lchown(fileName, statbuf->st_uid, *((long *) junk)) == 0) {
  36. return (TRUE);
  37. }
  38. bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
  39. return (FALSE);
  40. }
  41. int chgrp_main(int argc, char **argv)
  42. {
  43. long gid;
  44. int recursiveFlag;
  45. int retval = EXIT_SUCCESS;
  46. recursiveFlag = bb_getopt_ulflags(argc, argv, "R");
  47. if (argc - optind < 2) {
  48. bb_show_usage();
  49. }
  50. argv += optind;
  51. /* Find the selected group */
  52. gid = get_ug_id(*argv, my_getgrnam);
  53. ++argv;
  54. /* Ok, ready to do the deed now */
  55. do {
  56. if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
  57. fileAction, fileAction, &gid)) {
  58. retval = EXIT_FAILURE;
  59. }
  60. } while (*++argv);
  61. return retval;
  62. }
  63. /*
  64. Local Variables:
  65. c-file-style: "linux"
  66. c-basic-offset: 4
  67. tab-width: 4
  68. End:
  69. */