Compile fix

This commit is contained in:
pagefault
2001-08-29 07:17:52 +00:00
parent a81a5ca651
commit 8feda898ab

View File

@@ -1,336 +1,336 @@
//Copyright (C) 1997-2001 ZSNES Team ( zsknight@zsnes.com / _demo_@zsnes.com ) //Copyright (C) 1997-2001 ZSNES Team ( zsknight@zsnes.com / _demo_@zsnes.com )
// //
//This program is free software; you can redistribute it and/or //This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License //modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either //as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later //version 2 of the License, or (at your option) any later
//version. //version.
// //
//This program is distributed in the hope that it will be useful, //This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of //but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details. //GNU General Public License for more details.
// //
//You should have received a copy of the GNU General Public License //You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software //along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#ifdef __LINUX__ #ifdef __LINUX__
#include "../gblhdr.h" #include "../gblhdr.h"
#else #else
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <utime.h> #endif
#endif
#ifdef __MSDOS__
#ifdef __MSDOS__ #include <utime.h>
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
#include "unzip.h" #include "unzip.h"
#define CASESENSITIVITY (0) #define CASESENSITIVITY (0)
#define WRITEBUFFERSIZE (8192) #define WRITEBUFFERSIZE (8192)
unsigned int ZipError=0; unsigned int ZipError=0;
// 1 : Cannot open file // 1 : Cannot open file
// 2 : Could not create directory // 2 : Could not create directory
// 3 : Zip error // 3 : Zip error
// 4 : Memory error // 4 : Memory error
// 5 : Error opening file // 5 : Error opening file
// 6 : Error Writing file // 6 : Error Writing file
#ifndef __LINUX__ #ifndef __LINUX__
#ifndef __MSDOS__ #ifndef __MSDOS__
struct utimbuf { struct utimbuf {
time_t actime; time_t actime;
time_t modtime; time_t modtime;
}; };
#endif #endif
#endif #endif
void change_file_date(const char *filename,uLong dosdate,tm_unz tmu_date) void change_file_date(const char *filename,uLong dosdate,tm_unz tmu_date)
{ {
struct utimbuf ut; struct utimbuf ut;
struct tm newdate; struct tm newdate;
newdate.tm_sec = tmu_date.tm_sec; newdate.tm_sec = tmu_date.tm_sec;
newdate.tm_min=tmu_date.tm_min; newdate.tm_min=tmu_date.tm_min;
newdate.tm_hour=tmu_date.tm_hour; newdate.tm_hour=tmu_date.tm_hour;
newdate.tm_mday=tmu_date.tm_mday; newdate.tm_mday=tmu_date.tm_mday;
newdate.tm_mon=tmu_date.tm_mon; newdate.tm_mon=tmu_date.tm_mon;
if (tmu_date.tm_year > 1900) if (tmu_date.tm_year > 1900)
newdate.tm_year=tmu_date.tm_year - 1900; newdate.tm_year=tmu_date.tm_year - 1900;
else else
newdate.tm_year=tmu_date.tm_year ; newdate.tm_year=tmu_date.tm_year ;
newdate.tm_isdst=-1; newdate.tm_isdst=-1;
ut.actime=ut.modtime=mktime(&newdate); ut.actime=ut.modtime=mktime(&newdate);
utime(filename,&ut); utime(filename,&ut);
} }
int mymkdir(const char *dirname) int mymkdir(const char *dirname)
{ {
#ifdef __LINUX__ #ifdef __LINUX__
return(mkdir(dirname, (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))); return(mkdir(dirname, (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)));
#else #else
#ifdef __MSDOS__ #ifdef __MSDOS__
return(mkdir(dirname, (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))); return(mkdir(dirname, (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)));
#else #else
return(mkdir(dirname)); return(mkdir(dirname));
#endif #endif
#endif #endif
} }
int makedir (char *newdir) int makedir (char *newdir)
{ {
char *buffer ; char *buffer ;
char *p; char *p;
int len = strlen(newdir); int len = strlen(newdir);
if (len <= 0) if (len <= 0)
return 0; return 0;
buffer = (char*)malloc(len+1); buffer = (char*)malloc(len+1);
strcpy(buffer,newdir); strcpy(buffer,newdir);
if (buffer[len-1] == '/') { if (buffer[len-1] == '/') {
buffer[len-1] = '\0'; buffer[len-1] = '\0';
} }
if (mymkdir(buffer) == 0) if (mymkdir(buffer) == 0)
{ {
free(buffer); free(buffer);
return 1; return 1;
} }
p = buffer+1; p = buffer+1;
while (1) while (1)
{ {
char hold; char hold;
while(*p && *p != '\\' && *p != '/') while(*p && *p != '\\' && *p != '/')
p++; p++;
hold = *p; hold = *p;
*p = 0; *p = 0;
if ((mymkdir(buffer) == -1) && (errno == ENOENT)) if ((mymkdir(buffer) == -1) && (errno == ENOENT))
{ {
ZipError=2; ZipError=2;
free(buffer); free(buffer);
return 0; return 0;
} }
if (hold == 0) if (hold == 0)
break; break;
*p++ = hold; *p++ = hold;
} }
free(buffer); free(buffer);
return 1; return 1;
} }
int do_extract_currentfile(unzFile uf, int do_extract_currentfile(unzFile uf,
const int* popt_extract_without_path, const int* popt_extract_without_path,
int *popt_overwrite) int *popt_overwrite)
{ {
char filename_inzip[256]; char filename_inzip[256];
char* filename_withoutpath; char* filename_withoutpath;
char* p; char* p;
int err=UNZ_OK; int err=UNZ_OK;
FILE *fout=NULL; FILE *fout=NULL;
void* buf; void* buf;
uInt size_buf; uInt size_buf;
unz_file_info file_info; unz_file_info file_info;
err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
if (err!=UNZ_OK) if (err!=UNZ_OK)
{ {
ZipError=3; ZipError=3;
return err; return err;
} }
size_buf = WRITEBUFFERSIZE; size_buf = WRITEBUFFERSIZE;
buf = (void*)malloc(size_buf); buf = (void*)malloc(size_buf);
if (buf==NULL) if (buf==NULL)
{ {
ZipError=4; ZipError=4;
return UNZ_INTERNALERROR; return UNZ_INTERNALERROR;
} }
p = filename_withoutpath = filename_inzip; p = filename_withoutpath = filename_inzip;
while ((*p) != '\0') while ((*p) != '\0')
{ {
if (((*p)=='/') || ((*p)=='\\')) if (((*p)=='/') || ((*p)=='\\'))
filename_withoutpath = p+1; filename_withoutpath = p+1;
p++; p++;
} }
if ((*filename_withoutpath)=='\0') if ((*filename_withoutpath)=='\0')
{ {
if ((*popt_extract_without_path)==0) if ((*popt_extract_without_path)==0)
{ {
mymkdir(filename_inzip); mymkdir(filename_inzip);
} }
} }
else else
{ {
char* write_filename; char* write_filename;
int skip=0; int skip=0;
if ((*popt_extract_without_path)==0) if ((*popt_extract_without_path)==0)
write_filename = filename_inzip; write_filename = filename_inzip;
else else
write_filename = filename_withoutpath; write_filename = filename_withoutpath;
err = unzOpenCurrentFile(uf); err = unzOpenCurrentFile(uf);
if (err!=UNZ_OK) if (err!=UNZ_OK)
{ {
ZipError=4; ZipError=4;
} }
if (((*popt_overwrite)==0) && (err==UNZ_OK)) if (((*popt_overwrite)==0) && (err==UNZ_OK))
{ {
char rep='A'; char rep='A';
FILE* ftestexist; FILE* ftestexist;
ftestexist = fopen(write_filename,"rb"); ftestexist = fopen(write_filename,"rb");
if (ftestexist!=NULL) if (ftestexist!=NULL)
{ {
fclose(ftestexist); fclose(ftestexist);
rep='N'; rep='N';
} }
if (rep == 'N') if (rep == 'N')
skip = 1; skip = 1;
if (rep == 'A') if (rep == 'A')
*popt_overwrite=1; *popt_overwrite=1;
} }
if ((skip==0) && (err==UNZ_OK)) if ((skip==0) && (err==UNZ_OK))
{ {
fout=fopen(write_filename,"wb"); fout=fopen(write_filename,"wb");
/* some zipfile don't contain directory alone before file */ /* some zipfile don't contain directory alone before file */
if ((fout==NULL) && ((*popt_extract_without_path)==0) && if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
(filename_withoutpath!=(char*)filename_inzip)) (filename_withoutpath!=(char*)filename_inzip))
{ {
char c=*(filename_withoutpath-1); char c=*(filename_withoutpath-1);
*(filename_withoutpath-1)='\0'; *(filename_withoutpath-1)='\0';
makedir(write_filename); makedir(write_filename);
*(filename_withoutpath-1)=c; *(filename_withoutpath-1)=c;
fout=fopen(write_filename,"wb"); fout=fopen(write_filename,"wb");
} }
if (fout==NULL) if (fout==NULL)
{ {
ZipError=5; ZipError=5;
} }
} }
if (fout!=NULL) if (fout!=NULL)
{ {
do do
{ {
err = unzReadCurrentFile(uf,buf,size_buf); err = unzReadCurrentFile(uf,buf,size_buf);
if (err<0) if (err<0)
{ {
ZipError=4; ZipError=4;
break; break;
} }
if (err>0) if (err>0)
if (fwrite(buf,err,1,fout)!=1) if (fwrite(buf,err,1,fout)!=1)
{ {
ZipError=6; ZipError=6;
err=UNZ_ERRNO; err=UNZ_ERRNO;
break; break;
} }
} }
while (err>0); while (err>0);
fclose(fout); fclose(fout);
if (err==0) if (err==0)
change_file_date(write_filename,file_info.dosDate, change_file_date(write_filename,file_info.dosDate,
file_info.tmu_date); file_info.tmu_date);
} }
if (err==UNZ_OK) if (err==UNZ_OK)
{ {
err = unzCloseCurrentFile (uf); err = unzCloseCurrentFile (uf);
if (err!=UNZ_OK) if (err!=UNZ_OK)
{ {
ZipError=4; ZipError=4;
} }
} }
else else
unzCloseCurrentFile(uf); /* don't lose the error */ unzCloseCurrentFile(uf); /* don't lose the error */
} }
free(buf); free(buf);
return err; return err;
} }
int do_extract(unzFile uf,int opt_extract_without_path,int opt_overwrite) int do_extract(unzFile uf,int opt_extract_without_path,int opt_overwrite)
{ {
uLong i; uLong i;
unz_global_info gi; unz_global_info gi;
int err; int err;
err = unzGetGlobalInfo (uf,&gi); err = unzGetGlobalInfo (uf,&gi);
if (err!=UNZ_OK) if (err!=UNZ_OK)
ZipError=4; ZipError=4;
for (i=0;i<gi.number_entry;i++) for (i=0;i<gi.number_entry;i++)
{ {
if (do_extract_currentfile(uf,&opt_extract_without_path, if (do_extract_currentfile(uf,&opt_extract_without_path,
&opt_overwrite) != UNZ_OK) &opt_overwrite) != UNZ_OK)
break; break;
if ((i+1)<gi.number_entry) if ((i+1)<gi.number_entry)
{ {
err = unzGoToNextFile(uf); err = unzGoToNextFile(uf);
if (err!=UNZ_OK) if (err!=UNZ_OK)
{ {
ZipError=4; ZipError=4;
break; break;
} }
} }
} }
return 0; return 0;
} }
void extractzip(char *FileToExtract) void extractzip(char *FileToExtract)
{ {
unzFile uf=NULL; unzFile uf=NULL;
// I really don't like hardcoding these sizes... // I really don't like hardcoding these sizes...
char oldpath[128]; char oldpath[128];
#ifdef __LINUX__ #ifdef __LINUX__
extern char InitDir; extern char InitDir;
getcwd(oldpath, 128); getcwd(oldpath, 128);
chdir(&InitDir); chdir(&InitDir);
#endif #endif
uf = unzOpen(FileToExtract); uf = unzOpen(FileToExtract);
#ifdef __LINUX__ #ifdef __LINUX__
chdir(oldpath); chdir(oldpath);
#endif #endif
if (uf==NULL) if (uf==NULL)
{ {
ZipError=1; ZipError=1;
return; return;
} }
ZipError=0; ZipError=0;
do_extract(uf,0,0); do_extract(uf,0,0);
} }