2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
16 +----------------------------------------------------------------------+
22 #include "php_filestat.h"
23 #include "php_globals.h"
35 #include "win32/pwd.h"
42 #include "win32/grp.h"
51 #include "php_string.h"
53 /* {{{ proto string readlink(string filename)
54 Return the target of a symbolic link */
55 PHP_FUNCTION(readlink
)
59 char buff
[MAXPATHLEN
];
62 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &link
, &link_len
) == FAILURE
) {
66 if (php_check_open_basedir(link TSRMLS_CC
)) {
70 ret
= php_sys_readlink(link
, buff
, MAXPATHLEN
-1);
73 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "%s", strerror(errno
));
76 /* Append NULL to the end of the string */
79 RETURN_STRING(buff
, 1);
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
)
89 int link_len
, dir_len
;
93 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "p", &link
, &link_len
) == FAILURE
) {
97 dirname
= estrndup(link
, link_len
);
98 dir_len
= php_dirname(dirname
, link_len
);
100 if (php_check_open_basedir(dirname TSRMLS_CC
)) {
105 ret
= VCWD_LSTAT(link
, &sb
);
107 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "%s", strerror(errno
));
113 RETURN_LONG((long) sb
.st_dev
);
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
;
124 char source_p
[MAXPATHLEN
];
125 char dest_p
[MAXPATHLEN
];
126 char dirname
[MAXPATHLEN
];
129 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "pp", &topath
, &topath_len
, &frompath
, &frompath_len
) == FAILURE
) {
133 if (!expand_filepath(frompath
, source_p TSRMLS_CC
)) {
134 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "No such file or directory");
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");
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");
153 if (php_check_open_basedir(dest_p TSRMLS_CC
)) {
157 if (php_check_open_basedir(source_p TSRMLS_CC
)) {
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
);
167 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "%s", strerror(errno
));
175 /* {{{ proto int link(string target, string link)
176 Create a hard link */
179 char *topath
, *frompath
;
180 int topath_len
, frompath_len
;
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
) {
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");
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");
201 if (php_check_open_basedir(dest_p TSRMLS_CC
)) {
205 if (php_check_open_basedir(source_p TSRMLS_CC
)) {
210 ret
= link(topath
, frompath
);
212 ret
= link(dest_p
, source_p
);
215 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "%s", strerror(errno
));
230 * vim600: noet sw=4 ts=4 fdm=marker
231 * vim<600: noet sw=4 ts=4