fzappaandthem
fzappaandthem

Reputation: 11

C use Linux shell functions

I need to make use of bash libraries, like sort for example and I wanted to know if I can use it in a C program. For instance I have this file.

(hola)

hola
adios
bye bye
adieu
aloha
goodbye
zap
random word
word random

when I execute in terminal

$ sort hola > holaOrdenado

$ cat holaOrdenado

prints...

adieu
adios
aloha
bye bye
goodbye
hola
random word
word random
zap

Is there a way I can make this exact command

$ sort hola > holaOrdenado

work inside a C program?

Upvotes: 0

Views: 68

Answers (3)

cdarke
cdarke

Reputation: 44394

You could use the standard library command system() to run your bash command (it is a bash command, because you have redirection), but...

There is a performance overhead in using a shell, since you now have two child processes. Using a shell can be open to security holes like shell shock.

While using system() might be OK for a "quick and dirty", you might wish to consider using the native APIs instead, which does not rely on a shell. It is a lot of work though, which explains why people wimp out and use system()!

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main (int argc, char *argv[])
{
  pid_t pid;

  switch (pid=fork())
  {
    case -1: perror("fork failed");
             exit(1);
    case  0:
             // In the child
             // redirect stdout (fd 1) to file
             {
                 int fd = open("holaOrdenado", O_CREAT|O_WRONLY, 0);
                 if (fd < 0)
                     perror("Failed to open holaOrdenado");
                 else
                     dup2(fd, 1);

                 execlp ("sort", "sort", "hola", NULL);
                 perror ("sort exec failed");
                 exit(2);
             }
    default:
        // In the parent
        {
            int status;
            if (wait(&status) > 0 && WIFEXITED(status)) {
                fprintf(stderr, "Child exited with status %d\n",
                                WEXITSTATUS(status));
            }
        }
  }
  return 0;
}

There is a lot of code there, and probably a lot of APIs that you have not seen before. Use the man pages for a full explanation, for example: man 2 write.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409384

The system function invokes a shell, so you can use full shell syntax including redirection and piping in the command you pass to it.

However, if you want to read the output of the command in your program, instead of redirecting to a file and then read that file, you may want to use popen instead. It too creates a shell for the command you pass, but it also have an argument to let you read the output from the command using a normal FILE pointer.

Upvotes: 2

alifirat
alifirat

Reputation: 2937

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



int main () {
  int i;
  printf ("Executing command ...\n");
  i=system ("sort hola > holaOrdenado");
  printf ("The value returned was: %d.\n",i);
  return 0;
}

Upvotes: 1

Related Questions