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