home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / bin / p205.zip / exesrc / tl.c < prev    next >
C/C++ Source or Header  |  1994-12-18  |  4KB  |  184 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 <stdio.h>
  12. #include <stdlib.h>
  13. #define INCL_DOSFILEMGR
  14. #include <os2.h>
  15. #include <ctype.h>
  16. #include <malloc.h>
  17. #include <string.h>
  18. #include "typedefs.h"
  19. #include "common.h"
  20. #include "dir.h"
  21. #include "tl.h"
  22. #include "global.h"
  23.  
  24. /* Adds a file of PATH to the list TL. SIZE is the file size in bytes. */
  25.  
  26. void tl_add(TL **tl, U8 *path, U32 size) {
  27.  
  28.   U32 buf_idx;
  29.   TE *te;
  30.  
  31.   if ((te = malloc(sizeof(TE))) == NULL) {
  32.     fprintf(stderr, "Failed to allocate memory\n");
  33.     exit(1);
  34.   }
  35.   te->path_len = strlen(path);
  36.   if ((te->path = malloc(te->path_len + 1)) == NULL) {
  37.     fprintf(stderr, "Failed to allocate memory\n");
  38.     exit(1);
  39.   }
  40.   memcpy(te->path, path, te->path_len + 1);
  41.  
  42.   buf_idx = te->path_len + 1;
  43.   while (buf_idx > 0) {
  44.     buf_idx--;
  45.     if (te->path[buf_idx] == '/' ||
  46.     te->path[buf_idx] == '\\') {
  47.       buf_idx++;
  48.       break;
  49.     }
  50.   }
  51.   te->name_len = te->path_len - buf_idx;
  52.   te->name = &te->path[buf_idx];
  53.  
  54.   if (*tl == NULL) {
  55.     /* The list is empty */
  56.     if ((*tl = malloc(sizeof(TL))) == NULL) {
  57.       fprintf(stderr, "Failed to allocate memory\n");
  58.       exit(1);
  59.     }
  60.     (*tl)->f = te;
  61.     (*tl)->l = te;
  62.     (*tl)->c = te;
  63.     (*tl)->cnt = 0;
  64.     (*tl)->size = 0;
  65.     te->p = NULL;
  66.   } else {
  67.     (*tl)->l->n = te;
  68.     te->p = (*tl)->l;
  69.   }
  70.   (*tl)->cnt++;
  71.   (*tl)->size += size;
  72.   (*tl)->l = te;
  73.   te->n = NULL;
  74. }
  75.  
  76. /* Add files of PATH to the transfer list. Possible wildcards in PATH will */
  77. /* be expanded. If -recursive option is specified, files will be searched */
  78. /* recursively. */
  79.  
  80. void tl_expanded_add(TL **tl, U8 *path) {
  81.  
  82.   U8 expanded_path[260];
  83.   BOOLEAN found = 0;
  84.   DIR_ENTRY dir_entry;
  85.  
  86.   /* Search for files */
  87.   if (dir_find_first(path, 0, &dir_entry) == 0) {
  88.     found = 1;
  89.     do {
  90.       sprintf(expanded_path, "%.*s%s",
  91.           (int)get_dir_len(path), path,
  92.           dir_entry.name);
  93.       tl_add(tl, expanded_path, *dir_entry.size);
  94.     } while (!dir_find_next(&dir_entry));
  95.   }
  96.   dir_find_close(&dir_entry);
  97.  
  98.   /* Search for directories */
  99.   if (opt_recursive) {
  100.     if (dir_find_first(path, DIR_FIND_DIRECTORY, &dir_entry) == 0) {
  101.       found = 1;
  102.       do {
  103.     if (strcmp(dir_entry.name, ".") != 0 &&
  104.         strcmp(dir_entry.name, "..") != 0) {
  105.       sprintf(expanded_path, "%.*s%s\\*",
  106.           (int)get_dir_len(path), path,
  107.           dir_entry.name);
  108.       tl_expanded_add(tl, expanded_path);
  109.     }
  110.       } while (!dir_find_next(&dir_entry));
  111.     }
  112.     dir_find_close(&dir_entry);
  113.   }
  114.   if (!found)
  115.     tl_add(tl, path, 0);    /* If we are sending, we'll */
  116.                   /* notify about non-existing */
  117.                 /* files later... */
  118. }
  119.  
  120. /* Reads the files from a listfile */
  121.  
  122. void tl_read_from_list(TL **tl, U32 expand, U8 *path) {
  123.  
  124.   U8 str[512];
  125.   U32 str_len;
  126.   FILE *stream;
  127.  
  128.   path++; /* skip the @-character */
  129.   if ((stream = fopen(path, "r")) == NULL) {
  130.     perror(path);
  131.     return;
  132.   }
  133.   while (fgets(str, 511, stream) != NULL) {
  134.     if (str[0] != '\0') {
  135.       str_len = strlen(str);
  136.       str_len--;
  137.       if (str[str_len] == '\n')
  138.     str[str_len] = '\0';
  139.       if (expand)
  140.     tl_expanded_add(tl, str);
  141.       else
  142.     tl_add(tl, str, 0);
  143.     }
  144.   }
  145.   fclose(stream);
  146. }
  147.  
  148. /* Checks if file of PATH exists in the transfer list. This is used when */
  149. /* receiving to check that the file was specified on the command-line */
  150.  
  151. BOOLEAN tl_exists(TL *tl, U8 *path) {
  152.  
  153.   TE *te;
  154.  
  155.   te = tl->f;
  156.   while (te != NULL) {
  157.     if (stricmp(path, te->path) == 0)
  158.       return(1);
  159.     te = te->n;
  160.   }
  161.   return(0);
  162. }
  163.  
  164. /* Frees the memory reserved by the transfer list */
  165.  
  166. void tl_free(TL **tl) {
  167.  
  168.   TE *te1;
  169.   TE *te2;
  170.  
  171.   if (*tl != NULL) {
  172.     te1 = (*tl)->f;
  173.     while (te1 != NULL) {
  174.       te2 = te1->n;
  175.       free(te1->path);
  176.       free(te1);
  177.       te1 = te2;
  178.     }
  179.     free(*tl);
  180.     *tl = NULL;
  181.   }
  182. }
  183.  
  184.