home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / OLS / Os2 / LHA2P205 / LHA2P205.LZH / lha2-2.05pre / source.lzh / src / files.c < prev    next >
C/C++ Source or Header  |  1996-02-25  |  5KB  |  260 lines

  1. /*
  2.  * files.c --- collect filenames
  3.  *   Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
  4.  *   Copyright (C) 1991-1995, Satoshi HIRAMATSU (OS/2 HPFS version)
  5.  *
  6.  * $Log$
  7.  */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include "port2.h"
  16. #include "typedef.h"
  17. #include "lh.h"
  18. #include "errmes.h"
  19.  
  20.  
  21. int tcount, fcount;
  22. struct fb *fbuf;
  23. static struct fb *flst;
  24.  
  25.  
  26. /* initialize pointers for registing files */
  27. static void
  28. init_regfile(void)
  29. {
  30.   fbuf = flst = NULL;
  31.  
  32.   tcount = fcount = 0;
  33. }
  34.  
  35.  
  36. /* regist file name */
  37. #ifndef __SUPPORT_EA__
  38. static void
  39. regfile(char *p, char *q, char *f, long utc)
  40. #else
  41. static void
  42. regfile(char *p, char *q, char *f, long utc, FEA2LIST *ea)
  43. #endif
  44. /*
  45.  * p: directory name including drive & base directory
  46.  * q: partial directory name following base directory
  47.  * f: file name
  48.  *
  49.  * "d:pppp/qqqq/\0 ffff\0"
  50.  *  ^      ^       ^
  51.  *  p      q       f
  52.  */
  53. {
  54.   struct fb *fp, *fnxt;
  55.   char *r, *t;
  56.  
  57.   if(strcmp(f, lhtmp1) == 0 || strcmp(f, lhtmp2) == 0)
  58.     return;            /* temporary file ? */
  59.   if(flg_i == 0)
  60.     {
  61.       char *s;
  62.  
  63.       s = p;
  64.       while(*s)
  65.     {
  66.       if(iskanji(*s))
  67.         s++;
  68.       else
  69.         *s = toupper(*s);
  70.       if(*s)
  71.         s++;
  72.     }
  73.     }
  74.   fnxt = e_malloc(strlen(p) + strlen(f) + sizeof(struct fb));
  75.   fnxt -> time = utc;
  76. #ifdef __SUPPORT_EA__
  77.   fnxt->ea = ea;
  78. #endif
  79.   strcpy(r = fnxt -> fname, p);
  80.   strcpy(t = fnxt -> fpos = r + strlen(p), f);
  81.   if(flg_x)
  82.     t = r + (q - p);
  83.   fnxt -> cpos = t;
  84.  
  85.   for(fp = fbuf; fp != NULL; fp = fp -> nxt)
  86.     if(strcmp(fp -> cpos, t) == 0)
  87.       {
  88.     if(strcmp(fp -> fname, r) == 0)
  89.       {
  90.         free(fnxt);
  91.         return;
  92.       }
  93.     else
  94.       {
  95.         strcpy(work, t);
  96.         error(DUPFNERR, work);
  97.         /* same registing names of different files */
  98.       }
  99.       }
  100.   
  101.   if(flst)
  102.     flst -> nxt = fnxt;
  103.   else
  104.     fbuf = fnxt;
  105.   flst = fnxt;
  106.   flst -> nxt = NULL;
  107.   flst -> used = 0;
  108.  
  109.   tcount ++;
  110. }
  111.  
  112.  
  113. /* recursive collection of files */
  114. static int
  115. travel(char *p, char *q, char *f)
  116. /*
  117.  * p: directory name including drive & base directory
  118.  * q: directory name following base directory
  119.  * f: file name
  120.  *
  121.  * "pppp/qqqq/\0 ffff\0"
  122.  *  ^    ^       ^
  123.  *  p    q       f
  124.  */
  125. {
  126.   struct find_t srchbuf;
  127.   char *r, *s;
  128.   int done, cnt;
  129.  
  130.   cnt = 0;
  131.   convdelim(p, pathdelim);
  132.   done = _dos_findfirst(p, 0x17, &srchbuf); /* search the first file */
  133.   convdelim(p, DELIM);
  134.   s = backpath(q);
  135.   while(! done)
  136.     {
  137.       if(!(srchbuf.attrib & 0x06) || flg_a)
  138.     {
  139.       if(srchbuf.attrib & 0x10) /* if this is a sub-directory */
  140.         {
  141.           /* for directory archive */
  142.           if(flg_d && strcmp(srchbuf.name, ".") &&
  143.          strcmp(srchbuf.name, ".."))
  144.         {
  145.           cnt++;
  146. #ifndef __SUPPORT_EA__
  147.           regfile(p, q, strcat(srchbuf.name, DELIMSTR), 
  148.               dos2unix((struct ftime *)&(srchbuf.wr_time)));
  149. #else
  150.           regfile(p, q, strcat(srchbuf.name, DELIMSTR), 
  151.               dos2unix((struct ftime *)&(srchbuf.wr_time)),
  152.               srchbuf.ea);
  153. #endif
  154.         }
  155.           if(flg_r)
  156.         if(strcmp(srchbuf.name, ".") && strcmp(srchbuf.name, ".."))
  157.           {
  158.             if(flg_d) /* for directory archive */
  159.               r = stpcpy(stpcpy(s, srchbuf.name), "*.*");
  160.             else
  161.               r = stpcpy(stpcpy(s, srchbuf.name), DELIMSTR "*.*");
  162.             if(r - p > MAXPATHLEN)
  163.               error(TOOLONGERR, p);
  164.             cnt += travel(p, q, f);
  165.             /* search recursively */
  166.             *s = '\0';
  167.           }
  168.         }
  169.       else            /* if this is a file */
  170.         {
  171.           if(flg_r != 1 || *matchfname(f, srchbuf.name) == '\0')
  172.         {
  173.           cnt++;
  174. #ifndef __SUPPORT_EA__
  175.           regfile(p, q, srchbuf.name, 
  176.               dos2unix((struct ftime *)&(srchbuf.wr_time)));
  177. #else
  178.           regfile(p, q, srchbuf.name, 
  179.               dos2unix((struct ftime *)&(srchbuf.wr_time)),
  180.               srchbuf.ea);
  181. #endif
  182.         }
  183.         }
  184.     }
  185.       done = _dos_findnext(&srchbuf);
  186.     }
  187.  
  188.   return cnt;            /* number of registed files */
  189. }
  190.  
  191.  
  192. /* make file lists to append */
  193. void
  194. mklist(void)
  195. {
  196.   char *p, *q, *r;
  197.   int cnt, Nfile;
  198.   struct pat *pt;
  199.   char fname[MAXPATHLEN];
  200.  
  201.   Nfile = 0;
  202.   init_regfile();
  203.   for(pt = pbuf -> nxt; pt != NULL; pt = pt -> nxt)
  204.     {
  205.       p = pt -> pname;
  206.       r = work;
  207.       if(p[1] == ':')        /* if file name includes drive */
  208.     {
  209.       strcpy(r, p);    /* ignore base directory */
  210.       r += 2;        /* don't regist drive name */
  211.     }
  212.       else
  213.     {
  214.       if((uchar)*p != DELIM)
  215.         r = (char *)stpcpy(r, pt -> bdir);
  216.       else if(*(pt -> bdir) && (pt -> bdir)[1] == ':')
  217.         {
  218.           strcpy(r, pt -> bdir);
  219.           r += 2;
  220.         }
  221.       strcpy(r, p);
  222.     }
  223. #ifndef __SUPPORT_HPFS__
  224.       if((q = strchr(work, '+')) != NULL)
  225.     strcpy(q, "*.*");
  226. #endif
  227.       q = getfilename(work);
  228.       if(flg_r == 1)        /* /r+ mode */
  229.     {
  230.       strcpy(fname, q);
  231.       strcpy(q, "*.*");
  232.       cnt = travel(work, r, fname);
  233.     }
  234.       else
  235.     {
  236.       if(flg_r == 2)    /* /r2 mode */
  237.         if(*q == '.')
  238.           strcat(r, DELIMSTR "*.*");
  239.       cnt = travel(work, r, NULL);
  240.     }
  241.       Nfile += pt -> cnt = cnt;
  242.     }
  243. }
  244.  
  245.  
  246. struct fb *
  247. searchfile(char *path)
  248. {
  249.   struct fb *fp;
  250.  
  251.   for(fp = fbuf; fp != NULL; fp = fp -> nxt)
  252.     if(strcmp(path, fp -> cpos) == 0)
  253.       {
  254.     fp -> used = 1;
  255.     return fp;
  256.       }
  257.  
  258.   return NULL;
  259. }
  260.