summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builtins.c2
-rw-r--r--type.c57
2 files changed, 57 insertions, 2 deletions
diff --git a/builtins.c b/builtins.c
index a1952fa..55d765e 100644
--- a/builtins.c
+++ b/builtins.c
@@ -79,6 +79,7 @@ int newgrp_main(int argc, char *argv[]);
int pwd_main(int argc, char *argv[]);
int read_main(int argc, char *argv[]);
int true_main(int argc, char *argv[]);
+int type_main(int argc, char *argv[]);
int ulimit_main(int argc, char *argv[]);
int umask_main(int argc, char *argv[]);
int unalias_main(int argc, char *argv[]);
@@ -103,6 +104,7 @@ static struct builtin regular_builtins[] = {
{ "pwd", pwd_main },
{ "read", read_main },
{ "true", true_main },
+ { "type", type_main },
{ "ulimit", ulimit_main },
{ "umask", umask_main },
{ "unalias", unalias_main },
diff --git a/type.c b/type.c
index b3f0bf5..4be22c1 100644
--- a/type.c
+++ b/type.c
@@ -1,7 +1,60 @@
+#include "sh.h"
#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
int type_main(int argc, char **argv)
{
- printf("Sorry, %s isn't implemented yet.\n", argv[0]);
- return argc;
+ int c;
+ while ((c = getopt(argc, argv, "")) != -1) {
+ switch (c) {
+ default:
+ return 1;
+ }
+ }
+
+ if (optind >= argc) {
+ fprintf(stderr, "type: missing operands\n");
+ return 1;
+ }
+
+ int ret = 0;
+ for (int i = optind; i < argc; i++) {
+ if (sh_is_special_builtin(argv[i])) {
+ printf("%s: special built-in\n", argv[i]);
+ continue;
+ }
+
+ /*
+ if (sh_is_function(argv[i])) {
+ printf("%s: function\n", argv[i]);
+ continue;
+ }
+ */
+
+ if (sh_is_regular_builtin(argv[i])) {
+ printf("%s: built-in\n", argv[i]);
+ continue;
+ }
+
+ char *alias = sh_get_alias(argv[i]);
+ if (alias != NULL) {
+ printf("%s: alias (%s)\n", argv[i], alias);
+ continue;
+ }
+
+ // keyword
+
+ char *path = sh_find_in_path(argv[i], "PATH");
+ if (path) {
+ printf("%s: %s\n", argv[i], path);
+ free(path);
+ continue;
+ }
+
+ fprintf(stderr, "type: %s not found\n", argv[i]);
+ ret = 1;
+ }
+
+ return ret;
}