14746Srica /*
24746Srica * CDDL HEADER START
34746Srica *
44746Srica * The contents of this file are subject to the terms of the
54746Srica * Common Development and Distribution License (the "License").
64746Srica * You may not use this file except in compliance with the License.
74746Srica *
84746Srica * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94746Srica * or http://www.opensolaris.org/os/licensing.
104746Srica * See the License for the specific language governing permissions
114746Srica * and limitations under the License.
124746Srica *
134746Srica * When distributing Covered Code, include this CDDL HEADER in each
144746Srica * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154746Srica * If applicable, add the following below this CDDL HEADER, with the
164746Srica * fields enclosed by brackets "[]" replaced with your own identifying
174746Srica * information: Portions Copyright [yyyy] [name of copyright owner]
184746Srica *
194746Srica * CDDL HEADER END
204746Srica */
214746Srica
224746Srica /*
23*8717STon.Nguyen@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
244746Srica * Use is subject to license terms.
254746Srica */
264746Srica
274746Srica
284746Srica /*
294746Srica * atohexlabel - Convert a human readable label to its internal
304746Srica * equivalent.
314746Srica */
324746Srica
334746Srica #include <errno.h>
344746Srica #include <libintl.h>
354746Srica #include <locale.h>
364746Srica #include <stdio.h>
374746Srica #include <stdlib.h>
384746Srica #include <string.h>
394746Srica #include <unistd.h>
40*8717STon.Nguyen@Sun.COM #include <stropts.h>
414746Srica
424746Srica #include <sys/param.h>
434746Srica
444746Srica #include <tsol/label.h>
454746Srica
464746Srica #if !defined(TEXT_DOMAIN)
474746Srica #define TEXT_DOMAIN "SYS_TEST"
484746Srica #endif /* !defined(TEXT_DOMAIN) */
494746Srica
504746Srica static void
label_error(const char * ascii,const int err)514746Srica label_error(const char *ascii, const int err)
524746Srica {
534746Srica if (errno == EINVAL) {
544746Srica switch (err) {
554746Srica case M_BAD_STRING:
564746Srica (void) fprintf(stderr,
574746Srica gettext("atohexlabel: bad string %s\n"), ascii);
584746Srica break;
594746Srica case M_BAD_LABEL:
604746Srica (void) fprintf(stderr,
614746Srica gettext("atohexlabel: bad previous label\n"));
624746Srica break;
634746Srica default:
644746Srica (void) fprintf(stderr,
654746Srica gettext("atohexlabel: parsing error found in "
664746Srica "\"%s\" at position %d\n"), ascii, err);
674746Srica break;
684746Srica }
694746Srica } else {
704746Srica perror("atohexlabel");
714746Srica }
724746Srica exit(1);
734746Srica /*NOTREACHED*/
744746Srica }
754746Srica
764746Srica int
main(int argc,char ** argv)774746Srica main(int argc, char **argv)
784746Srica {
794746Srica int cflg = 0; /* true if Clearance only */
804746Srica int errflg = 0; /* true if arg error */
814746Srica m_label_t *label = NULL; /* binary labels */
824746Srica char ascii[PIPE_BUF]; /* human readable label */
834746Srica char *hex = NULL; /* internal label to print */
844746Srica int err = 0; /* label error */
854746Srica int c;
864746Srica
874746Srica (void) setlocale(LC_ALL, "");
884746Srica (void) textdomain(TEXT_DOMAIN);
894746Srica
904746Srica opterr = 0;
914746Srica while ((c = getopt(argc, argv, "c")) != EOF) {
924746Srica
934746Srica switch (c) {
944746Srica case 'c':
954746Srica cflg++;
964746Srica break;
974746Srica
984746Srica default:
994746Srica errflg++;
1004746Srica break;
1014746Srica }
1024746Srica }
1034746Srica
1044746Srica argc -= optind - 1;
1054746Srica if (errflg || argc > 2) {
1064746Srica
1074746Srica (void) fprintf(stderr,
1084746Srica gettext("usage: %s [-c] [human readable label]\n"),
1094746Srica argv[0]);
1104746Srica exit(1);
1114746Srica /*NOTREACHED*/
1124746Srica }
1134746Srica
1144746Srica if (argc == 2) {
1154746Srica /* use label on command line */
1164746Srica
1174746Srica (void) strlcpy(ascii, argv[optind], sizeof (ascii));
1184746Srica } else {
1194746Srica /* read label from standard input */
120*8717STon.Nguyen@Sun.COM if ((c = read(STDIN_FILENO, ascii, sizeof (ascii))) <= 0) {
1214746Srica perror(gettext("reading ASCII coded label"));
1224746Srica exit(1);
1234746Srica /*NOTREACHED*/
1244746Srica }
125*8717STon.Nguyen@Sun.COM
126*8717STon.Nguyen@Sun.COM /*
127*8717STon.Nguyen@Sun.COM * replace '\n' or (end of buffer) with end of string.
128*8717STon.Nguyen@Sun.COM */
129*8717STon.Nguyen@Sun.COM ascii[c-1] = '\0';
130*8717STon.Nguyen@Sun.COM
131*8717STon.Nguyen@Sun.COM /*
132*8717STon.Nguyen@Sun.COM * flush any remaining input past the size of the buffer.
133*8717STon.Nguyen@Sun.COM */
134*8717STon.Nguyen@Sun.COM (void) ioctl(STDIN_FILENO, I_FLUSH, FLUSHR);
1354746Srica }
1364746Srica
1374746Srica if (cflg) {
1384746Srica if (str_to_label(ascii, &label, USER_CLEAR, L_NO_CORRECTION,
1394746Srica &err) == -1) {
1404746Srica label_error(ascii, err);
1414746Srica }
1424746Srica if (label_to_str(label, &hex, M_INTERNAL, DEF_NAMES) != 0) {
1434746Srica perror("label_to_str");
1444746Srica exit(1);
1454746Srica }
1464746Srica (void) printf("%s\n", hex);
1474746Srica m_label_free(label);
1484746Srica free(hex);
1494746Srica } else {
1504746Srica if (str_to_label(ascii, &label, MAC_LABEL, L_NO_CORRECTION,
1514746Srica &err) == -1) {
1524746Srica label_error(ascii, err);
1534746Srica }
1544746Srica if (label_to_str(label, &hex, M_INTERNAL, DEF_NAMES) != 0) {
1554746Srica perror("label_to_str");
1564746Srica exit(1);
1574746Srica }
1584746Srica (void) printf("%s\n", hex);
1594746Srica m_label_free(label);
1604746Srica free(hex);
1614746Srica }
1624746Srica
1634746Srica return (0); /* really exit(0); */
1644746Srica }
165