xref: /illumos-gate/usr/src/cmd/dfs.cmds/sharemgr/shareutil.c (revision 34a0f871d192b33b865455a8812a3d34c1866315)
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 2006 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 #include <sys/types.h>
30 #include "sharemgr.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 /*
36  * Utility functions shared by sharemgr and sharectl.
37  */
38 
39 /*
40  * add_opt(optlist, optarg, security?)
41  *	Add a new parsed option to the option list provided.
42  *	If the option is a security option, only add if we are
43  *	processing security options.
44  */
45 int
46 add_opt(struct options **optlistp, char *optarg, int unset)
47 {
48 	struct options *newopt, *tmp, *optlist;
49 
50 	optlist = *optlistp;
51 	newopt = (struct options *)malloc(sizeof (struct options));
52 	if (newopt != NULL) {
53 		char *optname;
54 		char *optvalue;
55 
56 		/* extract property/value pair */
57 		optname = optarg;
58 		if (!unset) {
59 		    optvalue = strchr(optname, '=');
60 		    if (optvalue == NULL) {
61 			free(newopt);
62 			return (OPT_ADD_SYNTAX);
63 		    }
64 		    *optvalue++ = '\0'; /* separate the halves */
65 		} else {
66 		    optvalue = NULL;
67 		}
68 
69 		newopt->optname = optname;
70 		newopt->optvalue = optvalue;
71 		newopt->next = NULL;
72 		if (optlist == NULL) {
73 			optlist = newopt;
74 		} else {
75 			for (tmp = optlist; tmp->next != NULL;
76 			    tmp = tmp->next) {
77 			}
78 			tmp->next = newopt;
79 		}
80 		*optlistp = optlist;
81 		return (OPT_ADD_OK);
82 	}
83 	return (OPT_ADD_MEMORY);
84 }
85