parsing command-line arguments from proc/pid/cmdline

Something like this, but with more error checking, should be a good start (this is more C than C++, aside from the cout bit):

const int BUFSIZE = 4096; // should really get PAGESIZE or something instead...
unsigned char buffer[BUFSIZE]; // dynamic allocation rather than stack/global would be better

int fd = open("/proc/self/cmdline", O_RDONLY);
int nbytesread = read(fd, buffer, BUFSIZE);
unsigned char *end = buffer + nbytesread;
for (unsigned char *p = buffer; p < end; /**/)
{ cout << p << endl;
  while (*p++); // skip until start of next 0-terminated section
}
close(fd);

In particular, open() and read() should be checked for error conditions, but I haven't shown that part... This may also fail in extreme cases where your command line is > 4096 characters long, or if for some other reason, read() doesn't read the file in one call, which shouldn't happen in current /proc implementations, but is not always guaranteed...

Tags:

C++