8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / tsol / setlabel / setlabel.c
blob71b2c5dcae5205b4f3fc506c39940936d8bbf52d
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 * setlabel - sets a file label.
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <locale.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <utmp.h>
43 #include <tsol/label.h>
45 static int set_label(char *, char *);
46 static int setlabel(char *, bslabel_t *);
47 static void usage(void);
48 static void m_label_err(const char *, const int);
50 static char *prog = NULL;
52 int
53 main(int argc, char **argv)
55 int rc = 0;
56 char *label;
58 (void) setlocale(LC_ALL, "");
59 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
60 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
61 #endif
62 (void) textdomain(TEXT_DOMAIN);
64 if ((prog = strrchr(argv[0], '/')) == NULL)
65 prog = argv[0];
66 else
67 prog++;
69 if (argc < 3) {
70 usage();
71 return (2);
74 argv++;
75 argc--;
77 label = *argv;
78 argv++;
79 argc--;
80 while (argc-- > 0) {
81 if (set_label(*argv++, label) != 0)
82 rc = 1;
85 return (rc);
88 static int
89 set_label(char *filename, char *label)
91 int rval = 0;
92 int err;
93 m_label_t *blabel;
95 if ((blabel = m_label_alloc(MAC_LABEL)) == NULL) {
96 (void) fprintf(stderr, "setlabel: ");
97 perror(filename);
98 return (2);
100 rval = getlabel(filename, blabel);
101 if (rval) {
102 (void) fprintf(stderr, "setlabel: ");
103 perror(filename);
104 return (rval);
106 if (!bslvalid(blabel)) {
107 (void) fprintf(stderr,
108 gettext("%s: Current label is invalid\n"),
109 filename);
110 blabel = NULL;
112 if (str_to_label(label, &blabel, MAC_LABEL, L_DEFAULT, &err) == -1) {
113 m_label_err(label, err);
116 rval = setlabel(filename, blabel);
117 if (rval == 0)
118 m_label_free(blabel);
119 return (rval);
122 static int
123 setlabel(char *filename, bslabel_t *label)
125 int rval;
127 rval = setflabel(filename, label);
129 if (rval) {
130 (void) fprintf(stderr, "setlabel: ");
131 perror(filename);
133 return (rval);
136 static void
137 m_label_err(const char *ascii, const int err)
139 if (errno == EINVAL) {
140 switch (err) {
141 case M_BAD_STRING:
142 (void) fprintf(stderr,
143 gettext("setlabel: bad string %s\n"), ascii);
144 break;
145 case M_BAD_LABEL:
146 (void) fprintf(stderr,
147 gettext("setlabel: bad previous label\n"));
148 break;
149 default:
150 (void) fprintf(stderr,
151 gettext("setlabel: parsing error found in "
152 "\"%s\" at position %d\n"), ascii, err);
153 break;
155 } else {
156 perror("setlabel");
158 exit(1);
161 * usage()
163 * This routine is called whenever there is a usage type of error has
164 * occured. For example, when a invalid option has has been specified.
167 static void
168 usage(void)
171 (void) fprintf(stderr, gettext("Usage: \n"));
172 (void) fprintf(stderr, gettext(
173 " %s newlabel filename [...] \n"), prog);