Index: uspace/app/bdsh/cmds/builtin_cmds.c
===================================================================
--- uspace/app/bdsh/cmds/builtin_cmds.c	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/cmds/builtin_cmds.c	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -38,4 +38,5 @@
 #include "cmds.h"
 #include "builtin_aliases.h"
+#include "scli.h"
 
 extern volatile unsigned int cli_interactive;
@@ -101,13 +102,23 @@
 }
 
-int run_builtin(int builtin, char *argv[], cliuser_t *usr)
+int run_builtin(int builtin, char *argv[], cliuser_t *usr, iostate_t *new_iostate)
 {
+	int rc;
 	builtin_t *cmd = builtins;
 
 	cmd += builtin;
+	
+	iostate_t *old_iostate = get_iostate();
+	set_iostate(new_iostate);
+	
+	if (NULL != cmd->entry) {
+		rc = ((int)cmd->entry(argv, usr));
+	}
+	else {
+		rc = CL_ENOENT;
+	}
+	
+	set_iostate(old_iostate);
 
-	if (NULL != cmd->entry)
-		return((int)cmd->entry(argv, usr));
-
-	return CL_ENOENT;
+	return rc;
 }
Index: uspace/app/bdsh/cmds/cmds.h
===================================================================
--- uspace/app/bdsh/cmds/cmds.h	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/cmds/cmds.h	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -59,5 +59,5 @@
 extern char *alias_for_module(const char *);
 extern int help_module(int, unsigned int);
-extern int run_module(int, char *[]);
+extern int run_module(int, char *[], iostate_t *);
 
 /* Prototypes for builtin launchers */
@@ -67,5 +67,5 @@
 extern char *alias_for_builtin(const char *);
 extern int help_builtin(int, unsigned int);
-extern int run_builtin(int, char *[], cliuser_t *);
+extern int run_builtin(int, char *[], cliuser_t *, iostate_t *);
 
 #endif
Index: uspace/app/bdsh/cmds/mod_cmds.c
===================================================================
--- uspace/app/bdsh/cmds/mod_cmds.c	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/cmds/mod_cmds.c	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -124,13 +124,23 @@
 /* Invokes the module entry point modules[module], passing argv[] as an argument
  * stack. */
-int run_module(int module, char *argv[])
+int run_module(int module, char *argv[], iostate_t *new_iostate)
 {
+	int rc;
 	module_t *mod = modules;
 
 	mod += module;
+	
+	iostate_t *old_iostate = get_iostate();
+	set_iostate(new_iostate);
 
-	if (NULL != mod->entry)
-		return ((int)mod->entry(argv));
+	if (NULL != mod->entry) {
+		rc = ((int)mod->entry(argv));
+	}
+	else {
+		rc = CL_ENOENT;
+	}
+	
+	set_iostate(old_iostate);
 
-	return CL_ENOENT;
+	return rc;
 }
Index: uspace/app/bdsh/errors.c
===================================================================
--- uspace/app/bdsh/errors.c	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/errors.c	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -38,4 +38,5 @@
 #include "errors.h"
 #include "errstr.h"
+#include "scli.h"
 
 volatile int cli_errno = CL_EOK;
Index: uspace/app/bdsh/exec.c
===================================================================
--- uspace/app/bdsh/exec.c	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/exec.c	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -112,5 +112,5 @@
 }
 
