xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/realpath.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate  * Copyright (c) 1987 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <strings.h>
29*0Sstevel@tonic-gate #include <sys/param.h>
30*0Sstevel@tonic-gate #include <errno.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate extern char	*getwd();
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /* LINTLIBRARY */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate  * Input name in raw, canonicalized pathname output to canon.  If dosymlinks
38*0Sstevel@tonic-gate  * is nonzero, resolves all symbolic links encountered during canonicalization
39*0Sstevel@tonic-gate  * into an equivalent symlink-free form.  Returns 0 on success, -1 on failure.
40*0Sstevel@tonic-gate  * The routine fails if the current working directory can't be obtained or if
41*0Sstevel@tonic-gate  * either of the arguments is NULL.
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * Sets errno on failure.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate int
pathcanon(raw,canon,dosymlinks)46*0Sstevel@tonic-gate pathcanon(raw, canon, dosymlinks)
47*0Sstevel@tonic-gate     char	*raw,
48*0Sstevel@tonic-gate 		*canon;
49*0Sstevel@tonic-gate     int		dosymlinks;
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate     register char	*s,
52*0Sstevel@tonic-gate 			*d;
53*0Sstevel@tonic-gate     register char	*limit = canon + MAXPATHLEN;
54*0Sstevel@tonic-gate     char		*modcanon;
55*0Sstevel@tonic-gate     int			nlink = 0;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate     /*
58*0Sstevel@tonic-gate      * Do a bit of sanity checking.
59*0Sstevel@tonic-gate      */
60*0Sstevel@tonic-gate     if (raw == NULL || canon == NULL) {
61*0Sstevel@tonic-gate 	errno = EINVAL;
62*0Sstevel@tonic-gate 	return (-1);
63*0Sstevel@tonic-gate     }
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate     /*
66*0Sstevel@tonic-gate      * If the path in raw is not already absolute, convert it to that form.
67*0Sstevel@tonic-gate      * In any case, initialize canon with the absolute form of raw.  Make
68*0Sstevel@tonic-gate      * sure that none of the operations overflow the corresponding buffers.
69*0Sstevel@tonic-gate      * The code below does the copy operations by hand so that it can easily
70*0Sstevel@tonic-gate      * keep track of whether overflow is about to occur.
71*0Sstevel@tonic-gate      */
72*0Sstevel@tonic-gate     s = raw;
73*0Sstevel@tonic-gate     d = canon;
74*0Sstevel@tonic-gate     if (*s != '/') {
75*0Sstevel@tonic-gate 	/* Relative; prepend the working directory. */
76*0Sstevel@tonic-gate 	if (getwd(d) == NULL) {
77*0Sstevel@tonic-gate 	    /* Use whatever errno value getwd may have left around. */
78*0Sstevel@tonic-gate 	    return (-1);
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	d += strlen(d);
81*0Sstevel@tonic-gate 	/* Add slash to separate working directory from relative part. */
82*0Sstevel@tonic-gate 	if (d < limit)
83*0Sstevel@tonic-gate 	    *d++ = '/';
84*0Sstevel@tonic-gate 	modcanon = d;
85*0Sstevel@tonic-gate     } else
86*0Sstevel@tonic-gate 	modcanon = canon;
87*0Sstevel@tonic-gate     while (d < limit && *s)
88*0Sstevel@tonic-gate 	*d++ = *s++;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate     /* Add a trailing slash to simplify the code below. */
91*0Sstevel@tonic-gate     s = "/";
92*0Sstevel@tonic-gate     while (d < limit && (*d++ = *s++))
93*0Sstevel@tonic-gate 	continue;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate     /*
97*0Sstevel@tonic-gate      * Canonicalize the path.  The strategy is to update in place, with
98*0Sstevel@tonic-gate      * d pointing to the end of the canonicalized portion and s to the
99*0Sstevel@tonic-gate      * current spot from which we're copying.  This works because
100*0Sstevel@tonic-gate      * canonicalization doesn't increase path length, except as discussed
101*0Sstevel@tonic-gate      * below.  Note also that the path has had a slash added at its end.
102*0Sstevel@tonic-gate      * This greatly simplifies the treatment of boundary conditions.
103*0Sstevel@tonic-gate      */
104*0Sstevel@tonic-gate     d = s = modcanon;
105*0Sstevel@tonic-gate     while (d < limit && *s) {
106*0Sstevel@tonic-gate 	if ((*d++ = *s++) == '/' && d > canon + 1) {
107*0Sstevel@tonic-gate 	    register char  *t = d - 2;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	    switch (*t) {
110*0Sstevel@tonic-gate 	    case '/':
111*0Sstevel@tonic-gate 		/* Found // in the name. */
112*0Sstevel@tonic-gate 		d--;
113*0Sstevel@tonic-gate 		continue;
114*0Sstevel@tonic-gate 	    case '.':
115*0Sstevel@tonic-gate 		switch (*--t) {
116*0Sstevel@tonic-gate 		case '/':
117*0Sstevel@tonic-gate 		    /* Found /./ in the name. */
118*0Sstevel@tonic-gate 		    d -= 2;
119*0Sstevel@tonic-gate 		    continue;
120*0Sstevel@tonic-gate 		case '.':
121*0Sstevel@tonic-gate 		    if (*--t == '/') {
122*0Sstevel@tonic-gate 			/* Found /../ in the name. */
123*0Sstevel@tonic-gate 			while (t > canon && *--t != '/')
124*0Sstevel@tonic-gate 			    continue;
125*0Sstevel@tonic-gate 			d = t + 1;
126*0Sstevel@tonic-gate 		    }
127*0Sstevel@tonic-gate 		    continue;
128*0Sstevel@tonic-gate 		default:
129*0Sstevel@tonic-gate 		    break;
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 		break;
132*0Sstevel@tonic-gate 	    default:
133*0Sstevel@tonic-gate 		break;
134*0Sstevel@tonic-gate 	    }
135*0Sstevel@tonic-gate 	    /*
136*0Sstevel@tonic-gate 	     * We're at the end of a component.  If dosymlinks is set
137*0Sstevel@tonic-gate 	     * see whether the component is a symbolic link.  If so,
138*0Sstevel@tonic-gate 	     * replace it by its contents.
139*0Sstevel@tonic-gate 	     */
140*0Sstevel@tonic-gate 	    if (dosymlinks) {
141*0Sstevel@tonic-gate 		char		link[MAXPATHLEN + 1];
142*0Sstevel@tonic-gate 		register int	llen;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		/*
145*0Sstevel@tonic-gate 		 * See whether it's a symlink by trying to read it.
146*0Sstevel@tonic-gate 		 *
147*0Sstevel@tonic-gate 		 * Start by isolating it.
148*0Sstevel@tonic-gate 		 */
149*0Sstevel@tonic-gate 		*(d - 1) = '\0';
150*0Sstevel@tonic-gate 		if ((llen = readlink(canon, link, sizeof link)) >= 0) {
151*0Sstevel@tonic-gate 		    /* Make sure that there are no circular links. */
152*0Sstevel@tonic-gate 		    nlink++;
153*0Sstevel@tonic-gate 		    if (nlink > MAXSYMLINKS) {
154*0Sstevel@tonic-gate 			errno = ELOOP;
155*0Sstevel@tonic-gate 			return (-1);
156*0Sstevel@tonic-gate 		    }
157*0Sstevel@tonic-gate 		    /*
158*0Sstevel@tonic-gate 		     * The component is a symlink.  Since its value can be
159*0Sstevel@tonic-gate 		     * of arbitrary size, we can't continue copying in place.
160*0Sstevel@tonic-gate 		     * Instead, form the new path suffix in the link buffer
161*0Sstevel@tonic-gate 		     * and then copy it back to its proper spot in canon.
162*0Sstevel@tonic-gate 		     */
163*0Sstevel@tonic-gate 		    t = link + llen;
164*0Sstevel@tonic-gate 		    *t++ = '/';
165*0Sstevel@tonic-gate 		    /*
166*0Sstevel@tonic-gate 		     * Copy the remaining unresolved portion to the end
167*0Sstevel@tonic-gate 		     * of the symlink. If the sum of the unresolved part and
168*0Sstevel@tonic-gate 		     * the readlink exceeds MAXPATHLEN, the extra bytes
169*0Sstevel@tonic-gate 		     * will be dropped off. Too bad!
170*0Sstevel@tonic-gate 		     */
171*0Sstevel@tonic-gate 		    (void) strncpy(t, s, sizeof link - llen - 1);
172*0Sstevel@tonic-gate 		    link[sizeof link - 1] = '\0';
173*0Sstevel@tonic-gate 		    /*
174*0Sstevel@tonic-gate 		     * If the link's contents are absolute, copy it back
175*0Sstevel@tonic-gate 		     * to the start of canon, otherwise to the beginning of
176*0Sstevel@tonic-gate 		     * the link's position in the path.
177*0Sstevel@tonic-gate 		     */
178*0Sstevel@tonic-gate 		    if (link[0] == '/') {
179*0Sstevel@tonic-gate 			/* Absolute. */
180*0Sstevel@tonic-gate 			(void) strcpy(canon, link);
181*0Sstevel@tonic-gate 			d = s = canon;
182*0Sstevel@tonic-gate 		    }
183*0Sstevel@tonic-gate 		    else {
184*0Sstevel@tonic-gate 			/*
185*0Sstevel@tonic-gate 			 * Relative: find beginning of component and copy.
186*0Sstevel@tonic-gate 			 */
187*0Sstevel@tonic-gate 			--d;
188*0Sstevel@tonic-gate 			while (d > canon && *--d != '/')
189*0Sstevel@tonic-gate 			    continue;
190*0Sstevel@tonic-gate 			s = ++d;
191*0Sstevel@tonic-gate 			/*
192*0Sstevel@tonic-gate 			 * If the sum of the resolved part, the readlink
193*0Sstevel@tonic-gate 			 * and the remaining unresolved part exceeds
194*0Sstevel@tonic-gate 			 * MAXPATHLEN, the extra bytes will be dropped off.
195*0Sstevel@tonic-gate 			*/
196*0Sstevel@tonic-gate 			if (strlen(link) >= (limit - s)) {
197*0Sstevel@tonic-gate 				(void) strncpy(s, link, limit - s);
198*0Sstevel@tonic-gate 				*(limit - 1) = '\0';
199*0Sstevel@tonic-gate 			} else {
200*0Sstevel@tonic-gate 				(void) strcpy(s, link);
201*0Sstevel@tonic-gate 			}
202*0Sstevel@tonic-gate 		    }
203*0Sstevel@tonic-gate 		    continue;
204*0Sstevel@tonic-gate 		} else {
205*0Sstevel@tonic-gate 		   /*
206*0Sstevel@tonic-gate 		    * readlink call failed. It can be because it was
207*0Sstevel@tonic-gate 		    * not a link (i.e. a file, dir etc.) or because the
208*0Sstevel@tonic-gate 		    * the call actually failed.
209*0Sstevel@tonic-gate 		    */
210*0Sstevel@tonic-gate 		    if (errno != EINVAL)
211*0Sstevel@tonic-gate 			return (-1);
212*0Sstevel@tonic-gate 		    *(d - 1) = '/';	/* Restore it */
213*0Sstevel@tonic-gate 		}
214*0Sstevel@tonic-gate 	    } /* if (dosymlinks) */
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate     } /* while */
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate     /* Remove the trailing slash that was added above. */
219*0Sstevel@tonic-gate     if (*(d - 1) == '/' && d > canon + 1)
220*0Sstevel@tonic-gate 	    d--;
221*0Sstevel@tonic-gate     *d = '\0';
222*0Sstevel@tonic-gate     return (0);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate /*
226*0Sstevel@tonic-gate  * Canonicalize the path given in raw, resolving away all symbolic link
227*0Sstevel@tonic-gate  * components.  Store the result into the buffer named by canon, which
228*0Sstevel@tonic-gate  * must be long enough (MAXPATHLEN bytes will suffice).  Returns NULL
229*0Sstevel@tonic-gate  * on failure and canon on success.
230*0Sstevel@tonic-gate  *
231*0Sstevel@tonic-gate  * The routine indirectly invokes the readlink() system call and getwd()
232*0Sstevel@tonic-gate  * so it inherits the possibility of hanging due to inaccessible file
233*0Sstevel@tonic-gate  * system resources.
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate char *
realpath(raw,canon)236*0Sstevel@tonic-gate realpath(raw, canon)
237*0Sstevel@tonic-gate     char	*raw;
238*0Sstevel@tonic-gate     char	*canon;
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate     return (pathcanon(raw, canon, 1) < 0 ? NULL : canon);
241*0Sstevel@tonic-gate }
242