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 >
Wrap
C/C++ Source or Header
|
1996-02-04
|
6KB
|
337 lines
/*
* append.c --- append to archive
* Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
* Copyright (C) 1991-1996, Satoshi HIRAMATSU (OS/2 HPFS version)
*
* $Log$
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <time.h>
#include "typedef.h"
#include "port2.h"
#include "lh.h"
#include "header.h"
#include "intrface.h"
#include "slidehuf.h"
#include "disp.h"
#include "errmes.h"
struct interfacing interface;
void
copylzh(header)
HEADER *header;
{
int _flg_n = flg_n;
flg_n = 2;
update_arcstamp();
copyhdr(file1, file2, &hpb);
copyfile(file1, file2, hpb.packed, 0);
flg_n = _flg_n;
}
struct extension
{
struct extension *nxt;
char ext[1];
};
struct extension *extptr;
void
regext(char *ext)
{
struct extension *p;
if(*ext == '.')
ext++;
p = malloc(strlen(ext) + sizeof(struct extension));
p -> nxt = extptr;
strcpy(p -> ext, ext);
extptr = p;
}
static int
isarc(char *name)
{
static char arc[14][4] =
{
"zoo", "arc", "pak", "zip", "lzs",
"lzh", "taZ", "shZ", "pZ", "Z",
"tgz", "sgz", "pgz", "gz"
};
char *p, old_i = flg_i;
struct extension *q;
int i;
p = strrchr(name, '.');
if(p == NULL)
p = name + strlen(name) - 1;
p++;
flg_i = 0; /* ignore case */
for(i = 0; i < sizeof(arc) / 4; i++)
{
if(*matchfname(arc[i], p) == '\0')
{
flg_i = old_i;
return 1;
}
}
for(q = extptr; q; q = q -> nxt)
{
if(*matchfname(q -> ext, p) == '\0')
{
flg_i = old_i;
return 1;
}
}
flg_i = old_i;
return 0;
}
void
freeze(char *path, char *name)
{
static int method = -1;
int __isdir__ = 0;
HEADERLINK *baseheader;
hword crc = 0;
if(flg_i == 2)
{
hpb.pathname = jstrlwr(name);
hpb.filename = jstrlwr(getfilename(name));
}
else if(flg_i == 0)
{
hpb.pathname = jstrupr(name);
hpb.filename = jstrupr(getfilename(name));
}
else
{
hpb.pathname = name;
hpb.filename = getfilename(name);
}
hpb.pathlen = strlen(hpb.pathname);
hpb.namelen = strlen(hpb.filename);
dispflg = 0;
if(*(name + strlen(name) - 1) == DELIM) /* for directory archive */
{
dispflg = 1;
__isdir__ = 1;
file3 = (FILE *) NULL;
}
else
file3 = myropen(path);
if(method < 0)
{
method = 5;
if(flg_o)
method = 1;
if(flg_z == 1)
method = 0;
if(method > 0)
method = encode_alloc(method);
}
_dos_getfileattr(path, &hpb.attr);
if(!flg_a)
hpb.attr &= 0x21;
if(__isdir__)
hpb.mtime = getdirectorytime(path);
else
#ifndef __SUPPORT_CTIME_ATIME__
hpb.mtime = getfiletime(file3);
#else
_os2_getftime(fileno(file3), &hpb.mtime, &hpb.ctime, &hpb.atime);
#endif
hpb.level = flg_h;
hpb.info = 0;
if(__isdir__)
hpb.original = hpb.packed = 0;
else
hpb.original = hpb.packed = filelength(fileno(file3));
if(__isdir__)
strcpy(hpb.method, "-lhd-"); /* directory archive */
else
strcpy(hpb.method, "-lh5-");
interface.method = method;
interface.dicbit = (method == 1 || method == 4) ? 12 : 13;
interface.original = hpb.original;
regdisp("Freezing", path);
disp(0, outredir);
initdisp();
if(hpb.original == 0 || flg_z == 2 && isarc(hpb.filename))
interface.method = 0;
if(interface.method > 0)
{
baseheader = makehdr(&hpb);
writehdr(file2, baseheader, &hpb);
interface.infile = file3;
interface.outfile = file2;
crc = encode(&interface);
if(interface.packed >= interface.original)
{
rewind(file3);
fseek(file2, hpb.currentpos, SEEK_SET);
interface.method = 0;
dispname();
initdisp();
}
hpb.packed = interface.packed;
}
if(!__isdir__)
hpb.method[3] = interface.method + '0';
if(interface.method == 0)
{
baseheader = makehdr(&hpb);
writehdr(file2, baseheader, &hpb);
if(!__isdir__)
crc = copyfile(file3, file2, hpb.original, 1);
}
update_arcstamp();
hpb.filecrc = crc;
adjusthdr(file2, baseheader, &hpb);
if(!__isdir__)
fclose(file3);
if(ferror(file2))
error(WTERR, filename2);
if(flg_n != 2)
eprintf((flg_l) ? "\r ==> %3d%%\n" : "\r==> %3d%%\n",
ratio(hpb.packed, hpb.original, 2));
}
static void
freeze_fb(struct fb *fp)
{
char *p, *path;
path = fp -> fname;
p = e_malloc(strlen(path) + 1);
strcpy(p, path);
#ifdef __SUPPORT_EA__
hpb.ea = fp->ea;
#endif
freeze(p, p + (fp -> cpos - path));
free(p);
}
int
append(void)
{
struct fb *fp;
if((fp = searchfile(hpb.pathname)) != NULL &&
(flg_c || fp -> time > hpb.mtime))
{
freeze_fb(fp);
return 1;
}
else
{
if(fp != NULL)
increment_disp();
copylzh(&hpb);
return 0;
}
}
int
endappend(void)
{
struct fb *fp;
int modified;
modified = 0;
for(fp = fbuf; fp != NULL; fp = fp -> nxt)
if(fp -> used == 0)
{
freeze_fb(fp);
modified++;
}
return modified;
}
void
deletefiles(void)
{
struct fb *fp;
for(fp = fbuf; fp != NULL; fp = fp -> nxt)
{
strcpy(work, fp -> fname);
remove(work);
}
}
#ifdef __API16__
#pragma optimize("awgelzc", off)
#endif
int
freshen(char *bdir)
{
char *path, *name;
struct find_t srchbuf;
int flag;
if(bdir)
{
path = e_malloc(strlen(bdir) + strlen(hpb.pathname) + 1);
strcpy(path, bdir);
if(*hpb.pathname == DELIM)
{
if(path[1] == ':')
path[2] = '\0';
else
*path = '\0';
}
name = path + strlen(path);
strcpy(name, hpb.pathname);
flag = !_dos_findfirst(path, flg_a ? 0x07 : 0x01, &srchbuf);
if(flag)
if(!flg_c)
if(hpb.mtime >= dos2unix((struct ftime *)&(srchbuf.wr_time)))
flag = 0;
}
else
flag = 0;
if(flag)
{
#ifdef __SUPPORT_EA__
hpb.ea = srchbuf.ea;
#endif
freeze(path, name);
}
else
copylzh(&hpb);
free(path);
return flag;
}