]> granicus.if.org Git - php/commitdiff
port some fixes, add compat routines
authorSascha Schumann <sas@php.net>
Wed, 21 Apr 1999 17:11:01 +0000 (17:11 +0000)
committerSascha Schumann <sas@php.net>
Wed, 21 Apr 1999 17:11:01 +0000 (17:11 +0000)
ChangeLog.TODO
Makefile.in
ext/ereg/ereg.c
ext/standard/datetime.c
ext/standard/reg.c
ext/standard/string.c
main/fopen_wrappers.c
main/php.h
main/php_compat.h [new file with mode: 0644]
php_compat.c [new file with mode: 0644]

index f4a1e8a347b945770b0f92ec796b8332df7378f8..e503630983c7ef658117ea6fa76483c84415a9ae 100644 (file)
@@ -10,11 +10,6 @@ over to PHP4.
 - added Oracle-OCI8 persistent connections
 - fixed OCIDefineByName crash.
 - fixed some NULL-column related problems in OCI8-module.
-- avoid stripping double slashes when fopen'ing files (Sascha)
-- sql_regcase() change: only apply to alphabetic characters (Sascha)
-- add snmprealwalk() (Sascha Schumann)
-- fix serious bug in strftime() (Sascha Schumann)
-- fix bug in ucwords() (Sascha Schumann)
 - optimized fgets() and fread()
 - memory leak in operators.c fixed
 - socket leak fixed (Sascha Schumann)
index f363e5eac8bb7eac9541f093a79c289185a689e4..90185c5956647ccae8fc656dc5830ed32f7a359d 100644 (file)
@@ -62,11 +62,11 @@ WARNING_LEVEL = @WARNING_LEVEL@
 SOURCE = main.c internal_functions.c snprintf.c php3_sprintf.c \
        configuration-parser.c configuration-scanner.c request_info.c \
        safe_mode.c fopen-wrappers.c php3_realpath.c alloca.c output.c \
-       php_ini.c
+       php_ini.c php_compat.c
 OBJS = main.o internal_functions.o snprintf.o php3_sprintf.o \
        configuration-parser.o configuration-scanner.o request_info.o \
        safe_mode.o fopen-wrappers.o php3_realpath.o alloca.o output.o \
-       php_ini.o
+       php_ini.o php_compat.o
 
 FUNCTIONS_SOURCE = functions/apache.c functions/fhttpd.c \
        functions/crypt.c functions/db.c functions/dl.c functions/filepro.c \
@@ -184,6 +184,7 @@ indent:     clean
        rm -f *~ functions/*~
 
 .c.o:
+       @rm -f $@
        $(CC) $(CFLAGS) -c $< -o $@
        @bn=`echo $@ | sed -e 's#functions/##'`; test -f $@ || \
          (test "$@" != "$$bn" && test -f "$$bn" && mv $$bn $@)
index 42d36493d66c3ece70c6fbfb06333f9e1fbe1d17..8b81e82e7fe2fce7de99ae596aa529396eca618a 100644 (file)
@@ -537,7 +537,8 @@ PHPAPI void php3_sql_regcase(INTERNAL_FUNCTION_PARAMETERS)
 {
        pval *string;
        char *tmp;
-       register int i;
+       unsigned char c;
+       register int i, j;
        
        if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &string)==FAILURE) {
                WRONG_PARAM_COUNT;
@@ -547,17 +548,22 @@ PHPAPI void php3_sql_regcase(INTERNAL_FUNCTION_PARAMETERS)
        
        tmp = (char *) emalloc(string->value.str.len*4+1);
        
-       for (i=0; i<string->value.str.len; i++) {
-               tmp[i*4] = '[';
-               tmp[i*4+1]=toupper((unsigned char)string->value.str.val[i]);
-               tmp[i*4+2]=tolower((unsigned char)string->value.str.val[i]);
-               tmp[i*4+3]=']';
+       for (i=j=0; i<string->value.str.len; i++) {
+               c = (unsigned char) string->value.str.val[i];
+               if(isalpha(c)) {
+                       tmp[j++] = '[';
+                       tmp[j++] = toupper(c);
+                       tmp[j++] = tolower(c);
+                       tmp[j++] = ']';
+               } else {
+                       tmp[j++] = c;
+               }
        }
-       tmp[string->value.str.len*4]=0;
+       tmp[j]=0;
+       
+       tmp = erealloc(tmp, j + 1);
        
-       return_value->value.str.val = tmp;
-       return_value->value.str.len = string->value.str.len*4;
-       return_value->type = IS_STRING;
+       RETVAL_STRINGL(tmp, j, 0);
 }
 /* }}} */
 
