File: realpath.c

package info (click to toggle)
fdclone 3.01b-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 4,772 kB
  • ctags: 8,674
  • sloc: ansic: 100,552; makefile: 4,497; sh: 1,480; sed: 224
file content (268 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download | duplicates (3)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 *	realpath.c
 *
 *	alternative pathname parser
 */

#ifdef	FD
#include "fd.h"
#else
#include "headers.h"
#include "depend.h"
#include "printf.h"
#include "kctype.h"
#include "string.h"
#endif

#include "sysemu.h"
#include "pathname.h"
#include "unixemu.h"
#include "realpath.h"

#if	MSDOS && !defined (FD)
#include "termio.h"
#endif
#ifdef	DEP_URLPATH
#include "url.h"
#endif

#if	defined (FD) && (defined (DEP_DOSDRIVE) || defined (DEP_FTPPATH))
extern int reallstat __P_((CONST char *, struct stat *));
#else
#define	reallstat		Xlstat
#endif

#if	MSDOS
# ifdef	FD
extern int setcurdrv __P_((int, int));
extern VOID error __P_((CONST char *));
extern VOID lostcwd __P_((char *));
# else
static VOID NEAR error __P_((CONST char *));
# endif
#endif	/* MSDOS */
#ifdef	DEP_DOSEMU
extern char *dosgetcwd __P_((char *, int));
#endif

#ifdef	FD
extern char fullpath[];
#endif
#ifdef	DEP_DOSEMU
extern int lastdrive;
#endif

#ifndef	NOSYMLINK
static int NEAR evallink __P_((char *, int, int));
#endif
static int NEAR _Xrealpath __P_((CONST char *, int, char *, int, int, int));

int norealpath = 0;


#if	MSDOS && !defined (FD)
static VOID NEAR error(s)
CONST char *s;
{
	VOID_C Xfprintf(Xstderr, "%s: Error(%d).\n", errno);
	exit(2);
}
#endif	/* MSDOS && !FD */

#ifndef	NOSYMLINK
static int NEAR evallink(path, tlen, rlen)
char *path;
int tlen, rlen;
{
	struct stat st;
	char buf[MAXPATHLEN];
	int n, duperrno;

	duperrno = errno;
	if (reallstat(path, &st) < 0) {
		errno = duperrno;
		return(-1);
	}
	if ((st.st_mode & S_IFMT) != S_IFLNK) {
		errno = duperrno;
		return(0);
	}
	if ((n = Xreadlink(path, buf, sizeof(buf) - 1)) < 0) {
		errno = duperrno;
		return(-1);
	}

	buf[n] = '\0';
	if (*buf != _SC_ && rlen) path[rlen] = '\0';
	else {
		copyrootpath(&(path[tlen]));
		rlen = tlen + 1;
	}
	rlen = _Xrealpath(buf, n, path, tlen, rlen, RLP_READLINK);
	errno = duperrno;

	return(rlen);
}
#endif	/* !NOSYMLINK */

/*ARGSUSED*/
static int NEAR _Xrealpath(path, plen, resolved, tlen, rlen, flags)
CONST char *path;
int plen;
char *resolved;
int tlen, rlen, flags;
{
#ifndef	NOSYMLINK
	int n;
#endif
	char *cp, *top;
	int len;

	if (plen <= 0) return(rlen);
	else if (path[0] != '.') /*EMPTY*/;
	else if (plen == 1) return(rlen);
	else if (path[1] != '.') /*EMPTY*/;
	else if (plen == 2) {
		top = &(resolved[tlen]);
		cp = strrdelim(top, 0);
		if (!cp || cp <= top) {
			copyrootpath(top);
			return(++tlen);
		}
		*cp = '\0';
		return(cp - top);
	}

	if ((cp = Xmemchr(path, _SC_, plen))) {
		len = cp++ - path;
		rlen = _Xrealpath(path, len, resolved, tlen, rlen, flags);
		plen -= ++len;
		return(_Xrealpath(cp, plen, resolved, tlen, rlen, flags));
	}

	len = Xsnprintf(&(resolved[rlen]), MAXPATHLEN - rlen,
		"%s%-.*s", (rlen > tlen + 1) ? _SS_ : nullstr, plen, path);
#ifdef	CODEEUC
	len = strlen(&(resolved[rlen]));
#endif
#ifndef	NOSYMLINK
	if ((flags & RLP_READLINK)
	&& (n = evallink(resolved, tlen, rlen)) > 0) {
		rlen = n;
		len = 0;
	}
#endif
	rlen += len;

	return(rlen);
}

char *Xrealpath(path, resolved, flags)
CONST char *path;
char *resolved;
int flags;
{
#ifdef	DEP_DOSEMU
	int duplastdrive;
#endif
#ifdef	DEP_PATHTOP
	int drv;
#endif
#if	MSDOS
	int drive;
#endif
	char *cp, tmp[MAXPATHLEN];
	int tlen, rlen;

	Xstrcpy(tmp, path);
	path = tmp;

	cp = NULL;
	tlen = rlen = 0;
#ifdef	DEP_DOSPATH
	drv = 0;
#endif
#ifdef	DEP_PATHTOP
	if (!(flags & RLP_PSEUDOPATH)
	&& (tlen = getpathtop(path, &drv, NULL))) {
# ifdef	DEP_DOSEMU
		if (drv) {
			if (path[tlen] != _SC_) {
				duplastdrive = lastdrive;
				lastdrive = drv;
				cp = dosgetcwd(resolved, MAXPATHLEN - 1);
				lastdrive = duplastdrive;
			}
		}
		else
# endif
		rlen = Xsnprintf(resolved, MAXPATHLEN,
			"%-.*s%c", tlen, path, _SC_);
		path += tlen;
		flags &= ~RLP_READLINK;
	}
	else
#endif	/* DEP_PATHTOP */
	{
#if	MSDOS
		tlen = 2;
		drive = dospath(nullstr, NULL);
		if ((drv = _dospath(path))) path += 2;
		else drv = drive;
#endif

		if (*path == _SC_) /*EMPTY*/;
#if	MSDOS
		else if (Xtoupper(drv) != Xtoupper(drive)) {
			if (setcurdrv(drv, 0) >= 0) {
				cp = Xgetwd(resolved);
				if (setcurdrv(drive, 0) < 0)
					error("setcurdrv()");
# ifdef	FD
				if (!cp) lostcwd(resolved);
# else
				if (!cp) /*EMPTY*/;
				else
# endif
				cp = resolved;
			}
		}
#endif	/* MSDOS */
#ifdef	FD
		else if (!(flags & RLP_READLINK)
		&& resolved != fullpath && *fullpath) {
			cp = Xstrcpy(resolved, fullpath);
# ifdef	DEP_PATHTOP
			tlen = getpathtop(resolved, NULL, NULL);
# endif
		}
#endif	/* FD */
		else cp = Xgetwd(resolved);
	}

	if (rlen > 0) {
#ifdef	CODEEUC
		rlen = strlen(resolved);
#else
		/*EMPTY*/;
#endif
	}
	else if (cp) rlen = strlen(cp);
#ifdef	DEP_DOSPATH
	else if (drv) {
		cp = gendospath(resolved, drv, _SC_);
		rlen = cp - resolved;
	}
#endif
	else {
		copyrootpath(resolved);
		rlen = 1;
	}

	norealpath++;
	VOID_C _Xrealpath(path, strlen(path),
		resolved, tlen, strlen(resolved), flags);
	norealpath--;

	return(resolved);
}