Retrieve filename from file descriptor in C

前端 未结 7 1601
野性不改
野性不改 2020-11-22 12:48

Is it possible to get the filename of a file descriptor (Linux) in C?

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 13:43

    You can use fstat() to get the file's inode by struct stat. Then, using readdir() you can compare the inode you found with those that exist (struct dirent) in a directory (assuming that you know the directory, otherwise you'll have to search the whole filesystem) and find the corresponding file name. Nasty?

提交回复
热议问题