//Add your code here #include #include #include #include #include #include #include #define MAXCOM 1000 // max number of letters to be supported #define MAX_ARGS 100 // max number of commands to be supported char allDirectories[30][30]; // contains all paths specified by user // function for finding ampersand (parallel commands) // XXX need work int findParallelCmds(char* str, char** strAnd) { int i; for (i = 0; i < 2; i++) { strAnd[i] = strsep(&str, "&"); if (strAnd[i] == NULL) break; } if (strAnd[1] == NULL) return 0; // returns zero if no pipe is found. else { return 1; } } // function to handle built-in commands // if 0 is returned, it means that none of // the built-in commands was executed int useBuiltInCmds(int argc, char **args) { int noOfSysCmds = 3, chosenCmd = 0; char* listOfSysCmds[noOfSysCmds]; listOfSysCmds[0] = "exit"; listOfSysCmds[1] = "cd"; listOfSysCmds[2] = "path"; for (int i = 0; i < noOfSysCmds; i++) { if (strcmp(args[0], listOfSysCmds[i]) == 0) { chosenCmd = i + 1; break; } } switch (chosenCmd) { case 1: if (argc > 1) { char error_message[30] = "An error has occurred\n"; write(STDERR_FILENO, error_message, strlen(error_message)); return 1; } exit(0); case 2: if (argc == 1 || argc > 2) { char error_message[30] = "An error has occurred\n"; write(STDERR_FILENO, error_message, strlen(error_message)); return 1; } chdir(args[1]); return 0; case 3: if (argc == 1) { int numPaths = sizeof(allDirectories) / sizeof(allDirectories[0]); for (int i=0; i "); char *line = NULL; // buffer to store each line read size_t len = 0; // length of the line read ssize_t read; // number of characters read by getline // take input line by line while ((read = getline(&line, &len, stdin)) != -1) { // remove newline character from command string if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } // copy to pointer str strcpy(str, line); // Free the line buffer free(line); // Reset the line pointer and size for the next line line = NULL; len = 0; break; } /* // check for parallel cmds if (findParallel(str)) { } else { } */ parseSpace(str, argsCounter, args); // run something // XXX useBuiltInCmds(argsCounter, args); } } // function to execute given batch file void takeBatchInput(char *inputFile) { FILE * fp = fopen(inputFile, "r"); if (fp == NULL) { char error_message[30] = "An error has occurred\n"; write(STDERR_FILENO, error_message, strlen(error_message)); } } int main(int argc, char *argv[]) { char str[MAXCOM]; // ptr to store command line char *args[MAX_ARGS]; // ptr to a list of arguments int argsCounter = 0; // number of arguments, including the cmd arg sprintf(allDirectories[0], "%s", "/bin"); // If given more than one command-line argument -> error if (argc > 2) { char error_message[30] = "An error has occurred\n"; write(STDERR_FILENO, error_message, strlen(error_message)); } // If given one argument -> prompt input else if (argc == 1) { takeStdInput(str, argsCounter, args); } // If given a batch file -> execute commands in file else { takeBatchInput(argv[1]); } return 0; }