Updated docs/README-ATTACHMENTS, mainly to reflect that no setup is
[mmh] / docs / historical / mh-6.8.5 / sbr / pwd.c
1 /* pwd.c - return the current working directory */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: pwd.c,v 2.5 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #if     !defined (BSD42) && !defined (SYS5DIR)
8 #include "../h/local.h"
9 #endif  /* not BSD42 and not SYS5DIR */
10 #include <stdio.h>
11
12 #define MAXPATHLEN      1024
13
14 static char curwd[MAXPATHLEN];
15
16
17
18 char   *pwd () {
19     register char  *cp;
20
21 #ifndef BSD42
22 #ifndef SYS5DIR
23     if (getwd (curwd) == NOTOK) {
24         admonish (NULL, "unable to determine working directory");
25 #else   /* SYS5DIR */
26     if (getcwd (curwd, MAXPATHLEN) == NULL) {
27         admonish (NULL, "unable to determine working directory");
28 #endif  /* SYS5DIR */
29 #else   /* BSD42 */
30     if (getwd (curwd) == 0) {
31         admonish (NULLCP, "unable to determine working directory: %s", curwd);
32 #endif  /* BSD42 */
33         if (mypath == NULL
34                 || *mypath == 0
35                 || ((void) strcpy (curwd, mypath), chdir (curwd)) == NOTOK) {
36             (void) strcpy (curwd, "/");
37             (void) chdir (curwd);
38         }
39         return curwd;
40     }
41
42     if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
43         *cp = 0;
44
45     return curwd;
46 }
47
48 /* \f */
49
50 #if     !defined (BSD42) && !defined (SYS5DIR)
51 /* getwd() - get the current working directory */
52
53 /* Algorithm from several sources, -ljobs, pwd.c, etc., etc. */
54
55 getwd (cwd)
56 register char   *cwd;
57 {
58     int     found;
59     char    tmp1[BUFSIZ],
60             tmp2[BUFSIZ];
61     struct stat st1,
62                 st2,
63                 root;
64     register struct direct *dp;
65     register    DIR * dd;
66
67     (void) strcpy (cwd, "/");
68     (void) stat ("/", &root);
69
70     for (;;) {
71         if ((dd = opendir ("..")) == NULL)
72             return NOTOK;
73         if (stat (".", &st2) == NOTOK || stat ("..", &st1) == NOTOK)
74             goto out;
75         if (st2.st_ino == root.st_ino && st2.st_dev == root.st_dev) {
76             closedir (dd);
77             return chdir (cwd);
78         }
79
80         if (st2.st_ino == st1.st_ino && st2.st_dev == st1.st_dev) {
81             closedir (dd);
82             (void) chdir ("/");
83             if ((dd = opendir (".")) == NULL)
84                 return NOTOK;
85             if (stat (".", &st1) < 0)
86                 goto out;
87             if (st2.st_dev != st1.st_dev)
88                 while (dp = readdir (dd)) {
89                     if (stat (dp -> d_name, &st1) == NOTOK)
90                         goto out;
91                     if (st2.st_dev == st1.st_dev) {
92                         (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
93                         (void) strcpy (cwd + 1, tmp1);
94                         closedir (dd);
95                         return (chdir (cwd));
96                     }
97                 }
98             else {
99                 closedir (dd);
100                 return (chdir (cwd));
101             }
102         }
103
104         found = 0;
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) {
110                 closedir (dd);
111                 found++;
112                 (void) chdir ("..");
113                 (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
114                 (void) strcpy (cwd + 1, tmp1);
115                 break;
116             }
117         }
118         if (!found)
119             goto out;
120     }
121
122 out: ;
123     closedir (dd);
124     return NOTOK;
125 }
126 #endif  /* not BSD42 and not SYS5DIR */