xref: /titanic_44/usr/src/cmd/cdrw/options.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <string.h>
30 
31 #include "options.h"
32 
33 static uchar_t bitlocation[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
34 
35 /*
36  * Create a bytemask to store the command line options.
37  */
38 void
39 set_options_mask(options *msk, char *str)
40 {
41 	int i;
42 	(void) memset(msk, 0, sizeof (*msk));
43 	for (i = 0; str[i] != 0; i++) {
44 		add_option(msk, str[i]);
45 	}
46 }
47 
48 void
49 add_option(options *msk, char option)
50 {
51 	uint_t loc;
52 	loc = (uint_t)option;
53 	loc &= 0x7f;
54 	/* put option into the correct bucket */
55 	msk->bitmap[loc >> 3] |= bitlocation[loc & 7];
56 }
57 
58 /*
59  * Compare the bytemask of the command line options used with
60  * acceptable options. If an invalid option is found use it as
61  * the return value.
62  */
63 int
64 compare_options_mask(options *msk, options *specified)
65 {
66 	int i, j;
67 	uchar_t bmap = 0;
68 
69 	for (i = 0; i < 16; i++) {
70 		if (msk->bitmap[i] == specified->bitmap[i])
71 			continue;
72 		bmap = msk->bitmap[i] | specified->bitmap[i];
73 		bmap ^= msk->bitmap[i];
74 		if (bmap)
75 			break;
76 	}
77 	if (i == 16) {
78 		/* no invalid options found */
79 		return (0);
80 	}
81 
82 	for (j = 0; j < 8; j++) {
83 		if (bmap & bitlocation[j])
84 			break;
85 	}
86 	return ((i*8) + j);
87 }
88