[BusyBox] BusyBox 0.43: tar.c and sh.c

Marius Groeger mag at sysgo.de
Wed Jun 21 12:15:47 UTC 2000


Hi Erik,

its me again (I'm the one who bothered you about the controlling tty issue).
This time I'm hopefully more help, attached you can find two patches I'd
like you to include in BusyBox.

1. sh.c
   
The busybox sh wouldn't work with the popen() function, which invokes the
shell with "sh -c command args...". I implemented the -c option to support
this.

2. tar.c 
2.a 

The source wouldn't compile correctly on a libc5 system because of an
undifined MIN() macro

2.b

When giving tar bad arguments, it would ask the user to try --help.
Unfortunately, this option didn't exists at all. I took the liberty of
adding a new option -h to print the usage, and changed the help text
accordingly.

For the tar command, I'll probably come up with more patches. Eg. if one
issues a "tar tvf", the unlink command will delete local files with the same
name as in the tar archive due to an unconditional unlink().

Best Regards,

Marius Groeger

------------------------------------------------------------------------------
Marius Groeger <mgroeger at sysgo.de>              SYSGO Real-Time Solutions GmbH
Software Engineering                                        Am Pfaffenstein 14
Phone: +49-6136-9948-0                     D-55270 Klein-Winternheim (Germany)
FAX:   +49-6136-9948-10                                   http://www.sysgo.de/
-------------- next part --------------
Index: sh.c
===================================================================
RCS file: /home/elinos/CVS/sysgo/elinos/build/busybox/sh.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- sh.c	2000/06/20 16:02:39	1.2
+++ sh.c	2000/06/21 11:57:54	1.3
@@ -106,7 +106,7 @@
 static int parseCommand(char **commandPtr, struct job *job, int *isBg);
 static int setupRedirections(struct childProgram *prog);
 static int runCommand(struct job newJob, struct jobSet *jobList, int inBg);
-static int busy_loop(FILE * input);
+static int busy_loop(FILE * input, char *local_pending_command);
 
 
 /* Table of built-in functions */
@@ -127,7 +127,10 @@
 
 static const char shell_usage[] =
 
-	"sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n";
+	"sh [FILE]...\n"
+	"sh -c command [args]...\n"
+	"\n"
+	"The BusyBox command interpreter (shell).\n\n";
 
 
 static char cwd[1024];
@@ -305,7 +308,7 @@
 	}
 
 	/* Now run the file */
-	status = busy_loop(input);
+	status = busy_loop(input, NULL);
 	return (status);
 }
 
@@ -401,6 +404,10 @@
 
 static int getCommand(FILE * source, char *command)
 {
+	if (source == NULL) {
+		return 1;
+	}
+
 	if (source == stdin) {
 #ifdef BB_FEATURE_SH_COMMAND_EDITING
 		int len;
@@ -855,10 +862,10 @@
 }
 
 
-static int busy_loop(FILE * input)
+static int busy_loop(FILE * input, char *local_pending_command)
 {
 	char *command;
-	char *nextCommand = NULL;
+	char *nextCommand = local_pending_command;
 	struct jobSet jobList = { NULL, NULL };
 	struct job newJob;
 	int i;
@@ -939,10 +946,8 @@
 int shell_main(int argc, char **argv)
 {
 	FILE *input = stdin;
+	char *command = 0;
 
-	if (argc > 2) {
-		usage(shell_usage);
-	}
 	/* initialize the cwd */
 	getcwd(cwd, sizeof(cwd));
 
@@ -959,15 +964,35 @@
 		fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT);
 		fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n");
 	} else {
-		if (*argv[1]=='-') {
+		if (argv[1][0]=='-' && argv[1][1]=='c') {
+			int i;
+			command = (char *) calloc(BUFSIZ, sizeof(char));
+			if (command == 0) {
+				fatalError("sh: out of memory\n");
+			}
+			for(i=2; i<argc; i++)
+			{
+				if (strlen(command) + strlen(argv[i]) >= BUFSIZ) {
+				  fatalError("sh: commands for -c option too long\n");
+				}
+				strcat(command, argv[i]);
+				if (i + 1 < argc)
+				  strcat(command, " ");
+			}
+			input = NULL;
+			  
+		}
+		else if (argv[1][0]=='-') {
 			usage("sh\n\nlash -- the BusyBox LAme SHell (command interpreter)\n");
 		}
-		input = fopen(argv[1], "r");
-		if (!input) {
-			fatalError("sh: Couldn't open file '%s': %s\n", argv[1],
-					   strerror(errno));
+		else {
+			input = fopen(argv[1], "r");
+			if (!input) {
+				fatalError("sh: Couldn't open file '%s': %s\n", argv[1],
+						   strerror(errno));
+			}
 		}
 	}
 
-	return (busy_loop(input));
+	return (busy_loop(input, command));
 }
-------------- next part --------------
Index: tar.c
===================================================================
RCS file: /home/elinos/CVS/sysgo/elinos/build/busybox/tar.c,v
retrieving revision 1.1
retrieving revision 1.3
diff -u -r1.1 -r1.3
--- tar.c	2000/05/24 12:40:04	1.1
+++ tar.c	2000/06/21 11:58:15	1.3
@@ -51,6 +51,9 @@
 #include <sys/sysmacros.h>
 #include <sys/param.h>			/* for PATH_MAX */
 
+#ifndef MIN(a,b)
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
 
 static const char tar_usage[] =
 #ifdef BB_FEATURE_TAR_CREATE
@@ -78,6 +81,7 @@
 #endif
 	"\nInformative output:\n"
 	"\tv\t\tverbosely list files processed\n"
+	"\th\t\tthis help text\n"
 	;
 
 /* Tar file constants  */
@@ -200,6 +204,11 @@
 					stopIt=TRUE;
 					break;
 
+				case 'h':
+					usage(tar_usage);
+					exit(0);
+					break;
+			
 				case 't':
 					if (extractFlag == TRUE || createFlag == TRUE)
 						goto flagError;
@@ -249,7 +258,7 @@
 
 				default:
 					fatalError( "Unknown tar flag '%c'\n" 
-							"Try `tar --help' for more information\n", **argv);
+							"Try `tar -h' for more information\n", **argv);
 			}
 		}
 	}


More information about the busybox mailing list