Sujet : Re: Command line globber/tokenizer library for C?
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.cDate : 12. Sep 2024, 16:08:24
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vbv04v$aci0$1@raubtier-asyl.eternal-september.org>
References : 1
User-Agent : Mozilla Thunderbird
Am 10.09.2024 um 21:01 schrieb Ted Nolan <tednolan>:
I have the case where my C program is handed a string which is basically
a command line.
I tried to experiment with that with /proc/<pid>/cmdline. The first
problem was that the arguments aren't space delimited, but broken up
with zeroes. The second problem was the cmdline-file doesn't contain
the original commandline but with expanded files.
This was my code so far:
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <unistd.h>
using namespace std;
int main()
{
pid_t pid = getpid();
string cmdLineFile( (ostringstream() << "/proc/" << pid << "/cmdline").str() );
ifstream ifs;
ifs.exceptions( ifstream::failbit | ifstream::badbit );
ifs.open( cmdLineFile );
string fullCmdLine;
ifs >> fullCmdLine;
ifs.close();
replace( fullCmdLine.begin(), fullCmdLine.end(), (char)0, (char)' ' );
cout << fullCmdLine << endl;
}