2 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
\r
3 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
\r
5 * This software is available to you under the OpenIB.org BSD license
\r
8 * Redistribution and use in source and binary forms, with or
\r
9 * without modification, are permitted provided that the following
\r
10 * conditions are met:
\r
12 * - Redistributions of source code must retain the above
\r
13 * copyright notice, this list of conditions and the following
\r
16 * - Redistributions in binary form must reproduce the above
\r
17 * copyright notice, this list of conditions and the following
\r
18 * disclaimer in the documentation and/or other materials
\r
19 * provided with the distribution.
\r
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
\r
25 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
\r
26 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
42 int getopt(int argc, char * const argv[], char const *opts)
\r
48 if (optind >= argc) {
\r
52 if (argv[optind][0] != '-' && argv[optind][0] != '/') {
\r
56 if (!strcmp(argv[optind], "--")) {
\r
61 optopt = argv[optind][1];
\r
63 loc = strchr(opts, optopt);
\r
68 if (loc[1] != ':') {
\r
72 /* process switch argument */
\r
73 if (argv[optind][2] != '\0') {
\r
74 optarg = &argv[optind][2];
\r
78 /* switch argument is optional (::) - be careful */
\r
79 if (loc[2] == ':' ) {
\r
80 if ((argv[optind+1] == NULL)) {
\r
81 /* handle EOL without optional arg */
\r
85 if (argv[optind+1] && (argv[optind+1][0] == '-' || argv[optind+1][0] == '/'))
\r
89 optarg = argv[++optind];
\r
90 if (!optarg || !(*optarg)) {
\r
99 int getopt_long(int argc, char * const argv[], char const *opts,
\r
100 const struct option *longopts, int *longindex)
\r
106 if (optind == argc) {
\r
110 if (argv[optind][0] != '-' && argv[optind][0] != '/') {
\r
114 if (!strcmp(argv[optind], "--")) {
\r
119 if (argv[optind][1] != '-') {
\r
120 return getopt(argc, argv, opts);
\r
123 strcpy(arg, &argv[optind][2]);
\r
124 str = strtok(arg, "=");
\r
126 for (i = 0; longopts[i].name; i++) {
\r
128 if (strcmp(str, longopts[i].name)) {
\r
132 if (longindex != NULL) {
\r
136 if (longopts[i].flag != NULL) {
\r
137 *(longopts[i].flag) = longopts[i].val;
\r
140 switch (longopts[i].has_arg) {
\r
141 case required_argument:
\r
142 optarg = strtok(NULL, "=");
\r
143 if (optarg != NULL) {
\r
147 if (++optind == argc || argv[optind][0] == '-') {
\r
151 optarg = argv[optind];
\r
153 case optional_argument:
\r
154 optarg = strtok(NULL, "=");
\r
155 if (optarg != NULL) {
\r
159 if (optind + 1 == argc || argv[optind + 1][0] == '-') {
\r
163 optarg = argv[++optind];
\r
170 if (longopts[i].flag == 0) {
\r
171 return (longopts[i].val);
\r