home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / libncftp / io_list.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  4KB  |  148 lines

  1. /* io_list.c
  2.  *
  3.  * Copyright (c) 1996-2005 Mike Gleason, NcFTP Software.
  4.  * All rights reserved.
  5.  *
  6.  */
  7.  
  8. #include "syshdrs.h"
  9. #ifdef PRAGMA_HDRSTOP
  10. #    pragma hdrstop
  11. #endif
  12.  
  13. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  14. #    define ASCII_TRANSLATION 0
  15. #endif
  16.  
  17. #ifndef ASCII_TRANSLATION
  18. #    define ASCII_TRANSLATION 1
  19. #endif
  20.  
  21. #ifndef NO_SIGNALS
  22. #    define NO_SIGNALS 1
  23. #endif
  24.  
  25. #ifndef O_BINARY
  26.     /* Needed for platforms using different EOLN sequence (i.e. DOS) */
  27. #    ifdef _O_BINARY
  28. #        define O_BINARY _O_BINARY
  29. #    else
  30. #        define O_BINARY 0
  31. #    endif
  32. #endif
  33.  
  34. /* This isn't too useful -- it mostly serves as an example so you can write
  35.  * your own function to do what you need to do with the listing.
  36.  */
  37. int
  38. FTPList(const FTPCIPtr cip, const int outfd, const int longMode, const char *const lsflag)
  39. {
  40.     const char *cmd;
  41.     char line[512];
  42.     char secondaryBuf[768];
  43. #ifndef NO_SIGNALS
  44.     char *secBufPtr, *secBufLimit;
  45.     int nread;
  46.     volatile int result;
  47. #else    /* NO_SIGNALS */
  48.     SReadlineInfo lsSrl;
  49.     int result;
  50. #endif    /* NO_SIGNALS */
  51.  
  52.     if (cip == NULL)
  53.         return (kErrBadParameter);
  54.     if (strcmp(cip->magic, kLibraryMagic))
  55.         return (kErrBadMagic);
  56.  
  57.     cmd = (longMode != 0) ? "LIST" : "NLST";
  58.     if ((lsflag == NULL) || (lsflag[0] == '\0')) {
  59.         result = FTPStartDataCmd(cip, kNetReading, kTypeAscii, (longest_int) 0, "%s", cmd);
  60.     } else {
  61.         result = FTPStartDataCmd(cip, kNetReading, kTypeAscii, (longest_int) 0, "%s %s", cmd, lsflag);
  62.     }
  63.  
  64.  
  65. #ifdef NO_SIGNALS
  66.  
  67.     if (result == 0) {
  68.         if (InitSReadlineInfo(&lsSrl, cip->dataSocket, secondaryBuf, sizeof(secondaryBuf), (int) cip->xferTimeout, 1) < 0) {
  69.             /* Not really fdopen, but close in what we're trying to do. */
  70.             result = kErrFdopenR;
  71.             cip->errNo = kErrFdopenR;
  72.             FTPLogError(cip, kDoPerror, "Could not fdopen.\n");
  73.             return (result);
  74.         }
  75.         
  76.         for (;;) {
  77.             result = SReadline(&lsSrl, line, sizeof(line) - 2);
  78.             if (result == kTimeoutErr) {
  79.                 /* timeout */
  80.                 FTPLogError(cip, kDontPerror, "Could not directory listing data -- timed out.\n");
  81.                 cip->errNo = kErrDataTimedOut;
  82.                 return (cip->errNo);
  83.             } else if (result == 0) {
  84.                 /* end of listing -- done */
  85.                 cip->numListings++;
  86.                 break;
  87.             } else if (result < 0) {
  88.                 /* error */
  89.                 FTPLogError(cip, kDoPerror, "Could not read directory listing data");
  90.                 result = kErrLISTFailed;
  91.                 cip->errNo = kErrLISTFailed;
  92.                 break;
  93.             }
  94.  
  95.             (void) write(outfd, line, (write_size_t) strlen(line));
  96.         }
  97.  
  98.         DisposeSReadlineInfo(&lsSrl);
  99.         if (FTPEndDataCmd(cip, 1) < 0) {
  100.             result = kErrLISTFailed;
  101.             cip->errNo = kErrLISTFailed;
  102.         }
  103.     } else if (result == kErrGeneric) {
  104.         result = kErrLISTFailed;
  105.         cip->errNo = kErrLISTFailed;
  106.     }
  107.  
  108.  
  109. #else    /* NO_SIGNALS */
  110.     
  111.     if (result == 0) {
  112.         /* This line sets the buffer pointer so that the first thing
  113.          * BufferGets will do is reset and fill the buffer using
  114.          * real I/O.
  115.          */
  116.         secBufPtr = secondaryBuf + sizeof(secondaryBuf);
  117.         secBufLimit = (char *) 0;
  118.  
  119.         for (;;) {
  120.             if (cip->xferTimeout > 0)
  121.                 (void) alarm(cip->xferTimeout);
  122.             nread = BufferGets(line, sizeof(line), cip->dataSocket, secondaryBuf, &secBufPtr, &secBufLimit, sizeof(secondaryBuf));
  123.             if (nread <= 0) {
  124.                 if (nread < 0)
  125.                     break;
  126.             } else {
  127.                 cip->bytesTransferred += (longest_int) nread;
  128.                 (void) STRNCAT(line, "\n");
  129.                 (void) write(outfd, line, (write_size_t) strlen(line));
  130.             }
  131.         }
  132.         if (cip->xferTimeout > 0)
  133.             (void) alarm(0);
  134.         result = FTPEndDataCmd(cip, 1);
  135.         if (result < 0) {
  136.             result = kErrLISTFailed;
  137.             cip->errNo = kErrLISTFailed;
  138.         }
  139.         result = kNoErr;
  140.         cip->numListings++;
  141.     } else if (result == kErrGeneric) {
  142.         result = kErrLISTFailed;
  143.         cip->errNo = kErrLISTFailed;
  144.     }
  145. #endif    /* NO_SIGNALS */
  146.     return (result);
  147. }    /* FTPList */
  148.