📄 fileio.c
字号:
/* Copyright (C) 1990-1993 Mark Adler, Richard B. Wales, Jean-loup Gailly, Kai Uwe Rommel and Igor Mandrichenko. Permission is granted to any individual or institution to use, copy, or redistribute this software so long as all of the original files are included, that it is not sold for profit, and that this copyright notice is retained.*//* * fileio.c by Mark Adler. * * This file has become a big mess over time, and will be split in the * next version, one file per operating system. */#include "zip.h"#include <time.h>#ifdef WIN32# include <sys/utime.h># include <windows.h> /* for findfirst/findnext */ char *GetLongPathEA OF((char *name));#endif#ifdef MACOS# define EXDEV 1#endif#ifdef OSF# define EXDEV 18 /* avoid a bug in the DEC OSF/1 header files. */#else# include <errno.h>#endif#ifdef MINIX# ifdef S_IWRITE# undef S_IWRITE# endif /* S_IWRITE */# define S_IWRITE S_IWUSR#endif /* S_IWUSR */#ifdef ATARI_ST# undef MSDOS#endif#if defined(MSDOS) && !defined(__GO32__)# include <io.h># ifdef __TURBOC__# include <dir.h># else /* !__TURBOC__ */# if !defined(__EMX__) && !defined(__WATCOMC__)# include <direct.h># endif# endif /* ?__TURBOC__ */# define link rename# if defined(OS2) || defined(WIN32)# define MATCH shmatch# else# define MATCH dosmatch# endif#else /* !MSDOS */ extern int errno; /* error number from system functions */# ifdef TOPS20# define PATH_START '<'# define PATH_END '>'# define link rename# endif# ifdef VMS# define PATH_START '['# define PATH_END ']'# define RMDIR# define link rename# include "VMSmunch.h"# endif /* VMS */# ifdef MACOS# define link rename# define mktemp tmpnam# endif# define MATCH shmatch#endif /* ?MSDOS */#ifdef ATARI_ST# define MSDOS 1#endif#ifdef UTS# define RMDIR#endif /* UTS */#ifdef AMIGA# define link rename# define utime FileDate# ifdef __SASC_60# include <dos.h> /* SAS/C 6.x , pulled by wild() */# include <sys/dir.h># define direct dirent# define disk_not_mounted 0 # else# define dirent direct# endif# ifdef MODERN# include <clib/exec_protos.h># include <clib/dos_protos.h># endif# ifdef AZTEC_C# include <pragmas/exec_lib.h># include <pragmas/dos_lib.h># endif#endif #ifdef __human68k__# include <jctype.h># define link rename#endif/* Extra malloc() space in names for cutpath() */#if (defined(VMS) || defined(TOPS20))# define PAD 5 /* may have to change .FOO] to ]FOO.DIR;1 */#else# define PAD 0# define PATH_END '/'#endif#ifdef VMS# include <descrip.h># include <rms.h>#endif/* For directory access. */#ifndef UTIL#ifdef __human68k__#include <sys/dos.h>#endif#if defined(SYSV) || defined(__GO32__) || defined(__386BSD__) || defined(__human68k__)/* use readdir() */# include <dirent.h># define dstrm DIR# define direct dirent#else /* !SYSV && !__GO32__ && !__386BSD__ && !__human68k__*/#ifdef DIRENT /* use getdents() */#ifndef TOPS20# if defined(MINIX) || defined(OSF)# include <dirent.h># else /* !MINIX */# include <sys/dirent.h># endif /* ?MINIX */# define direct dirent# ifdef MINIX int getdents OF((int, char *, unsigned));# else /* !MINIX */ int getdents OF((int, char *, int));# endif /* ?MINIX */# define DBSZ 4096 /* This has to be bigger than a directory block */ typedef struct { /* directory stream buffer */ int f; /* file descriptor for the directory "file" */ char *p; /* pointer to next entry in buffer */ char *q; /* pointer after end of buffer contents */ char b[DBSZ]; /* buffer */ } dstrm;#else /* TOPS20 */# define TRUE 1# define FALSE 0# define O_RDONLY (0)# define O_T20_WILD (1<<18)# include <monsym.h> /* Get amazing monsym() macro */ extern int jsys(), fstat(); extern char *getcwd(); extern int _gtjfn(), _rljfn();# define JSYS_CLASS 0070000000000# define FLD(val,mask) (((unsigned)(val)*((mask)&(-(mask))))&(mask))# define _DEFJS(name,class) (FLD(class, JSYS_CLASS) | (monsym(name)&0777777))# define JFNS _DEFJS("JFNS%", 1)# define GNJFN _DEFJS("GNJFN%", 0) static int wfopen(), wfnext(), strlower(), strupper(); static char *wfname(); typedef struct { int wfjfn; int more; } dstrm;#endif /* ?TOPS20 */#else /* !DIRENT */ /* use opendir(), etc. */# if defined(CONVEX) || defined(ultrix)# include <dirent.h># ifdef direct# undef direct /* ultrix 4.2, at least if !__POSIX */# endif# define direct dirent# endif /* CONVEX || ultrix */# ifdef NDIR# include "ndir.h" /* for HPUX */# else /* !NDIR */# ifdef MSDOS# ifdef OS2# include "os2zip.h"# else /* !OS2 */# ifdef WIN32# include "ntzip.h"# endif# ifndef ATARI_ST# include <dos.h># endif# ifdef __TURBOC__# define FATTR (hidden_files ? FA_HIDDEN+FA_SYSTEM+FA_DIREC : FA_DIREC)# define FFIRST(n,d,a) findfirst(n,(struct ffblk *)d,a)# define FNEXT(d) findnext((struct ffblk *)d)# else /* !__TURBOC__ */# define FATTR (hidden_files ? _A_HIDDEN+_A_SYSTEM+_A_SUBDIR :_A_SUBDIR)# define FA_LABEL _A_VOLID# define FFIRST(n,d,a) _dos_findfirst(n,a,(struct find_t *)d)# define FNEXT(d) _dos_findnext((struct find_t *)d)# endif /* ?__TURBOC__ */ typedef struct direct { char reserved [21]; char ff_attrib; short ff_ftime; short ff_fdate; long size;# ifndef WIN32 char d_name[13]; int d_first;# else char d_name[MAX_PATH]; int d_first; HANDLE d_hFindFile;# endif /* WIN32 */ } DIR; typedef DIR ff_dir;# define ff_name d_name /* ff_name is used by DJGPP */# endif /* ?OS2 */# else /* !MSDOS */# ifdef VMS# include <ssdef.h> typedef struct direct { int d_wild; /* flag for wildcard vs. non-wild */ struct FAB fab; struct NAM nam; char d_qualwildname[NAM$C_MAXRSS + 1]; char d_name[NAM$C_MAXRSS + 1]; } DIR;# else /* !VMS */# ifdef MACOS typedef struct direct { int d_wild; /* flag for wildcard vs. non-wild */ char *d_name; } DIR;# endif# if defined(M_XENIX) || defined(SYSNDIR)# include <sys/ndir.h># else /* !M_XENIX */# ifndef AZTEC_C# include <sys/dir.h># endif# endif /* ?M_XENIX */# ifdef NODIR /* for AT&T 3B1 + Amiga non SAS */# define dirent direct typedef FILE DIR;# endif /* NODIR */# endif /* ?VMS */# endif /* ?MSDOS */# endif /* ?NDIR */# define dstrm DIR# if defined(MSDOS) && !defined(OS2)# ifndef __GO32__ DIR *opendir OF((const char *)); struct direct *readdir OF((DIR *));# endif# ifndef WIN32 char *getVolumeLabel OF((int drive, ulg *vtime, ulg *vmode));# endif# endif#endif /* ?DIRENT */#endif /* ?SYSV */#endif /* !UTIL */#ifdef __GO32__ typedef struct ffblk ff_dir;# define FFIRST(n,d,a) findfirst(n,(struct ffblk *)d,a)#endif#define MSDOS_DIR_ATTR 0x10/* Library functions not in (most) header files */#ifdef TOPS20 extern int stat(), chmod(), toupper(), tolower();#endif#if defined(__IBMC__) || defined(__WATCOMC__)# define NO_MKTEMP#endifchar *mktemp OF((char *));#ifdef __GO32__# include <dos.h>#else /* int link OF((const char *, const char *)); */ /* int unlink OF((const char *)); */ /* int chmod OF((const char *, int)); */ /* For many targets, chmod is already defined by sys/stat.h, and second * parameter is an unsigned long. */#endif#ifndef UTIL /* the companion #endif is a bit of ways down ... */#if !defined(__TURBOC__) && !defined(WIN32) && !defined(sgi) int utime OF((char *, time_t *));#endif#ifndef MSDOS /* int open OF((char *, int, ...)); */ /* int close OF((int)); */# ifndef RMDIR /* int rmdir OF((const char *)); */# endif /* !RMDIR */#endif /* !MSDOS *//* Local globals (kinda like "military intelligence" or "broadcast quality") */local struct stat zipstatb;local int zipstate = -1;/* -1 unknown, 0 old zip file exists, 1 new zip file */local char *label = NULL;local ulg label_time = 0;local ulg label_mode = 0;#ifdef VMS typedef int statime;#else /* !VMS */ typedef time_t statime;#endif /* ?VMS *//* Local functions */#ifdef PROTO# ifdef VMS local void vms_wild(char *, dstrm *);# endif /* VMS */# ifdef DIRENT local dstrm *opend(char *); local void closed(dstrm *);# endif /* DIRENT */ local char *readd(dstrm *); local int fqcmp(const voidp *, const voidp *); local int fqcmpz(const voidp *, const voidp *); local char *last(char *); local char *msname(char *);# ifdef VMS local char *strlower(char *); local char *strupper(char *);# endif /* VMS */ local int newname(char *, int); local void inctime(struct tm *); local ulg unix2dostime(statime *);# if !defined(__TURBOC__) && !defined(OS2) && !defined(__GO32__) local int cmptime(struct tm *, struct tm *); local time_t invlocal(struct tm *);# endif /* !__TURBOC__ */#endif /* PROTO */#if defined(MSDOS) && !defined(OS2) && !defined(__GO32__)dstrm *opendir(n)const char *n; /* directory to open *//* Start searching for files in the MSDOS directory n */{ dstrm *d; /* malloc'd return value */ char *p; /* malloc'd temporary string */ if ((d = (dstrm *)malloc(sizeof(dstrm))) == NULL || (p = malloc(strlen(n) + 5)) == NULL) { if (d != NULL) free((void *) d); return NULL; } strcat(strcpy(p, n), "/"); strcat(p, "*.*"); /* don't use strcat in one step, TOPS20 is confused */#ifdef WIN32 { WIN32_FIND_DATA fd; DWORD dwAttr; if ( INVALID_HANDLE_VALUE == (d->d_hFindFile = FindFirstFile(p, &fd))) { free((voidp *)p); return NULL; } strcpy(d->d_name, fd.cFileName); if (-1 != (dwAttr = GetFileAttributes(fd.cFileName))) { if (!hidden_files && (FILE_ATTRIBUTE_SYSTEM == (dwAttr & FILE_ATTRIBUTE_SYSTEM) || FILE_ATTRIBUTE_HIDDEN == (dwAttr & FILE_ATTRIBUTE_HIDDEN))) { free ((voidp *)p); d->d_first = 0; return d; } } }#else if (FFIRST(p, d, FATTR)) { free((voidp *)p); return NULL; } free((voidp *)p);#endif d->d_first = 1; return d;}struct direct *readdir(d)dstrm *d; /* directory stream to read from *//* Return pointer to first or next directory entry, or NULL if end. */{ if (d->d_first) d->d_first = 0; else#ifdef WIN32 { WIN32_FIND_DATA fd; DWORD dwAttr; if (!FindNextFile(d->d_hFindFile, &fd)) return NULL; strcpy(d->d_name, fd.cFileName); if (!hidden_files && (-1 != (dwAttr = GetFileAttributes(fd.cFileName))) && (FILE_ATTRIBUTE_SYSTEM == (dwAttr & FILE_ATTRIBUTE_SYSTEM) || FILE_ATTRIBUTE_HIDDEN == (dwAttr & FILE_ATTRIBUTE_HIDDEN))) { return (readdir(d)); } }#else /* !WIN32 */ if (FNEXT(d)) return NULL;#endif return (struct direct *)d;}#ifdef WIN32void closedir(d)dstrm *d;{ FindClose(d->d_hFindFile); free(d);}#else# define closedir free#endif#endif /* MSDOS && !OS2 && !__GO32__ */#ifdef VMS/*--------------------------------------------------------------------------- _vms_findfirst() and _vms_findnext(), based on public-domain DECUS C fwild() and fnext() routines (originally written by Martin Minow, poss- ibly modified by Jerry Leichter for bintnxvms.c), were written by Greg Roelofs and are still in the public domain. Routines approximate the behavior of MS-DOS (MSC and Turbo C) findfirst and findnext functions. ---------------------------------------------------------------------------*/static char wild_version_part[10]="\0";local void vms_wild(p, d)char *p;dstrm *d;{ /* * Do wildcard setup */ /* set up the FAB and NAM blocks. */ d->fab = cc$rms_fab; /* initialize fab */ d->nam = cc$rms_nam; /* initialize nam */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -