home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / k95source / p_tl.c < prev    next >
C/C++ Source or Header  |  2003-05-01  |  6KB  |  253 lines

  1. /*****************************************************************************/
  2. /*             Copyright (c) 1994 by Jyrki Salmi <jytasa@jyu.fi>             */
  3. /*        You may modify, recompile and distribute this file freely.         */
  4. /*****************************************************************************/
  5.  
  6. /*
  7.    Routines used to create a linked list of file names specified on the
  8.    command-line and in the listfiles.
  9. */
  10.  
  11. #include "ckcdeb.h"
  12. #ifndef NOXFER
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17.  
  18. #ifdef OS2
  19. #ifdef NT
  20. #include <windows.h>
  21. #define stricmp _stricmp
  22. #else
  23. #define INCL_DOSFILEMGR
  24. #include <os2.h>
  25. #undef COMMENT
  26. #endif
  27. #include "ckocon.h"
  28. #endif /* OS2 */
  29.  
  30. #include "p_type.h"
  31. #include "p_common.h"
  32. #include "p_dir.h"
  33. #include "p_tl.h"
  34. #include "p_global.h"
  35.  
  36. /* Adds a file of PATH to the list TL. SIZE is the file size in bytes. */
  37.  
  38. VOID
  39. #ifdef CK_ANSIC
  40. tl_add(TL **tl, U8 *path, U32 size, U8* as_name, U8 convert )
  41. #else
  42. tl_add(tl,path,size,as_name,convert)
  43.      TL **tl; U8 *path; U32 size; U8* as_name; U8 convert;
  44. #endif
  45. {
  46.  
  47.     U32 buf_idx;
  48.     TE *te;
  49.     char tmpbuf[257] ;
  50.  
  51.     debug(F110,"tl_add path",path,0);
  52.     debug(F101,"tl_add size","",size);
  53.     debug(F110,"tl_add as_name",as_name,0);
  54.     debug(F101,"tl_add convert","",convert);
  55.  
  56.     if ((te = malloc(sizeof(TE))) == NULL) {
  57.         fprintf(stderr, "Failed to allocate memory\n");
  58.         exit(1);
  59.     }
  60.  
  61.     te->path_len = strlen(path);
  62.     if ((te->path = malloc(te->path_len + 1)) == NULL) {
  63.         fprintf(stderr, "Failed to allocate memory\n");
  64.         exit(1);
  65.     }
  66.     memcpy(te->path, path, te->path_len + 1);
  67.  
  68.     buf_idx = te->path_len + 1;
  69.     while (buf_idx > 0) {
  70.         buf_idx--;
  71.         if (te->path[buf_idx] == '/'
  72. #ifdef OS2
  73.              || te->path[buf_idx] == '\\'
  74. #endif /* OS2 */
  75.              ) {
  76.             buf_idx++;
  77.             break;
  78.         }
  79.     }
  80.     te->name_len = te->path_len - buf_idx;
  81.     te->name = &te->path[buf_idx];
  82.  
  83.     if ( as_name )
  84.     {
  85.         te->as_name_len = strlen(as_name);
  86.         if ((te->as_name = malloc(te->as_name_len + 1)) == NULL) {
  87.             fprintf(stderr, "Failed to allocate memory\n");
  88.             exit(1);
  89.         }
  90.         memcpy(te->as_name, as_name, te->as_name_len + 1);
  91.     }
  92.     else
  93.     {
  94.         te->as_name = NULL ;
  95.         te->as_name_len = 0 ;
  96.     }
  97.     te->convert = convert ;
  98.  
  99.     if (*tl == NULL) {
  100.         /* The list is empty */
  101.         if ((*tl = malloc(sizeof(TL))) == NULL) {
  102.             fprintf(stderr, "Failed to allocate memory\n");
  103.             exit(1);
  104.         }
  105.         (*tl)->f = te;
  106.         (*tl)->l = te;
  107.         (*tl)->c = te;
  108.         (*tl)->cnt = 0;
  109.         (*tl)->size = 0;
  110.         te->p = NULL;
  111.     } else {
  112.         (*tl)->l->n = te;
  113.         te->p = (*tl)->l;
  114.     }
  115.     (*tl)->cnt++;
  116.     (*tl)->size += size;
  117.     (*tl)->l = te;
  118.     te->n = NULL;
  119. }
  120.  
  121. /* Add files of PATH to the transfer list. Possible wildcards in PATH will */
  122. /* be expanded. If -recursive option is specified, files will be searched */
  123. /* recursively. */
  124.  
  125. VOID
  126. #ifdef CK_ANSIC
  127. tl_expanded_add(TL **tl, U8 *path)
  128. #else
  129. tl_expanded_add(tl,path) TL **tl; U8 *path ;
  130. #endif
  131. {
  132.  
  133.   U8 expanded_path[260];
  134.   BOOLEAN found = 0;
  135.   DIR_ENTRY dir_entry;
  136.  
  137.   /* Search for files */
  138.   if (dir_find_first(path, 0, &dir_entry) == 0) {
  139.     found = 1;
  140.     do {
  141.       sprintf(expanded_path, "%.*s%s",
  142.               (int)get_dir_len(path), path,
  143.               dir_entry.name);
  144.       tl_add(tl, expanded_path, *dir_entry.size, NULL, opt_text);
  145.     } while (!dir_find_next(&dir_entry));
  146.   }
  147.   dir_find_close(&dir_entry);
  148.  
  149.   /* Search for directories */
  150.   if (opt_recursive) {
  151.     if (dir_find_first(path, DIR_FIND_DIRECTORY, &dir_entry) == 0) {
  152.       found = 1;
  153.       do {
  154.         if (strcmp(dir_entry.name, ".") != 0 &&
  155.             strcmp(dir_entry.name, "..") != 0) {
  156.           sprintf(expanded_path, "%.*s%s\\*",
  157.                   (int)get_dir_len(path), path,
  158.                   dir_entry.name);
  159.           tl_expanded_add(tl, expanded_path);
  160.         }
  161.       } while (!dir_find_next(&dir_entry));
  162.     }
  163.     dir_find_close(&dir_entry);
  164.   }
  165.   if (!found)
  166.     tl_add(tl, path, 0, NULL, opt_text);        /* If we are sending, we'll */
  167.                                 /* notify about non-existing */
  168.                                 /* files later... */
  169. }
  170.  
  171. /* Reads the files from a listfile */
  172.  
  173. VOID
  174. #ifdef CK_ANSIC
  175. tl_read_from_list(TL **tl, U32 expand, U8 *path)
  176. #else
  177. tl_read_from_list(tl,expand,path) TL **tl; U32 expand; U8 *path ;
  178. #endif
  179. {
  180.  
  181.   U8 str[512];
  182.   U32 str_len;
  183.   FILE *stream;
  184.  
  185.   path++; /* skip the @-character */
  186.   if ((stream = fopen(path, "r")) == NULL) {
  187.     perror(path);
  188.     return;
  189.   }
  190.   while (fgets(str, 511, stream) != NULL) {
  191.     if (str[0] != '\0') {
  192.       str_len = strlen(str);
  193.       str_len--;
  194.       if (str[str_len] == '\n')
  195.         str[str_len] = '\0';
  196.       if (expand)
  197.         tl_expanded_add(tl, str);
  198.       else
  199.         tl_add(tl, str, 0, NULL, opt_text);
  200.     }
  201.   }
  202.   fclose(stream);
  203. }
  204.  
  205. /* Checks if file of PATH exists in the transfer list. This is used when */
  206. /* receiving to check that the file was specified on the command-line */
  207.  
  208. BOOLEAN
  209. #ifdef CK_ANSIC
  210. tl_exists(TL *tl, U8 *path)
  211. #else
  212. tl_exists(tl,path) TL *tl; U8 *path;
  213. #endif
  214. {
  215.  
  216.   TE *te;
  217.  
  218.   te = tl->f;
  219.   while (te != NULL) {
  220.     if (stricmp(path, te->path) == 0)
  221.       return(1);
  222.     te = te->n;
  223.   }
  224.   return(0);
  225. }
  226.  
  227. /* Frees the memory reserved by the transfer list */
  228.  
  229. VOID
  230. #ifdef CK_ANSIC
  231. tl_free(TL **tl)
  232. #else
  233. tl_free(tl) TL **tl;
  234. #endif
  235. {
  236.  
  237.   TE *te1;
  238.   TE *te2;
  239.  
  240.   if (*tl != NULL) {
  241.     te1 = (*tl)->f;
  242.     while (te1 != NULL) {
  243.       te2 = te1->n;
  244.       free(te1->path);
  245.       free(te1);
  246.       te1 = te2;
  247.     }
  248.     free(*tl);
  249.     *tl = NULL;
  250.   }
  251. }
  252.  
  253. #endif /* NOXFER */