ID:               21053
 Updated by:       [EMAIL PROTECTED]
-Summary:          ftp_exec() operation doesn't match documentation
 Reported By:      [EMAIL PROTECTED]
 Status:           Open
-Bug Type:         Documentation problem
+Bug Type:         FTP related
 Operating System: FreeBSD 4.6.2
 PHP Version:      4.2.3
 New Comment:

What you sent in your report is not a documentation problem, but an FTP
related feature request. Because you includeded C source code too, I
recategorize this as FTP related. Also modified summary


Previous Comments:
------------------------------------------------------------------------

[2002-12-16 15:24:20] [EMAIL PROTECTED]

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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=21053&edit=1

Reply via email to