]> granicus.if.org Git - php/commitdiff
Always use getopt with CGI, never use it for anything else
authorZeev Suraski <zeev@php.net>
Thu, 10 Feb 2000 16:27:28 +0000 (16:27 +0000)
committerZeev Suraski <zeev@php.net>
Thu, 10 Feb 2000 16:27:28 +0000 (16:27 +0000)
main/main.c
php_getopt.h [deleted file]
sapi/cgi/Makefile.in
sapi/cgi/cgi_main.c
sapi/cgi/getopt.c [moved from getopt.c with 60% similarity]
sapi/cgi/php_getopt.h [new file with mode: 0644]
sapi/servlet/servlet.c

index 3a1dc2e8e26673d871e01f08e3302fc96751afce..3df5d5e5dc01c235a76f58eef520bffa69323b89 100644 (file)
 
 #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 (file)
index e84e117..0000000
+++ /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);
index 3519692fd17972898e3bcea7b0254d12398e8c74..b25c005d81979d163a005f77907303a1eca4d1fe 100644 (file)
@@ -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
index b0634c21926e5839afb991c2a3a93456fb41a64e..eb51a9ef95625dce0ddc9ad715cfd54a6b40d02d 100644 (file)
@@ -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.
similarity index 60%
rename from getopt.c
rename to sapi/cgi/getopt.c
index 3b431f4408d189f1a3f491c82972a16c17fbc2e7..e34356a6b0065948613ffd3d8e5c71791369f7c0 100644 (file)
--- a/getopt.c
@@ -1,7 +1,5 @@
 /* Borrowed from Apache NT Port */
 
-#if !APACHE
-
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
 #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 <filename>] 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 (file)
index 0000000..5f3af64
--- /dev/null
@@ -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);
index 463baeaecbc5df1cbddd34267aaa904a2448cc1f..3cc4e6e3dcdf1c33070b9837c1cc6c0af9011835 100644 (file)
 #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;