/**************************************************************/ /* */ /* UNIX 1 / WS 92/93 Gruppe ux803 */ /* 6. Uebung - Aufgabe 4 - builtin.c */ /* */ /* Vorname Name Matrikelnr. */ /* --------- ------- ------------- */ /* Dietmar Dierks 125761 */ /* Roman Czyborra 127221 */ /* Torsten Buller 117894 */ /* Gerasimos Paliatsaras 140956 */ /* */ /**************************************************************/ #include "3/parser.h" /* struct kommando */ #include /* getenv */ #include /* MAXPATHLEN */ #include /* pw_dir */ #define EXIT 1 #define CHDIR 2 #define PWD 3 #define LISTALL 4 #define WAIT 5 #define MENU 6 #define DONE 1 #define NONE 0 static struct map { char * name; int function; } builtins [] = { {"exit", EXIT}, {"quit", EXIT}, {"cd", CHDIR}, {"pwd", PWD}, {"la", LISTALL}, {"wait", WAIT}, {"help", MENU}, {"menu", MENU}, {NONE, NONE} }; extern void do_ls(/* kp */); int do_builtin (kp) struct kommando * kp; { static struct map * mp; static char *home, buf [MAXPATHLEN]; if (!kp-> num_tok1) return DONE; for (mp = builtins; mp->name && strcmp (mp->name, kp->token_1[0]); ++mp); switch (mp -> function) { case EXIT: exit (0); case CHDIR: if (kp->num_tok1 > 1) chdir (kp->token_1[1]) == -1 ? perror ("cd") : 0; else { home = getenv ("HOME"); if (!home) home = getpwuid (getuid ()) -> pw_dir; chdir (home); } /* flow through */ case PWD: printf ("cwd=%s\n", getwd (buf)); return DONE; case LISTALL: do_ls (kp); return DONE; case WAIT: do_wait (kp->num_tok1 > 1 ? atoi (kp->token_1[1]) : -1, 1); return DONE; case MENU: printf ("help menu of msh builtin functions:\n"); for (mp = builtins; mp->name; ++mp) printf ("%s ", mp->name); printf ("\n"); return DONE; case NONE: return NONE; default: printf ("error in builtin's map!\n"); return DONE; } }