[BusyBox] SH patches.

Gennady Feldman gfeldman at mail.com
Wed Nov 29 21:19:24 UTC 2000


I am attaching two separate patches:
1) sh_profile - I know that the code was commented out, but here is the
working version. Apparently the interface for built-in functions was
changed, so here is the new working version.(Does . /etc/profile when a
login shell) sh is still lacking many features but for simple exports
it's more than enough to initialize sane values for environment
variables and run a few things.

2) sh_prompt - the new code tried to emulate bash prompt in the
following ways:
    - sets '$' for regular user and '#' for root
    - shows last path component and not the whole path. This was
actually quite annoying esp when you are located deep within the
directory structure and prompt takes up more than half width of the
screen (which shows another problem w/ command editing).
    - added user name and hostname to the prompt (to look like bash
shell, comes in handy too)

G.F.
-------------- next part --------------
--- sh.orig	Wed Nov 29 15:33:56 2000
+++ sh.c	Wed Nov 29 16:06:12 2000
@@ -166,7 +166,8 @@
 	{NULL, NULL, NULL}
 };
 
-static char *prompt = "# ";
+//static char *prompt = "# ";
+static char prompt[3];
 static char *cwd;
 static char *local_pending_command = NULL;
 static char *promptStr = NULL;
@@ -655,6 +656,9 @@
 
 static int getCommand(FILE * source, char *command)
 {
+	int i;
+	char *user,buf[255],*s;
+	
 	if (source == NULL) {
 		if (local_pending_command) {
 			/* a command specified (-c option): return it & mark it done */
@@ -666,6 +670,17 @@
 		return 1;
 	}
 
+	/* get User Name and setup prompt */
+	strcpy(prompt,( getuid() != 0 ) ? "$ ":"# ");
+	user=xmalloc(9);
+	my_getpwuid(user, getuid());
+	
+	/* get HostName */
+	gethostname(buf, 255);
+	s = strchr(buf, '.');
+	if (s)
+		*s = 0;
+	
 	if (source == stdin) {
 #ifdef BB_FEATURE_SH_COMMAND_EDITING
 		int len;
@@ -678,21 +693,38 @@
 		*/
 		cmdedit_init();
 		signal(SIGWINCH, win_changed);
-		len=fprintf(stdout, "%s %s", cwd, prompt);
+		i=strlen(cwd);
+		i--;
+		if (i>1){
+		 while ((i>0) && (*(cwd+i)!='/') ) i--;
+		
+		 if (*(cwd+i)=='/') i++;
+		}
+		len=fprintf(stdout, "[%s@%s %s]%s", user, buf, (cwd+i), prompt);
 		fflush(stdout);
 		promptStr=(char*)xmalloc(sizeof(char)*(len+1));
-		sprintf(promptStr, "%s %s", cwd, prompt);
+		sprintf(promptStr, "[%s@%s %s]%s", user, buf, (cwd+i), prompt);
 		cmdedit_read_input(promptStr, command);
 		free( promptStr);
 		cmdedit_terminate();
 		signal(SIGWINCH, SIG_DFL);
 		return 0;
 #else
-		fprintf(stdout, "%s %s", cwd, prompt);
+		i=strlen(cwd);
+		i--;
+		if (i>1){
+			while ((i>0) && (*(cwd+i)!='/') ) i--;
+			if (*(cwd+i)=='/') i++;
+		}
+		
+		fprintf(stdout, "[%s@%s %s]%s",user, buf, (cwd+i), prompt);
 		fflush(stdout);
 #endif
 	}
 
+	/* don't leak memory */
+	free(user);
+	
 	if (!fgets(command, BUFSIZ - 2, source)) {
 		if (source == stdin)
 			printf("\n");
-------------- next part --------------
--- sh.orig	Wed Nov 29 15:33:56 2000
+++ sh.c	Wed Nov 29 15:55:32 2000
@@ -1437,11 +1466,23 @@
 	FILE *input = stdin;
 	argc = argc_l;
 	argv = argv_l;
+	
 
-
-	//if (argv[0] && argv[0][0] == '-') {
-	//      builtin_source("/etc/profile");
-	//}
+	if (argv[0] && argv[0][0] == '-') {
+		struct job newJob;
+		struct jobSet jobList = { NULL, NULL };
+		int inBg;
+		char *nextCommand;
+		
+		nextCommand=xmalloc(20);
+		strcpy(nextCommand,". /etc/profile");
+		if (!parseCommand(&nextCommand, &newJob, &jobList, &inBg) && newJob.numProgs) {
+			int pipefds[2] = {-1,-1};
+			runCommand(&newJob, &jobList, inBg, pipefds);
+		}
+		free(nextCommand);
+	}
+	
 
 	while ((opt = getopt(argc_l, argv_l, "cx")) > 0) {
 		switch (opt) {


More information about the busybox mailing list