8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / tsol / lslabels / lslabels.c
blob1c3eb353e68e50000cbedc84d352af0569e345d2
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * lslabels - Display all labels dominating the specified label.
32 #include <errno.h>
33 #include <libintl.h>
34 #include <locale.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <stropts.h>
41 #include <sys/param.h>
43 #include <tsol/label.h>
44 #include <sys/tsol/label_macro.h>
45 #include <iso/limits_iso.h>
47 #if !defined(TEXT_DOMAIN)
48 #define TEXT_DOMAIN "SYS_TEST"
49 #endif /* !defined(TEXT_DOMAIN) */
51 int hflg = 0; /* true if hex output */
54 * Compartment mask macros.
57 typedef uint32_t comp_chunk_t;
59 #define __NBWRD (CHAR_BIT * sizeof (comp_chunk_t))
60 #define COMP_BITS (CHAR_BIT * sizeof (Compartments_t))
61 #define compmask(n) (1 << ((__NBWRD - 1) - ((n) % __NBWRD)))
62 #define compword(n) ((n)/__NBWRD)
64 #define COMP_ADDSET(a, p) ((comp_chunk_t *)(a))[compword(p)] |= \
65 compmask(p)
66 #define COMP_DELSET(a, p) ((comp_chunk_t *)(a))[compword(p)] &= \
67 ~compmask(p)
68 #define COMP_ISMEMBER(a, p) ((((comp_chunk_t *)(a))[compword(p)] & \
69 compmask(p)) != 0)
71 /* Need functions to test if bit is on */
74 void
75 bitfinder(m_label_t label, int next_bit) {
76 char *labelstr = NULL;
78 Compartments_t *comps = &label.compartments;
80 while (next_bit < COMP_BITS) {
81 if (COMP_ISMEMBER(comps, next_bit)) {
82 bitfinder(label, next_bit + 1);
83 COMP_DELSET(comps, next_bit);
85 if (label_to_str(&label, &labelstr, M_LABEL,
86 LONG_NAMES) == 0) {
87 m_label_t *label2 = NULL;
88 int err;
90 if (str_to_label(labelstr, &label2, MAC_LABEL,
91 L_NO_CORRECTION, &err) == 0) {
92 if (!hflg) {
93 (void) printf("%s\n", labelstr);
94 } else {
95 free(labelstr);
96 (void) label_to_str(&label,
97 &labelstr, M_INTERNAL, 0);
98 (void) printf("%s\n", labelstr);
100 m_label_free(label2);
102 free(labelstr);
104 bitfinder(label, next_bit + 1);
105 break;
107 next_bit++;
111 static void
112 label_error(const char *ascii, const int err)
114 if (errno == EINVAL) {
115 switch (err) {
116 case M_BAD_STRING:
117 (void) fprintf(stderr,
118 gettext("lslabels: bad string %s\n"), ascii);
119 break;
120 case M_BAD_LABEL:
121 (void) fprintf(stderr,
122 gettext("lslabels: bad previous label\n"));
123 break;
124 default:
125 (void) fprintf(stderr,
126 gettext("lslabels: parsing error found in "
127 "\"%s\" at position %d\n"), ascii, err);
128 break;
130 } else {
131 perror("lslabels");
133 exit(1);
134 /*NOTREACHED*/
138 main(int argc, char **argv)
140 int errflg = 0; /* true if arg error */
141 m_label_t *label = NULL; /* binary labels */
142 char ascii[PIPE_BUF]; /* human readable label */
143 char *labelstr = NULL; /* external label to start from */
144 int err = 0; /* label error */
145 int c;
146 int mode = M_LABEL;
147 _Classification *level;
149 (void) setlocale(LC_ALL, "");
150 (void) textdomain(TEXT_DOMAIN);
152 opterr = 0;
153 while ((c = getopt(argc, argv, "h")) != EOF) {
155 switch (c) {
156 case 'h':
157 hflg++;
158 mode = M_INTERNAL;
159 break;
161 default:
162 errflg++;
163 break;
167 argc -= optind - 1;
168 if (errflg || argc > 2) {
170 (void) fprintf(stderr,
171 gettext("usage: %s [-h] [label]\n"),
172 argv[0]);
173 exit(1);
174 /*NOTREACHED*/
177 if (argc == 2) {
178 /* use label on command line */
180 (void) strlcpy(ascii, argv[optind], sizeof (ascii));
181 } else {
182 /* read label from standard input */
183 if ((c = read(STDIN_FILENO, ascii, sizeof (ascii))) <= 0) {
184 perror(gettext("reading ASCII coded label"));
185 exit(1);
186 /*NOTREACHED*/
190 * replace '\n' or (end of buffer) with end of string.
192 ascii[c-1] = '\0';
195 * flush any remaining input past the size of the buffer.
197 (void) ioctl(STDIN_FILENO, I_FLUSH, FLUSHR);
200 if (str_to_label(ascii, &label, MAC_LABEL, L_NO_CORRECTION,
201 &err) == -1) {
202 label_error(ascii, err);
204 if (label_to_str(label, &labelstr, mode,
205 DEF_NAMES) == 0) {
206 (void) printf("%s\n", labelstr);
209 level = &label->classification.class_u.class_chunk;
210 while (*level > 0) {
211 bitfinder(*label, 0);
212 *level -= 1;
214 m_label_free(label);
216 return (0); /* really exit(0); */