index 3c82fe09185a90b7ce93ea85522fe44b90657965..191371a8c2b1f8d18511d7ce546b1fb296fe5ccd 100644 (file)
@@ -463,6 +463,7 @@ void php3_strftime(INTERNAL_FUNCTION_PARAMETERS)
        char *format,*buf;
        time_t timestamp;
        struct tm *ta;
+       int max_reallocs = 5;
        size_t buf_len=64, real_len;
 
        switch (ARG_COUNT(ht)) {
@@ -495,11 +496,15 @@ void php3_strftime(INTERNAL_FUNCTION_PARAMETERS)
        while ((real_len=strftime(buf,buf_len,format,ta))==buf_len || real_len==0) {
                buf_len *= 2;
                buf = (char *) erealloc(buf, buf_len);
+               if(!--max_reallocs) break;
        }
        
-       return_value->value.str.val = (char *) erealloc(buf,real_len+1);
-       return_value->value.str.len = real_len;
-       return_value->type = IS_STRING;
+       if(real_len && real_len != buf_len) {
+               buf = (char *) erealloc(buf,real_len+1);
+               RETURN_STRINGL(buf, real_len, 0);
+       }
+       efree(buf);
+       RETURN_FALSE;
 }
 #endif
 /*
index 42d36493d66c3ece70c6fbfb06333f9e1fbe1d17..8b81e82e7fe2fce7de99ae596aa529396eca618a 100644 (file)
@@ -537,7 +537,8 @@ PHPAPI void php3_sql_regcase(INTERNAL_FUNCTION_PARAMETERS)
 {
        pval *string;
        char *tmp;
-       register int i;
+       unsigned char c;
+       register int i, j;
        
        if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &string)==FAILURE) {
                WRONG_PARAM_COUNT;
@@ -547,17 +548,22 @@ PHPAPI void php3_sql_regcase(INTERNAL_FUNCTION_PARAMETERS)
        
        tmp = (char *) emalloc(string->value.str.len*4+1);
        
-       for (i=0; i<string->value.str.len; i++) {
-               tmp[i*4] = '[';
-               tmp[i*4+1]=toupper((unsigned char)string->value.str.val[i]);
-               tmp[i*4+2]=tolower((unsigned char)string->value.str.val[i]);
-               tmp[i*4+3]=']';
+       for (i=j=0; i<string->value.str.len; i++) {
+               c = (unsigned char) string->value.str.val[i];
+               if(isalpha(c)) {
+                       tmp[j++] = '[';
+                       tmp[j++] = toupper(c);
+                       tmp[j++] = tolower(c);
+                       tmp[j++] = ']';
+               } else {
+                       tmp[j++] = c;
+               }
        }
-       tmp[string->value.str.len*4]=0;
+       tmp[j]=0;
+       
+       tmp = erealloc(tmp, j + 1);
        
-       return_value->value.str.val = tmp;
-       return_value->value.str.len = string->value.str.len*4;
-       return_value->type = IS_STRING;
+       RETVAL_STRINGL(tmp, j, 0);
 }
 /* }}} */
 
index 0750aa873432ca3eb9e410f8030460045359c067..b9db2614b9954f62c73c575cdec11ac5f9bb9d15 100644 (file)
@@ -972,7 +972,7 @@ void php3_ucwords(INTERNAL_FUNCTION_PARAMETERS)
                if(*(r+1)){
                        r++;
                        *r=toupper((unsigned char)*r);
-               }
+               } else break;
        }
        RETVAL_STRING(arg->value.str.val,1);
 }
index 206bfa5c76f2462418266a9521f112e1e03c0a74..01a550a958be4201a7e8d7657418e4f8346ea7a8 100644 (file)
@@ -131,6 +131,7 @@ PHPAPI int _php3_check_open_basedir(char *path)
                                local_open_basedir[local_open_basedir_pos--] = 0;
                        }
                        
