8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / tsol / getlabel / getlabel.c
blob1f6a3b1239faf9f6adc0122f4f1a5513bfa73bf7
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 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * getlabel - gets file label.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <locale.h>
39 #include <tsol/label.h>
41 #define s_flag 0x04
42 #define S_flag 0x08
45 static int
46 get_label(char *filename, uint_t opt_flag)
48 m_label_t *fl;
49 char *label;
51 if ((fl = m_label_alloc(MAC_LABEL)) == NULL) {
52 perror("m_label_alloc");
53 return (1);
54 } else if (getlabel(filename, fl) != 0) {
55 perror(filename);
56 m_label_free(fl);
57 return (1);
60 (void) printf("%s:\t", filename);
61 switch (opt_flag) {
62 case S_flag:
63 if (label_to_str(fl, &label, M_LABEL, LONG_NAMES) != 0) {
64 perror(gettext("%s:unable to translate "
65 "Sensitivity label"));
66 m_label_free(fl);
67 return (2);
69 break;
70 case s_flag:
71 if (label_to_str(fl, &label, M_LABEL, SHORT_NAMES) != 0) {
72 perror(gettext("unable to translate "
73 "Sensitivity label"));
74 m_label_free(fl);
75 return (2);
77 break;
78 default:
79 if (label_to_str(fl, &label, M_LABEL, DEF_NAMES) != 0) {
80 perror(gettext("unable to translate "
81 "Sensitivity label"));
82 m_label_free(fl);
83 return (2);
85 break;
87 (void) printf("%s\n", label);
89 free(label);
90 m_label_free(fl);
91 return (0);
94 void
95 usage(char *prog)
97 (void) fprintf(stderr, gettext("Usage: \n"));
98 (void) fprintf(stderr, gettext("\t%s [-S | -s] filename ...\n"),
99 prog);
100 exit(1);
105 main(int argc, char **argv)
107 uint_t opt_flag = 0;
108 int rc = 0;
109 int opt;
110 char *prog;
112 (void) setlocale(LC_ALL, "");
113 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
114 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
115 #endif
116 (void) textdomain(TEXT_DOMAIN);
118 if ((prog = strrchr(argv[0], '/')) == NULL)
119 prog = argv[0];
120 else
121 prog++;
123 if (argc < 2) {
124 usage(prog);
127 while ((opt = getopt(argc, argv, ":sS")) != EOF) {
128 switch (opt) {
129 case 's':
130 if (opt_flag != 0)
131 usage(prog);
132 opt_flag = s_flag;
133 break;
134 case 'S':
135 if (opt_flag != 0)
136 usage(prog);
137 opt_flag = S_flag;
138 break;
139 default:
140 usage(prog);
143 if ((argc -= optind) < 1) {
144 usage(prog);
146 argv += optind;
147 while (argc-- > 0) {
148 if (get_label(*argv++, opt_flag) != 0)
149 rc = 2;
151 return (rc);