Skip to content

Commit 1c7d86a

Browse files
committed
Added new function: posix_mknod().
1 parent 4f778ca commit 1c7d86a

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ PHP NEWS
8686
. inet_ntop() (Sara)
8787
. mysqli::client_info property (Georg)
8888
. posix_access() (Magnus)
89+
. posix_mknod() (Magnus)
8990
. SimpleXMLElement::registerXPathNamespace() (Christian)
9091
. stream_context_get_default() (Wez)
9192
. stream_socket_enable_crypto() (Wez)

ext/posix/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ if test "$PHP_POSIX" = "yes"; then
99
AC_DEFINE(HAVE_POSIX, 1, [whether to include POSIX-like functions])
1010
PHP_NEW_EXTENSION(posix, posix.c, $ext_shared)
1111

12-
AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo getrlimit getlogin getgroups)
12+
AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo mknod getrlimit getlogin getgroups)
1313
fi

ext/posix/php_posix.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ PHP_FUNCTION(posix_getcwd);
9393
#ifdef HAVE_MKFIFO
9494
PHP_FUNCTION(posix_mkfifo);
9595
#endif
96+
#ifdef HAVE_MKNOD
97+
PHP_FUNCTION(posix_mknod);
98+
#endif
9699

97100
/* POSIX.1, 5.6 */
98101
PHP_FUNCTION(posix_access);

ext/posix/posix.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ function_entry posix_functions[] = {
111111
#ifdef HAVE_MKFIFO
112112
PHP_FE(posix_mkfifo, NULL)
113113
#endif
114+
#ifdef HAVE_MKNOD
115+
PHP_FE(posix_mknod, NULL)
116+
#endif
114117

115118
/* POSIX.1, 5.6 */
116119
PHP_FE(posix_access, NULL)
@@ -156,6 +159,22 @@ static PHP_MINIT_FUNCTION(posix)
156159
REGISTER_LONG_CONSTANT("POSIX_X_OK", X_OK, CONST_CS | CONST_PERSISTENT);
157160
REGISTER_LONG_CONSTANT("POSIX_W_OK", W_OK, CONST_CS | CONST_PERSISTENT);
158161
REGISTER_LONG_CONSTANT("POSIX_R_OK", R_OK, CONST_CS | CONST_PERSISTENT);
162+
#ifdef S_IFREG
163+
REGISTER_LONG_CONSTANT("POSIX_S_IFREG", S_IFREG, CONST_CS | CONST_PERSISTENT);
164+
#endif
165+
#ifdef S_IFCHR
166+
REGISTER_LONG_CONSTANT("POSIX_S_IFCHR", S_IFCHR, CONST_CS | CONST_PERSISTENT);
167+
#endif
168+
#ifdef S_IFBLK
169+
REGISTER_LONG_CONSTANT("POSIX_S_IFBLK", S_IFBLK, CONST_CS | CONST_PERSISTENT);
170+
#endif
171+
#ifdef S_IFIFO
172+
REGISTER_LONG_CONSTANT("POSIX_S_IFIFO", S_IFIFO, CONST_CS | CONST_PERSISTENT);
173+
#endif
174+
#ifdef S_IFSOCK
175+
REGISTER_LONG_CONSTANT("POSIX_S_IFSOCK", S_IFSOCK, CONST_CS | CONST_PERSISTENT);
176+
#endif
177+
159178
return SUCCESS;
160179
}
161180
/* }}} */
@@ -646,6 +665,51 @@ PHP_FUNCTION(posix_mkfifo)
646665
#endif
647666
/* }}} */
648667

668+
/* {{{ proto bool posix_mknod(string pathname, int mode [, int major [, int minor]])
669+
Make a special or ordinary file (POSIX.1) */
670+
#ifdef HAVE_MKNOD
671+
PHP_FUNCTION(posix_mknod)
672+
{
673+
char *path;
674+
int path_len;
675+
long mode;
676+
long major, minor = 0;
677+
int result;
678+
dev_t php_dev;
679+
680+
php_dev = 0;
681+
682+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|ll", &path, &path_len,
683+
&mode, &major, &minor) == FAILURE) {
684+
RETURN_FALSE;
685+
}
686+
687+
if (php_check_open_basedir_ex(path, 0 TSRMLS_CC) ||
688+
(PG(safe_mode) && (!php_checkuid(path, NULL, CHECKUID_ALLOW_ONLY_DIR)))) {
689+
RETURN_FALSE;
690+
}
691+
692+
if ((mode & S_IFCHR) || (mode & S_IFBLK)) {
693+
if (major == 0) {
694+
php_error_docref(NULL TSRMLS_CC, E_WARNING,
695+
"expects argument 4 to be non-zero for POSIX_S_IFCHR and POSIX_S_IFBLK");
696+
RETURN_FALSE;
697+
} else {
698+
php_dev = makedev(major, minor);
699+
}
700+
}
701+
702+
result = mknod(path, mode, php_dev);
703+
if (result < 0) {
704+
POSIX_G(last_error) = errno;
705+
RETURN_FALSE;
706+
}
707+
708+
RETURN_TRUE;
709+
}
710+
#endif
711+
/* }}} */
712+
649713
/* Takes a pointer to posix group and a pointer to an already initialized ZVAL
650714
* array container and fills the array with the posix group member data. */
651715
int php_posix_group_to_array(struct group *g, zval *array_group) {

0 commit comments

Comments
 (0)