Move our own getopt() implementation to _PyOS_GetOpt(), and use it
regardless of whether the system getopt() does what we want. This avoids the
hassle with prototypes and externs, and the check to see if the system
getopt() does what we want. Prefix optind, optarg and opterr with _PyOS_ to
avoid name clashes. Add new include file to define the right symbols. Fix
Demo/pyserv/pyserv.c to include getopt.h itself, instead of relying on
Python to provide it.
diff --git a/Modules/main.c b/Modules/main.c
index b4d7cdf..c508eb6 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -17,15 +17,11 @@
 #define PYTHONHOMEHELP "<prefix>/python2.0"
 #endif
 
+#include "pygetopt.h"
+
 #define COPYRIGHT \
     "Type \"copyright\", \"credits\" or \"license\" for more information."
 
-/* Interface to getopt(): */
-extern int optind;
-extern char *optarg;
-extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
-
-
 /* For Py_GetArgcArgv(); set by main() */
 static char **orig_argv;
 static int  orig_argc;
@@ -105,16 +101,16 @@
 	if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
 		unbuffered = 1;
 
-	while ((c = getopt(argc, argv, "c:diOStuUvxXhV")) != EOF) {
+	while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhV")) != EOF) {
 		if (c == 'c') {
 			/* -c is the last option; following arguments
 			   that look like options are left for the
 			   the command to interpret. */
-			command = malloc(strlen(optarg) + 2);
+			command = malloc(strlen(_PyOS_optarg) + 2);
 			if (command == NULL)
 				Py_FatalError(
 				   "not enough memory to copy -c argument");
-			strcpy(command, optarg);
+			strcpy(command, _PyOS_optarg);
 			strcat(command, "\n");
 			break;
 		}
@@ -181,10 +177,10 @@
 		exit(0);
 	}
 
-	if (command == NULL && optind < argc &&
-	    strcmp(argv[optind], "-") != 0)
+	if (command == NULL && _PyOS_optind < argc &&
+	    strcmp(argv[_PyOS_optind], "-") != 0)
 	{
-		filename = argv[optind];
+		filename = argv[_PyOS_optind];
 		if (filename != NULL) {
 			if ((fp = fopen(filename, "r")) == NULL) {
 				fprintf(stderr, "%s: can't open file '%s'\n",
@@ -253,12 +249,12 @@
 	
 	
 	if (command != NULL) {
-		/* Backup optind and force sys.argv[0] = '-c' */
-		optind--;
-		argv[optind] = "-c";
+		/* Backup _PyOS_optind and force sys.argv[0] = '-c' */
+		_PyOS_optind--;
+		argv[_PyOS_optind] = "-c";
 	}
 
-	PySys_SetArgv(argc-optind, argv+optind);
+	PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
 
 	if ((inspect || (command == NULL && filename == NULL)) &&
 	    isatty(fileno(stdin))) {