retire BIOS_SEG and umap_bios
[minix3.git] / lib / libc / sys-minix / __getcwd.c
blobaf4a60037cae36898ddc95052bf2fa239e27ae30
1 /* getcwd() - get the name of the current working directory.
2 * Author: Kees J. Bot
3 * 30 Apr 1989
4 */
6 #define chdir _chdir
7 #include <sys/cdefs.h>
8 #include "namespace.h"
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <dirent.h>
15 #include <limits.h>
16 #include <string.h>
18 /* libc-private interface */
19 int __getcwd(char *, size_t);
21 static int addpath(const char *path, char **ap, const char *entry)
22 /* Add the name of a directory entry at the front of the path being built.
23 * Note that the result always starts with a slash.
26 const char *e= entry;
27 char *p= *ap;
29 while (*e != 0) e++;
31 while (e > entry && p > path) *--p = *--e;
33 if (p == path) return -1;
34 *--p = '/';
35 *ap= p;
36 return 0;
39 static int recover(char *p)
40 /* Undo all those chdir("..")'s that have been recorded by addpath. This
41 * has to be done entry by entry, because the whole pathname may be too long.
44 int e= errno, slash;
45 char *p0;
47 while (*p != 0) {
48 p0= ++p;
50 do p++; while (*p != 0 && *p != '/');
51 slash= *p; *p= 0;
53 if (chdir(p0) < 0) return -1;
54 *p= slash;
56 errno= e;
57 return 0;
60 int __getcwd(char *path, size_t size)
62 struct stat above, current, tmp;
63 struct dirent *entry;
64 DIR *d;
65 char *p, *up;
66 const char *dotdot = "..";
67 int cycle;
69 if (path == NULL || size <= 1) { errno= EINVAL; return -1; }
71 p= path + size;
72 *--p = 0;
74 if (stat(".", &current) < 0) return -1;
76 while (1) {
77 if (stat(dotdot, &above) < 0) { recover(p); return -1; }
79 if (above.st_dev == current.st_dev
80 && above.st_ino == current.st_ino)
81 break; /* Root dir found */
83 if ((d= opendir(dotdot)) == NULL) { recover(p); return -1; }
85 /* Cycle is 0 for a simple inode nr search, or 1 for a search
86 * for inode *and* device nr.
88 cycle= above.st_dev == current.st_dev ? 0 : 1;
90 do {
91 char name[3 + NAME_MAX + 1];
93 tmp.st_ino= 0;
94 if ((entry= readdir(d)) == NULL) {
95 switch (++cycle) {
96 case 1:
97 rewinddir(d);
98 continue;
99 case 2:
100 closedir(d);
101 errno= ENOENT;
102 recover(p);
103 return -1;
106 if (strcmp(entry->d_name, ".") == 0) continue;
107 if (strcmp(entry->d_name, "..") == 0) continue;
109 switch (cycle) {
110 case 0:
111 /* Simple test on inode nr. */
112 if (entry->d_ino != current.st_ino) continue;
113 /*FALL THROUGH*/
115 case 1:
116 /* Current is mounted. */
117 strcpy(name, "../");
118 strcpy(name+3, entry->d_name);
119 if (stat(name, &tmp) < 0) continue;
120 break;
122 } while (tmp.st_ino != current.st_ino
123 || tmp.st_dev != current.st_dev);
125 up= p;
126 if (addpath(path, &up, entry->d_name) < 0) {
127 closedir(d);
128 errno = ERANGE;
129 recover(p);
130 return -1;
132 closedir(d);
134 if (chdir(dotdot) < 0) { recover(p); return -1; }
135 p= up;
137 current= above;
139 if (recover(p) < 0) return -1; /* Undo all those chdir("..")'s. */
140 if (*p == 0) *--p = '/'; /* Cwd is "/" if nothing added */
141 if (p > path) strcpy(path, p); /* Move string to start of path. */
142 return 0;