3 * pwd.c -- return the current working directory
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 static char curwd[PATH_MAX];
22 if (!getcwd (curwd, PATH_MAX)) {
23 admonish (NULL, "unable to determine working directory");
24 if (!mypath || !*mypath
25 || (strcpy (curwd, mypath), chdir (curwd)) == -1) {
32 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
42 * Currently commented out. Everyone seems
43 * to have a native version these days.
47 * getwd() - get the current working directory
54 char tmp1[BUFSIZ], tmp2[BUFSIZ];
55 struct stat st1, st2, root;
56 register struct direct *dp;
63 if ((dd = opendir ("..")) == NULL)
65 if (stat (".", &st2) == -1 || stat ("..", &st1) == -1)
67 if (st2.st_ino == root.st_ino && st2.st_dev == root.st_dev) {
72 if (st2.st_ino == st1.st_ino && st2.st_dev == st1.st_dev) {
75 if ((dd = opendir (".")) == NULL)
77 if (stat (".", &st1) < 0)
79 if (st2.st_dev != st1.st_dev)
80 while (dp = readdir (dd)) {
81 if (stat (dp->d_name, &st1) == -1)
83 if (st2.st_dev == st1.st_dev) {
84 snprintf (tmp1, sizeof(tmp1), "%s%s", dp->d_name, cwd);
85 strcpy (cwd + 1, tmp1);
97 while (dp = readdir (dd)) {
98 snprintf (tmp2, sizeof(tmp2), "../%s", dp->d_name);
99 if (stat (tmp2, &st1) != -1
100 && st1.st_ino == st2.st_ino
101 && st1.st_dev == st2.st_dev) {
105 snprintf (tmp1, sizeof(tmp1), "%s%s", dp->d_name, cwd);
106 strcpy (cwd + 1, tmp1);