diff options
author | LAN-TW <lantw44@gmail.com> | 2013-11-08 17:44:55 +0800 |
---|---|---|
committer | b01902062 <b01902062@linux5.csie.ntu.edu.tw> | 2013-11-08 17:44:55 +0800 |
commit | 823c42c817300889a13ca37d721dc7214d33adba (patch) | |
tree | 1b7d0269376e00a59e66fe9e06995705355b9419 | |
parent | 328e88167e396b8d72e88019c67b7391ad7f601a (diff) | |
download | cn2013-823c42c817300889a13ca37d721dc7214d33adba.tar.gz cn2013-823c42c817300889a13ca37d721dc7214d33adba.tar.zst cn2013-823c42c817300889a13ca37d721dc7214d33adba.zip |
HW1: 加入 TA pipe 輔助指令、離開程式前清除 TA pipe 暫存檔
-rw-r--r-- | hw1/shell-builtin.c | 27 | ||||
-rw-r--r-- | hw1/shell-builtin.h | 1 | ||||
-rw-r--r-- | hw1/shell.c | 8 |
3 files changed, 35 insertions, 1 deletions
diff --git a/hw1/shell-builtin.c b/hw1/shell-builtin.c index 7d5abb0..a39d191 100644 --- a/hw1/shell-builtin.c +++ b/hw1/shell-builtin.c @@ -7,6 +7,7 @@ #include "xwrap.h" #include <errno.h> +#include <fcntl.h> #include <inttypes.h> #include <stdbool.h> #include <stdio.h> @@ -26,6 +27,7 @@ const RasShellBuiltin ras_shell_builtins[] = { RAS_SHELL_BUILTIN_ENTRY (set), RAS_SHELL_BUILTIN_ENTRY (setenv), RAS_SHELL_BUILTIN_ENTRY (shopt), + RAS_SHELL_BUILTIN_ENTRY (tacat), RAS_SHELL_BUILTIN_ENTRY (tapipe), RAS_SHELL_BUILTIN_ENTRY (umask), RAS_SHELL_BUILTIN_ENTRY (unset), @@ -217,6 +219,31 @@ int ras_shell_builtin_umask (int argc, char* argv[], RasShell* shell) { return 0; } +int ras_shell_builtin_tacat (int argc, char* argv[], RasShell* shell) { + int rval = 0; + for (int i = 1; i < argc; i++) { + char* f = xstrcat (shell->saved_tmpdir, "/", argv[i], NULL); + int fd = open (f, O_RDONLY); + + if (fd < 0) { + fprintf (stderr, "%s: fail to open `%s\': %s\n", + argv[0], argv[i], strerror (errno)); + rval = 1; + free (f); + break; + } + + char buf[8192]; + int readval; + while ((readval = read (fd, &buf, 8192)) > 0) { + write (STDOUT_FILENO, &buf, readval); + } + + free (f); + } + return rval; +} + int ras_shell_builtin_tapipe (int argc, char* argv[], RasShell* shell) { ListNode* iter = list_node_front (shell->saved_list); for (; iter != NULL; iter = list_next (iter)) { diff --git a/hw1/shell-builtin.h b/hw1/shell-builtin.h index 8fb1a77..9f0c086 100644 --- a/hw1/shell-builtin.h +++ b/hw1/shell-builtin.h @@ -26,6 +26,7 @@ RAS_SHELL_BUILTIN_DECL (pwd); RAS_SHELL_BUILTIN_DECL (set); RAS_SHELL_BUILTIN_DECL (setenv); RAS_SHELL_BUILTIN_DECL (shopt); +RAS_SHELL_BUILTIN_DECL (tacat); RAS_SHELL_BUILTIN_DECL (tapipe); RAS_SHELL_BUILTIN_DECL (umask); RAS_SHELL_BUILTIN_DECL (unset); diff --git a/hw1/shell.c b/hw1/shell.c index d5791ba..3531e9b 100644 --- a/hw1/shell.c +++ b/hw1/shell.c @@ -97,7 +97,6 @@ static void complete_string (Array*** ws, int* append, *ws[*append] = array_create (sizeof (char), 0); } } - printf ("%d\n", *append); *append = APPEND_STR; } @@ -613,6 +612,13 @@ static int ras_shell_init (RasShell* shell, int argc, char* argv[]) { } static void ras_shell_destroy (RasShell* shell) { + for (uint64_t i = 1; i <= shell->count; i++) { + char* f = xsprintf ("%s/%" PRIu64, shell->saved_tmpdir, i); + unlink (f); + free (f); + } + rmdir (shell->saved_tmpdir); + ListNode* iter = list_node_front (shell->saved_list); for (; iter != NULL; iter = list_next (iter)) { free (RAS_SHELL_SAVED (list_data (iter))->cmdorg); |