1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 /*
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
34 *
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
38 */
39
40
41 #include <sys/types.h>
42 #include <sys/pathname.h>
43 #include <sys/kmem.h>
44 #include <sys/sysmacros.h>
45
46 /*
47 * Pathname utilities.
48 *
49 * In translating file names we copy each argument file
50 * name into a pathname structure where we operate on it.
51 * Each pathname structure can hold "pn_bufsize" characters
52 * including a terminating null, and operations here support
53 * allocating and freeing pathname structures, fetching
54 * strings from user space, getting the next character from
55 * a pathname, combining two pathnames (used in symbolic
56 * link processing), and peeling off the first component
57 * of a pathname.
58 */
59
60 /*
61 * Allocate contents of pathname structure. Structure is typically
62 * an automatic variable in calling routine for convenience.
63 *
64 * May sleep in the call to kmem_alloc() and so must not be called
65 * from interrupt level.
66 */
67 void
pn_alloc(struct pathname * pnp)68 pn_alloc(struct pathname *pnp)
69 {
70 pn_alloc_sz(pnp, MAXPATHLEN);
71 }
72 void
pn_alloc_sz(struct pathname * pnp,size_t sz)73 pn_alloc_sz(struct pathname *pnp, size_t sz)
74 {
75 pnp->pn_buf = kmem_alloc(sz, KM_SLEEP);
76 pnp->pn_bufsize = sz;
77 }
78
79 /*
80 * Free pathname resources.
81 */
82 void
pn_free(struct pathname * pnp)83 pn_free(struct pathname *pnp)
84 {
85 /* pn_bufsize is usually MAXPATHLEN, but may not be */
86 kmem_free(pnp->pn_buf, pnp->pn_bufsize);
87 pnp->pn_buf = NULL;
88 pnp->pn_bufsize = 0;
89 }
90