-unsigned int try_exec(char *cmd, char **argv, FILE **files)
+unsigned int try_exec(char *cmd, char **argv, iostate_t *io)
 {
 	task_id_t tid;
@@ -120,7 +120,12 @@
 	fdi_node_t file_nodes[3];
 	fdi_node_t *file_nodes_p[4];
+	FILE *files[3];
 
 	tmp = str_dup(find_command(cmd));
 	free(found);
+	
+	files[0] = io->stdin;
+	files[1] = io->stdout;
+	files[2] = io->stderr;
 	
 	for (i = 0; i < 3 && files[i] != NULL; i++) {
Index: uspace/app/bdsh/exec.h
===================================================================
--- uspace/app/bdsh/exec.h	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/exec.h	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -3,6 +3,7 @@
 
 #include <task.h>
+#include "scli.h"
 
-extern unsigned int try_exec(char *, char **, FILE **);
+extern unsigned int try_exec(char *, char **, iostate_t *);
 
 #endif
Index: uspace/app/bdsh/input.c
===================================================================
--- uspace/app/bdsh/input.c	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/input.c	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -58,5 +58,5 @@
 
 /* Private helpers */
-static int run_command(char **, cliuser_t *, FILE **);
+static int run_command(char **, cliuser_t *, iostate_t *);
 static void print_pipe_usage(void);
 
@@ -140,31 +140,43 @@
 	}
 	
-	FILE *files[4];
-	files[0] = stdin;
-	files[1] = stdout;
-	files[2] = stderr;
-	files[3] = 0;
+	iostate_t new_iostate = {
+		.stdin = stdin,
+		.stdout = stdout,
+		.stderr = stderr
+	};
+	
+	FILE *from = NULL;
+	FILE *to = NULL;
 	
 	if (redir_from) {
-		FILE *from = fopen(redir_from, "r");
+		from = fopen(redir_from, "r");
 		if (from == NULL) {
 			printf("Cannot open file %s\n", redir_from);
 			rc = errno;
-			goto finit;
+			goto finit_with_files;
 		}
-		files[0] = from;
-	}
+		new_iostate.stdin = from;
+	}
+	
 	
 	if (redir_to) {
-		FILE *to = fopen(redir_to, "w");
+		to = fopen(redir_to, "w");
 		if (to == NULL) {
 			printf("Cannot open file %s\n", redir_to);
 			rc = errno;
-			goto finit;
+			goto finit_with_files;
 		}
-		files[1] = to;
-	}
-	
-	rc = run_command(cmd, usr, files);
+		new_iostate.stdout = to;
+	}
+	
+	rc = run_command(cmd, usr, &new_iostate);
+	
+finit_with_files:
+	if (from != NULL) {
+		fclose(from);
+	}
+	if (to != NULL) {
+		fclose(to);
+	}
 	
 finit:
@@ -188,5 +200,5 @@
 }
 
-int run_command(char **cmd, cliuser_t *usr, FILE *files[])
+int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate)
 {
 	int id = 0;
@@ -199,14 +211,14 @@
 	/* Is it a builtin command ? */
 	if ((id = (is_builtin(cmd[0]))) > -1) {
-		return run_builtin(id, cmd, usr);
+		return run_builtin(id, cmd, usr, new_iostate);
 	}
 	
 	/* Is it a module ? */
 	if ((id = (is_module(cmd[0]))) > -1) {
-		return run_module(id, cmd);
+		return run_module(id, cmd, new_iostate);
 	}
 
 	/* See what try_exec thinks of it */
-	return try_exec(cmd[0], cmd, files);
+	return try_exec(cmd[0], cmd, new_iostate);
 }
 
Index: uspace/app/bdsh/scli.c
===================================================================
--- uspace/app/bdsh/scli.c	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/scli.c	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -42,4 +42,6 @@
 /* See scli.h */
 static cliuser_t usr;
+static iostate_t *iostate;
+static iostate_t stdiostate;
 
 /* Globals that are modified during start-up that modules/builtins
@@ -82,7 +84,26 @@
 }
 
+iostate_t *get_iostate(void)
+{
+	return iostate;
+}
+
+
+void set_iostate(iostate_t *ios)
+{
+	iostate = ios;
+	stdin = ios->stdin;
+	stdout = ios->stdout;
+	stderr = ios->stderr;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret = 0;
+	
+	stdiostate.stdin = stdin;
+	stdiostate.stdout = stdout;
+	stdiostate.stderr = stderr;
+	iostate = &stdiostate;
 
 	if (cli_init(&usr))
Index: uspace/app/bdsh/scli.h
===================================================================
--- uspace/app/bdsh/scli.h	(revision ae452016941d5b8f8cb26c7fc394ebe556a555db)
+++ uspace/app/bdsh/scli.h	(revision 6ea9a1dfdb4f3891ca3275e64ad5d39475726960)
@@ -13,5 +13,14 @@
 } cliuser_t;
 
+typedef struct {
+	FILE *stdin;
+	FILE *stdout;
+	FILE *stderr;
+} iostate_t;
+
 extern const char *progname;
 
+extern iostate_t *get_iostate(void);
+extern void set_iostate(iostate_t *);
+
 #endif
