10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9694SScott.Rotondo@Sun.COM * Common Development and Distribution License (the "License").
6*9694SScott.Rotondo@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*9694SScott.Rotondo@Sun.COM
220Sstevel@tonic-gate /*
23*9694SScott.Rotondo@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate * under license from the Regents of the University of California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/errno.h>
38*9694SScott.Rotondo@Sun.COM #include <st_pathname.h>
390Sstevel@tonic-gate #include <sys/promif.h>
400Sstevel@tonic-gate #include <sys/salib.h>
410Sstevel@tonic-gate #include <sys/bootdebug.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * Pathname utilities.
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * In translating file names we copy each argument file
470Sstevel@tonic-gate * name into a pathname structure where we operate on it.
480Sstevel@tonic-gate * Each pathname structure can hold MAXPATHLEN characters
490Sstevel@tonic-gate * including a terminating null, and operations here support
500Sstevel@tonic-gate * fetching strings from user space, getting the next character from
510Sstevel@tonic-gate * a pathname, combining two pathnames (used in symbolic
520Sstevel@tonic-gate * link processing), and peeling off the first component
530Sstevel@tonic-gate * of a pathname.
540Sstevel@tonic-gate */
550Sstevel@tonic-gate
560Sstevel@tonic-gate #define dprintf if (boothowto & RB_DEBUG) printf
570Sstevel@tonic-gate
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate * Setup contents of pathname structure. Warn about missing allocations.
600Sstevel@tonic-gate * Structure itself is typically automatic
610Sstevel@tonic-gate * variable in calling routine for convenience.
620Sstevel@tonic-gate *
630Sstevel@tonic-gate * NOTE: if buf is NULL, failure occurs.
640Sstevel@tonic-gate */
650Sstevel@tonic-gate int
stpn_alloc(struct st_pathname * pnp)66*9694SScott.Rotondo@Sun.COM stpn_alloc(struct st_pathname *pnp)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate if (pnp->pn_buf == NULL)
690Sstevel@tonic-gate return (-1);
700Sstevel@tonic-gate pnp->pn_path = (char *)pnp->pn_buf;
710Sstevel@tonic-gate pnp->pn_pathlen = 0;
720Sstevel@tonic-gate return (0);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Pull a pathname from user user or kernel space
770Sstevel@tonic-gate */
780Sstevel@tonic-gate int
stpn_get(char * str,struct st_pathname * pnp)79*9694SScott.Rotondo@Sun.COM stpn_get(char *str, struct st_pathname *pnp)
800Sstevel@tonic-gate {
81*9694SScott.Rotondo@Sun.COM if (stpn_alloc(pnp) != 0)
820Sstevel@tonic-gate return (-1);
830Sstevel@tonic-gate bcopy(str, pnp->pn_path, strlen(str));
840Sstevel@tonic-gate pnp->pn_pathlen = strlen(str); /* don't count null byte */
850Sstevel@tonic-gate return (0);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * Set pathname to argument string.
900Sstevel@tonic-gate */
910Sstevel@tonic-gate int
stpn_set(struct st_pathname * pnp,char * path)92*9694SScott.Rotondo@Sun.COM stpn_set(struct st_pathname *pnp, char *path)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate pnp->pn_path = pnp->pn_buf;
950Sstevel@tonic-gate pnp->pn_pathlen = strlen(pnp->pn_path); /* don't count null byte */
960Sstevel@tonic-gate bcopy(pnp->pn_path, path, pnp->pn_pathlen);
970Sstevel@tonic-gate return (0);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * Combine two argument pathnames by putting
1020Sstevel@tonic-gate * second argument before first in first's buffer,
1030Sstevel@tonic-gate * and freeing second argument.
1040Sstevel@tonic-gate * This isn't very general: it is designed specifically
1050Sstevel@tonic-gate * for symbolic link processing.
1060Sstevel@tonic-gate */
1070Sstevel@tonic-gate int
stpn_combine(struct st_pathname * pnp,struct st_pathname * sympnp)108*9694SScott.Rotondo@Sun.COM stpn_combine(struct st_pathname *pnp, struct st_pathname *sympnp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (pnp->pn_pathlen + sympnp->pn_pathlen >= MAXPATHLEN)
1120Sstevel@tonic-gate return (ENAMETOOLONG);
1130Sstevel@tonic-gate bcopy(pnp->pn_path, pnp->pn_buf + sympnp->pn_pathlen,
1140Sstevel@tonic-gate (uint_t)pnp->pn_pathlen);
1150Sstevel@tonic-gate bcopy(sympnp->pn_path, pnp->pn_buf, (uint_t)sympnp->pn_pathlen);
1160Sstevel@tonic-gate pnp->pn_pathlen += sympnp->pn_pathlen;
1170Sstevel@tonic-gate pnp->pn_buf[pnp->pn_pathlen] = '\0';
1180Sstevel@tonic-gate pnp->pn_path = pnp->pn_buf;
1190Sstevel@tonic-gate return (0);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate * Get next component off a pathname and leave in
1240Sstevel@tonic-gate * buffer comoponent which should have room for
1250Sstevel@tonic-gate * NFS_MAXNAMLEN (1024) bytes and a null terminator character.
1260Sstevel@tonic-gate * If PEEK is set in flags, just peek at the component,
1270Sstevel@tonic-gate * i.e., don't strip it out of pnp.
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate int
stpn_getcomponent(struct st_pathname * pnp,char * component,int flags)130*9694SScott.Rotondo@Sun.COM stpn_getcomponent(struct st_pathname *pnp, char *component, int flags)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate char *cp;
1330Sstevel@tonic-gate int l;
1340Sstevel@tonic-gate int n;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate cp = pnp->pn_path;
1370Sstevel@tonic-gate l = pnp->pn_pathlen;
1380Sstevel@tonic-gate n = 1024;
1390Sstevel@tonic-gate while ((l > 0) && (*cp != '/')) {
1400Sstevel@tonic-gate if (--n < 0)
1410Sstevel@tonic-gate return (ENAMETOOLONG);
1420Sstevel@tonic-gate *component++ = *cp++;
1430Sstevel@tonic-gate --l;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate if (!(flags & PN_PEEK)) {
1460Sstevel@tonic-gate pnp->pn_path = cp;
1470Sstevel@tonic-gate pnp->pn_pathlen = l;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate *component = 0;
1500Sstevel@tonic-gate return (0);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * skip over consecutive slashes in the pathname
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate void
stpn_skipslash(struct st_pathname * pnp)157*9694SScott.Rotondo@Sun.COM stpn_skipslash(struct st_pathname *pnp)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate while ((pnp->pn_pathlen != 0) && (*pnp->pn_path == '/')) {
1600Sstevel@tonic-gate pnp->pn_path++;
1610Sstevel@tonic-gate pnp->pn_pathlen--;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * free pathname resources. This is a nop - the user of these
1670Sstevel@tonic-gate * routines is responsible for allocating and freeing their memory.
1680Sstevel@tonic-gate */
1690Sstevel@tonic-gate /*ARGSUSED*/
1700Sstevel@tonic-gate void
stpn_free(struct st_pathname * pnp)171*9694SScott.Rotondo@Sun.COM stpn_free(struct st_pathname *pnp)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate /* nop */
1740Sstevel@tonic-gate dprintf("pn_free(): you shouldn't be calling pn_free()!\n");
1750Sstevel@tonic-gate }
176