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 / append.c next >
C/C++ Source or Header  |  1996-02-04  |  6KB  |  337 lines

  1. /*
  2.  * append.c --- append to archive
  3.  *   Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
  4.  *   Copyright (C) 1991-1996, Satoshi HIRAMATSU (OS/2 HPFS version)
  5.  *
  6.  * $Log$
  7.  */
  8.  
  9.  
  10. #include <sys/types.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <io.h>
  15. #include <time.h>
  16. #include "typedef.h"
  17. #include "port2.h"
  18. #include "lh.h"
  19. #include "header.h"
  20. #include "intrface.h"
  21. #include "slidehuf.h"
  22. #include "disp.h"
  23. #include "errmes.h"
  24.  
  25.  
  26. struct interfacing interface;
  27.  
  28.  
  29. void
  30. copylzh(header)
  31.      HEADER *header;
  32. {
  33.   int _flg_n = flg_n;
  34.  
  35.   flg_n = 2;
  36.   update_arcstamp();
  37.   copyhdr(file1, file2, &hpb);
  38.   copyfile(file1, file2, hpb.packed, 0);
  39.   flg_n = _flg_n;
  40. }
  41.  
  42.  
  43. struct extension
  44. {
  45.   struct extension *nxt;
  46.   char ext[1];
  47. };
  48. struct extension *extptr;
  49.  
  50.  
  51. void
  52. regext(char *ext)
  53. {
  54.   struct extension *p;
  55.  
  56.   if(*ext == '.')
  57.     ext++;
  58.   p = malloc(strlen(ext) + sizeof(struct extension));
  59.   p -> nxt = extptr;
  60.   strcpy(p -> ext, ext);
  61.   extptr = p;
  62. }
  63.  
  64.  
  65. static int
  66. isarc(char *name)
  67. {
  68.   static char arc[14][4] =
  69.     {
  70.       "zoo", "arc", "pak", "zip", "lzs",
  71.       "lzh", "taZ", "shZ", "pZ", "Z",
  72.       "tgz", "sgz", "pgz", "gz"
  73.     };
  74.   char *p, old_i = flg_i;
  75.   struct extension *q;
  76.   int i;
  77.  
  78.   p = strrchr(name, '.');
  79.   if(p == NULL)
  80.     p = name + strlen(name) - 1;
  81.   p++;
  82.  
  83.   flg_i = 0;            /* ignore case */
  84.   for(i = 0; i < sizeof(arc) / 4; i++)
  85.     {
  86.       if(*matchfname(arc[i], p) == '\0')
  87.     {
  88.       flg_i = old_i;
  89.       return 1;
  90.     }
  91.     }
  92.   for(q = extptr; q; q = q -> nxt)
  93.     {
  94.       if(*matchfname(q -> ext, p) == '\0')
  95.     {
  96.       flg_i = old_i;
  97.       return 1;
  98.     }
  99.     }
  100.   flg_i = old_i;
  101.  
  102.   return 0;
  103. }
  104.  
  105.  
  106. void
  107. freeze(char *path, char *name)
  108. {
  109.   static int method = -1;
  110.   int __isdir__ = 0;
  111.   HEADERLINK *baseheader;
  112.   hword crc = 0;
  113.  
  114.   if(flg_i == 2)
  115.     {
  116.       hpb.pathname = jstrlwr(name);
  117.       hpb.filename = jstrlwr(getfilename(name));
  118.     }
  119.   else if(flg_i == 0)
  120.     {
  121.       hpb.pathname = jstrupr(name);
  122.       hpb.filename = jstrupr(getfilename(name));
  123.     }
  124.   else
  125.     {
  126.       hpb.pathname = name;
  127.       hpb.filename = getfilename(name);
  128.     }
  129.   hpb.pathlen = strlen(hpb.pathname);
  130.   hpb.namelen = strlen(hpb.filename);
  131.  
  132.   dispflg = 0;
  133.   if(*(name + strlen(name) - 1) == DELIM) /* for directory archive */
  134.     {
  135.       dispflg = 1;
  136.       __isdir__ = 1;
  137.       file3 = (FILE *) NULL;
  138.     }
  139.   else
  140.     file3 = myropen(path);
  141.  
  142.   if(method < 0)
  143.     {
  144.       method = 5;
  145.       if(flg_o)
  146.     method = 1;
  147.       if(flg_z == 1)
  148.     method = 0;
  149.       if(method > 0)
  150.     method = encode_alloc(method);
  151.     }
  152.   _dos_getfileattr(path, &hpb.attr);
  153.   if(!flg_a)
  154.     hpb.attr &= 0x21;
  155.   if(__isdir__)
  156.     hpb.mtime = getdirectorytime(path);
  157.   else
  158. #ifndef __SUPPORT_CTIME_ATIME__
  159.     hpb.mtime = getfiletime(file3);
  160. #else
  161.     _os2_getftime(fileno(file3), &hpb.mtime, &hpb.ctime, &hpb.atime);
  162. #endif
  163.   hpb.level = flg_h;
  164.   hpb.info = 0;
  165.   if(__isdir__)
  166.     hpb.original = hpb.packed = 0;
  167.   else
  168.     hpb.original = hpb.packed = filelength(fileno(file3));
  169.   if(__isdir__)
  170.     strcpy(hpb.method, "-lhd-"); /* directory archive */
  171.   else
  172.     strcpy(hpb.method, "-lh5-");
  173.   interface.method = method;
  174.   interface.dicbit = (method == 1 || method == 4) ? 12 : 13;
  175.   interface.original = hpb.original;
  176.   regdisp("Freezing", path);
  177.   disp(0, outredir);
  178.   initdisp();
  179.  
  180.   if(hpb.original == 0 || flg_z == 2 && isarc(hpb.filename))
  181.     interface.method = 0;
  182.  
  183.   if(interface.method > 0)
  184.     {
  185.       baseheader = makehdr(&hpb);
  186.       writehdr(file2, baseheader, &hpb);
  187.       interface.infile = file3;
  188.       interface.outfile = file2;
  189.       crc = encode(&interface);
  190.       if(interface.packed >= interface.original)
  191.     {
  192.       rewind(file3);
  193.       fseek(file2, hpb.currentpos, SEEK_SET);
  194.       interface.method = 0;
  195.       dispname();
  196.       initdisp();
  197.     }
  198.       hpb.packed = interface.packed;
  199.     }
  200.   if(!__isdir__)
  201.     hpb.method[3] = interface.method + '0';
  202.  
  203.   if(interface.method == 0)
  204.     {
  205.       baseheader = makehdr(&hpb);
  206.       writehdr(file2, baseheader, &hpb);
  207.       if(!__isdir__)
  208.     crc = copyfile(file3, file2, hpb.original, 1);
  209.     }
  210.   update_arcstamp();
  211.   hpb.filecrc = crc;
  212.   adjusthdr(file2, baseheader, &hpb);
  213.   if(!__isdir__)
  214.     fclose(file3);
  215.   if(ferror(file2))
  216.     error(WTERR, filename2);
  217.   if(flg_n != 2)
  218.     eprintf((flg_l) ? "\r  ==> %3d%%\n" : "\r==> %3d%%\n", 
  219.         ratio(hpb.packed, hpb.original, 2));
  220. }
  221.  
  222.  
  223. static void
  224. freeze_fb(struct fb *fp)
  225. {
  226.   char *p, *path;
  227.  
  228.   path = fp -> fname;
  229.   p = e_malloc(strlen(path) + 1);
  230.   strcpy(p, path);
  231. #ifdef __SUPPORT_EA__
  232.   hpb.ea = fp->ea;
  233. #endif
  234.   freeze(p, p + (fp -> cpos - path));
  235.   free(p);
  236. }
  237.  
  238.  
  239. int
  240. append(void)
  241. {
  242.   struct fb *fp;
  243.  
  244.   if((fp = searchfile(hpb.pathname)) != NULL &&
  245.      (flg_c || fp -> time > hpb.mtime))
  246.     {
  247.       freeze_fb(fp);
  248.       return 1;
  249.     }
  250.   else
  251.     {
  252.       if(fp != NULL)
  253.     increment_disp();
  254.       copylzh(&hpb);
  255.       return 0;
  256.     }
  257. }
  258.  
  259.  
  260. int
  261. endappend(void)
  262. {
  263.   struct fb *fp;
  264.   int modified;
  265.  
  266.   modified = 0;
  267.   for(fp = fbuf; fp != NULL; fp = fp -> nxt)
  268.     if(fp -> used == 0)
  269.       {
  270.     freeze_fb(fp);
  271.     modified++;
  272.       }
  273.  
  274.   return modified;
  275. }
  276.  
  277.  
  278. void
  279. deletefiles(void)
  280. {
  281.   struct fb *fp;
  282.  
  283.   for(fp = fbuf; fp != NULL; fp = fp -> nxt)
  284.     {
  285.       strcpy(work, fp -> fname);
  286.       remove(work);
  287.     }
  288. }
  289.  
  290.  
  291. #ifdef __API16__
  292. #pragma optimize("awgelzc", off)
  293. #endif
  294. int
  295. freshen(char *bdir)
  296. {
  297.   char *path, *name;
  298.   struct find_t srchbuf;
  299.   int flag;
  300.  
  301.   if(bdir)
  302.     {
  303.       path = e_malloc(strlen(bdir) + strlen(hpb.pathname) + 1);
  304.       strcpy(path, bdir);
  305.       if(*hpb.pathname == DELIM)
  306.     {
  307.       if(path[1] == ':')
  308.         path[2] = '\0';
  309.       else
  310.         *path = '\0';
  311.     }
  312.       name = path + strlen(path);
  313.       strcpy(name, hpb.pathname);
  314.       flag = !_dos_findfirst(path, flg_a ? 0x07 : 0x01, &srchbuf);
  315.       if(flag)
  316.     if(!flg_c)
  317.       if(hpb.mtime >= dos2unix((struct ftime *)&(srchbuf.wr_time)))
  318.         flag = 0;
  319.     }
  320.   else
  321.     flag = 0;
  322.  
  323.   if(flag)
  324.     {
  325. #ifdef __SUPPORT_EA__
  326.       hpb.ea = srchbuf.ea;
  327. #endif
  328.       freeze(path, name);
  329.     }
  330.   
  331.   else
  332.     copylzh(&hpb);
  333.   free(path);
  334.  
  335.   return flag;
  336. }
  337.