Problem
The ftp_putfile(SLIB) library routine destroys file descriptor 0 upon the first call, and abandons file descriptors upon subsequent calls.
Cause
There is a flaw in the ftp_putfile(SLIB) routine. The problem is illustrated with the following two code stubs:
--------------------------- cut here --------------------------------------/* Compile with "cc testfile -lftp" */# include <stdio.h>
# include <errno.h>
# include <net/libftp.h># define USAGE "usage: %s remote-host username password rmtdir\n"FTPINFO ftpinfo;
FTPINFO ftp_check(int argc, char *argv[]);main(int argc, char *argv[]) {char buf[256];
int retval, i; ftpinfo=ftp_check(argc, argv);
/* Error handling *//**************************************************************//* This illustrates how ftp_putfile(SLIB) destroys file * descriptor 0 upon the first call. */
printf("before ftp_putfile - press <ret> to continue\n"); retval = read(0, buf, 24); /* This read works ok */ printf("read retval = %d, errno = %d\n", retval, errno); if(ftp_putfile(&ftpinfo, "2kfile", "2kfile") < 0) { printf("ftp_putfile failed: (%d) %s\n", ftpinfo.reply, ftpinfo.ftp_msg); if(ftpinfo.sockfd >= 0) close(ftpinfo.sockfd); exit(1); } printf("after ftp_putfile: "); retval = read(0, buf, 24); /* This read returns an error 111 (Socket not connected) */ printf("retval = %d, errno = %d (%s)\n", retval, errno, (errno));
/**************************************************************//* Here we loop through four more times, illustrating how * ftp_putfile(SLIB) abandons an open socket file descriptor * upon each call. Use crash(ADM) or netstat(TC) to see * open sockets. */
for(i = 1; i < 5; i++) {
if(ftp_putfile(&ftpinfo, "2kfile", "2kfile") < 0) {
printf("ftp_putfile failed: (%d) %s\n",
ftpinfo.reply, ftpinfo.ftp_msg);
if(ftpinfo.sockfd >= 0)
close(ftpinfo.sockfd);
exit(1);
}
}/**************************************************************/if(ftp_bye(&ftpinfo) < 0) {
printf("ftp_bye failed: (%d) %s\n", ftpinfo.reply,
ftpinfo.ftp_msg);
if(ftpinfo.sockfd >= 0)
close(ftpinfo.sockfd);
exit(1);
}pause();}FTPINFO ftp_check(int argc, char *argv[]){ FTPINFO ftpinfo_local;
/* Error handling */
if(argc != 5) {
printf(USAGE, argv[0]);
exit(1);
}
if(ftp_login(&ftpinfo_local, argv[1], argv[2],
argv[3], (char *)0) < 0) {
printf("ftp_login failed: (%d) %s\n",
ftpinfo_local.reply, ftpinfo_local.ftp_msg);
if(ftpinfo_local.sockfd >= 0)
close(ftpinfo_local.sockfd);
exit(1);
}
if(ftp_chdir(&ftpinfo_local, argv[4]) < 0) {
printf("ftp_chdir failed: (%d) %s\n",
ftpinfo_local.reply, ftpinfo_local.ftp_msg);
if(ftpinfo_local.sockfd >= 0)
close(ftpinfo_local.sockfd);
exit(1);
}
if(ftp_binary(&ftpinfo_local) < 0) {
printf("ftp_binary failed: (%d) %s\n",
ftpinfo_local.reply, ftpinfo_local.ftp_msg);
if(ftpinfo_local.sockfd >= 0)
close(ftpinfo_local.sockfd);
exit(1);
}
return (ftpinfo_local);
}--------------------------- cut here -------------------------------------- Solution
This has been reported to SCO Engineering.