dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / getopt / getopt.c
blob5356b4a2ec8504de7c02769a8aa0a9f97e38ec5e
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.7 */
34 #include <stdio.h>
35 #include <locale.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #define BLOCKLEN 5120
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 } \
56 int
57 main(int argc, char **argv)
59 int c;
60 int errflg = 0;
61 char tmpstr[4];
62 char *outstr;
63 char *goarg;
64 size_t bufsize;
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);
72 if (argc < 2) {
73 (void) fputs(gettext("usage: getopt legal-args $*\n"), stderr);
74 exit(2);
77 goarg = argv[1];
78 argv[1] = argv[0];
79 argv++;
80 argc--;
82 bufsize = BLOCKLEN;
83 if ((outstr = (char *)malloc(bufsize)) == NULL) {
84 (void) fputs(gettext("getopt: Out of memory\n"), stderr);
85 exit(2);
87 outstr[0] = '\0';
89 while ((c = getopt(argc, argv, goarg)) != EOF) {
90 if (c == '?') {
91 errflg++;
92 continue;
95 tmpstr[0] = '-';
96 tmpstr[1] = (char)c;
97 tmpstr[2] = ' ';
98 tmpstr[3] = '\0';
100 /* If the buffer is full, expand it as appropriate */
101 ALLOC_BUFMEM(outstr, bufsize, 3);
103 (void) strcat(outstr, tmpstr);
105 if (*(strchr(goarg, c)+1) == ':') {
106 ALLOC_BUFMEM(outstr, bufsize, strlen(optarg)+1)
107 (void) strcat(outstr, optarg);
108 (void) strcat(outstr, " ");
112 if (errflg) {
113 exit(2);
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, " ");
124 (void) printf("%s\n", outstr);
125 return (0);