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 **argv, char const *opts)
\r
48 if (optind >= argc) {
\r
52 if (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 if (argv[optind][2] != '\0') {
\r
73 optarg = &argv[optind][2];
\r
77 /* switch argument is optional (::) - be careful */
\r
78 if (loc[2] == ':' && (argv[optind+1] && argv[optind+1][0] == '-'))
\r
81 optarg = argv[++optind];
\r
82 while(optarg && *optarg && (*optarg == ' ' || *optarg == ' ')) optarg++;
\r
83 if (optarg && !(*optarg))
\r
91 int getopt_long(int argc, char **argv, char const *opts,
\r
92 const struct option *longopts, int *longindex)
\r
98 if (optind == argc) {
\r
102 if (argv[optind][0] != '-') {
\r
106 if (!strcmp(argv[optind], "--")) {
\r
111 if (argv[optind][1] != '-') {
\r
112 return getopt(argc, argv, opts);
\r
115 strcpy(arg, &argv[optind][2]);
\r
116 str = strtok(arg, "=");
\r
118 for (i = 0; longopts[i].name; i++) {
\r
120 if (strcmp(str, longopts[i].name)) {
\r
124 if (longindex != NULL) {
\r
128 if (longopts[i].flag != NULL) {
\r
129 *(longopts[i].flag) = longopts[i].val;
\r
132 switch (longopts[i].has_arg) {
\r
133 case required_argument:
\r
134 optarg = strtok(NULL, "=");
\r
135 if (optarg != NULL) {
\r
139 if (++optind == argc || argv[optind][0] == '-') {
\r
143 optarg = argv[optind];
\r
145 case optional_argument:
\r
146 optarg = strtok(NULL, "=");
\r
147 if (optarg != NULL) {
\r
151 if (optind + 1 == argc || argv[optind + 1][0] == '-') {
\r
155 optarg = argv[++optind];
\r
162 if (longopts[i].flag == 0) {
\r
163 return (longopts[i].val);
\r