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