linux - Getting length of /proc/self/exe symlink -


as mentioned on so, readlink on /proc/self/exe can used executable path on linux. man 2 readlink recommends 1 should use lstat extract required length of path. however, when stat /proc/self/exe, st_size member set 0. how can length allocating buffer?

in practice, tend use reasonable size (e.g. 256 or 1024, or path_max) readlink of /proc/*/exe (or /proc/self/exe)

the point always, executables supposed started humans, either path (for execvp(3) or shell) or entire file path human friendly. don't know people explicitly uses long filenames (not fitting in width in terminal screen). never heard of executable programs (or scripts) filename exceeds hundred of bytes.

so use local buffer of reasonable size (and perhaps strdup on success if needed). , readlink(2) returns number of meaningful bytes in buffer (so if care, grow buffer , make loop till fits).

for readlink of /proc/self/exe, 256 bytes buffer @ initialization, , abort (with meaningful error message) if not fit (or fail, e.g. because /proc/ not mounted).


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -