X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=compat%2Fgetopt.c;h=f3ccb4b49781c59b886f922ce65b1ec2dc0d3b20;hb=d462e89c81b9bb0986087abf30a8ea62dcc123a3;hp=1d0beb76953e787571a3e3faa276279d82a2305c;hpb=4c2bdb019375b5b2167959a167710791225b6670;p=pciutils.git diff --git a/compat/getopt.c b/compat/getopt.c index 1d0beb7..f3ccb4b 100644 --- a/compat/getopt.c +++ b/compat/getopt.c @@ -36,7 +36,6 @@ #endif #include -#include /* Comment out all this code if we are using the GNU C Library, and are not actually compiling the library itself. This code is part of the GNU C @@ -163,6 +162,8 @@ static enum { #include #define my_index strchr #define my_strlen strlen +#define my_strcmp strcmp +#define my_strncmp strncmp #else /* Avoid depending on library functions or files @@ -170,11 +171,11 @@ static enum { #if __STDC__ || defined(PROTO) extern char *getenv(const char *name); -extern int strcmp(const char *s1, const char *s2); -extern int strncmp(const char *s1, const char *s2, int n); static int my_strlen(const char *s); static char *my_index(const char *str, int chr); +static int my_strncmp(const char *s1, const char *s2, int n); +static int my_strcmp(const char *s1, const char *s2); #else extern char *getenv(); #endif @@ -197,6 +198,23 @@ static char *my_index(const char *str, int chr) return 0; } +static int my_strncmp(const char *s1, const char *s2, int n) +{ + while (n && *s1 && (*s1 == *s2)) { + ++s1; + ++s2; + --n; + } + if (n == 0) + return 0; + return *(const unsigned char *)s1 - *(const unsigned char *)s2; +} + +static int my_strcmp(const char *s1, const char *s2) +{ + return my_strncmp(s1, s2, -1); +} + #endif /* GNU C library. */ /* Handle permutation of arguments. */ @@ -385,7 +403,7 @@ int _getopt_internal(int argc, char *const *argv, const char *optstring, then exchange with previous non-options as if it were an option, then skip everything else like a non-option. */ - if (optind != argc && !strcmp(argv[optind], "--")) { + if (optind != argc && !my_strcmp(argv[optind], "--")) { optind++; if (first_nonopt != last_nonopt && last_nonopt != optind) @@ -445,7 +463,7 @@ int _getopt_internal(int argc, char *const *argv, const char *optstring, /* Test all options for either exact match or abbreviated matches. */ for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp(p->name, nextchar, s - nextchar)) { + if (!my_strncmp(p->name, nextchar, s - nextchar)) { if (s - nextchar == my_strlen(p->name)) { /* Exact match found. */ pfound = p;