There's also /proc/<PID>/exename if you only want the executable name:
Code:
$ cat /proc/self/exename
/usr/bin/bash
I think you'll have a problem finding the length of any file under /proc because their contents are generated on the fly by the kernel.
I would follow laserlight's advice of reading the file line by line and parsing it as you go (though with exename the contents is only the name of the executable, so there's no parsing involved). If you still want to parse /proc/<PID>/status, it's pretty easy to limit how many characters are read with scanf:
Code:
char buf[1000];
fgets(buf, sizeof buf, file);
sscanf(buf, "%100[^:]:%*[\t]%100[^\n]", key, value);
(Not tested.)
That will read a whole line and then limit the key (e.g., "Name") and value (e.g., "cat") to 100 characters each. It also properly handles spaces inside the executable name too, if the name happens to include spaces. (It assumes the key doesn't contain spaces, which is true for /proc/<PID>/status.)
(Of course, you'll need to check to see if fgets() read a complete line (if the file contains line longer than the buffer, then fgets() won't read the whole line). If the string doesn't end in a newline character, then the line in the file was too long. You'll have to decide how best to handle that situation.)