[ofw] [PATCH] getopt: add use of const to interface
Sean Hefty
sean.hefty at intel.com
Tue Feb 24 11:54:25 PST 2009
Add const to getopt argv argument, and allow getopt to be included by
c++.
This allows the ib-diags to call getopt without build warnings.
Signed-off-by: Sean Hefty <sean.hefty at intel.com>
---
I will commit this to the trunk unless there are concerns.
diff -up -r -X trunk\docs\dontdiff.txt -I '\$Id:' trunk\etc\user/getopt.c branches\winverbs\etc\user/getopt.c
--- trunk\etc\user/getopt.c 2009-02-11 05:28:36.399500000 -0800
+++ branches\winverbs\etc\user/getopt.c 2009-02-12 17:22:01.255925600 -0800
@@ -39,7 +39,7 @@ int optind = 1;
int opterr = 1;
int optopt = '?';
-int getopt(int argc, char **argv, char const *opts)
+int getopt(int argc, char * const argv[], char const *opts)
{
char *loc;
@@ -79,16 +79,20 @@ int getopt(int argc, char **argv, char c
goto out;
optarg = argv[++optind];
- while(optarg && *optarg && (*optarg == ' ' || *optarg == ' ')) optarg++;
- if (optarg && !(*optarg))
- optarg = NULL;
+ while(optarg && *optarg && (*optarg == ' ' || *optarg == ' ')) {
+ optarg++;
+ }
+
+ if (!optarg || !(*optarg)) {
+ return '?';
+ }
out:
optind++;
return optopt;
}
-int getopt_long(int argc, char **argv, char const *opts,
+int getopt_long(int argc, char * const argv[], char const *opts,
const struct option *longopts, int *longindex)
{
char arg[256];
--- trunk\inc\user\getopt.h 2009-02-11 05:28:36.274500000 -0800
+++ branches\winverbs\inc\user\getopt.h 2009-02-12 17:21:41.368737700 -0800
@@ -31,6 +31,11 @@
#ifndef _GETOPT_H_
#define _GETOPT_H_
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
extern char *optarg;
extern int optind;
extern int opterr;
@@ -51,8 +56,12 @@ enum
optional_argument
};
-extern int getopt(int argc, char **argv, char const *opts);
-extern int getopt_long(int argc, char **argv, char const *opts,
+extern int getopt(int argc, char * const argv[], char const *opts);
+extern int getopt_long(int argc, char * const argv[], char const *opts,
const struct option *longopts, int *longindex);
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
#endif
More information about the ofw
mailing list