0

So, I have this command line I want to execute in C:

ps -eo user,pid,ppid 2> log.txt | grep user 2>>log.txt | sort -nk2 > out.txt

And I need to figure out how I'd make the code... fd What I understand is that I have the father ps... Which needs to redirect the output as an input to the grep, and it also needs to error output to log.txt...

The same with the grep... the output must be redirected to the sort, and the error must be saved in log.txt.

and I'd just output the sort to the file...

Something like this:

FATHER(ps)               SON(grep)         SON-SON? (sort)

0->must be closed  ----> 0           ----> 0
                  /                 /
1 ---------------/       1 --------/       1 -->out.txt

2 ---> log.txt           2 ---> log.txt    2 -->nowhere?

But I don't know how this would be coded... I'd appreciate your help.

magalenyo
  • 275
  • 6
  • 17
  • There are hundreds of questions in SO about how to implement pipelines in C, as well as many off-site tutorials. I don't see evidence that you've done any research on this yourself. – Barmar Apr 27 '16 at 20:14
  • @Barmar k bro, what about this... int main(){ int pipe0[2], pipe1[2], pid0, pid1; pipe(pipe0); pid0 = fork(); if (pid == 0){ close(1); dup(pipe0); fd_fitxer = open(“./out.txt”, O_WRONLY | O_CREAT | O_TRUNC, 00600); execl("sort","-nk2",">fd_fitxer"); pipe(pipe1); pid1 = fork(); if (pid1 == 0){ close(1); dup(pipe1); } } else if (pid == -1){ perror("ERROR AL SORT!\n"); exit(1); } close(1); //tancar entrada dup(fd0); return 0; } This is what I have, but still have no idea on how to keep on going. – magalenyo Apr 27 '16 at 20:19

1 Answers1

1

You can execute shell commands in C programs directly using sh -c

(How do I execute a Shell built-in command with a C function?)

Pipes can also be used in C programs directly using popen()

The following program example shows how to pipe the output of the ps -A command to the grep init command: ps -A | grep init

#include <stdio.h>
#include <stdlib.h>

int
main ()
{
  FILE *ps_pipe;
  FILE *grep_pipe;

  int bytes_read;
  int nbytes = 100;
  char *my_string;

  /* Open our two pipes */
  ps_pipe = popen ("ps -A", "r");
  grep_pipe = popen ("grep init", "w");

  /* Check that pipes are non-null, therefore open */
  if ((!ps_pipe) || (!grep_pipe))
    {
      fprintf (stderr,
               "One or both pipes failed.\n");
      return EXIT_FAILURE;
    }

  /* Read from ps_pipe until two newlines */
  my_string = (char *) malloc (nbytes + 1);
  bytes_read = getdelim (&my_string, &nbytes, "\n\n", ps_pipe);

  /* Close ps_pipe, checking for errors */
  if (pclose (ps_pipe) != 0)
    {
      fprintf (stderr,
               "Could not run 'ps', or other error.\n");
    }

  /* Send output of 'ps -A' to 'grep init', with two newlines */
  fprintf (grep_pipe, "%s\n\n", my_string);

  /* Close grep_pipe, checking for errors */
  if (pclose (grep_pipe) != 0)
    {
      fprintf (stderr,
               "Could not run 'grep', or other error.\n");
    }

  /* Exit! */
  return 0;
}

source: http://crasseux.com/books/ctutorial/Programming-with-pipes.html

else use named pipes (https://en.wikipedia.org/wiki/Named_pipe) http://www.cs.fredonia.edu/zubairi/s2k2/csit431/more_pipes.html

See also this C program to perform a pipe on three commands (uses fork())

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • I suspect this is homework in a class where he's supposed to make the pipes himself, not just execute the whole thing with a shell. – Barmar Apr 27 '16 at 20:14