Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one get the raw command line in C under Linux?

I'm attempting to create a simple C program to dump the raw command line to debug the output of programs that call other programs. Here is what I have so far:

#include <stdio.h> 

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

    for (i = 0; i < argc; i++)
        fprintf (stderr, "%s ", argv[i]);
    fputs ("\n", stderr);
    return (0);
}

There are several problems with this method. First of all, I have to insert a space manually after every argument. Secondly, the qouting on the original command like is lost, so with input like this:

./argvdump "'something'" """'"'""""other things""""'"'"""

I get output like this:

./argvdump something other things

which isn't very useful for debugging since I can't see what was actually on the command line.

Does anyone know how to get the actual raw command line?

like image 674
user2780684 Avatar asked Dec 03 '25 11:12

user2780684


1 Answers

The OS (or more specifically the shell) is the piece doing things like removing whitespace and quotes. There is no way to reconstruct the whole command line including all of those artifacts.

You could assume that if an argv member contains a space then originally it had quotes around it...

like image 186
John3136 Avatar answered Dec 05 '25 02:12

John3136



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!