16185db8dougm/*
26185db8dougm * CDDL HEADER START
36185db8dougm *
46185db8dougm * The contents of this file are subject to the terms of the
56185db8dougm * Common Development and Distribution License (the "License").
66185db8dougm * You may not use this file except in compliance with the License.
76185db8dougm *
86185db8dougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96185db8dougm * or http://www.opensolaris.org/os/licensing.
106185db8dougm * See the License for the specific language governing permissions
116185db8dougm * and limitations under the License.
126185db8dougm *
136185db8dougm * When distributing Covered Code, include this CDDL HEADER in each
146185db8dougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156185db8dougm * If applicable, add the following below this CDDL HEADER, with the
166185db8dougm * fields enclosed by brackets "[]" replaced with your own identifying
176185db8dougm * information: Portions Copyright [yyyy] [name of copyright owner]
186185db8dougm *
196185db8dougm * CDDL HEADER END
206185db8dougm */
216185db8dougm
226185db8dougm/*
23546405cdougm * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246185db8dougm * Use is subject to license terms.
256185db8dougm */
266185db8dougm
276185db8dougm#pragma ident	"%Z%%M%	%I%	%E% SMI"
286185db8dougm
296185db8dougm#include <sys/types.h>
306185db8dougm#include "sharemgr.h"
316185db8dougm#include <stdlib.h>
326185db8dougm#include <stdio.h>
336185db8dougm#include <string.h>
346185db8dougm
356185db8dougm/*
366185db8dougm * Utility functions shared by sharemgr and sharectl.
376185db8dougm */
386185db8dougm
396185db8dougm/*
406185db8dougm * add_opt(optlist, optarg, security?)
416185db8dougm *	Add a new parsed option to the option list provided.
426185db8dougm *	If the option is a security option, only add if we are
436185db8dougm *	processing security options.
446185db8dougm */
456185db8dougmint
466185db8dougmadd_opt(struct options **optlistp, char *optarg, int unset)
476185db8dougm{
486185db8dougm	struct options *newopt, *tmp, *optlist;
49546405cdougm	char *optname;
50546405cdougm	char *optvalue;
516185db8dougm
526185db8dougm	optlist = *optlistp;
536185db8dougm	newopt = (struct options *)malloc(sizeof (struct options));
54546405cdougm	if (newopt == NULL)
55546405cdougm		return (OPT_ADD_MEMORY);
566185db8dougm
57546405cdougm	/* extract property/value pair */
58546405cdougm	optname = optarg;
59546405cdougm	if (!unset) {
60546405cdougm		optvalue = strchr(optname, '=');
61546405cdougm		if (optvalue == NULL) {
626185db8dougm			free(newopt);
636185db8dougm			return (OPT_ADD_SYNTAX);
646185db8dougm		}
65546405cdougm		*optvalue++ = '\0'; /* separate the halves */
66546405cdougm	} else {
67546405cdougm		optvalue = NULL;
68546405cdougm	}
696185db8dougm
70546405cdougm	newopt->optname = optname;
71546405cdougm	newopt->optvalue = optvalue;
72546405cdougm	newopt->next = NULL;
73546405cdougm	if (optlist == NULL) {
74546405cdougm		optlist = newopt;
75546405cdougm	} else {
76546405cdougm		for (tmp = optlist; tmp->next != NULL;
77546405cdougm		    tmp = tmp->next) {
78546405cdougm			/*
79546405cdougm			 * Check to see if this is a duplicate
80546405cdougm			 * value. We want to replace the first
81546405cdougm			 * instance with the second.
82546405cdougm			 */
83546405cdougm			if (strcmp(tmp->optname, optname) == 0) {
84546405cdougm				tmp->optvalue = optvalue;
85546405cdougm				free(newopt);
86546405cdougm				goto done;
876185db8dougm			}
886185db8dougm		}
89546405cdougm		tmp->next = newopt;
906185db8dougm	}
91546405cdougmdone:
92546405cdougm	*optlistp = optlist;
93546405cdougm	return (OPT_ADD_OK);
946185db8dougm}
95