xref: /titanic_51/usr/src/cmd/tsol/hextoalabel/hextoalabel.c (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
1*f875b4ebSrica /*
2*f875b4ebSrica  * CDDL HEADER START
3*f875b4ebSrica  *
4*f875b4ebSrica  * The contents of this file are subject to the terms of the
5*f875b4ebSrica  * Common Development and Distribution License (the "License").
6*f875b4ebSrica  * You may not use this file except in compliance with the License.
7*f875b4ebSrica  *
8*f875b4ebSrica  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*f875b4ebSrica  * or http://www.opensolaris.org/os/licensing.
10*f875b4ebSrica  * See the License for the specific language governing permissions
11*f875b4ebSrica  * and limitations under the License.
12*f875b4ebSrica  *
13*f875b4ebSrica  * When distributing Covered Code, include this CDDL HEADER in each
14*f875b4ebSrica  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*f875b4ebSrica  * If applicable, add the following below this CDDL HEADER, with the
16*f875b4ebSrica  * fields enclosed by brackets "[]" replaced with your own identifying
17*f875b4ebSrica  * information: Portions Copyright [yyyy] [name of copyright owner]
18*f875b4ebSrica  *
19*f875b4ebSrica  * CDDL HEADER END
20*f875b4ebSrica  */
21*f875b4ebSrica 
22*f875b4ebSrica /*
23*f875b4ebSrica  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*f875b4ebSrica  * Use is subject to license terms.
25*f875b4ebSrica  */
26*f875b4ebSrica 
27*f875b4ebSrica #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*f875b4ebSrica 
29*f875b4ebSrica /*
30*f875b4ebSrica  *	hextoalabel - Convert an internal label to its human readable
31*f875b4ebSrica  *		      equivalent.
32*f875b4ebSrica  */
33*f875b4ebSrica 
34*f875b4ebSrica #include <errno.h>
35*f875b4ebSrica #include <libintl.h>
36*f875b4ebSrica #include <locale.h>
37*f875b4ebSrica #include <stdio.h>
38*f875b4ebSrica #include <stdlib.h>
39*f875b4ebSrica #include <string.h>
40*f875b4ebSrica #include <unistd.h>
41*f875b4ebSrica 
42*f875b4ebSrica #include <sys/param.h>
43*f875b4ebSrica 
44*f875b4ebSrica #include <tsol/label.h>
45*f875b4ebSrica 
46*f875b4ebSrica #if !defined(TEXT_DOMAIN)
47*f875b4ebSrica #define	TEXT_DOMAIN	"SYS_TEST"
48*f875b4ebSrica #endif	/* !defined(TEXT_DOMAIN) */
49*f875b4ebSrica 
50*f875b4ebSrica static void
51*f875b4ebSrica label_error(const char *hex, const int err)
52*f875b4ebSrica {
53*f875b4ebSrica 	if (errno == EINVAL) {
54*f875b4ebSrica 		switch (err) {
55*f875b4ebSrica 		case M_BAD_STRING:
56*f875b4ebSrica 			(void) fprintf(stderr,
57*f875b4ebSrica 			    gettext("hextoalabel: bad string %s\n"), hex);
58*f875b4ebSrica 		break;
59*f875b4ebSrica 		case M_BAD_LABEL:
60*f875b4ebSrica 			(void) fprintf(stderr,
61*f875b4ebSrica 			    gettext("hextoalabel: bad previous label\n"));
62*f875b4ebSrica 		break;
63*f875b4ebSrica 		default:
64*f875b4ebSrica 			(void) fprintf(stderr,
65*f875b4ebSrica 			    gettext("hextoalabel: parsing error found in "
66*f875b4ebSrica 			    "\"%s\" at position %d\n"), hex, err);
67*f875b4ebSrica 		break;
68*f875b4ebSrica 		}
69*f875b4ebSrica 	} else {
70*f875b4ebSrica 		perror("hextoalabel");
71*f875b4ebSrica 	}
72*f875b4ebSrica 	exit(1);
73*f875b4ebSrica 	/*NOTREACHED*/
74*f875b4ebSrica }
75*f875b4ebSrica 
76*f875b4ebSrica int
77*f875b4ebSrica main(int argc, char **argv)
78*f875b4ebSrica {
79*f875b4ebSrica 	int cflg = 0;			/* true if Clearance only */
80*f875b4ebSrica 	int errflg = 0;			/* true if arg error */
81*f875b4ebSrica 	m_label_t *label = NULL;
82*f875b4ebSrica 	char hex[PIPE_BUF];		/* internal label */
83*f875b4ebSrica 	char *ascii = NULL;		/* human readable label to print */
84*f875b4ebSrica 	int err = 0;			/* label error */
85*f875b4ebSrica 	int c;
86*f875b4ebSrica 
87*f875b4ebSrica 	(void) setlocale(LC_ALL, "");
88*f875b4ebSrica 	(void) textdomain(TEXT_DOMAIN);
89*f875b4ebSrica 
90*f875b4ebSrica 	opterr = 0;
91*f875b4ebSrica 	while ((c = getopt(argc, argv, "c")) != EOF) {
92*f875b4ebSrica 
93*f875b4ebSrica 		switch (c) {
94*f875b4ebSrica 		case 'c':
95*f875b4ebSrica 			cflg++;
96*f875b4ebSrica 			break;
97*f875b4ebSrica 		default:
98*f875b4ebSrica 			errflg++;
99*f875b4ebSrica 			break;
100*f875b4ebSrica 		}
101*f875b4ebSrica 	}
102*f875b4ebSrica 
103*f875b4ebSrica 	argc -= optind - 1;
104*f875b4ebSrica 	if (errflg || argc > 2) {
105*f875b4ebSrica 
106*f875b4ebSrica 		(void) fprintf(stderr,
107*f875b4ebSrica 		    gettext("usage: %s [-c] [hexadecimal label]\n"), argv[0]);
108*f875b4ebSrica 		exit(1);
109*f875b4ebSrica 		/*NOTREACHED*/
110*f875b4ebSrica 	}
111*f875b4ebSrica 
112*f875b4ebSrica 	if (argc == 2) {
113*f875b4ebSrica 		/* use label on command line */
114*f875b4ebSrica 
115*f875b4ebSrica 		(void) strlcpy(hex, argv[optind], sizeof (hex));
116*f875b4ebSrica 	} else {
117*f875b4ebSrica 		/* read label from standard input */
118*f875b4ebSrica 
119*f875b4ebSrica 		if ((c = read(STDIN_FILENO, hex, sizeof (hex))) < 0) {
120*f875b4ebSrica 
121*f875b4ebSrica 			perror(gettext("reading hexadecimal label"));
122*f875b4ebSrica 			exit(1);
123*f875b4ebSrica 			/*NOTREACHED*/
124*f875b4ebSrica 		}
125*f875b4ebSrica 		if (hex[c-1] == '\n') {
126*f875b4ebSrica 			/* replace '\n' with end of string */
127*f875b4ebSrica 			hex[c-1] = '\000';
128*f875b4ebSrica 		} else {
129*f875b4ebSrica 			/* ensure end of string */
130*f875b4ebSrica 			hex[c] = '\000';
131*f875b4ebSrica 		}
132*f875b4ebSrica 	}
133*f875b4ebSrica 
134*f875b4ebSrica 	if (cflg) {
135*f875b4ebSrica 		if (str_to_label(hex, &label, USER_CLEAR, L_NO_CORRECTION,
136*f875b4ebSrica 		    &err) == -1) {
137*f875b4ebSrica 			label_error(hex, err);
138*f875b4ebSrica 		}
139*f875b4ebSrica 		if (label_to_str(label, &ascii, M_LABEL, DEF_NAMES) != 0) {
140*f875b4ebSrica 			perror("label_to_str");
141*f875b4ebSrica 			exit(1);
142*f875b4ebSrica 		}
143*f875b4ebSrica 		(void) printf("%s\n", ascii);
144*f875b4ebSrica 		m_label_free(label);
145*f875b4ebSrica 		free(ascii);
146*f875b4ebSrica 	} else {
147*f875b4ebSrica 		if (str_to_label(hex, &label, MAC_LABEL, L_NO_CORRECTION,
148*f875b4ebSrica 		    &err) == -1) {
149*f875b4ebSrica 			label_error(hex, err);
150*f875b4ebSrica 		}
151*f875b4ebSrica 		if (label_to_str(label, &ascii, M_LABEL, DEF_NAMES) != 0) {
152*f875b4ebSrica 			perror("label_to_str");
153*f875b4ebSrica 			exit(1);
154*f875b4ebSrica 		}
155*f875b4ebSrica 		(void) printf("%s\n", ascii);
156*f875b4ebSrica 		m_label_free(label);
157*f875b4ebSrica 		free(ascii);
158*f875b4ebSrica 	}
159*f875b4ebSrica 
160*f875b4ebSrica 	return (0);	/* really exit(0); */
161*f875b4ebSrica }
162