Thread: Pipelines and I/O redirections

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    10

    Pipelines and I/O redirections

    I have to extend and modify the program to:

    Redirect(>)standard output from a command to a file. If the file already exist, it will beerased and overwritten without warning.
    Examples:
    Code:
    COP$ ls > 1
    COP$ sort myshell.c > 2
    Append( >>)standard output from a command to a file if the file exists; if the file does not exist,create one. Examples:
    Code:
    COP$ sort myshell.c >> 1
    COP$ echo "Add A Line" >> 1
    Redirect the standard input to be from a file, rather than the keyboard. For example:

    Code:
    COP$ sort < myshell.c
    COP$ sort < myshell.c > 1
    COP$ sort > 1 < myshell.c
    and lastly pass ( |)the standard output of one command to another for further processing. Examples:
    Code:
    COP$ ls | sort
    COP$ sort < myshell.c | grep main | cat > output

    Heres what I got so far:
    Code:
    #include <stdio.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define MAX_ARGS 20
    #define BUFSIZ 1024
    
    
    int get_args(char* cmdline, char* args[])
    {
      int i = 0;
    
    
      /* if no args */
      if((args[0] = strtok(cmdline, "\n\t ")) == NULL)
        return 0;
    
    
      while((args[++i] = strtok(NULL, "\n\t ")) != NULL) {
        if(i >= MAX_ARGS) {
          printf("Too many arguments!\n");
          exit(1);
        }
      }
      /* the last one is always NULL */
      return i;
    }
    
    
    void execute(char* cmdline)
    {
      int pid, async;
      char* args[MAX_ARGS];
    
    
      int nargs = get_args(cmdline, args);
      if(nargs <= 0) return;
    
    
      if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit")) {
        exit(0);
      }
    
    
      /* check if async call */
      if(!strcmp(args[nargs-1], "&")) { async = 1; args[--nargs] = 0; }
      else async = 0;
    
    
      pid = fork();
      if(pid == 0) { /* child process */
        execvp(args[0], args);
        /* return only when exec fails */
        perror("exec failed");
        exit(-1);
      } else if(pid > 0) { /* parent process */
        if(!async) waitpid(pid, NULL, 0);
        else printf("this is an async call\n");
      } else { /* error occurred */
        perror("fork failed");
        exit(1);
      }
    }
    
    
    int main (int argc, char* argv [])
    {
      char cmdline[BUFSIZ];
    
    
      for(;;) {
        printf("COP4338$ ");
        if(fgets(cmdline, BUFSIZ, stdin) == NULL) {
          perror("fgets failed");
          exit(1);
        }
        execute(cmdline) ;
      }
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    dup2(2): duplicate file descriptor - Linux man page
    For the redirections, you basically
    open() a file for write, using O_CREAT depending on whether you have > or >>
    dup () the resulting descriptor to descriptor 1 (ie, stdout).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    Ill look into dup(), how would I check for > or >> or | to do the according process?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well that's just string parsing.

    command>file
    command >> file
    command -option argfile argfile "string with > embedded" > file


    Before you do much of anything else, you need to be able to reliably identify
    - the command
    - whether it has a redirection (>, >>, |)
    - the associated file or command for that redirection.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    Thank you Salem, you're the bomb.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unix Minishell Need Help Adding Pipelines
    By Scriptonaut in forum C Programming
    Replies: 7
    Last Post: 12-01-2012, 04:32 PM
  2. Pipelines - please help, 6 hours and I am losing my mind.
    By Johnny_010 in forum C Programming
    Replies: 4
    Last Post: 04-04-2012, 02:06 PM
  3. Handling I/O redirections in my shell.
    By sbho in forum Linux Programming
    Replies: 40
    Last Post: 04-14-2007, 03:24 AM
  4. Google Redirections
    By sean in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-27-2005, 05:39 PM

Tags for this Thread