Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / php / ext / standard / link.c
blob8da63958a7ba22590ff4fddaddd27439345238c5
1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2013 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: |
16 +----------------------------------------------------------------------+
19 /* $Id$ */
21 #include "php.h"
22 #include "php_filestat.h"
23 #include "php_globals.h"
25 #ifdef HAVE_SYMLINK
27 #include <stdlib.h>
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <sys/stat.h>
32 #include <string.h>
33 #if HAVE_PWD_H
34 #ifdef PHP_WIN32
35 #include "win32/pwd.h"
36 #else
37 #include <pwd.h>
38 #endif
39 #endif
40 #if HAVE_GRP_H
41 #ifdef PHP_WIN32
42 #include "win32/grp.h"
43 #else
44 #include <grp.h>
45 #endif
46 #endif
47 #include <errno.h>
48 #include <ctype.h>
50 #include "php_link.h"
51 #include "php_string.h"
53 /* {{{ proto string readlink(string filename)
54 Return the target of a symbolic link */
55 PHP_FUNCTION(readlink)
57 char *link;
58 int link_len;
59 char buff[MAXPATHLEN];
60 int ret;
62 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &link, &link_len) == FAILURE) {
63 return;
66 if (php_check_open_basedir(link TSRMLS_CC)) {
67 RETURN_FALSE;
70 ret = php_sys_readlink(link, buff, MAXPATHLEN-1);
72 if (ret == -1) {
73 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
74 RETURN_FALSE;
76 /* Append NULL to the end of the string */
77 buff[ret] = '\0';
79 RETURN_STRING(buff, 1);
81 /* }}} */
83 /* {{{ proto int linkinfo(string filename)
84 Returns the st_dev field of the UNIX C stat structure describing the link */
85 PHP_FUNCTION(linkinfo)
87 char *link;
88 char *dirname;
89 int link_len, dir_len;
90 struct stat sb;
91 int ret;
93 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &link, &link_len) == FAILURE) {
94 return;
97 dirname = estrndup(link, link_len);
98 dir_len = php_dirname(dirname, link_len);
100 if (php_check_open_basedir(dirname TSRMLS_CC)) {
101 efree(dirname);
102 RETURN_FALSE;
105 ret = VCWD_LSTAT(link, &sb);
106 if (ret == -1) {
107 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
108 efree(dirname);
109 RETURN_LONG(-1L);
112 efree(dirname);
113 RETURN_LONG((long) sb.st_dev);
115 /* }}} */
117 /* {{{ proto int symlink(string target, string link)
118 Create a symbolic link */
119 PHP_FUNCTION(symlink)
121 char *topath, *frompath;
122 int topath_len, frompath_len;
123 int ret;
124 char source_p[MAXPATHLEN];
125 char dest_p[MAXPATHLEN];
126 char dirname[MAXPATHLEN];
127 size_t len;
129 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pp", &topath, &topath_len, &frompath, &frompath_len) == FAILURE) {
130 return;
133 if (!expand_filepath(frompath, source_p TSRMLS_CC)) {
134 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
135 RETURN_FALSE;
138 memcpy(dirname, source_p, sizeof(source_p));
139 len = php_dirname(dirname, strlen(dirname));
141 if (!expand_filepath_ex(topath, dest_p, dirname, len TSRMLS_CC)) {
142 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
143 RETURN_FALSE;
146 if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) ||
147 php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) )
149 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to symlink to a URL");
150 RETURN_FALSE;
153 if (php_check_open_basedir(dest_p TSRMLS_CC)) {
154 RETURN_FALSE;
157 if (php_check_open_basedir(source_p TSRMLS_CC)) {
158 RETURN_FALSE;
161 /* For the source, an expanded path must be used (in ZTS an other thread could have changed the CWD).
162 * For the target the exact string given by the user must be used, relative or not, existing or not.
163 * The target is relative to the link itself, not to the CWD. */
164 ret = symlink(topath, source_p);
166 if (ret == -1) {
167 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
168 RETURN_FALSE;
171 RETURN_TRUE;
173 /* }}} */
175 /* {{{ proto int link(string target, string link)
176 Create a hard link */
177 PHP_FUNCTION(link)
179 char *topath, *frompath;
180 int topath_len, frompath_len;
181 int ret;
182 char source_p[MAXPATHLEN];
183 char dest_p[MAXPATHLEN];
185 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pp", &topath, &topath_len, &frompath, &frompath_len) == FAILURE) {
186 return;
189 if (!expand_filepath(frompath, source_p TSRMLS_CC) || !expand_filepath(topath, dest_p TSRMLS_CC)) {
190 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
191 RETURN_FALSE;
194 if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) ||
195 php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) )
197 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to link to a URL");
198 RETURN_FALSE;
201 if (php_check_open_basedir(dest_p TSRMLS_CC)) {
202 RETURN_FALSE;
205 if (php_check_open_basedir(source_p TSRMLS_CC)) {
206 RETURN_FALSE;
209 #ifndef ZTS
210 ret = link(topath, frompath);
211 #else
212 ret = link(dest_p, source_p);
213 #endif
214 if (ret == -1) {
215 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
216 RETURN_FALSE;
219 RETURN_TRUE;
221 /* }}} */
223 #endif
226 * Local variables:
227 * tab-width: 4
228 * c-basic-offset: 4
229 * End:
230 * vim600: noet sw=4 ts=4 fdm=marker
231 * vim<600: noet sw=4 ts=4