1 /* pwd.c - return the current working directory */
3 static char ident[] = "@(#)$Id: pwd.c,v 2.5 1992/12/15 00:20:22 jromine Exp $";
7 #if !defined (BSD42) && !defined (SYS5DIR)
8 #include "../h/local.h"
9 #endif /* not BSD42 and not SYS5DIR */
12 #define MAXPATHLEN 1024
14 static char curwd[MAXPATHLEN];
23 if (getwd (curwd) == NOTOK) {
24 admonish (NULL, "unable to determine working directory");
26 if (getcwd (curwd, MAXPATHLEN) == NULL) {
27 admonish (NULL, "unable to determine working directory");
30 if (getwd (curwd) == 0) {
31 admonish (NULLCP, "unable to determine working directory: %s", curwd);
35 || ((void) strcpy (curwd, mypath), chdir (curwd)) == NOTOK) {
36 (void) strcpy (curwd, "/");
42 if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
50 #if !defined (BSD42) && !defined (SYS5DIR)
51 /* getwd() - get the current working directory */
53 /* Algorithm from several sources, -ljobs, pwd.c, etc., etc. */
64 register struct direct *dp;
67 (void) strcpy (cwd, "/");
68 (void) stat ("/", &root);
71 if ((dd = opendir ("..")) == NULL)
73 if (stat (".", &st2) == NOTOK || stat ("..", &st1) == NOTOK)
75 if (st2.st_ino == root.st_ino && st2.st_dev == root.st_dev) {
80 if (st2.st_ino == st1.st_ino && st2.st_dev == st1.st_dev) {
83 if ((dd = opendir (".")) == NULL)
85 if (stat (".", &st1) < 0)
87 if (st2.st_dev != st1.st_dev)
88 while (dp = readdir (dd)) {
89 if (stat (dp -> d_name, &st1) == NOTOK)
91 if (st2.st_dev == st1.st_dev) {
92 (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
93 (void) strcpy (cwd + 1, tmp1);
100 return (chdir (cwd));
105 while (dp = readdir (dd)) {
106 (void) sprintf (tmp2, "../%s", dp -> d_name);
107 if (stat (tmp2, &st1) != NOTOK
108 && st1.st_ino == st2.st_ino
109 && st1.st_dev == st2.st_dev) {
113 (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
114 (void) strcpy (cwd + 1, tmp1);
126 #endif /* not BSD42 and not SYS5DIR */