From: Zeev Suraski Date: Thu, 10 Feb 2000 16:27:28 +0000 (+0000) Subject: Always use getopt with CGI, never use it for anything else X-Git-Tag: BEFORE_SAPIFICATION_FEB_10_2000~19 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=59b53ea2c8f1dbb4797ffac2a31caad392927b48;p=php Always use getopt with CGI, never use it for anything else --- diff --git a/main/main.c b/main/main.c index 3a1dc2e8e2..3df5d5e5dc 100644 --- a/main/main.c +++ b/main/main.c @@ -69,11 +69,6 @@ #include "SAPI.h" -#if MSVC5 || !defined(HAVE_GETOPT) -#include "php_getopt.h" -#endif - - #ifndef ZTS php_core_globals core_globals; #else diff --git a/php_getopt.h b/php_getopt.h deleted file mode 100644 index e84e117527..0000000000 --- a/php_getopt.h +++ /dev/null @@ -1,9 +0,0 @@ -/* Borrowed from Apache NT Port */ -#include "php.h" - -PHPAPI extern char *optarg; -PHPAPI extern int optind; -extern int opterr; -extern int optopt; - -PHPAPI int getopt(int argc, char* const *argv, const char *optstr); diff --git a/sapi/cgi/Makefile.in b/sapi/cgi/Makefile.in index 3519692fd1..b25c005d81 100644 --- a/sapi/cgi/Makefile.in +++ b/sapi/cgi/Makefile.in @@ -5,6 +5,6 @@ srcdir = @srcdir@ VPATH = @srcdir@ LTLIBRARY_NAME = libsapi.la -LTLIBRARY_SOURCES = cgi_main.c +LTLIBRARY_SOURCES = cgi_main.c getopt.c include $(topsrcdir)/build/ltlib.mk diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index b0634c2192..eb51a9ef95 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -70,9 +70,7 @@ #include "zend_indent.h" -#if MSVC5 || !defined(HAVE_GETOPT) #include "php_getopt.h" -#endif PHPAPI extern char *php_ini_path; @@ -80,8 +78,8 @@ PHPAPI extern char *php_ini_path; #define PHP_MODE_HIGHLIGHT 2 #define PHP_MODE_INDENT 3 -PHPAPI extern char *optarg; -PHPAPI extern int optind; +PHPAPI extern char *ap_php_optarg; +PHPAPI extern int ap_php_optind; static int sapi_cgibin_ub_write(const char *str, uint str_length) @@ -266,8 +264,8 @@ int main(int argc, char *argv[]) int behavior=PHP_MODE_STANDARD; int no_headers=0; int free_path_translated=0; - int orig_optind=optind; - char *orig_optarg=optarg; + int orig_optind=ap_php_optind; + char *orig_optarg=ap_php_optarg; #if SUPPORT_INTERACTIVE int interactive=0; #endif @@ -335,15 +333,15 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine } if (!cgi) { - while ((c=getopt(argc, argv, "c:d:qvisnaeh?vf:"))!=-1) { + while ((c=ap_php_getopt(argc, argv, "c:d:qvisnaeh?vf:"))!=-1) { switch (c) { case 'c': - php_ini_path = strdup(optarg); /* intentional leak */ + php_ini_path = strdup(ap_php_optarg); /* intentional leak */ break; } } - optind = orig_optind; - optarg = orig_optarg; + ap_php_optind = orig_optind; + ap_php_optarg = orig_optarg; } @@ -366,7 +364,7 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine if (!cgi) { /* never execute the arguments if you are a CGI */ request_info.php_argv0 = NULL; - while ((c = getopt(argc, argv, "c:d:qvisnaeh?vf:")) != -1) { + while ((c = ap_php_getopt(argc, argv, "c:d:qvisnaeh?vf:")) != -1) { switch (c) { case 'f': if (!cgi_started){ @@ -379,7 +377,7 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine SG(headers_sent) = 1; } cgi_started=1; - SG(request_info).path_translated = estrdup(optarg); + SG(request_info).path_translated = estrdup(ap_php_optarg); free_path_translated=1; /* break missing intentionally */ case 'q': @@ -437,7 +435,7 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine exit(1); break; case 'd': - define_command_line_ini_entry(optarg); + define_command_line_ini_entry(ap_php_optarg); break; default: break; @@ -465,20 +463,20 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine if (!cgi) { if (!SG(request_info).query_string) { - for (i = optind, len = 0; i < argc; i++) + for (i = ap_php_optind, len = 0; i < argc; i++) len += strlen(argv[i]) + 1; s = malloc(len + 1); /* leak - but only for command line version, so ok */ *s = '\0'; /* we are pretending it came from the environment */ - for (i = optind, len = 0; i < argc; i++) { + for (i = ap_php_optind, len = 0; i < argc; i++) { strcat(s, argv[i]); if (i < (argc - 1)) strcat(s, "+"); } SG(request_info).query_string = s; } - if (!SG(request_info).path_translated && argc > optind) - SG(request_info).path_translated = argv[optind]; + if (!SG(request_info).path_translated && argc > ap_php_optind) + SG(request_info).path_translated = argv[ap_php_optind]; } /* If for some reason the CGI interface is not setting the PATH_TRANSLATED correctly, SG(request_info).path_translated is NULL. diff --git a/getopt.c b/sapi/cgi/getopt.c similarity index 60% rename from getopt.c rename to sapi/cgi/getopt.c index 3b431f4408..e34356a6b0 100644 --- a/getopt.c +++ b/sapi/cgi/getopt.c @@ -1,7 +1,5 @@ /* Borrowed from Apache NT Port */ -#if !APACHE - #include #include #include @@ -12,16 +10,16 @@ #define OPTERRARG (3) -PHPAPI char *optarg; -PHPAPI int optind = 1; -static int opterr = 1; -static int optopt; +PHPAPI char *ap_php_optarg; +PHPAPI int ap_php_optind = 1; +static int ap_php_opterr = 1; +static int ap_php_optopt; static int -optiserr(int argc, char * const *argv, int oint, const char *optstr, +ap_php_optiserr(int argc, char * const *argv, int oint, const char *optstr, int optchr, int err) { - if (opterr) + if (ap_php_opterr) { fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); switch(err) @@ -40,22 +38,22 @@ optiserr(int argc, char * const *argv, int oint, const char *optstr, break; } } - optopt = argv[oint][optchr]; + ap_php_optopt = argv[oint][optchr]; return('?'); } -PHPAPI int getopt(int argc, char* const *argv, const char *optstr) +PHPAPI int ap_php_getopt(int argc, char* const *argv, const char *optstr) { static int optchr = 0; static int dash = 0; /* have already seen the - */ char *cp; - if (optind >= argc) + if (ap_php_optind >= argc) return(EOF); - if (!dash && (argv[optind][0] != '-')) + if (!dash && (argv[ap_php_optind][0] != '-')) return(EOF); - if (!dash && (argv[optind][0] == '-') && !argv[optind][1]) + if (!dash && (argv[ap_php_optind][0] == '-') && !argv[ap_php_optind][1]) { /* * use to specify stdin. Need to let pgm process this and @@ -63,56 +61,56 @@ PHPAPI int getopt(int argc, char* const *argv, const char *optstr) */ return(EOF); } - if ((argv[optind][0] == '-') && (argv[optind][1] == '-')) + if ((argv[ap_php_optind][0] == '-') && (argv[ap_php_optind][1] == '-')) { /* -- indicates end of args */ - optind++; + ap_php_optind++; return(EOF); } if (!dash) { - assert((argv[optind][0] == '-') && argv[optind][1]); + assert((argv[ap_php_optind][0] == '-') && argv[ap_php_optind][1]); dash = 1; optchr = 1; } /* Check if the guy tries to do a -: kind of flag */ assert(dash); - if (argv[optind][optchr] == ':') + if (argv[ap_php_optind][optchr] == ':') { dash = 0; - optind++; - return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRCOLON)); + ap_php_optind++; + return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRCOLON)); } - if (!(cp = strchr(optstr, argv[optind][optchr]))) + if (!(cp = strchr(optstr, argv[ap_php_optind][optchr]))) { - int errind = optind; + int errind = ap_php_optind; int errchr = optchr; - if (!argv[optind][optchr+1]) + if (!argv[ap_php_optind][optchr+1]) { dash = 0; - optind++; + ap_php_optind++; } else optchr++; - return(optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); + return(ap_php_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); } if (cp[1] == ':') { dash = 0; - optind++; - if (optind == argc) - return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRARG)); - optarg = argv[optind++]; + ap_php_optind++; + if (ap_php_optind == argc) + return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRARG)); + ap_php_optarg = argv[ap_php_optind++]; return(*cp); } else { - if (!argv[optind][optchr+1]) + if (!argv[ap_php_optind][optchr+1]) { dash = 0; - optind++; + ap_php_optind++; } else optchr++; @@ -122,21 +120,19 @@ PHPAPI int getopt(int argc, char* const *argv, const char *optstr) return(0); } -#endif /* !APACHE */ - #ifdef TESTGETOPT int main (int argc, char **argv) { int c; - extern char *optarg; - extern int optind; + extern char *ap_php_optarg; + extern int ap_php_optind; int aflg = 0; int bflg = 0; int errflg = 0; char *ofile = NULL; - while ((c = getopt(argc, argv, "abo:")) != EOF) + while ((c = ap_php_getopt(argc, argv, "abo:")) != EOF) switch (c) { case 'a': if (bflg) @@ -151,7 +147,7 @@ int bflg++; break; case 'o': - ofile = optarg; + ofile = ap_php_optarg; (void)printf("ofile = %s\n", ofile); break; case '?': @@ -162,8 +158,8 @@ int "usage: cmd [-a|-b] [-o ] files...\n"); exit (2); } - for ( ; optind < argc; optind++) - (void)printf("%s\n", argv[optind]); + for ( ; ap_php_optind < argc; ap_php_optind++) + (void)printf("%s\n", argv[ap_php_optind]); return 0; } diff --git a/sapi/cgi/php_getopt.h b/sapi/cgi/php_getopt.h new file mode 100644 index 0000000000..5f3af64b64 --- /dev/null +++ b/sapi/cgi/php_getopt.h @@ -0,0 +1,9 @@ +/* Borrowed from Apache NT Port */ +#include "php.h" + +PHPAPI extern char *ap_php_optarg; +PHPAPI extern int ap_php_optind; +extern int ap_php_opterr; +extern int ap_php_optopt; + +PHPAPI int ap_php_getopt(int argc, char* const *argv, const char *optstr); diff --git a/sapi/servlet/servlet.c b/sapi/servlet/servlet.c index 463baeaecb..3cc4e6e3dc 100644 --- a/sapi/servlet/servlet.c +++ b/sapi/servlet/servlet.c @@ -67,18 +67,11 @@ #include "zend_highlight.h" #include "zend_indent.h" -#if WIN32|WINNT || !defined(HAVE_GETOPT) -#include "php_getopt.h" -#endif - PHPAPI extern char *php_ini_path; JNIEXPORT void JNICALL Java_net_php_reflect_setEnv (JNIEnv *newJenv, jclass self); -PHPAPI extern char *optarg; -PHPAPI extern int optind; - typedef struct { JNIEnv *jenv; jobject servlet;