System() function in c

Do you want to execute a shell command in your program and check the status of the command? Do you want to create a child process and wait for that process to end? Do you want to check if the program has the access to the shell execution? You often have these requirements for your program and we have the solution.

C runtime library has an interesting function called system() to invoke commands in the shell. It takes commands and arguments in a string separated by spaces. We generally call fork() and then execxx() to spawn a new process. It does same and hopefully wraps those calls inside it. It also makes calling interface simpler and easy to use. We have next example to elaborate this.

System() function Description

The C library function int system(const char *command) is used to create a child process. A string containing the command name or program name followed by the arguments are passed to the function. This simple but effective function can be used to execute any command like pause, cls, echo etc.

System() - Declaration

Following is the declaration for system() function.


int system(const char *command);

System() - Parameters

command − This is the C string containing the command.

System() - Return Value

This function can return these following values:

User can pass command string as NULL. This function will return a nonzero value if shell is available else a value of zero will be returned.

If a child process could not be created, or its status could not be retrieved, the return value is -1 and errno is set to indicate the error.

If a shell could not be executed in the child process, then the return value is as though the child shell terminated by calling _exit(2) with the status 127.

If all system calls succeed, then the return value is the termination status of the child shell used to execute command. (The termination status of a shell is the termination status of the last command it executes.)

System function - check if shell exection available or not.


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

int main()
{
    int status;
    
    status = system(NULL);
    if (status)
      printf("Shell execution is supported.");
    else
      printf("Shell execution is not supported.");

    return 0;
}

Output:

Shell execution is supported.

System function in c example


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
int main (int argc, char * argv[])
{
  char * cmd_line;
  char * arg;
  int index;
  int len;
  int ret = -1;
  if (argc > 1)
  {
    index = 1;
    while (arg = argv [index])
    {
      len += strlen (arg);
      len ++;
      index ++;
    }
    cmd_line = (char *) calloc (1, len);
    if (cmd_line == NULL)
    {
        perror ("memory allocation error!\n");
        return -1;
    }
    index = 1;
    while (arg = argv [index])
    {
      index ++;
      strcat (cmd_line, arg);
      if (argv [index])
      {
        strcat (cmd_line," ");
      }
    }
    ret = system (cmd_line);
    free (cmd_line);
    return ret;
  }
  else
  {
    printf ("At least one argument needed.\n");
    printf ("Syntax: redo \n");
    return -1;
  }
}

Output:


$ ./redo ls -l redo.c
-rw-r--r-- 1 user root 771 Jul 15 14:07 redo.c
$
$ ./redo uname -a
Linux home 2.6.18-164.10.1 #1 SMP Mon May 3 17:50:00 PDT 2010 x86_64 GNU/Linux
$
$ ./redo ./redo
At least one argument needed.
Syntax: redo <args>
$ ./redo ./redo abcd
sh: abcd: command not found
$

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

Further readings

What is argc, argv in main function in c?[Explain]
Argc and argv are argument count and argument vector. Argc=number of arguments passed. Argv=Aarray of pointers, holds individual arguments. More..

Who calls main() function? What is C startup routine and assembly, explain?
Explains how control comes from C startup routine and assembly code to main routine. Setting up argc argv, environment variables before main

How does shell pass paramaters to my C program? [details of argv and environ]
How shell passes arguments and environment? Argc=number of arguments. Argv=Aarray of pointers, environ=Array of environment srtings. More..

How to install cleanup handler during exit using atexit?
How to install cleanup handler during exit using atexit?

#