forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_getcwd.c
More file actions
143 lines (120 loc) · 2.99 KB
/
Copy path_getcwd.c
File metadata and controls
143 lines (120 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* getcwd() - get the name of the current working directory.
* Author: Kees J. Bot
* 30 Apr 1989
*/
#define nil 0
#define chdir _chdir
#define closedir _closedir
#define getcwd _getcwd
#define opendir _opendir
#define readdir _readdir
#define rewinddir _rewinddir
#define stat _stat
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
static int addpath(const char *path, char **ap, const char *entry)
/* Add the name of a directory entry at the front of the path being built.
* Note that the result always starts with a slash.
*/
{
const char *e= entry;
char *p= *ap;
while (*e != 0) e++;
while (e > entry && p > path) *--p = *--e;
if (p == path) return -1;
*--p = '/';
*ap= p;
return 0;
}
static int recover(char *p)
/* Undo all those chdir("..")'s that have been recorded by addpath. This
* has to be done entry by entry, because the whole pathname may be too long.
*/
{
int e= errno, slash;
char *p0;
while (*p != 0) {
p0= ++p;
do p++; while (*p != 0 && *p != '/');
slash= *p; *p= 0;
if (chdir(p0) < 0) return -1;
*p= slash;
}
errno= e;
return 0;
}
char *getcwd(char *path, size_t size)
{
struct stat above, current, tmp;
struct dirent *entry;
DIR *d;
char *p, *up, *dotdot;
int cycle;
if (path == nil || size <= 1) { errno= EINVAL; return nil; }
p= path + size;
*--p = 0;
if (stat(".", ¤t) < 0) return nil;
while (1) {
dotdot= "..";
if (stat(dotdot, &above) < 0) { recover(p); return nil; }
if (above.st_dev == current.st_dev
&& above.st_ino == current.st_ino)
break; /* Root dir found */
if ((d= opendir(dotdot)) == nil) { recover(p); return nil; }
/* Cycle is 0 for a simple inode nr search, or 1 for a search
* for inode *and* device nr.
*/
cycle= above.st_dev == current.st_dev ? 0 : 1;
do {
char name[3 + NAME_MAX + 1];
tmp.st_ino= 0;
if ((entry= readdir(d)) == nil) {
switch (++cycle) {
case 1:
rewinddir(d);
continue;
case 2:
closedir(d);
errno= ENOENT;
recover(p);
return nil;
}
}
if (strcmp(entry->d_name, ".") == 0) continue;
if (strcmp(entry->d_name, "..") == 0) continue;
switch (cycle) {
case 0:
/* Simple test on inode nr. */
if (entry->d_ino != current.st_ino) continue;
/*FALL THROUGH*/
case 1:
/* Current is mounted. */
strcpy(name, "../");
strcpy(name+3, entry->d_name);
if (stat(name, &tmp) < 0) continue;
break;
}
} while (tmp.st_ino != current.st_ino
|| tmp.st_dev != current.st_dev);
up= p;
if (addpath(path, &up, entry->d_name) < 0) {
closedir(d);
errno = ERANGE;
recover(p);
return nil;
}
closedir(d);
if (chdir(dotdot) < 0) { recover(p); return nil; }
p= up;
current= above;
}
if (recover(p) < 0) return nil; /* Undo all those chdir("..")'s. */
if (*p == 0) *--p = '/'; /* Cwd is "/" if nothing added */
if (p > path) strcpy(path, p); /* Move string to start of path. */
return path;
}