xref: /illumos-gate/usr/src/cmd/dfs.cmds/sharemgr/shareutil.c (revision a61ed2ce7a86a4d6428f2a83eb4739fae945447e)
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 #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 	char *optname;
50 	char *optvalue;
51 
52 	optlist = *optlistp;
53 	newopt = (struct options *)malloc(sizeof (struct options));
54 	if (newopt == NULL)
55 		return (OPT_ADD_MEMORY);
56 
57 	/* extract property/value pair */
58 	optname = optarg;
59 	if (!unset) {
60 		optvalue = strchr(optname, '=');
61 		if (optvalue == NULL) {
62 			free(newopt);
63 			return (OPT_ADD_SYNTAX);
64 		}
65 		*optvalue++ = '\0'; /* separate the halves */
66 	} else {
67 		optvalue = NULL;
68 	}
69 
70 	newopt->optname = optname;
71 	newopt->optvalue = optvalue;
72 	newopt->next = NULL;
73 	if (optlist == NULL) {
74 		optlist = newopt;
75 	} else {
76 		for (tmp = optlist; tmp->next != NULL;
77 		    tmp = tmp->next) {
78 			/*
79 			 * Check to see if this is a duplicate
80 			 * value. We want to replace the first
81 			 * instance with the second.
82 			 */
83 			if (strcmp(tmp->optname, optname) == 0) {
84 				tmp->optvalue = optvalue;
85 				free(newopt);
86 				goto done;
87 			}
88 		}
89 		tmp->next = newopt;
90 	}
91 done:
92 	*optlistp = optlist;
93 	return (OPT_ADD_OK);
94 }
95