[BusyBox] SUSv2-compliant rmdir implementation

Matt Kraai kraai at alumni.carnegiemellon.edu
Wed Aug 29 15:31:12 MDT 2001


Howdy,

SUSv2 requires that rmdir accept the -p option.  The following
patch implements this.  OK to commit?

Matt

Index: rmdir.c
===================================================================
RCS file: /var/cvs/busybox/rmdir.c,v
retrieving revision 1.20
diff -c -3 -p -r1.20 rmdir.c
*** rmdir.c	2001/02/20 06:14:08	1.20
--- rmdir.c	2001/08/29 20:24:37
***************
*** 22,45 ****
   *
   */
  
! #include <stdio.h>
! #include <errno.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include "busybox.h"
  
! extern int rmdir_main(int argc, char **argv)
  {
  	int status = EXIT_SUCCESS;
  
! 	if (argc == 1 || **(argv + 1) == '-')
  		show_usage();
  
! 	while (--argc > 0) {
! 		if (rmdir(*(++argv)) == -1) {
! 			perror_msg("%s", *argv);
  			status = EXIT_FAILURE;
! 		}
! 	}
  	return status;
  }
--- 22,97 ----
   *
   */
  
! #include <getopt.h>
  #include <unistd.h>
  #include <stdlib.h>
+ 
  #include "busybox.h"
+ 
+ 
+ /* Return true if a path is composed of multiple components.  */
+ 
+ static int
+ multiple_components_p (const char *path)
+ {
+ 	const char *s = path;
+ 
+ 	while (s[0] != '\0' && s[0] != '/')
+ 		s++;
  
! 	while (s[0] == '/')
! 		s++;
! 
! 	return (s[0] != '\0');
! }
! 
! 
! /* Remove a directory.  Returns 0 if successful, -1 on error.  */
! 
! static int
! remove_directory (char *path, int flags)
  {
+ 	if (!(flags & FILEUTILS_RECUR)) {
+ 		if (rmdir (path) < 0) {
+ 			perror_msg ("unable to remove `%s'", path);
+ 			return -1;
+ 		}
+ 	} else {
+ 		if (remove_directory (path, 0) < 0)
+ 			return -1;
+ 
+ 		if (multiple_components_p (path))
+ 			if (remove_directory (dirname (path), flags) < 0)
+ 				return -1;
+ 	}
+ 
+ 	return 0;
+ }
+ 
+ 
+ extern int
+ rmdir_main (int argc, char **argv)
+ {
  	int status = EXIT_SUCCESS;
+ 	int flags = 0;
+ 	int i, opt;
+ 
+ 	while ((opt = getopt (argc, argv, "p")) != -1)
+ 		switch (opt) {
+ 			case 'p':
+ 				flags |= FILEUTILS_RECUR;
+ 				break;
  
! 			default:
! 				show_usage ();
! 		}
! 
! 	if (optind == argc)
  		show_usage();
  
! 	for (i = optind; i < argc; i++)
! 		if (remove_directory (argv[i], flags) < 0)
  			status = EXIT_FAILURE;
! 
  	return status;
  }





More information about the busybox mailing list