Question

i want to change the source code of the netcat application on windows. The goal is to declare the arguments in the source code and build it, so i just need to start the program and it is working.

The first lines of the source code are the following:

main (argc, argv)
int argc;
char ** argv;

And I want to change it like

argv = "commandline arguments":

I've tried it (very long) with different solutions But it doesn't work I think I need a solution with a pointer... but of course I'm not sure.

Was it helpful?

Solution

You need to create an array of pointers to mutable strings, terminated with NULL. Remember also that argv[0] represents the program name, not a supplied argument. For example, if you want netcat to behave as if it was invoked with the command nc -l -p 80, you could do:

char arg0[] = "nc";
char arg1[] = "-l";
char arg2[] = "-p";
char arg3[] = "80";
char *fixed_argv[] = { arg0, arg1, arg2, arg3, NULL };

argc = 4;
argv = fixed_argv;

OTHER TIPS

argv holds an array of pointers to the strings themselves:

char *my_argv[] = {
    "arg 0",
    "arg 1",
    "arg 2",
    "...",
    "arg N",
    NULL
};

int my_argc = N;

From there, it's generally easiest to rename the existing main to some other name, then create a new main that calls the renamed version:

int main(){ 
    // argv & argc from above

    return renamed_main(my_argc, my_argv);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top