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 /*	from OpenSolaris "getopt.c	1.10	05/06/08 SMI"	 SVr4.0 1.7		*/
32 
33 /*
34  * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
35  */
36 #if __GNUC__ >= 3 && __GNUC_MINOR__ >= 4 || __GNUC__ >= 4
37 #define	USED	__attribute__ ((used))
38 #elif defined __GNUC__
39 #define	USED	__attribute__ ((unused))
40 #else
41 #define	USED
42 #endif
43 static const char sccsid[] USED = "@(#)getopt.c	1.4 (gritter) 7/1/05";
44 
45 #include <stdio.h>
46 #include <locale.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <libgen.h>
51 
52 #define	BLOCKLEN	5120
53 
54 /* incr doesn't include a null termination */
55 #define	ALLOC_BUFMEM(buf, size, incr) \
56 	{ \
57 		size_t	len = strlen(buf); \
58 		if ((len + incr) >= size) { \
59 			size = len + incr + 1; \
60 			if ((buf = realloc(buf, size)) == NULL) { \
61 				fprintf(stderr, "%s: Out of memory\n", \
62 						progname); \
63 				exit(2); \
64 			} \
65 		} \
66 	}
67 
68 static const char	*progname;
69 
70 int
main(int argc,char ** argv)71 main(int argc, char **argv)
72 {
73 	int	c;
74 	int	errflg = 0;
75 	char	tmpstr[4];
76 	char	*outstr;
77 	char	*goarg;
78 	size_t	bufsize;
79 
80 	progname = basename(argv[0]);
81 	if (argc < 2) {
82 		fprintf(stderr, "usage: %s legal-args $*\n", progname);
83 		exit(2);
84 	}
85 
86 	goarg = argv[1];
87 	argv[1] = argv[0];
88 	argv++;
89 	argc--;
90 
91 	bufsize = BLOCKLEN;
92 	if ((outstr = malloc(bufsize)) == NULL) {
93 		fprintf(stderr, "%s: Out of memory\n", progname);
94 		exit(2);
95 	}
96 	outstr[0] = '\0';
97 
98 	while ((c = getopt(argc, argv, goarg)) != EOF) {
99 		if (c == '?') {
100 			errflg++;
101 			continue;
102 		}
103 
104 		tmpstr[0] = '-';
105 		tmpstr[1] = (char)c;
106 		tmpstr[2] = ' ';
107 		tmpstr[3] = '\0';
108 
109 		/* If the buffer is full, expand it as appropriate */
110 		ALLOC_BUFMEM(outstr, bufsize, 3);
111 
112 		strcat(outstr, tmpstr);
113 
114 		if (*(strchr(goarg, c)+1) == ':') {
115 			ALLOC_BUFMEM(outstr, bufsize, strlen(optarg)+1)
116 			strcat(outstr, optarg);
117 			strcat(outstr, " ");
118 		}
119 	}
120 
121 	if (errflg) {
122 		exit(2);
123 	}
124 
125 	ALLOC_BUFMEM(outstr, bufsize, 3)
126 	strcat(outstr, "-- ");
127 	while (optind < argc) {
128 		ALLOC_BUFMEM(outstr, bufsize, strlen(argv[optind])+1)
129 		strcat(outstr, argv[optind++]);
130 		strcat(outstr, " ");
131 	}
132 
133 	printf("%s\n", outstr);
134 	return 0;
135 }
136