xref: /titanic_41/usr/src/lib/lvm/libmeta/common/metasplitname.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
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 /*
24  * Copyright (c) 1992, 1993, 2000 by Sun Microsystems, Inc.
25  * All rights reserved.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * split and splice name
32  */
33 
34 #include <meta.h>
35 
36 int
37 splitname(char *name, md_splitname *spn)
38 {
39 	size_t prefixlen;
40 	size_t suffixlen;
41 	char	*lastslash;
42 	lastslash = strrchr(name, '/');
43 	if (lastslash != NULL) {
44 		prefixlen = lastslash - name;
45 		suffixlen = (strlen(name) - prefixlen) - 1; /* slash dropped */
46 	} else {
47 		prefixlen = 0;
48 		suffixlen = strlen(name);
49 	}
50 	if (prefixlen > MD_MAXPREFIX ||
51 	    suffixlen > MD_MAXSUFFIX)
52 		return (1);
53 	(void) memcpy(SPN_PREFIX(spn).pre_data, name, prefixlen);
54 	SPN_PREFIX(spn).pre_len = prefixlen;
55 	(void) memcpy(SPN_SUFFIX(spn).suf_data, lastslash + 1, suffixlen);
56 	SPN_SUFFIX(spn).suf_len = suffixlen;
57 	return (0);
58 }
59 
60 char *
61 splicename(md_splitname *spn)
62 {
63 	char *name;
64 	char *suffix;
65 	size_t prefixlen;
66 	size_t suffixlen;
67 
68 	prefixlen = SPN_PREFIX(spn).pre_len;
69 	suffixlen = SPN_SUFFIX(spn).suf_len;
70 	name = Malloc(prefixlen + suffixlen + 2);
71 	(void) memcpy(name, SPN_PREFIX(spn).pre_data, prefixlen);
72 	name[prefixlen] = '/';
73 	suffix = name + (prefixlen + 1);
74 	(void) memcpy(suffix, SPN_SUFFIX(spn).suf_data, suffixlen);
75 	name[prefixlen + suffixlen + 1] = 0;
76 	return (name);
77 }
78