note 27709 added to function.ftp-exec
| From: | pdmckone at engmail dot uwaterloo dot ca | Date: | Mon, 16 Dec 2002 18:40:20 +0000 |
| Subject: | note 27709 added to function.ftp-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40973@lists.php.net to get a copy of this message | ||
The ftp_exec routine in PHP 4.2.3 doesn't work as advertised in the manual, returning true or
false status rather than command output.
I've written a replacement version of the routine, which has worked well for me under FreeBSD
4.6.2. My changes were:
in ftp.h:
/* exec a command [special features], return response on success, false on error */
char** ftp_exec(ftpbuf_t *ftp, const char *cmd);
(change return type from int to char**)
in php_ftp.c:
/* {{{ proto array ftp_exec(resource stream, string cmd)
Returns the results of a system command as an array of output lines */
PHP_FUNCTION(ftp_exec)
{
pval *z_ftp;
ftpbuf_t *ftp;
char **llist, **ptr, *cmd;
int cmd_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd,
&cmd_len) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
/* get raw command output */
if (NULL == (llist = ftp_exec(ftp, cmd))) {
RETURN_FALSE;
}
array_init(return_value);
for (ptr = llist; *ptr; ptr++)
add_next_index_string(return_value, *ptr, 1);
free(llist);
}
/* }}} */
Finally, in ftp.c:
/* {{{ ftp_exec
*/
char**
ftp_exec(ftpbuf_t *ftp, const char *cmd)
{
char **ret = NULL;
char **entry;
char *text;
if (!ftp_type(ftp, FTPTYPE_ASCII))
return NULL;
if (!ftp_putcmd(ftp, "SITE EXEC", cmd))
return NULL;
ret = malloc(FTP_BUFSIZE * sizeof(char*));
if (ret == NULL) {
perror("malloc");
return NULL;
}
entry = ret;
text = (char*) (ret + 100);
*entry = text;
while(1) {
if (!ftp_readline(ftp)) {
free(ret);
return NULL;
}
strcpy(text, ftp->inbuf);
text += strlen(ftp->inbuf) + 1;
*++entry = text;
/* Break out when the end-tag is found */
if (isdigit(ftp->inbuf[0]) &&
isdigit(ftp->inbuf[1]) &&
isdigit(ftp->inbuf[2]) &&
ftp->inbuf[3] == ' ') {
break;
}
}
*entry = NULL;
return ret;
}
/* }}} */
I had to make, make install, etc, then restart the httpd to pick up the new version.
The code works for me, returning an array of the raw output of a command, one line per array entry.
We needed the output from "site exec quota -v", which from our mailserver comes prefixed
with intermediate status levels:
200-quota -v
200-Disk quotas for jqpublic (uid 12345):
200-Filesystem usage quota limit timeleft ...
...
200 (end of 'quota -v')
It requires some extra parsing at the application end, but I figured it's safer to have to
parse, than to have to figure out all the possible styles of output ahead of time.
PDM
--
http://www.php.net/manual/en/function.ftp-exec.php
http://master.php.net/manage/user-notes.php?action=edit+27709
http://master.php.net/manage/user-notes.php?action=delete+27709
http://master.php.net/manage/user-notes.php?action=reject+27709