- return NULL;
-
- snprintf(linkpath, sizeof linkpath, "/proc/%d/fd/%d", tcp->pid, fd);
- n = readlink(linkpath, path, (sizeof path) - 1);
- if (n <= 0)
- return NULL;
- path[n] = '\0';
- return path;
-#else
- return NULL;
-#endif
+ return -1;
+
+ sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
+ n = readlink(linkpath, buf, bufsize - 1);
+ /*
+ * NB: if buf is too small, readlink doesn't fail,
+ * it returns truncated result (IOW: n == bufsize - 1).
+ */
+ if (n >= 0)
+ buf[n] = '\0';
+ return n;