I want to create a program in C language that would allow me to run a command in the terminal.

I have made a program in shell script which would give me the ip of any website that is opened in my browser. This shell script is executed by entering this command in the terminal:

sudo tcpdump -n dst port 80 -i eth

My professor told me to create a program in C language which would open the terminal and enter this command and then my shell script would work.

Please tell me how to create such a program.

3 Answers 3


you could use the system() function available in stdlib.h to run commands.


system() executes a command specified in string by calling /bin/sh -c string, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

You could read more about it over here http://linux.about.com/library/cmd/blcmdl3_system.htm

  • 1
    This is certainly one way, but you probably don't want to run commands from a c program, instead, most Linux programs have a lib that can just the programs' code directly. For example ssh can use libssh. There's usually a VERY limited reason to run system commands from code. If your going it a lot your almost certainly doing something wrong.
    – coteyr
    Dec 9, 2012 at 16:28
  • I made a program in C like this Dec 10, 2012 at 4:04

Hello i will write for you an example code, explain it to you and really hope this helps you. the function's prototype is something like:

int system(const char* cmd);

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

#define MAX_CMN_LEN 100

int main(int argc, char *argv[])
    char cmd[MAX_CMN_LEN] = "", **p;

    if (argc < 2) /*no command specified*/
        fprintf(stderr, "Usage: ./program_name terminal_command ...");
        strcat(cmd, argv[1]);
        for (p = &argv[2]; *p; p++)
            strcat(cmd, " ");
            strcat(cmd, *p);

    return 0;

1). open up a terminal and compile the program

2). run it (for example in Ubuntu) ./program_name comman_name -anything - anything

example: ./a.out locale -a

this example prints all locales supported by my compiler which is gcc.

more info:

p is a poniter to pointer to char (like argv is) p = &argv[2], points to -anything string i cat all -anythings to my cmd string i quit the loop when *p points to NULL look at this: -> i will use this symbol to say points to (dont confuse it with right arrow selection operator).

argv[0] -> program_name

argv[1] -> command_name (in this example command name will be locale, but enter the command you want to check instead)

argv[2] -> -anything (in this example -a, which is all locales)

argv[3] -> NULL (in this example, this quits the loop)

ok thats it, i think.

  • Make sure you don't use this as a setuid executable, because it has an obvious buffer-overflow exploit.
    – Martin
    Apr 30, 2017 at 3:30

I'm going to assume that this is about using a setuid-root binary to replace sudo, rather than just arbitrary command execution, so I'm going to include the other parts of the solution.

As a security matter, we avoid system() because it can be hijacked.

After compiling this, install the resulting binary as setuid-root.

#include <unistd.h>
#include <stdio.h>
#include <sysexits.h>

int main(int argc, char **argv) {
    if (argc > 2) { fputs("too many args\n", stderr); return EX_USAGE; }
    if (argc > 1 && argv[1][0] == '-') { fputs("no options allowed\n", stderr); return EX_USAGE; }
    if (geteuid() != 0) { fputs("not correctly installed as setuid-root\n",stderr); return EX_UNAVAILABLE; }
    if (setuid(0)  1) p[6] = argv[1];

    /* If all goes well, execv won't return
     * (because this program will have been replaced by tcpdump) */
    execv(p0, p);
    /* if we reach here then execv must have failed */

    return EX_OSFILE;

if you have saved this as foo.c, and want it installed as /usr/local/sbin/foo, run:

make foo && install -o root -m 4511 foo /usr/local/sbin/foo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .