summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLAN-TW <lantw44@gmail.com>2013-11-10 23:00:06 +0800
committerLAN-TW <lantw44@gmail.com>2013-11-10 23:00:06 +0800
commit2d283af61c9ed5da47d33d0b59ed2814eb9e107e (patch)
treefd58f28b4529e6a631dd9948735df12cb9fd6340
parent731a3ed2ab7f6197dba5a2515b31ff698c4ead97 (diff)
downloadcn2013-2d283af61c9ed5da47d33d0b59ed2814eb9e107e.tar.gz
cn2013-2d283af61c9ed5da47d33d0b59ed2814eb9e107e.tar.zst
cn2013-2d283af61c9ed5da47d33d0b59ed2814eb9e107e.zip
HW1: 加入 false true tty type 四個指令並於提示列上顯示 return value
-rw-r--r--hw1/shell-builtin.c42
-rw-r--r--hw1/shell-builtin.h4
-rw-r--r--hw1/shell.c3
3 files changed, 48 insertions, 1 deletions
diff --git a/hw1/shell-builtin.c b/hw1/shell-builtin.c
index a39d191..79e5390 100644
--- a/hw1/shell-builtin.c
+++ b/hw1/shell-builtin.c
@@ -21,6 +21,7 @@ const RasShellBuiltin ras_shell_builtins[] = {
RAS_SHELL_BUILTIN_ENTRY (cd),
RAS_SHELL_BUILTIN_ENTRY (echo),
RAS_SHELL_BUILTIN_ENTRY (exit),
+ RAS_SHELL_BUILTIN_ENTRY (false),
RAS_SHELL_BUILTIN_ENTRY (logout),
RAS_SHELL_BUILTIN_ENTRY (printenv),
RAS_SHELL_BUILTIN_ENTRY (pwd),
@@ -29,6 +30,9 @@ const RasShellBuiltin ras_shell_builtins[] = {
RAS_SHELL_BUILTIN_ENTRY (shopt),
RAS_SHELL_BUILTIN_ENTRY (tacat),
RAS_SHELL_BUILTIN_ENTRY (tapipe),
+ RAS_SHELL_BUILTIN_ENTRY (true),
+ RAS_SHELL_BUILTIN_ENTRY (tty),
+ RAS_SHELL_BUILTIN_ENTRY (type),
RAS_SHELL_BUILTIN_ENTRY (umask),
RAS_SHELL_BUILTIN_ENTRY (unset),
RAS_SHELL_BUILTIN_ENTRY (unsetenv),
@@ -80,6 +84,10 @@ int ras_shell_builtin_exit (int argc, char* argv[], RasShell* shell) {
return 0;
}
+int ras_shell_builtin_false (int argc, char* argv[], RasShell* shell) {
+ return 1;
+}
+
int ras_shell_builtin_logout (int argc, char* argv[], RasShell* shell) {
if (!shell->attr_login) {
fprintf (stderr, "%s: not a login shell\n", argv[0]);
@@ -255,6 +263,40 @@ int ras_shell_builtin_tapipe (int argc, char* argv[], RasShell* shell) {
return 0;
}
+int ras_shell_builtin_true (int argc, char* argv[], RasShell* shell) {
+ return 0;
+}
+
+int ras_shell_builtin_tty (int argc, char* argv[], RasShell* shell) {
+ char* ttyn = ttyname (STDIN_FILENO);
+
+ if (ttyn == NULL) {
+ puts ("not a tty");
+ return 1;
+ }
+
+ puts (ttyn);
+ return 0;
+}
+
+int ras_shell_builtin_type (int argc, char* argv[], RasShell* shell) {
+ if (argv[1] == NULL) {
+ return 0;
+ }
+
+ for (int i = 0; ras_shell_builtins[i].cmd != NULL; i++) {
+ if (strcmp (ras_shell_builtins[i].cmd, argv[1]) == 0) {
+ printf ("%s is a shell builtin\n", argv[1]);
+ return 0;
+ }
+ }
+
+ printf (
+ "%s is not understood by the shell ... Is it an external command?\n",
+ argv[1]);
+ return 1;
+}
+
int ras_shell_builtin_unset (int argc, char* argv[], RasShell* shell) {
fputs ("This command has not been implemented. Try `unsetenv\'.\n", stderr);
return 1;
diff --git a/hw1/shell-builtin.h b/hw1/shell-builtin.h
index 9f0c086..5ebc06b 100644
--- a/hw1/shell-builtin.h
+++ b/hw1/shell-builtin.h
@@ -20,6 +20,7 @@ extern const RasShellBuiltin ras_shell_builtins[];
RAS_SHELL_BUILTIN_DECL (cd);
RAS_SHELL_BUILTIN_DECL (echo);
RAS_SHELL_BUILTIN_DECL (exit);
+RAS_SHELL_BUILTIN_DECL (false);
RAS_SHELL_BUILTIN_DECL (logout);
RAS_SHELL_BUILTIN_DECL (printenv);
RAS_SHELL_BUILTIN_DECL (pwd);
@@ -28,6 +29,9 @@ RAS_SHELL_BUILTIN_DECL (setenv);
RAS_SHELL_BUILTIN_DECL (shopt);
RAS_SHELL_BUILTIN_DECL (tacat);
RAS_SHELL_BUILTIN_DECL (tapipe);
+RAS_SHELL_BUILTIN_DECL (true);
+RAS_SHELL_BUILTIN_DECL (tty);
+RAS_SHELL_BUILTIN_DECL (type);
RAS_SHELL_BUILTIN_DECL (umask);
RAS_SHELL_BUILTIN_DECL (unset);
RAS_SHELL_BUILTIN_DECL (unsetenv);
diff --git a/hw1/shell.c b/hw1/shell.c
index cf71bd1..af8e5d5 100644
--- a/hw1/shell.c
+++ b/hw1/shell.c
@@ -695,7 +695,8 @@ int ras_shell_main (int argc, char* argv[]) {
char* user = shell.user_name == NULL ? "I have no name!" : shell.user_name;
char* cwd = xgetcwd ();
- printf ("%" PRIu64 ":%s[%s]%c ", shell.count, user, cwd, shell.user_ps);
+ printf ("%" PRIu64 ":%s[%s](%d)%c ", shell.count, user, cwd,
+ shell.last_rval, shell.user_ps);
fflush (stdout);
free (cwd);