模仿'找到' C 中的命令

发布于 2024-11-29 15:28:22 字数 70 浏览 1 评论 0原文

在 C 编程语言中执行此操作的最佳方法是什么?

find fileName

What would be the best way to do this in the C programming language?

find fileName

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花心好男孩 2024-12-06 15:28:23

您可以从分叉子进程调用 find 并从管道获取 find 的输出:

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

#define BUFSIZE 1000

int main(void) {
  int pfd[2], n;
  char str[BUFSIZE + 1];

  if (pipe(pfd) < 0) {
    printf("Oups, pipe failed.  Exiting\n");
    exit(-1);
  }

  n = fork();

  if (n < 0) {
    printf("Oups, fork failed.  Exiting\n");
    exit(-2);
  } else if (n == 0) {
    close(pfd[0]);

    dup2(pfd[1], 1);
    close(pfd[1]);

    execlp("find", "find", "filename", (char *) 0);
    printf("Oups, execlp failed.  Exiting\n"); /* This will be read by the  parent. */
    exit(-1); /* To avoid problem if execlp fails, especially if in a loop. */
  } else {
    close(pfd[1]);

    while ((n = read(pfd[0], str, BUFSIZE)) > 0) {
      str[n] = '\0';
      printf("%s", str);
    }

    close(pfd[0]);
    wait(&n); /* To avoid the zombie process. */

    if (n != 0) {
       printf("Oups, find or execlp failed.\n");
    }
  }
}

You could call find from a forked child process and get back find's output from a pipe:

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

#define BUFSIZE 1000

int main(void) {
  int pfd[2], n;
  char str[BUFSIZE + 1];

  if (pipe(pfd) < 0) {
    printf("Oups, pipe failed.  Exiting\n");
    exit(-1);
  }

  n = fork();

  if (n < 0) {
    printf("Oups, fork failed.  Exiting\n");
    exit(-2);
  } else if (n == 0) {
    close(pfd[0]);

    dup2(pfd[1], 1);
    close(pfd[1]);

    execlp("find", "find", "filename", (char *) 0);
    printf("Oups, execlp failed.  Exiting\n"); /* This will be read by the  parent. */
    exit(-1); /* To avoid problem if execlp fails, especially if in a loop. */
  } else {
    close(pfd[1]);

    while ((n = read(pfd[0], str, BUFSIZE)) > 0) {
      str[n] = '\0';
      printf("%s", str);
    }

    close(pfd[0]);
    wait(&n); /* To avoid the zombie process. */

    if (n != 0) {
       printf("Oups, find or execlp failed.\n");
    }
  }
}
椵侞 2024-12-06 15:28:23

这是一个复杂的话题。查看GNU libc 文档。然后尝试使用 scandir 扫描当前目录。如果可行,您可以实现递归版本,假设您正在讨论 UNIX find 命令并且想要对文件名进行递归搜索。

That's a complex topic. Have a look at the GNU libc documentation. Then try to scan the current directory using scandir. If that works, you can implement a recursive version, assuming you are talking about the UNIX find command and want to do recursive search for file names.

请爱~陌生人 2024-12-06 15:28:22

查找 POSIX 函数 nftw() 。它被设计为“新文件树遍历”功能。

有一个相关但不是立即有用的函数 scandir() 您可能会使用它。例如,选择函数可用于调用子目录的递归扫描,但 nftw() 可能更合适。

Look up the POSIX function nftw(). It is designed as a 'new file tree walk' function.

There's a related but not immediately as useful function scandir() which you might use. The selection function might be used to invoke a recursive scan on sub-directories, for example, but nftw() is probably more appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文