xref: /illumos-gate/usr/src/stand/lib/fs/nfs/pathname.c (revision d67944fb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d67944fbSScott Rotondo  * Common Development and Distribution License (the "License").
6*d67944fbSScott Rotondo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*d67944fbSScott Rotondo 
227c478bd9Sstevel@tonic-gate /*
23*d67944fbSScott Rotondo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
38*d67944fbSScott Rotondo #include <st_pathname.h>
397c478bd9Sstevel@tonic-gate #include <sys/promif.h>
407c478bd9Sstevel@tonic-gate #include <sys/salib.h>
417c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * Pathname utilities.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * In translating file names we copy each argument file
477c478bd9Sstevel@tonic-gate  * name into a pathname structure where we operate on it.
487c478bd9Sstevel@tonic-gate  * Each pathname structure can hold MAXPATHLEN characters
497c478bd9Sstevel@tonic-gate  * including a terminating null, and operations here support
507c478bd9Sstevel@tonic-gate  * fetching strings from user space, getting the next character from
517c478bd9Sstevel@tonic-gate  * a pathname, combining two pathnames (used in symbolic
527c478bd9Sstevel@tonic-gate  * link processing), and peeling off the first component
537c478bd9Sstevel@tonic-gate  * of a pathname.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	dprintf	if (boothowto & RB_DEBUG) printf
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * Setup contents of pathname structure. Warn about missing allocations.
607c478bd9Sstevel@tonic-gate  * Structure itself is typically automatic
617c478bd9Sstevel@tonic-gate  * variable in calling routine for convenience.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * NOTE: if buf is NULL, failure occurs.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate int
stpn_alloc(struct st_pathname * pnp)66*d67944fbSScott Rotondo stpn_alloc(struct st_pathname *pnp)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	if (pnp->pn_buf == NULL)
697c478bd9Sstevel@tonic-gate 		return (-1);
707c478bd9Sstevel@tonic-gate 	pnp->pn_path = (char *)pnp->pn_buf;
717c478bd9Sstevel@tonic-gate 	pnp->pn_pathlen = 0;
727c478bd9Sstevel@tonic-gate 	return (0);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Pull a pathname from user user or kernel space
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate int
stpn_get(char * str,struct st_pathname * pnp)79*d67944fbSScott Rotondo stpn_get(char *str, struct st_pathname *pnp)
807c478bd9Sstevel@tonic-gate {
81*d67944fbSScott Rotondo 	if (stpn_alloc(pnp) != 0)
827c478bd9Sstevel@tonic-gate 		return (-1);
837c478bd9Sstevel@tonic-gate 	bcopy(str, pnp->pn_path, strlen(str));
847c478bd9Sstevel@tonic-gate 	pnp->pn_pathlen = strlen(str);		/* don't count null byte */
857c478bd9Sstevel@tonic-gate 	return (0);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Set pathname to argument string.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate int
stpn_set(struct st_pathname * pnp,char * path)92*d67944fbSScott Rotondo stpn_set(struct st_pathname *pnp, char *path)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	pnp->pn_path = pnp->pn_buf;
957c478bd9Sstevel@tonic-gate 	pnp->pn_pathlen = strlen(pnp->pn_path); /* don't count null byte */
967c478bd9Sstevel@tonic-gate 	bcopy(pnp->pn_path, path, pnp->pn_pathlen);
977c478bd9Sstevel@tonic-gate 	return (0);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Combine two argument pathnames by putting
1027c478bd9Sstevel@tonic-gate  * second argument before first in first's buffer,
1037c478bd9Sstevel@tonic-gate  * and freeing second argument.
1047c478bd9Sstevel@tonic-gate  * This isn't very general: it is designed specifically
1057c478bd9Sstevel@tonic-gate  * for symbolic link processing.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate int
stpn_combine(struct st_pathname * pnp,struct st_pathname * sympnp)108*d67944fbSScott Rotondo stpn_combine(struct st_pathname *pnp, struct st_pathname *sympnp)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (pnp->pn_pathlen + sympnp->pn_pathlen >= MAXPATHLEN)
1127c478bd9Sstevel@tonic-gate 		return (ENAMETOOLONG);
1137c478bd9Sstevel@tonic-gate 	bcopy(pnp->pn_path, pnp->pn_buf + sympnp->pn_pathlen,
1147c478bd9Sstevel@tonic-gate 	    (uint_t)pnp->pn_pathlen);
1157c478bd9Sstevel@tonic-gate 	bcopy(sympnp->pn_path, pnp->pn_buf, (uint_t)sympnp->pn_pathlen);
1167c478bd9Sstevel@tonic-gate 	pnp->pn_pathlen += sympnp->pn_pathlen;
1177c478bd9Sstevel@tonic-gate 	pnp->pn_buf[pnp->pn_pathlen] = '\0';
1187c478bd9Sstevel@tonic-gate 	pnp->pn_path = pnp->pn_buf;
1197c478bd9Sstevel@tonic-gate 	return (0);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Get next component off a pathname and leave in
1247c478bd9Sstevel@tonic-gate  * buffer comoponent which should have room for
1257c478bd9Sstevel@tonic-gate  * NFS_MAXNAMLEN (1024) bytes and a null terminator character.
1267c478bd9Sstevel@tonic-gate  * If PEEK is set in flags, just peek at the component,
1277c478bd9Sstevel@tonic-gate  * i.e., don't strip it out of pnp.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate int
stpn_getcomponent(struct st_pathname * pnp,char * component,int flags)130*d67944fbSScott Rotondo stpn_getcomponent(struct st_pathname *pnp, char *component, int flags)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	char *cp;
1337c478bd9Sstevel@tonic-gate 	int l;
1347c478bd9Sstevel@tonic-gate 	int n;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	cp = pnp->pn_path;
1377c478bd9Sstevel@tonic-gate 	l = pnp->pn_pathlen;
1387c478bd9Sstevel@tonic-gate 	n = 1024;
1397c478bd9Sstevel@tonic-gate 	while ((l > 0) && (*cp != '/')) {
1407c478bd9Sstevel@tonic-gate 		if (--n < 0)
1417c478bd9Sstevel@tonic-gate 			return (ENAMETOOLONG);
1427c478bd9Sstevel@tonic-gate 		*component++ = *cp++;
1437c478bd9Sstevel@tonic-gate 		--l;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	if (!(flags & PN_PEEK)) {
1467c478bd9Sstevel@tonic-gate 		pnp->pn_path = cp;
1477c478bd9Sstevel@tonic-gate 		pnp->pn_pathlen = l;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	*component = 0;
1507c478bd9Sstevel@tonic-gate 	return (0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * skip over consecutive slashes in the pathname
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate void
stpn_skipslash(struct st_pathname * pnp)157*d67944fbSScott Rotondo stpn_skipslash(struct st_pathname *pnp)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	while ((pnp->pn_pathlen != 0) && (*pnp->pn_path == '/')) {
1607c478bd9Sstevel@tonic-gate 		pnp->pn_path++;
1617c478bd9Sstevel@tonic-gate 		pnp->pn_pathlen--;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * free pathname resources. This is a nop - the user of these
1677c478bd9Sstevel@tonic-gate  * routines is responsible for allocating and freeing their memory.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1707c478bd9Sstevel@tonic-gate void
stpn_free(struct st_pathname * pnp)171*d67944fbSScott Rotondo stpn_free(struct st_pathname *pnp)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	/* nop */
1747c478bd9Sstevel@tonic-gate 	dprintf("pn_free(): you shouldn't be calling pn_free()!\n");
1757c478bd9Sstevel@tonic-gate }
176