I'm trying to extract the parameter with which an app was called by using the data inside cmdline.
If I start an application instance like this:
myapp 1 2
and then cat the cmdline of myapp I will see something like myapp12.
I needed to extract these values and I used this piece of code to do it
pid_t proc_id = getpid();
sprintf(buf,"/proc/%i/cmdline",proc_id);
FILE * pFile;
pFile = fopen (buf,"r");
if (pFile!=NULL)
{
fread(buf,100,100,pFile);
cout << "PID " << proc_id << endl;
string str = buf;
cout << buf << endl;
size_t found=str.find_last_of("/\\");
cout << " file: " << str.substr(found+1) << endl;
fclose (pFile);
}
But what I am getting is only the app name and no parameters...
Update coppied from answer:
well, my question now seems to be how do I read the cmdline file without it stopping at the first NULL character...
fopen(cmdline, "rb")
doesn't do anything else so...
/usr/bin/strings /proc/1337/cmdline
usually do the job for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With