F GETPATH
From Attie's Wiki
(Difference between revisions)
m |
m |
||
(2 intermediate revisions by one user not shown) | |||
Line 2: | Line 2: | ||
<source lang="c"> | <source lang="c"> | ||
− | char fname[PATH_MAX]; | + | char es[1024]; |
+ | |||
+ | int getpath(int fd) { | ||
+ | static char fname[PATH_MAX]; | ||
#if 0 | #if 0 | ||
/* this would be nice... */ | /* this would be nice... */ | ||
if (fcntl(fd, F_GETPATH, fname) == -1) { | if (fcntl(fd, F_GETPATH, fname) == -1) { | ||
− | strerror_r(errno, es, | + | strerror_r(errno, es, sizeof(es)); |
fprintf(stderr, "fcntl(): %s\n", es); | fprintf(stderr, "fcntl(): %s\n", es); | ||
return -1; | return -1; | ||
Line 16: | Line 19: | ||
snprintf(t, sizeof(t), "/proc/self/fd/%d", fd); | snprintf(t, sizeof(t), "/proc/self/fd/%d", fd); | ||
if (readlink(t, fname, sizeof(fname)) == -1) { | if (readlink(t, fname, sizeof(fname)) == -1) { | ||
− | strerror_r(errno, es, | + | strerror_r(errno, es, sizeof(es)); |
fprintf(stderr, "readlink(%s): %s\n", t, es); | fprintf(stderr, "readlink(%s): %s\n", t, es); | ||
return -1; | return -1; | ||
Line 22: | Line 25: | ||
if (stat(fname, &a) == -1) { | if (stat(fname, &a) == -1) { | ||
− | strerror_r(errno, es, | + | strerror_r(errno, es, sizeof(es)); |
fprintf(stderr, "stat(%s): %s\n", fname, es); | fprintf(stderr, "stat(%s): %s\n", fname, es); | ||
return -1; | return -1; | ||
} | } | ||
if (stat(t, &b) == -1) { | if (stat(t, &b) == -1) { | ||
− | strerror_r(errno, es, | + | strerror_r(errno, es, sizeof(es)); |
fprintf(stderr, "stat(%s): %s\n", t, es); | fprintf(stderr, "stat(%s): %s\n", t, es); | ||
return -1; | return -1; | ||
Line 33: | Line 36: | ||
if (a.st_dev != b.st_dev || a.st_ino != b.st_ino) { | if (a.st_dev != b.st_dev || a.st_ino != b.st_ino) { | ||
− | fprintf(stderr, " | + | fprintf(stderr, "getpath(): file lost...\n"); |
return -1; | return -1; | ||
} | } | ||
} | } | ||
#endif | #endif | ||
+ | return 0; | ||
+ | } | ||
</source> | </source> |
Latest revision as of 09:33, 23 December 2013
This is untested code...
char es[1024]; int getpath(int fd) { static char fname[PATH_MAX]; #if 0 /* this would be nice... */ if (fcntl(fd, F_GETPATH, fname) == -1) { strerror_r(errno, es, sizeof(es)); fprintf(stderr, "fcntl(): %s\n", es); return -1; } #else { const char t[PATH_MAX]; struct stat a, b; snprintf(t, sizeof(t), "/proc/self/fd/%d", fd); if (readlink(t, fname, sizeof(fname)) == -1) { strerror_r(errno, es, sizeof(es)); fprintf(stderr, "readlink(%s): %s\n", t, es); return -1; } if (stat(fname, &a) == -1) { strerror_r(errno, es, sizeof(es)); fprintf(stderr, "stat(%s): %s\n", fname, es); return -1; } if (stat(t, &b) == -1) { strerror_r(errno, es, sizeof(es)); fprintf(stderr, "stat(%s): %s\n", t, es); return -1; } if (a.st_dev != b.st_dev || a.st_ino != b.st_ino) { fprintf(stderr, "getpath(): file lost...\n"); return -1; } } #endif return 0; }