xref: /illumos-gate/usr/src/cmd/cdrw/options.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <string.h>
28 
29 #include "options.h"
30 
31 static uchar_t bitlocation[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
32 
33 /*
34  * Create a bytemask to store the command line options.
35  */
36 void
37 set_options_mask(options *msk, char *str)
38 {
39 	int i;
40 	(void) memset(msk, 0, sizeof (*msk));
41 	for (i = 0; str[i] != 0; i++) {
42 		add_option(msk, str[i]);
43 	}
44 }
45 
46 void
47 add_option(options *msk, char option)
48 {
49 	uint_t loc;
50 	loc = (uint_t)option;
51 	loc &= 0x7f;
52 	/* put option into the correct bucket */
53 	msk->bitmap[loc >> 3] |= bitlocation[loc & 7];
54 }
55 
56 /*
57  * Compare the bytemask of the command line options used with
58  * acceptable options. If an invalid option is found use it as
59  * the return value.
60  */
61 int
62 compare_options_mask(options *msk, options *specified)
63 {
64 	int i, j;
65 	uchar_t bmap = 0;
66 
67 	for (i = 0; i < 16; i++) {
68 		if (msk->bitmap[i] == specified->bitmap[i])
69 			continue;
70 		bmap = msk->bitmap[i] | specified->bitmap[i];
71 		bmap ^= msk->bitmap[i];
72 		if (bmap)
73 			break;
74 	}
75 	if (i == 16) {
76 		/* no invalid options found */
77 		return (0);
78 	}
79 
80 	for (j = 0; j < 8; j++) {
81 		if (bmap & bitlocation[j])
82 			break;
83 	}
84 	return ((i*8) + j);
85 }
86