From:             [EMAIL PROTECTED]
Operating system: FreeBSD 4.6.2
PHP version:      4.2.3
PHP Bug Type:     Documentation problem
Bug description:  ftp_exec() operation doesn't match documentation

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 bug report at http://bugs.php.net/?id=21053&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=21053&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=21053&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=21053&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=21053&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=21053&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=21053&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=21053&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=21053&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=21053&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=21053&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=21053&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=21053&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=21053&r=isapi


-- 
PHP Documentation Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to