]> granicus.if.org Git - php/commitdiff
cleanup, flock() support
authorSascha Schumann <sas@php.net>
Wed, 21 Apr 1999 18:10:18 +0000 (18:10 +0000)
committerSascha Schumann <sas@php.net>
Wed, 21 Apr 1999 18:10:18 +0000 (18:10 +0000)
ChangeLog.TODO
ext/standard/file.c
ext/standard/file.h

index a76d834d6f43cdf9286a0ac6820a59d62b4d830a..b44729dc49fdbf8609767922c3d5ff6b58dcf5b1 100644 (file)
@@ -53,7 +53,6 @@ March 1 1999, Version 3.0.7
 - Add another version of WDDX module
   (we need to pick a single implementation here)
 - Fixed includes for iODBC to support both the old and the new LGPL version
-- Add flock() function
 - Fix implode() bug - When imploding an array that contained unset() elements
   it wasn't correctly skipping past these
 - Add connection_status() function.  This returns the raw bitfield which
index 0bd00625a43bfa24a76cc071372c1d2a35e3a43f..066c69420b94e351a9d61e697d74b79b66618822 100644 (file)
@@ -198,6 +198,7 @@ function_entry php3_file_functions[] = {
        {"copy",                php3_file_copy, NULL},
        {"tempnam",             php3_tempnam,   NULL},
        {"file",                php3_file,              NULL},
+    PHP_FE(flock, NULL)
        {"get_meta_tags",       php3_get_meta_tags,     NULL},
        {"set_socket_blocking", php3_set_socket_blocking,       NULL},
 #if (0 && HAVE_SYS_TIME_H && HAVE_SETSOCKOPT && defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO))
@@ -210,6 +211,59 @@ php3_module_entry php3_file_module_entry = {
        "PHP_file", php3_file_functions, php3_minit_file, NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
 };
 
+static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN };
+
+/* {{{ proto bool flock(int fp, int operation)
+   portable file locking */
+PHP_FUNCTION(flock)
+{
+    pval *arg1, *arg2;
+    FILE *fp;
+    int type;
+    int issock=0;
+    int *sock, fd=0;
+    int act = 0;
+    TLS_VARS;
+
+    if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) {
+        WRONG_PARAM_COUNT;
+    }
+
+    convert_to_long(arg1);
+    convert_to_long(arg2);
+
+    fp = php3_list_find(arg1->value.lval, &type);
+    if (type == GLOBAL(wsa_fp)){
+        issock = 1;
+        sock = php3_list_find(arg1->value.lval, &type);
+        fd = *sock;
+    }
+
+    if ((!fp || (type!=GLOBAL(le_fp) && type!=GLOBAL(le_pp))) && (!fd || type!=GLOBAL(wsa_fp))) {
+        php3_error(E_WARNING,"Unable to find file identifier %d",arg1->value.lval);
+        RETURN_FALSE;
+    }
+
+    if (!issock) {
+        fd = fileno(fp);
+    }
+
+    act = arg2->value.lval & 3;
+    if(act < 1 || act > 3) {
+            php3_error(E_WARNING, "illegal value for second argument");
+            RETURN_FALSE;
+    }
+    /* flock_values contains all possible actions
+       if (arg2 & 4) we won't block on the lock */
+    act = flock_values[act - 1] | (arg2->value.lval & 4 ? LOCK_NB : 0);
+    if (flock(fd, act) == -1) {
+        RETURN_FALSE;
+    }
+
+    RETURN_TRUE;
+}
+/* }}} */
+
 
 /* {{{ proto array get_meta_tags(string filename [, int use_include_path])
        Extracts all meta tag content attributes from a file and returns an array */
index 08f1440f13ad9e0061a6fd9dacbc454b6d077463..e0f1340119115f5570116d635c83bb0e9ddf92ef 100644 (file)
@@ -61,5 +61,6 @@ extern void php3_file(INTERNAL_FUNCTION_PARAMETERS);
 extern void php3_set_socket_blocking(INTERNAL_FUNCTION_PARAMETERS);
 extern void php3_set_socket_timeout(INTERNAL_FUNCTION_PARAMETERS);
 extern void php3_get_meta_tags(INTERNAL_FUNCTION_PARAMETERS);
+extern PHP_FUNCTION(flock);
 
 #endif /* _FILE_H */