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 #include <sys/types.h>
28 #include "sharemgr.h"
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 
33 /*
34  * Utility functions shared by sharemgr and sharectl.
35  */
36 
37 /*
38  * add_opt(optlist, optarg, security?)
39  *	Add a new parsed option to the option list provided.
40  *	If the option is a security option, only add if we are
41  *	processing security options.
42  */
43 int
add_opt(struct options ** optlistp,char * optarg,int unset)44 add_opt(struct options **optlistp, char *optarg, int unset)
45 {
46 	struct options *newopt, *tmp, *optlist;
47 	char *optname;
48 	char *optvalue;
49 
50 	optlist = *optlistp;
51 	newopt = (struct options *)malloc(sizeof (struct options));
52 	if (newopt == NULL)
53 		return (OPT_ADD_MEMORY);
54 
55 	/* extract property/value pair */
56 	optname = optarg;
57 	if (!unset) {
58 		optvalue = strchr(optname, '=');
59 		if (optvalue == NULL) {
60 			free(newopt);
61 			return (OPT_ADD_SYNTAX);
62 		}
63 		*optvalue++ = '\0'; /* separate the halves */
64 	} else {
65 		optvalue = NULL;
66 	}
67 
68 	newopt->optname = optname;
69 	newopt->optvalue = optvalue;
70 	newopt->next = NULL;
71 	if (optlist == NULL) {
72 		optlist = newopt;
73 	} else {
74 		for (tmp = optlist; tmp->next != NULL;
75 		    tmp = tmp->next) {
76 			/*
77 			 * Check to see if this is a duplicate
78 			 * value. We want to replace the first
79 			 * instance with the second.
80 			 */
81 			if (strcmp(tmp->optname, optname) == 0) {
82 				tmp->optvalue = optvalue;
83 				free(newopt);
84 				goto done;
85 			}
86 		}
87 		tmp->next = newopt;
88 	}
89 done:
90 	*optlistp = optlist;
91 	return (OPT_ADD_OK);
92 }
93