etc/services - sync with NetBSD-8
[minix.git] / minix / lib / libc / sys / __getcwd.c
blob9949d481045f8703f9f7d825470b2d62637fcfeb
1 /* getcwd() - get the name of the current working directory.
2 * Author: Kees J. Bot
3 * 30 Apr 1989
4 */
6 #include <sys/cdefs.h>
7 #include "namespace.h"
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <errno.h>
12 #include <unistd.h>
13 #include <dirent.h>
14 #include <limits.h>
15 #include <string.h>
17 /* libc-private interface */
18 int __getcwd(char *, size_t);
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 int __getcwd(char *path, size_t size)
61 struct stat above, current, tmp;
62 struct dirent *entry;
63 DIR *d;
64 char *p, *up;
65 const char *dotdot = "..";
66 int cycle;
68 if (path == NULL || size <= 1) { errno= EINVAL; return -1; }
70 p= path + size;
71 *--p = 0;
73 if (stat(".", &current) < 0) return -1;
75 while (1) {
76 if (stat(dotdot, &above) < 0) { recover(p); return -1; }
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 -1; }
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 -1;
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 -1;
131 closedir(d);
133 if (chdir(dotdot) < 0) { recover(p); return -1; }
134 p= up;
136 current= above;
138 if (recover(p) < 0) return -1; /* 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 0;