getcwd 是否有任何替代 API 来获取进程的工作目录

发布于 2024-11-16 20:42:37 字数 1539 浏览 3 评论 0原文

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

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

发布评论

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

评论(3

滥情哥ㄟ 2024-11-23 20:42:37

我不完全确定您将如何处理当前工作目录的结果,因为该目录仅在保持打开状态时才会继续存在 - 您无法在该目录中创建新文件,并且该目录必须为空,以便可以将其删除 - 但您可以/ 上使用 readlink(2) proc/self/cwd 来发现名称:

$ mkdir syedsma
$ cd syedsma/
$ /tmp/proccwd 
/proc/self/cwd reports: /tmp/syedsma
$ /tmp/getcwd 
getcwd: /tmp/syedsma
$ rmdir ../syedsma/
$ /tmp/getcwd
getcwd failed: No such file or directory
$ /tmp/proccwd 
/proc/self/cwd reports: /tmp/syedsma (deleted)
$ 

这是我的getcwd.c:

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

int main(int argc, char* argv[]) {
    char p[1000];
    char *r;
    r = getcwd(p, sizeof(p));
    if (!r)
        perror("getcwd failed");
    else
        printf("getcwd: %s\n", p);
    return 0;
}

这是我的proccwd.c:

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

int main(int argc, char* argv[]) {
    char buf[PATH_MAX];

    ssize_t r = readlink("/proc/self/cwd", buf, sizeof(buf));

    if (r < 0) {
        perror("readlink /proc/self/cwd failed");
        return 1;
    } else {
        buf[PATH_MAX-1] = '\0';
        printf("/proc/self/cwd reports: %s\n", buf);
    }
    return 0;
}

mu 太短 是正确的,他对 chdir("/"); 的建议是正确的,如果它是一个守护进程——我可以想象你可能有充分的理由让你的程序否则知道其当前工作目录,并且甚至知道如果路径名仍然存在的话它可能是什么——但一般来说,你不应该关心。路径名 "." 几乎在所有需要当前工作目录的情况下都有效,直到您需要为该路径实现内置的 pwd shell用户。

I'm not entirely sure what you will do with the results of the current working directory when the directory will continue to exist only as long as it is held open -- you can't create new files in the directory, and it had to be empty so it could be deleted -- but you can use readlink(2) on /proc/self/cwd to discover the name:

$ mkdir syedsma
$ cd syedsma/
$ /tmp/proccwd 
/proc/self/cwd reports: /tmp/syedsma
$ /tmp/getcwd 
getcwd: /tmp/syedsma
$ rmdir ../syedsma/
$ /tmp/getcwd
getcwd failed: No such file or directory
$ /tmp/proccwd 
/proc/self/cwd reports: /tmp/syedsma (deleted)
$ 

Here's my getcwd.c:

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

int main(int argc, char* argv[]) {
    char p[1000];
    char *r;
    r = getcwd(p, sizeof(p));
    if (!r)
        perror("getcwd failed");
    else
        printf("getcwd: %s\n", p);
    return 0;
}

And here's my proccwd.c:

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

int main(int argc, char* argv[]) {
    char buf[PATH_MAX];

    ssize_t r = readlink("/proc/self/cwd", buf, sizeof(buf));

    if (r < 0) {
        perror("readlink /proc/self/cwd failed");
        return 1;
    } else {
        buf[PATH_MAX-1] = '\0';
        printf("/proc/self/cwd reports: %s\n", buf);
    }
    return 0;
}

mu is too short is correct with his advice to chdir("/"); if it is a daemon -- I can imagine that you might have a good reason for your program to otherwise know its current working directory, and even have an idea of what the pathname might have been if it did still exist -- but in general, you shouldn't care. The pathname "." will work in just about every case where it makes sense to need the current working directory, until you need to implement a pwd shell built-in for the user.

生活了然无味 2024-11-23 20:42:37

试试这个。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main() {
  char path[256];
  char buf[BUFSIZ];

  mkdir("/tmp/foo", 0755);
  chdir("/tmp/foo");
  rmdir("/tmp/foo");

  sprintf(path, "/proc/%d/cwd", getpid());
  if (readlink(path, buf, sizeof(buf)) != -1) {
    char* stop = buf+strlen(buf)-10;
    if (!strcmp(stop, " (deleted)")) {
      *stop = 0;
    }
    printf("[%s]\n", buf);
  }
}

Try this.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main() {
  char path[256];
  char buf[BUFSIZ];

  mkdir("/tmp/foo", 0755);
  chdir("/tmp/foo");
  rmdir("/tmp/foo");

  sprintf(path, "/proc/%d/cwd", getpid());
  if (readlink(path, buf, sizeof(buf)) != -1) {
    char* stop = buf+strlen(buf)-10;
    if (!strcmp(stop, " (deleted)")) {
      *stop = 0;
    }
    printf("[%s]\n", buf);
  }
}
心房敞 2024-11-23 20:42:37

获取 $PWD 环境变量并将其缓存在您的应用程序中。

get the $PWD environment variable and cache it in your application.

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