Originally Posted by
christop
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.)
Took me a few tries but I got that one to work for me which is good enough, here's the snippet:
Code:
space = &(glance->space);
key = &(glance->key);
val = &(glance->val);
memset( space->block, 0, space->given );
memset( key->block, 0, key->given );
memset( val->block, 0, val->given );
if ( !more_space( err, space, BUFSIZ ) ) {
fclose(file);
proc_glance_shut( glance );
return NULL;
}
while ( 1 ) {
if ( !fgets(space->block, space->given, file) ) {
fclose(file);
proc_glance_shut( glance );
return NULL;
}
if ( strstr( space->block, "\n" ) ||
strlen(space->block) < space->given - 1 )
break;
if ( !more_space( err, space, space->given += BUFSIZ ) ) {
fclose(file);
proc_glance_shut( glance );
return NULL;
}
fseek( file, 0, SEEK_SET );
}
if ( !more_space( err, key, space->given ) ) {
fclose(file);
proc_glance_shut( glance );
return NULL;
}
if ( !more_space( err, val, space->given ) ) {
fclose(file);
proc_glance_shut( glance );
return NULL;
}
(void)sscanf( space->block, "%s%*[\t]%s[^\n]",
(char*)(key->block), (char*)(val->block) );
notice->name = val->block;
notice->name_size = val->given;
Now I just gotta resolve an issue with filtering then find out how to get the parent id before I can move onto the main part of my little project (assuming I can resolve my GUI issues, consulting the moon libs developer about that atm)