Skip to content

Commit

Permalink
Add namespace to getopt functions
Browse files Browse the repository at this point in the history
This enables static linking. Otherwise, these functions conflict with
the standard getopt functions.
  • Loading branch information
luispedro committed Mar 19, 2018
1 parent b81d455 commit 5a5a116
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
17 changes: 11 additions & 6 deletions getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
#include <string.h>
#include "getopt.h"

char *optarg;
int optind=1, opterr=1, optopt, __optpos, optreset=0;
char *mm_optarg;
int mm_optind=1, mm_opterr=1, mm_optopt, __optpos, mm_optreset=0;

#define optpos __optpos
#define optarg mm_optarg
#define optind mm_optind
#define opterr mm_opterr
#define optopt mm_optopt
#define optreset mm_optreset

static void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
{
Expand All @@ -23,7 +28,7 @@ static void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
#endif
}

int getopt(int argc, char * const argv[], const char *optstring)
int mm_getopt(int argc, char * const argv[], const char *optstring)
{
int i, c, d;
int k, l;
Expand Down Expand Up @@ -173,7 +178,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
return '?';
}
}
return getopt(argc, argv, optstring);
return mm_getopt(argc, argv, optstring);
}

static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
Expand Down Expand Up @@ -205,12 +210,12 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
return ret;
}

int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
int mm_getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
{
return __getopt_long(argc, argv, optstring, longopts, idx, 0);
}

int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
int mm_getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
{
return __getopt_long(argc, argv, optstring, longopts, idx, 1);
}
10 changes: 5 additions & 5 deletions getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
extern "C" {
#endif

int getopt(int, char * const [], const char *);
extern char *optarg;
extern int optind, opterr, optopt, optreset;
int mm_getopt(int, char * const [], const char *);
extern char *mm_optarg;
extern int mm_optind, mm_opterr, mm_optopt, mm_optreset;

struct option {
const char *name;
Expand All @@ -39,8 +39,8 @@ struct option {
int val;
};

int getopt_long(int, char *const *, const char *, const struct option *, int *);
int getopt_long_only(int, char *const *, const char *, const struct option *, int *);
int mm_getopt_long(int, char *const *, const char *, const struct option *, int *);
int mm_getopt_long_only(int, char *const *, const char *, const struct option *, int *);

#define no_argument 0
#define required_argument 1
Expand Down

0 comments on commit 5a5a116

Please sign in to comment.