+#if 0
                        /* Strip double (back)slashes */
                        if (local_open_basedir_pos > 0) {
                                while ((
@@ -144,7 +145,8 @@ PHPAPI int _php3_check_open_basedir(char *path)
                                        local_open_basedir[local_open_basedir_pos--] = 0;
                                }
                        }
-                       
+#endif
+
                } else {
                        /* Else use the unmodified path */
                        strcpy(local_open_basedir, PG(open_basedir));
index 06ae5ce7ffa85efa33a58da385c880c8cf1bf969..72434c72928a336845a4400b421add15b85209f3 100644 (file)
@@ -510,6 +510,7 @@ extern void phprestart(FILE *input_file);
 #define XtOffsetOf(s_type,field) XtOffset(s_type*,field)
 #endif
 
+#include "php_compat.h"
 
 #endif
 
diff --git a/main/php_compat.h b/main/php_compat.h
new file mode 100644 (file)
index 0000000..2c4aa3e
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef PHP_COMPAT_H
+#define PHP_COMPAT_H
+
+#if (WIN32|WINNT) & HAVE_BINDLIB
+#ifndef WINNT
+#define WINNT 1
+#endif
+#include "arpa/inet.h"
+#include "netdb.h"
+#include "arpa/nameser.h"
+#include "resolv.h"
+#endif
+
+#if !HAVE_FLOCK
+
+#define LOCK_SH 1
+#define LOCK_EX 2
+#define LOCK_NB 4
+#define LOCK_UN 8
+
+extern PHPAPI int flock(int fd, int operation);
+
+#endif
+
+#if WIN32|WINNT
+
+/* These were in win32/flock.h, dunno if they are really needed
+   (or maybe break something) */
+
+#define fsync _commit
+#define ftruncate chsize
+
+#endif /* WIN32|WINNT */
+
+#if !HAVE_INET_ATON
+extern PHPAPI int inet_aton(const char *, struct in_addr *);
+#endif
+
+#endif
diff --git a/php_compat.c b/php_compat.c
new file mode 100644 (file)
index 0000000..8a5f973
--- /dev/null
@@ -0,0 +1,230 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP HTML Embedded Scripting Language Version 3.0                     |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-1999 PHP Development Team (See Credits file)      |
+   +----------------------------------------------------------------------+
+   | This program is free software; you can redistribute it and/or modify |
+   | it under the terms of one of the following licenses:                 |
+   |                                                                      |
+   |  A) the GNU General Public License as published by the Free Software |
+   |     Foundation; either version 2 of the License, or (at your option) |
+   |     any later version.                                               |
+   |                                                                      |
+   |  B) the PHP License as published by the PHP Development Team and     |
+   |     included in the distribution in the file: LICENSE                |
+   |                                                                      |
+   | This program is distributed in the hope that it will be useful,      |
+   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
+   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
+   | GNU General Public License for more details.                         |
+   |                                                                      |
+   | You should have received a copy of both licenses referred to here.   |
+   | If you did not, or have any questions about PHP licensing, please    |
+   | contact core@php.net.                                                |
+   +----------------------------------------------------------------------+
+   | Authors: Sascha Schumann <sas@schell.de>                             |
+   +----------------------------------------------------------------------+
+
+   $Id$
+ */
+
+
+#include <errno.h>
+#include "php_compat.h"
+
+#if HAVE_STRUCT_FLOCK
+#include <unistd.h>
+#include <fcntl.h>
+#endif
+
+#if WIN32|WINNT
+#include <windows.h>
+#include <io.h>
+#endif
+
+#if !HAVE_FLOCK
+PHPAPI int flock(int fd, int operation)
+#if HAVE_STRUCT_FLOCK
+{
+       struct flock flck;
+       int ret;
+
+       flck.l_start = flck.l_len = 0;
+       flck.l_whence = SEEK_SET;
+       
+       if (operation & LOCK_SH)
+               flck.l_type = F_RDLCK;
+       else if (operation & LOCK_EX)
+               flck.l_type = F_WRLCK;
+       else if (operation & LOCK_UN)
+               flck.l_type = F_UNLCK;
+       else {
+               errno = EINVAL;
+               return -1;
+       }
+
+       ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck);
+
+       if (operation & LOCK_NB && ret == -1 && 
+                       (errno == EACCES || errno == EAGAIN))
+               errno = EWOULDBLOCK;
+
+       if (ret != -1) ret = 0;
+
+       return ret;
+}
+#elif WIN32|WINNT
+/*
+ * Program:   Unix compatibility routines
+ *
+ * Author:  Mark Crispin
+ *      Networks and Distributed Computing
+ *      Computing & Communications
+ *      University of Washington
+ *      Administration Building, AG-44
+ *      Seattle, WA  98195
+ *      Internet: MRC@CAC.Washington.EDU
+ *
+ * Date:    14 September 1996
+ * Last Edited: 14 August 1997
+ *
+ * Copyright 1997 by the University of Washington
+ *
+ *  Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted, provided
+ * that the above copyright notice appears in all copies and that both the
+ * above copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the University of Washington not be
+ * used in advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  This software is made available
+ * "as is", and
+ * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
+ * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
+ * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
+ * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+/*              DEDICATION
+
+ *  This file is dedicated to my dog, Unix, also known as Yun-chan and
+ * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast.  Unix
+ * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
+ * a two-month bout with cirrhosis of the liver.
+ *
+ *  He was a dear friend, and I miss him terribly.
+ *
+ *  Lift a leg, Yunie.  Luv ya forever!!!!
+ */
+{
+    HANDLE hdl = (HANDLE) _get_osfhandle(fd);
+    DWORD low = 1, high = 0;
+    OVERLAPPED offset =
+    {0, 0, 0, 0, NULL};
+    if (hdl < 0)
+        return -1;              /* error in file descriptor */
+    /* bug for bug compatible with Unix */
+    UnlockFileEx(hdl, 0, low, high, &offset);
+    switch (operation & ~LOCK_NB) {    /* translate to LockFileEx() op */
+        case LOCK_EX:           /* exclusive */
+            if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
+                        ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
+                           0, low, high, &offset))
+                return 0;
+            break;
+        case LOCK_SH:           /* shared */
+            if (LockFileEx(hdl, ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
+                           0, low, high, &offset))
+                return 0;
+            break;
+        case LOCK_UN:           /* unlock */
+            return 0;           /* always succeeds */
+        default:                /* default */
+            break;
+    }
+       /* Under Win32 MT library, errno is not a variable but a function call,
+        * which cannot be assigned to.
+        */
+#if !(WIN32|WINNT)
+    errno = EINVAL;             /* bad call */
+#endif
+    return -1;
+}
+#else
+#warning no proper flock support for your site
+{
+       errno = 0;
+       return 0;
+}
+#endif
+#endif /* !HAVE_FLOCK */
+
+#if !(HAVE_INET_ATON)
+
+/* 
+ * Check whether "cp" is a valid ascii representation
+ * of an Internet address and convert to a binary address.
+ * Returns 1 if the address is valid, 0 if not.
+ * This replaces inet_addr, the return value from which
+ * cannot distinguish between failure and a local broadcast address.
+ */
+
+PHPAPI int
+inet_aton(const char *cp, struct in_addr *ap)
+{
+    int dots = 0;
+    register unsigned long acc = 0, addr = 0;
+
+    do {
+        register char cc = *cp;
+
+        switch (cc) {
+        case '0':
+        case '1':
+        case '2':
+        case '3':
+        case '4':
+        case '5':
+        case '6':
+        case '7':
+        case '8':
+        case '9':
+            acc = acc * 10 + (cc - '0');
+            break;
+
+        case '.':
+            if (++dots > 3) {
+                return 0;
+            }
+            /* Fall through */
+
+        case '\0':
+            if (acc > 255) {
+                return 0;
+            }
+            addr = addr << 8 | acc;
+            acc = 0;
+            break;
+
+        default:
+            return 0;
+        }
+    } while (*cp++) ;
+
+    /* Normalize the address */
+    if (dots < 3) {
+        addr <<= 8 * (3 - dots) ;
+    }
+
+    /* Store it if requested */
+    if (ap) {
+        ap->s_addr = htonl(addr);
+    }
+
+    return 1;    
+}
+
+#endif /* !HAVE_INET_ATON */