2

I want to use Linux commands in C source code.

Can I use the System() function? Is this possible on Linux?

If I can't use System() function, what should I do? I want to "tar xvf example.tar".

1
  • 3
    Yes you can use system(), popen() or fork()/exec() the command. Commented Nov 30, 2012 at 2:31

2 Answers 2

3

You can use system() and exec() functions

Sign up to request clarification or add additional context in comments.

Comments

1

If you just want to execute shell command and not looking for any return value then you can use system() function call.

Using system call is not advisable (see system man page). I would recommend you using exec() which should be invoked in child process after doing fork().

The next alternative can be using popen().

    piff = (FILE *)popen("ls -l", "r");
    if (piff == (FILE *)0)
            return (-1);
    while ((i = read(fileno(piff), buff, sizeof (buf))) == -1) {
    if (errno != EINTR) {
        break;
    }
    (void)pclose(piff);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.