1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2000 Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.7 */ 32 33 #include <stdio.h> 34 #include <locale.h> 35 #include <stdlib.h> 36 #include <string.h> 37 38 #define BLOCKLEN 5120 39 40 /* incr doesn't include a null termination */ 41 #define ALLOC_BUFMEM(buf, size, incr) \ 42 { \ 43 size_t len = strlen(buf); \ 44 if ((len + incr) >= size) { \ 45 size = len + incr + 1; \ 46 if ((buf = (char *)realloc((void *)buf, size)) \ 47 == NULL) { \ 48 (void) fputs( \ 49 gettext("getopt: Out of memory\n"), stderr); \ 50 exit(2); \ 51 } \ 52 } \ 53 } 54 55 void 56 main(int argc, char **argv) 57 { 58 int c; 59 int errflg = 0; 60 char tmpstr[4]; 61 char *outstr; 62 char *goarg; 63 size_t bufsize; 64 65 (void) setlocale(LC_ALL, ""); 66 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 67 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 68 #endif 69 (void) textdomain(TEXT_DOMAIN); 70 71 if (argc < 2) { 72 (void) fputs(gettext("usage: getopt legal-args $*\n"), stderr); 73 exit(2); 74 } 75 76 goarg = argv[1]; 77 argv[1] = argv[0]; 78 argv++; 79 argc--; 80 81 bufsize = BLOCKLEN; 82 if ((outstr = (char *)malloc(bufsize)) == NULL) { 83 (void) fputs(gettext("getopt: Out of memory\n"), stderr); 84 exit(2); 85 } 86 outstr[0] = '\0'; 87 88 while ((c = getopt(argc, argv, goarg)) != EOF) { 89 if (c == '?') { 90 errflg++; 91 continue; 92 } 93 94 tmpstr[0] = '-'; 95 tmpstr[1] = (char)c; 96 tmpstr[2] = ' '; 97 tmpstr[3] = '\0'; 98 99 /* If the buffer is full, expand it as appropriate */ 100 ALLOC_BUFMEM(outstr, bufsize, 3); 101 102 (void) strcat(outstr, tmpstr); 103 104 if (*(strchr(goarg, c)+1) == ':') { 105 ALLOC_BUFMEM(outstr, bufsize, strlen(optarg)+1) 106 (void) strcat(outstr, optarg); 107 (void) strcat(outstr, " "); 108 } 109 } 110 111 if (errflg) { 112 exit(2); 113 } 114 115 ALLOC_BUFMEM(outstr, bufsize, 3) 116 (void) strcat(outstr, "-- "); 117 while (optind < argc) { 118 ALLOC_BUFMEM(outstr, bufsize, strlen(argv[optind])+1) 119 (void) strcat(outstr, argv[optind++]); 120 (void) strcat(outstr, " "); 121 } 122 123 (void) printf("%s\n", outstr); 124 exit(0); 125 } 126