Use cmake for determinate if system has getopt function, use util.h for getopt

This commit is contained in:
Pali Rohár 2013-02-05 15:54:34 +01:00
parent 606d5ef476
commit cfc336586b
4 changed files with 24 additions and 25 deletions

View file

@ -788,6 +788,7 @@ check_function_exists("strcpy_s" HAVE_STRCPYS)
check_function_exists("strncpy_s" HAVE_STRNCPYS)
check_function_exists("strcasestr" HAVE_STRCASESTR)
check_function_exists("strnlen" HAVE_STRNLEN)
check_function_exists("getopt" HAVE_GETOPT)
# mingw-w64 does not have strcat_s in any include file, but function symbol in library exists
# so rather check if we have strcat_s in string.h file
@ -817,6 +818,10 @@ if(HAVE_STRNLEN)
add_definitions(-DHAVE_STRNLEN)
endif()
if(HAVE_GETOPT)
add_definitions(-DHAVE_GETOPT)
endif()
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")

View file

@ -113,6 +113,18 @@ extern char *strcasestr(const char *str, const char *substr);
extern size_t strnlen(const char *str, size_t strsize);
#endif // !HAVE_STRNLEN
/*----------------------------------------------------------------------------
-- Getopt
----------------------------------------------------------------------------*/
#ifdef HAVE_GETOPT
#include <unistd.h>
#else
extern char *optarg;
extern int optind, opterr, optopt;
int getopt(int argc, char * const argv[], const char *optstring);
#endif
/*----------------------------------------------------------------------------
-- Clipboard
----------------------------------------------------------------------------*/

View file

@ -169,18 +169,6 @@ extern void beos_init(int argc, char **argv);
#endif
#ifndef _MSC_VER
#include <unistd.h>
#endif
#ifdef __CYGWIN__
#include <getopt.h>
#endif
#if defined(_MSC_VER) || defined(__MINGW32__)
extern char *optarg;
extern int optind;
extern int getopt(int argc, char *const *argv, const char *opt);
#endif
#ifdef MAC_BUNDLE
#define Button ButtonOSX
#include <Carbon/Carbon.h>
@ -213,6 +201,7 @@ extern int getopt(int argc, char *const *argv, const char *opt);
#include "version.h"
#include "video.h"
#include "widgets.h"
#include "util.h"
#ifdef DEBUG
#include "missile.h" //for FreeBurningBuildingFrames

View file

@ -269,6 +269,8 @@ char *strcasestr(const char *a, const char *b)
-- Getopt
----------------------------------------------------------------------------*/
#ifndef HAVE_GETOPT
/**
** Standard implementation of getopt(3).
**
@ -278,9 +280,6 @@ char *strcasestr(const char *a, const char *b)
** an 'argument required' error.
*/
#if defined(_MSC_VER)
#include <io.h>
#include <string.h>
int opterr = 1;
@ -288,22 +287,16 @@ int optind = 1;
int optopt;
char *optarg;
static void getopt_err(char *argv0, char *str, char opt)
static void getopt_err(const char *argv0, const char *str, char opt)
{
if (opterr) {
char errbuf[2];
char *x;
errbuf[0] = opt;
errbuf[1] = '\n';
const char *x;
while ((x = strchr(argv0, '/'))) {
argv0 = x + 1;
}
write(2, argv0, strlen(argv0));
write(2, str, strlen(str));
write(2, errbuf, 2);
fprintf(stderr, "%s%s%c\n", argv0, str, opt);
}
}
@ -346,7 +339,7 @@ int getopt(int argc, char *const *argv, const char *opts)
return c;
}
#endif /* _MSC_VER */
#endif
/*----------------------------------------------------------------------------