Site Loader

 #include  #include  #include  #include  #include   /*   Function Declarations for builtin shell commands:  */ int lsh_cd(char **args); int lsh_help(char **args); int lsh_exit(char **args);  /*   List of builtin commands, followed by their corresponding functions.  */ char *builtin_str = {   “cd”,   “help”,   “exit” };  int (*builtin_func) (char **) = {   &lsh_cd,   &lsh_help,   &lsh_exit };  int lsh_num_builtins() {   return sizeof(builtin_str) / sizeof(char *); }  /*   Builtin function implementations. */  /**    @brief Bultin command: change directory.    @param args List of args.  args0 is “cd”.  args1 is the directory.    @return Always returns 1, to continue executing.  */ int lsh_cd(char **args) {   if (args1 == NULL) {     fprintf(stderr, “lsh: expected argument to “cd”
“);   } else {     if (chdir(args1) != 0) {       perror(“lsh”);     }   }   return 1; }  /**    @brief Builtin command: print help.    @param args List of args.  Not examined.    @return Always returns 1, to continue executing.  */ int lsh_help(char **args) {   int i;   printf(“Stephen Brennan’s LSH
“);   printf(“Type program names and arguments, and hit enter.
“);   printf(“The following are built in:
“);    for (i = 0; i < lsh_num_builtins(); i++) {     printf("  %s ", builtin_stri);   }    printf("Use the man command for information on other programs. ");   return 1; }  /**    @brief Builtin command: exit.    @param args List of args.  Not examined.    @return Always returns 0, to terminate execution.  */ int lsh_exit(char **args) {   return 0; }  /**   @brief Launch a program and wait for it to terminate.   @param args Null terminated list of arguments (including program).   @return Always returns 1, to continue execution.  */ int lsh_launch(char **args) {   pid_t pid, wpid;   int status;    pid = fork();   if (pid == 0) {     // Child process     if (execvp(args0, args) == -1) {       perror("lsh");     }     exit(EXIT_FAILURE);   } else if (pid < 0) {     // Error forking     perror("lsh");   } else {     // Parent process     do {       wpid = waitpid(pid, &status, WUNTRACED);     } while (!WIFEXITED(status) && !WIFSIGNALED(status));   }    return 1; }  /**    @brief Execute shell built-in or launch program.    @param args Null terminated list of arguments.    @return 1 if the shell should continue running, 0 if it should terminate  */ int lsh_execute(char **args) {   int i;    if (args0 == NULL) {     // An empty command was entered.     return 1;   }    for (i = 0; i < lsh_num_builtins(); i++) {     if (strcmp(args0, builtin_stri) == 0) {       return (*builtin_funci)(args);     }   }    return lsh_launch(args); }  #define LSH_RL_BUFSIZE 1024 /**    @brief Read a line of input from stdin.    @return The line from stdin.  */ char *lsh_read_line(void) {   int bufsize = LSH_RL_BUFSIZE;   int position = 0;   char *buffer = malloc(sizeof(char) * bufsize);   int c;    if (!buffer) {     fprintf(stderr, "lsh: allocation error ");     exit(EXIT_FAILURE);   }    while (1) {     // Read a character     c = getchar();      // If we hit EOF, replace it with a null character and return.     if (c == EOF || c == ' ') {       bufferposition = '';       return buffer;     } else {       bufferposition = c;     }     position++;      // If we have exceeded the buffer, reallocate.     if (position >= bufsize) {       bufsize += LSH_RL_BUFSIZE;       buffer = realloc(buffer, bufsize);       if (!buffer) {         fprintf(stderr, “lsh: allocation error
“);         exit(EXIT_FAILURE);       }     }   } }  #define LSH_TOK_BUFSIZE 64 #define LSH_TOK_DELIM ”
a” /**    @brief Split a line into tokens (very naively).    @param line The line.    @return Null-terminated array of tokens.  */ char **lsh_split_line(char *line) {   int bufsize = LSH_TOK_BUFSIZE, position = 0;   char **tokens = malloc(bufsize * sizeof(char*));   char *token; +  if (!tokens) {+    fprintf(stderr, “lsh: allocation error
“);+    exit(EXIT_FAILURE);+  }+   token = strtok(line, LSH_TOK_DELIM);   while (token != NULL) {     tokensposition = token;     position++;      if (position >= bufsize) {       bufsize += LSH_TOK_BUFSIZE;       tokens = realloc(tokens, bufsize * sizeof(char*));       if (!tokens) {         fprintf(stderr, “lsh: allocation error
“);         exit(EXIT_FAILURE);       }     }      token = strtok(NULL, LSH_TOK_DELIM);   }   tokensposition = NULL;   return tokens; }  /**    @brief Loop getting input and executing it.  */ void lsh_loop(void) {   char *line;   char **args;   int status;    do {     printf(“> “);     line = lsh_read_line();     args = lsh_split_line(line);     status = lsh_execute(args);      free(line);     free(args);   } while (status); }  /**    @brief Main entry point.    @param argc Argument count.    @param argv Argument vector.    @return status code  */ int main(int argc, char **argv) {   // Load config files, if any.    // Run command loop.   lsh_loop();    // Perform any shutdown/cleanup.    return EXIT_SUCCESS; } 

Post Author: admin

x

Hi!
I'm Erica!

Would you like to get a custom essay? How about receiving a customized one?

Check it out