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 / rglobr.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  5KB  |  200 lines

  1. /* rglobr.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 0
  14. static void
  15. printls(LineListPtr list)
  16. {
  17.     LinePtr fip;
  18.     int i;
  19.  
  20.     for (i = 1, fip = list->first; fip != NULL; fip = fip->next, i++)
  21.         printf("%4d: %s\n", i, fip->line == NULL ? "NULL" : fip->line);
  22. }
  23.  
  24. static void
  25. print1(FTPFileInfoListPtr list)
  26. {
  27.     FTPFileInfoPtr fip;
  28.     int i;
  29.  
  30.     for (i = 1, fip = list->first; fip != NULL; fip = fip->next, i++)
  31.         printf("%4d: %s\n", i, fip->relname == NULL ? "NULL" : fip->relname);
  32. }
  33.  
  34.  
  35.  
  36. static void
  37. print2(FTPFileInfoListPtr list)
  38. {
  39.     FTPFileInfoPtr fip;
  40.     int i, n;
  41.  
  42.     n = list->nFileInfos;
  43.     for (i=0; i<n; i++) {
  44.         fip = list->vec[i];
  45.         printf("%4d: %s\n", i + 1, fip->relname == NULL ? "NULL" : fip->relname);
  46.     }
  47. }
  48.  
  49.  
  50.  
  51.  
  52. static void
  53. SortRecursiveFileList(FTPFileInfoListPtr files)
  54. {
  55.     VectorizeFileInfoList(files);
  56.     SortFileInfoList(files, 'b', '?');
  57.     UnvectorizeFileInfoList(files);
  58. }    /* SortRecursiveFileList */
  59. #endif
  60.  
  61.  
  62.  
  63.  
  64. int
  65. FTPRemoteRecursiveFileList1(FTPCIPtr cip, char *const rdir, FTPFileInfoListPtr files)
  66. {
  67.     FTPLineList dirContents;
  68.     FTPFileInfoList fil;
  69.     int result;
  70.     char rcwd[512];
  71.  
  72.     if ((result = FTPGetCWD(cip, rcwd, sizeof(rcwd))) < 0)
  73.         return (result);
  74.  
  75.     InitFileInfoList(files);
  76.  
  77.     if (rdir == NULL)
  78.         return (-1);
  79.  
  80.     if (FTPChdir(cip, rdir) < 0) {
  81.         /* Probably not a directory.
  82.          * Just add it as a plain file
  83.          * to the list.
  84.          */
  85.         (void) ConcatFileToFileInfoList(files, rdir);
  86.         return (kNoErr);
  87.     }
  88.  
  89.     /* Paths collected must be relative. */
  90.     if ((result = FTPListToMemory2(cip, "", &dirContents, "-lRa", 1, (int *) 0)) < 0) {
  91.         if ((result = FTPChdir(cip, rcwd)) < 0) {
  92.             rcwd[0] = '\0';
  93.             return (result);
  94.         }
  95.     }
  96.  
  97. #if 0
  98. DisposeLineListContents(&dirContents);
  99. InitLineList(&dirContents);
  100. AddLine(&dirContents, "drwx------   2 ftpuser  ftpuser       4096 Oct 13 02:12 in");
  101. AddLine(&dirContents, "drwx------   2 ftpuser  ftpuser       4096 Oct 13 02:07 ../out");
  102. AddLine(&dirContents, "drwx------   2 ftpuser  ftpuser       4096 Oct 13 02:11 out2");
  103. AddLine(&dirContents, "drwx------   2 ftpuser  ftpuser       4096 Oct 13 02:11 /usr/tmp/zzzin");
  104. AddLine(&dirContents, "");
  105. AddLine(&dirContents, "./in:");
  106. AddLine(&dirContents, "-rw-r--r--   1 ftpuser  ftpuser      18475 Oct 13 01:58 test_dos.txt");
  107. AddLine(&dirContents, "-rw-r--r--   1 ftpuser  ftpuser      17959 Oct 13 01:57 ../test_mac.txt");
  108. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17959 Oct 13 01:56 test_unix.txt");
  109. AddLine(&dirContents, "");
  110. AddLine(&dirContents, "./../out:");
  111. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17969 Oct 13 01:58 test_dos.txt");
  112. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17959 Oct 13 01:57 test_mac.txt");
  113. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17959 Oct 13 01:56 test_unix.txt");
  114. AddLine(&dirContents, "");
  115. AddLine(&dirContents, "./out2/../foob:");
  116. /* AddLine(&dirContents, "/tmp/out2:"); */
  117. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17969 Oct 13 02:08 test_dos.txt");
  118. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17959 Oct 13 02:08 test_mac.txt");
  119. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17959 Oct 13 02:08 test_unix.txt");
  120. AddLine(&dirContents, "");
  121. AddLine(&dirContents, "/usr/tmp/zzzin:");
  122. AddLine(&dirContents, "-rw-r--r--   1 ftpuser  ftpuser      18475 Oct 13 01:58 test_dos.txt");
  123. AddLine(&dirContents, "-rw-r--r--   1 ftpuser  ftpuser      17959 Oct 13 01:57 test_mac.txt");
  124. AddLine(&dirContents, "-rw-------   1 ftpuser  ftpuser      17959 Oct 13 01:56 test_unix.txt");
  125. #endif
  126.  
  127.     /* printls(&dirContents); */
  128.     (void) UnLslR(cip, &fil, &dirContents, cip->serverType);
  129.     DisposeLineListContents(&dirContents);
  130.     /* print1(&fil); */
  131.     /* Could sort it to breadth-first here. */
  132.     /* (void) SortRecursiveFileList(&fil); */
  133.     (void) ComputeRNames(&fil, rdir, 1, 1);
  134.     (void) ConcatFileInfoList(files, &fil);
  135.     DisposeFileInfoListContents(&fil);
  136.  
  137.     if ((result = FTPChdir(cip, rcwd)) < 0) {
  138.         rcwd[0] = '\0';
  139.         return (result);
  140.     }
  141.     return (kNoErr);
  142. }    /* FTPRemoteRecursiveFileList1 */
  143.  
  144.  
  145.  
  146.  
  147. int
  148. FTPRemoteRecursiveFileList(FTPCIPtr cip, FTPLineListPtr fileList, FTPFileInfoListPtr files)
  149. {
  150.     FTPLinePtr filePtr, nextFilePtr;
  151.     FTPLineList dirContents;
  152.     FTPFileInfoList fil;
  153.     int result;
  154.     char *rdir;
  155.     char rcwd[512];
  156.  
  157.     if ((result = FTPGetCWD(cip, rcwd, sizeof(rcwd))) < 0)
  158.         return (result);
  159.  
  160.     InitFileInfoList(files);
  161.  
  162.     for (filePtr = fileList->first;
  163.         filePtr != NULL;
  164.         filePtr = nextFilePtr)
  165.     {
  166.         nextFilePtr = filePtr->next;
  167.  
  168.         rdir = filePtr->line;
  169.         if (rdir == NULL)
  170.             continue;
  171.  
  172.         if (FTPChdir(cip, rdir) < 0) {
  173.             /* Probably not a directory.
  174.              * Just add it as a plain file
  175.              * to the list.
  176.              */
  177.             (void) ConcatFileToFileInfoList(files, rdir);
  178.             continue;
  179.         }
  180.  
  181.         /* Paths collected must be relative. */
  182.         if ((result = FTPListToMemory2(cip, "", &dirContents, "-lRa", 1, (int *) 0)) < 0) {
  183.             goto goback;
  184.         }
  185.  
  186.         (void) UnLslR(cip, &fil, &dirContents, cip->serverType);
  187.         DisposeLineListContents(&dirContents);
  188.         (void) ComputeRNames(&fil, rdir, 1, 1);
  189.         (void) ConcatFileInfoList(files, &fil);
  190.         DisposeFileInfoListContents(&fil);
  191.  
  192. goback:
  193.         if ((result = FTPChdir(cip, rcwd)) < 0) {
  194.             rcwd[0] = '\0';
  195.             return (result);
  196.         }
  197.     }    
  198.     return (kNoErr);
  199. }    /* FTPRemoteRecursiveFileList */
  200.