xref: /titanic_41/usr/src/lib/lvm/libmeta/common/metasplitname.c (revision 0cfdb6036e046270988a17ac442e4d717d426a44)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * split and splice name
31  */
32 
33 #include <meta.h>
34 
35 int
36 splitname(char *name, md_splitname *spn)
37 {
38 	size_t prefixlen;
39 	size_t suffixlen;
40 	char	*lastslash;
41 	int	retval = METASPLIT_SUCCESS;
42 
43 	lastslash = strrchr(name, '/');
44 	if (lastslash != NULL) {
45 		prefixlen = lastslash - name;
46 		suffixlen = (strlen(name) - prefixlen) - 1; /* slash dropped */
47 	} else {
48 		prefixlen = 0;
49 		suffixlen = strlen(name);
50 	}
51 	if (prefixlen > MD_MAXPREFIX)
52 		return (METASPLIT_LONGPREFIX);
53 
54 	if (suffixlen > MD_MAXSUFFIX) {
55 		lastslash = META_LONGDISKNAME_STR;
56 		prefixlen = 0;
57 		suffixlen = strlen(lastslash);
58 		(void) memcpy(SPN_SUFFIX(spn).suf_data, lastslash, suffixlen);
59 		SPN_SUFFIX(spn).suf_len = suffixlen;
60 		retval = METASPLIT_LONGDISKNAME;
61 	} else {
62 		(void) memcpy(SPN_SUFFIX(spn).suf_data, lastslash + 1,
63 		    suffixlen);
64 		SPN_SUFFIX(spn).suf_len = suffixlen;
65 	}
66 
67 	(void) memcpy(SPN_PREFIX(spn).pre_data, name, prefixlen);
68 	SPN_PREFIX(spn).pre_len = prefixlen;
69 
70 	return (retval);
71 }
72 
73 char *
74 splicename(md_splitname *spn)
75 {
76 	char *name;
77 	char *suffix;
78 	size_t prefixlen;
79 	size_t suffixlen;
80 
81 	prefixlen = SPN_PREFIX(spn).pre_len;
82 	suffixlen = SPN_SUFFIX(spn).suf_len;
83 	name = Malloc(prefixlen + suffixlen + 2);
84 	(void) memcpy(name, SPN_PREFIX(spn).pre_data, prefixlen);
85 	name[prefixlen] = '/';
86 	suffix = name + (prefixlen + 1);
87 	(void) memcpy(suffix, SPN_SUFFIX(spn).suf_data, suffixlen);
88 	name[prefixlen + suffixlen + 1] = 0;
89 	return (name);
90 }
91