Thread: How to redirect using pipelines

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

    How to redirect using pipelines

    I am trying to implement the command
    Code:
    sort myshell.c  > 2
    in C.

    But I am having difficulties understanding how to use pipelines to do so.

    Code:
    int main(){
      char *args[] = {"sort", "myshell.c", ">", "2"};
      int size = sizeof(args)/sizeof(*args);
      int rw[2]//pipes
      int i;
      pipe(rw);
      FILE* file;
    
    
      for(i = 0; i < size; i++){
            if(!strcomp(args[i], ">")){
                    file = args[i+1];//getting the file name (in this case 2)
            }
      }
    
    
      if((pid = fork()) == 0){//child
    
    
            int fd = open(file, O_CREAT | O_RDWR);
    
    
      }else{//parent
    
    
      }
    
    
    
    
    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
    Are you trying to redirect to descriptor 2 (stderr), or just a file called "2" ?

    You're half-way there based on your previous thread.
    All you need now is to dup() fd onto descriptor 1 (stdout) or 2 (stderr) as you like.
    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
    Hey salem lol. glad to see you again. well the file is literally called "2". I just dont know how to sort "myshell.c" and putting that output in a file called "2"...

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    hum,
    it's not much but hope its helps a little, that compiles, and runs.
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
           
           
    int main(){
      char *args[] = {"sort", "myshell.c", ">", "2"};
      int size = sizeof(args)/sizeof(args[0]);
      int rw[2];//pipes
      int i;
      pipe(rw);
      //FILE* file;
     
      char file[30];
     
     
      for(i = 0; i < size; i++){
           // if(!strcomp(args[i], ">")){
            if(!strcmp(args[i], ">")){ //Linux not windows 
                    strcpy(file,args[i+1]);
                    printf("%s\n", file);
                    //file = args[i+1];//getting the file name (in this case 2)
            }
      }
     
     /*
     // not really sure what you're doing here 
       if((pid = fork()) == 0){//child
     
     
            int fd  = open(file, O_CREAT | O_RDWR);
     
     
      }else{//parent
        //printf("Parent\n");
     
     }
     */
     
     
    return 0;
    }
    fork
    The fork() System Call

    open
    http://www.techytalk.info/linux-syst...nd-write-file/

    redirect
    Code:
    userx@slackwhere:~
    $ touch 2
    userx@slackwhere:~
    $ echo "hey stdout" > 2
    userx@slackwhere:~
    $ cat 2
    hey stdout
    userx@slackwhere:~
    $ sort ~/bin/term2.c >> 2
    userx@slackwhere:~
    $ cat 2  
    hey stdout
        //printf("Parent\n");
     
     
     
     
     
     
     
     
     
     
     
           
           
                    //file = args[i+1];//getting the file name (in this case 2)
                    printf("%s\n", file);
                    strcpy(file,args[i+1]);
            if(!strcmp(args[i], ">")){ //Linux not windows 
            int fd  = open(file, O_CREAT | O_RDWR);
            }
           // if(!strcomp(args[i], ">")){
       if((pid = fork()) == 0){//child
      //FILE* file;
      char *args[] = {"sort", "myshell.c", ">", "2"};
      char file[30];
      for(i = 0; i < size; i++){
      int i;
      int rw[2];//pipes
      int size = sizeof(args)/sizeof(args[0]);
      pipe(rw);
      }
      }else{//parent
     */
     /*
     // not really sure what you're doing here 
     }
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    int main(){
    return 0;
    }
    Last edited by userxbw; 12-09-2017 at 10:58 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, you do this
    Code:
            if(!strcomp(args[i], ">")){
                    file = args[i+1];//getting the file name (in this case 2)
                    args[i] = NULL;
            }
    This is so you now have an argv compatible with the exec() functions.

    Then you call dup() to map stdout to the fd you just opened (see manual pages posted previously)

    Then finally, something like
    execvp( argv[0], argv );
    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.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    term2.c:26:22: error: assignment to expression with array type
                     file = args[i+1];//getting the file name (in this case 2)
    so if it is C then how can that be valid in windows? just wondering.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    To redirect stdout just close(1) before opening the file. Newly opened files always use the lowest available file descriptor, so it will be opened with fd 1.

  8. #8
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    I got it working guys. Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipelines and I/O redirections
    By codeshark in forum C Programming
    Replies: 4
    Last Post: 12-06-2017, 07:48 PM
  2. Unix Minishell Need Help Adding Pipelines
    By Scriptonaut in forum C Programming
    Replies: 7
    Last Post: 12-01-2012, 04:32 PM
  3. 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
  4. Redirect FAQ...
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 05-21-2007, 04:48 PM
  5. DLL Redirect
    By username in forum Windows Programming
    Replies: 5
    Last Post: 05-30-2003, 09:58 AM

Tags for this Thread