xref: /illumos-gate/usr/src/cmd/getopt/getopt.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #include <stdio.h>
32 #include <locale.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #define	BLOCKLEN	5120
37 
38 /* incr doesn't include a null termination */
39 #define	ALLOC_BUFMEM(buf, size, incr) \
40 	{ \
41 		size_t	len = strlen(buf); \
42 		if ((len + incr) >= size) { \
43 			size = len + incr + 1; \
44 			if ((buf = (char *)realloc((void *)buf, size)) \
45 			    == NULL) { \
46 				(void) fputs( \
47 				gettext("getopt: Out of memory\n"), stderr); \
48 				exit(2); \
49 			} \
50 		} \
51 	}
52 
53 int
54 main(int argc, char **argv)
55 {
56 	int	c;
57 	int	errflg = 0;
58 	char	tmpstr[4];
59 	char	*outstr;
60 	char	*goarg;
61 	size_t	bufsize;
62 
63 	(void) setlocale(LC_ALL, "");
64 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
65 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
66 #endif
67 	(void) textdomain(TEXT_DOMAIN);
68 
69 	if (argc < 2) {
70 		(void) fputs(gettext("usage: getopt legal-args $*\n"), stderr);
71 		exit(2);
72 	}
73 
74 	goarg = argv[1];
75 	argv[1] = argv[0];
76 	argv++;
77 	argc--;
78 
79 	bufsize = BLOCKLEN;
80 	if ((outstr = (char *)malloc(bufsize)) == NULL) {
81 		(void) fputs(gettext("getopt: Out of memory\n"), stderr);
82 		exit(2);
83 	}
84 	outstr[0] = '\0';
85 
86 	while ((c = getopt(argc, argv, goarg)) != EOF) {
87 		if (c == '?') {
88 			errflg++;
89 			continue;
90 		}
91 
92 		tmpstr[0] = '-';
93 		tmpstr[1] = (char)c;
94 		tmpstr[2] = ' ';
95 		tmpstr[3] = '\0';
96 
97 		/* If the buffer is full, expand it as appropriate */
98 		ALLOC_BUFMEM(outstr, bufsize, 3);
99 
100 		(void) strcat(outstr, tmpstr);
101 
102 		if (*(strchr(goarg, c)+1) == ':') {
103 			ALLOC_BUFMEM(outstr, bufsize, strlen(optarg)+1)
104 			(void) strcat(outstr, optarg);
105 			(void) strcat(outstr, " ");
106 		}
107 	}
108 
109 	if (errflg) {
110 		exit(2);
111 	}
112 
113 	ALLOC_BUFMEM(outstr, bufsize, 3)
114 	(void) strcat(outstr, "-- ");
115 	while (optind < argc) {
116 		ALLOC_BUFMEM(outstr, bufsize, strlen(argv[optind])+1)
117 		(void) strcat(outstr, argv[optind++]);
118 		(void) strcat(outstr, " ");
119 	}
120 
121 	(void) printf("%s\n", outstr);
122 	return (0);
123 }
124