【问题标题】:How do I find a filename, given a FILE pointer? [duplicate]给定 FILE 指针,我如何找到文件名? [复制]
【发布时间】:2012-06-28 14:07:36
【问题描述】:

可能重复:
Getting Filename from file descriptor in C
Obtain filename from file pointer in C

我这里有个函数:

handle_file(FILE *file)
{
    if(condition)
    {
        print filename and other msg;
    }
}

我们在这里唯一知道的是指向文件的指针;是否可以根据指针获取文件名?

【问题讨论】:

  • 我相信这是不可能的。您应该单独跟踪文件名。
  • 不可能。无论如何check this
  • 我同意仅使用指向文件的指针是不可能的。也许尝试存储一个 char* 指针,该指针引用文件名,您首先从哪里获取文件。您应该能够在与 fopen 相同的块中执行此操作而不会遇到太多麻烦。
  • 对于 macOS,请参阅answerD.Nathanael 的另一个问题。

标签: c file


【解决方案1】:

查看this answer 以获取文件描述符和this answer 以从文件描述符中获取文件名。在 Linux 上应该没问题(不确定其他操作系统)。

这是一个快速工作示例(在 Cygwin/Win7 下测试):

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    int MAXSIZE = 0xFFF;
    char proclnk[0xFFF];
    char filename[0xFFF];
    FILE *fp;
    int fno;
    ssize_t r;

    // test.txt created earlier
    fp = fopen("test.txt", "r");
    if (fp != NULL)
    {
        fno = fileno(fp);
        sprintf(proclnk, "/proc/self/fd/%d", fno);
        r = readlink(proclnk, filename, MAXSIZE);
        if (r < 0)
        {
            printf("failed to readlink\n");
            exit(1);
        }
        filename[r] = '\0';
        printf("fp -> fno -> filename: %p -> %d -> %s\n",
                fp, fno, filename);
    }
    return 0;
}

输出:

fp -> fno -> filename: 0x80010294 -> 3 -> /tmp/test.txt

【讨论】:

  • 这适用于linux,windows有什么替代品吗?
  • 如果在#include 中定义了PATH_MAX,为什么MAXSIZE = 0xFFF?
【解决方案2】:

这可以分两个阶段完成。首先,您需要获取文件描述符,然后您需要恢复文件名。下面是一个例子,但是有一些严重的缓冲区溢出漏洞!

#include <stdio.h>
#include <unistd.h>

char * recover_filename(FILE * f) {
  int fd;
  char fd_path[255];
  char * filename = malloc(255);
  ssize_t n;

  fd = fileno(f);
  sprintf(fd_path, "/proc/self/fd/%d", fd);
  n = readlink(fd_path, filename, 255);
  if (n < 0)
      return NULL;
  filename[n] = '\0';
  return filename;
}

【讨论】:

    【解决方案3】:

    正如我搜索的那样,如果您是root并且您的系统中存在命令lsof,那么您可以使用fileno(fp)获取打开文件的描述符lsof -d descriptor。你可以试试看。

    【讨论】:

      【解决方案4】:

      否则,如果您想分析文件夹中的所有目录和文件,您可以使用 opendir 和 struct dirent。这不使用文件指针,但可能对您有用,具体取决于您的代码的工作方式。

      DIR *BaseDIR;
      struct dirent *curentDir;
      BaseDIR = opendir("path/to/the/directory");
      while((curentDir = readdir(BaseDIR)) != NULL){
            printf("directory or file name : %s\n",curentDir->d_name);
      }
      closedir(BaseDIR);
      

      【讨论】:

        【解决方案5】:

        我认为这是可能的。
        使用fileno(fp) 获取描述符。
        然后,您可以使用fstat() 函数。

        The fstat() function shall obtain information about an open file  asso‐
               ciated  with the file descriptor fildes, and shall write it to the area
               pointed to by buf.
        

        int fstat(int fildes, struct stat *buf);

        然后,您可以在*buf 中获得大量文件信息。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-18
        • 1970-01-01
        • 2017-11-16
        • 1970-01-01
        • 2015-07-09
        • 1970-01-01
        • 2010-12-18
        • 1970-01-01
        相关资源
        最近更新 更多