top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can I use C system() to replace bash script?

+1 vote
382 views

I can write a C program with strings and to pass it to the system to avoid the ugly bash syntax?
My Program

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

static char command_name[2040] = {0};
int main(int argc, char** argv) {

    if ( argc != 2 ) {
        fprintf(stderr, "Provide command line argument for virtual image \n");
        exit(1);
    } else {
            sprintf(command_name, "qemu-kvm -m 1024 %s -netdev user,id=user.0 -device rtl8139,netdev=user.0",
                argv[1]);

    }

    return system(command_name);
}

Any suggestions are welcome?

posted Sep 2, 2015 by Ritika

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

yes you can do that.
as per the manual of system()

int system(const char *command);

You can see that, will will accept a command as string.
Just make sure your string is a valid command. If the string provided in system() is not valid then the system will return -1.

answer Sep 14, 2015 by Arshad Khan
Similar Questions
+4 votes

Lets assume I have a system with RAM of 1GB. and virtual memory is 500MB. That brings to 1.5GB i.e. 1500 MBytes.

I have read somewhere that when I process is created stack of 8MB is associated to that process. So, assuming that any of the process is not allocating any dynamic memory or anything, then does it mean that, Maximum number of process that i can create is 1500/8 and i.e. 187 Process.

Please clarify my understanding,

0 votes

There are two files.

    #include <stdio.h>
    void fun1(int i)
    {
            int a;
            printf("Enter the number\n");
            scanf("%d",&a);
            if(a==i)
                    printf("Equal\n");
            else
                    printf("Equal\n");
    }

    int main()
    {
        fun1(18);
    }

/* INPUT  */    
    18

What will be in file in OUTPUT file both cases.
Case-1:

./a.out < INPUT  > OUTPUT 

Case-2: Entered value is "18" from key board.

./a.out > output
+2 votes

Please share a sample program with detail code.

+1 vote

I am using for loop. In for loop there is one system call OR POSIX API (EX : read , write, fopen, fclose, open, close , pthread_create etc...)
How can I calculate the complexity of program. Is there any link where I can see code of system call or complexity ??

+4 votes

Code to write data in to file example.txt.. which is working fine

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

File : filelimit.c below which takes input as the compiled binary of above code
In this code i am setting the file size limit of process to 10 bytes and the above code is trying to write more than 10 bytes. According to below code if a process is trying to write more than 10 bytes to file it should send the signal SIGXFSZ (see more about this here)

It is not sending here... Why??

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

void handler(int sig)
{
    if(sig == SIGXFSZ)
        printf("Inside handler with correct signal\n");
    else
        printf("Inside handler with wrong signal\n");
}


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

    if(pid == 0)
    {
        signal(SIGXFSZ, handler);

        char command[256];
        scanf("%s", command);

        int ret;

        struct rlimit rl;
        rl.rlim_cur = 10;
        rl.rlim_max = 10;
        setrlimit (RLIMIT_FSIZE, &rl);

        char *envp[] = { NULL };
        char *argv[] = { command , NULL };
        ret = execve(command, argv, envp);

        while(1);
    }
    else
    {
        printf("%d - %d\n", getpid(), pid);
        signal(SIGXFSZ, handler);
        while(1);
    }
    return 0;
}
...