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 / c_size.c < prev    next >
C/C++ Source or Header  |  2008-07-13  |  3KB  |  141 lines

  1. /* c_size.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. #define _CRT_SECURE_NO_WARNINGS 1
  14.  
  15. /* If the remote host supports the SIZE command, we can find out the exact
  16.  * size of a remote file, depending on the transfer type in use.  SIZE
  17.  * returns different values for ascii and binary modes!
  18.  */
  19. int
  20. FTPFileSize(const FTPCIPtr cip, const char *const file, longest_int *const size, const int type)
  21. {
  22.     int result;
  23.     ResponsePtr rp;
  24.  
  25.     if (cip == NULL)
  26.         return (kErrBadParameter);
  27.     if (strcmp(cip->magic, kLibraryMagic))
  28.         return (kErrBadMagic);
  29.  
  30.     if ((size == NULL) || (file == NULL))
  31.         return (kErrBadParameter);
  32.     *size = kSizeUnknown;
  33.  
  34.     result = FTPSetTransferType(cip, type);
  35.     if (result < 0)
  36.         return (result);
  37.  
  38.     if (cip->hasSIZE == kCommandNotAvailable) {
  39.         cip->errNo = kErrSIZENotAvailable;
  40.         result = kErrSIZENotAvailable;
  41.     } else {
  42.         rp = InitResponse();
  43.         if (rp == NULL) {
  44.             result = kErrMallocFailed;
  45.             cip->errNo = kErrMallocFailed;
  46.             FTPLogError(cip, kDontPerror, "Malloc failed.\n");
  47.         } else {
  48.             result = RCmd(cip, rp, "SIZE %s", file);
  49.             if (result < 0) {
  50.                 DoneWithResponse(cip, rp);
  51.                 return (result);
  52.             } else if (result == 2) {
  53. #if defined(HAVE_LONG_LONG) && defined(SCANF_LONG_LONG)
  54.                 (void) sscanf(rp->msg.first->line, SCANF_LONG_LONG, size);
  55. #elif defined(HAVE_LONG_LONG) && defined(HAVE_STRTOQ)
  56.                 *size = (longest_int) strtoq(rp->msg.first->line, NULL, 0);
  57. #else
  58.                 (void) sscanf(rp->msg.first->line, "%ld", size);
  59. #endif
  60.                 cip->hasSIZE = kCommandAvailable;
  61.                 result = kNoErr;
  62.             } else if (FTP_UNIMPLEMENTED_CMD(rp->code)) {
  63.                 cip->hasSIZE = kCommandNotAvailable;
  64.                 cip->errNo = kErrSIZENotAvailable;
  65.                 result = kErrSIZENotAvailable;
  66.             } else {
  67.                 cip->errNo = kErrSIZEFailed;
  68.                 result = kErrSIZEFailed;
  69.             }
  70.             DoneWithResponse(cip, rp);
  71.         }
  72.     }
  73.     return (result);
  74. }    /* FTPFileSize */
  75.  
  76.  
  77.  
  78.  
  79. longest_int
  80. FTPLocalASCIIFileSize(const char *const fn, char *buf, const size_t bufsize)
  81. {
  82.     char *tbuf = NULL;
  83.     int c, prevc;
  84.     longest_int asize;
  85.     const char *scp, *slim;
  86.     int fd;
  87.     read_return_t nread;
  88.     int oerrno;
  89.  
  90.     if (buf == NULL) {
  91.         tbuf = (char *) malloc(bufsize);
  92.         if (tbuf == NULL)
  93.             return ((longest_int) -1);
  94.         buf = tbuf;
  95.     }
  96.  
  97.     fd = Open(fn, O_RDONLY, 00666);
  98.     if (fd < 0) {
  99.         if (tbuf != NULL)
  100.             free(tbuf);
  101.         return ((longest_int) -1);
  102.     }
  103.  
  104.     prevc = 0;
  105.     for (asize = (longest_int) 0; ; asize += (longest_int) nread) {
  106.         nread = read(fd, buf, bufsize);
  107.         if (nread < 0) {
  108.             oerrno = errno;
  109.             (void) close(fd);
  110.             if (tbuf != NULL)
  111.                 free(tbuf);
  112.             errno = oerrno;
  113.             return ((longest_int) -1);
  114.         } else if (nread == 0) {
  115.             break;
  116.         }
  117.  
  118.         for (scp = buf, slim = buf + nread; scp < slim; ) {
  119.             c = *scp++;
  120.             if ((c == '\n') && (prevc != '\r')) {
  121.                 /* When we actually use
  122.                  * the file later, we translate
  123.                  * newlines into newline+carriage return,
  124.                  * so add one to the count for the
  125.                  * CR byte we'll add then.
  126.                  */
  127.                 nread++;
  128.             }
  129.             prevc = c;
  130.         }
  131.     }
  132.  
  133.     if (tbuf != NULL)
  134.         free(tbuf);
  135.     (void) close(fd);
  136.     return (asize);
  137. }    /* FTPLocalASCIIFileSize */
  138.  
  139.  
  140.  
  141.