secondary cache feature in vm.
[minix.git] / lib / libc / posix / _getcwd.c
blob17117ca56c0160ab9e1f2c9dcc6fe89590b2c084
1 /* getcwd() - get the name of the current working directory.
2 * Author: Kees J. Bot
3 * 30 Apr 1989
4 */
5 #define chdir _chdir
6 #define closedir _closedir
7 #define getcwd _getcwd
8 #define opendir _opendir
9 #define readdir _readdir
10 #define rewinddir _rewinddir
11 #define stat _stat
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <dirent.h>
17 #include <limits.h>
18 #include <string.h>
20 static int addpath(const char *path, char **ap, const char *entry)
21 /* Add the name of a directory entry at the front of the path being built.
22 * Note that the result always starts with a slash.
25 const char *e= entry;
26 char *p= *ap;
28 while (*e != 0) e++;
30 while (e > entry && p > path) *--p = *--e;
32 if (p == path) return -1;
33 *--p = '/';
34 *ap= p;
35 return 0;
38 static int recover(char *p)
39 /* Undo all those chdir("..")'s that have been recorded by addpath. This
40 * has to be done entry by entry, because the whole pathname may be too long.
43 int e= errno, slash;
44 char *p0;
46 while (*p != 0) {
47 p0= ++p;
49 do p++; while (*p != 0 && *p != '/');
50 slash= *p; *p= 0;
52 if (chdir(p0) < 0) return -1;
53 *p= slash;
55 errno= e;
56 return 0;
59 char *getcwd(char *path, size_t size)
61 struct stat above, current, tmp;
62 struct dirent *entry;
63 DIR *d;
64 char *p, *up, *dotdot;
65 int cycle;
67 if (path == NULL || size <= 1) { errno= EINVAL; return NULL; }
69 p= path + size;
70 *--p = 0;
72 if (stat(".", &current) < 0) return NULL;
74 while (1) {
75 dotdot= "..";
76 if (stat(dotdot, &above) < 0) { recover(p); return NULL; }
78 if (above.st_dev == current.st_dev
79 && above.st_ino == current.st_ino)
80 break; /* Root dir found */
82 if ((d= opendir(dotdot)) == NULL) { recover(p); return NULL; }
84 /* Cycle is 0 for a simple inode nr search, or 1 for a search
85 * for inode *and* device nr.
87 cycle= above.st_dev == current.st_dev ? 0 : 1;
89 do {
90 char name[3 + NAME_MAX + 1];
92 tmp.st_ino= 0;
93 if ((entry= readdir(d)) == NULL) {
94 switch (++cycle) {
95 case 1:
96 rewinddir(d);
97 continue;
98 case 2:
99 closedir(d);
100 errno= ENOENT;
101 recover(p);
102 return NULL;
105 if (strcmp(entry->d_name, ".") == 0) continue;
106 if (strcmp(entry->d_name, "..") == 0) continue;
108 switch (cycle) {
109 case 0:
110 /* Simple test on inode nr. */
111 if (entry->d_ino != current.st_ino) continue;
112 /*FALL THROUGH*/
114 case 1:
115 /* Current is mounted. */
116 strcpy(name, "../");
117 strcpy(name+3, entry->d_name);
118 if (stat(name, &tmp) < 0) continue;
119 break;
121 } while (tmp.st_ino != current.st_ino
122 || tmp.st_dev != current.st_dev);
124 up= p;
125 if (addpath(path, &up, entry->d_name) < 0) {
126 closedir(d);
127 errno = ERANGE;
128 recover(p);
129 return NULL;
131 closedir(d);
133 if (chdir(dotdot) < 0) { recover(p); return NULL; }
134 p= up;
136 current= above;
138 if (recover(p) < 0) return NULL; /* Undo all those chdir("..")'s. */
139 if (*p == 0) *--p = '/'; /* Cwd is "/" if nothing added */
140 if (p > path) strcpy(path, p); /* Move string to start of path. */
141 return path;