📄 msdos.c
字号:
/***********************//* Function checkdir() *//***********************/int checkdir(__G__ pathcomp, flag) __GDEF char *pathcomp; int flag;/* * returns: 1 - (on APPEND_NAME) truncated filename * 2 - path doesn't exist, not allowed to create * 3 - path doesn't exist, tried to create and failed; or * path exists and is not a directory, but is supposed to be * 4 - path is too long * 10 - can't allocate memory for filename buffers */{ static int rootlen = 0; /* length of rootpath */ static char *rootpath; /* user's "extract-to" directory */ static char *buildpath; /* full path (so far) to extracted file */ static char *end; /* pointer to end of buildpath ('\0') */#ifdef MSC int attrs; /* work around MSC stat() bug */#endif# define FN_MASK 7# define FUNCTION (flag & FN_MASK)/*--------------------------------------------------------------------------- APPEND_DIR: append the path component to the path being built and check for its existence. If doesn't exist and we are creating directories, do so for this one; else signal success or error as appropriate. ---------------------------------------------------------------------------*/ if (FUNCTION == APPEND_DIR) { int too_long = FALSE; Trace((stderr, "appending dir segment [%s]\n", pathcomp)); while ((*end = *pathcomp++) != '\0') ++end; /* GRR: could do better check, see if overrunning buffer as we go: * check end-buildpath after each append, set warning variable if * within 20 of FILNAMSIZ; then if var set, do careful check when * appending. Clear variable when begin new path. */ if ((end-buildpath) > FILNAMSIZ-3) /* need '/', one-char name, '\0' */ too_long = TRUE; /* check if extracting directory? */#ifdef MSC /* MSC 6.00 bug: stat(non-existent-dir) == 0 [exists!] */ if (_dos_getfileattr(buildpath, &attrs) || stat(buildpath, &G.statbuf))#else if (SSTAT(buildpath, &G.statbuf)) /* path doesn't exist */#endif { if (!G.create_dirs) { /* told not to create (freshening) */ free(buildpath); return 2; /* path doesn't exist: nothing to do */ } if (too_long) { Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong), FnFilter1(buildpath))); free(buildpath); return 4; /* no room for filenames: fatal */ } if (MKDIR(buildpath, 0777) == -1) { /* create the directory */ Info(slide, 1, ((char *)slide, LoadFarString(CantCreateDir), FnFilter2(buildpath), FnFilter1(G.filename))); free(buildpath); return 3; /* path didn't exist, tried to create, failed */ } created_dir = TRUE; } else if (!S_ISDIR(G.statbuf.st_mode)) { Info(slide, 1, ((char *)slide, LoadFarString(DirIsntDirectory), FnFilter2(buildpath), FnFilter1(G.filename))); free(buildpath); return 3; /* path existed but wasn't dir */ } if (too_long) { Info(slide, 1, ((char *)slide, LoadFarString(PathTooLong), FnFilter1(buildpath))); free(buildpath); return 4; /* no room for filenames: fatal */ } *end++ = '/'; *end = '\0'; Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath))); return 0; } /* end if (FUNCTION == APPEND_DIR) *//*--------------------------------------------------------------------------- GETPATH: copy full path to the string pointed at by pathcomp, and free buildpath. ---------------------------------------------------------------------------*/ if (FUNCTION == GETPATH) { strcpy(pathcomp, buildpath); Trace((stderr, "getting and freeing path [%s]\n", FnFilter1(pathcomp))); free(buildpath); buildpath = end = (char *)NULL; return 0; }/*--------------------------------------------------------------------------- APPEND_NAME: assume the path component is the filename; append it and return without checking for existence. ---------------------------------------------------------------------------*/ if (FUNCTION == APPEND_NAME) {#ifdef NOVELL_BUG_WORKAROUND if (end == buildpath && !G.pInfo->vollabel) { /* work-around for Novell's "overwriting executables" bug: prepend "./" to name when no path component is specified */ *end++ = '.'; *end++ = '/'; }#endif /* NOVELL_BUG_WORKAROUND */ Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp))); while ((*end = *pathcomp++) != '\0') { ++end; if ((end-buildpath) >= FILNAMSIZ) { *--end = '\0'; Info(slide, 1, ((char *)slide, LoadFarString(PathTooLongTrunc), FnFilter1(G.filename), FnFilter2(buildpath))); return 1; /* filename truncated */ } } Trace((stderr, "buildpath now = [%s]\n", FnFilter1(buildpath))); return 0; /* could check for existence here, prompt for new name... */ }/*--------------------------------------------------------------------------- INIT: allocate and initialize buffer space for the file currently being extracted. If file was renamed with an absolute path, don't prepend the extract-to path. ---------------------------------------------------------------------------*/ if (FUNCTION == INIT) { Trace((stderr, "initializing buildpath to ")); /* allocate space for full filename, root path, and maybe "./" */ if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+3)) == (char *)NULL) return 10; if (G.pInfo->vollabel) {/* GRR: for network drives, do strchr() and return IZ_VOL_LABEL if not [1] */ if (renamed_fullpath && pathcomp[1] == ':') *buildpath = (char)ToLower(*pathcomp); else if (!renamed_fullpath && rootlen > 1 && rootpath[1] == ':') *buildpath = (char)ToLower(*rootpath); else { GETDRIVE(nLabelDrive); /* assumed that a == 1, b == 2, etc. */ *buildpath = (char)(nLabelDrive - 1 + 'a'); } nLabelDrive = *buildpath - 'a' + 1; /* save for mapname() */ if (uO.volflag == 0 || *buildpath < 'a' || /* no label/bogus disk */ (uO.volflag == 1 && !isfloppy(nLabelDrive))) /* -$: no fixed */ { free(buildpath); return IZ_VOL_LABEL; /* skipping with message */ } *buildpath = '\0'; end = buildpath; } else if (renamed_fullpath) { /* pathcomp = valid data */ end = buildpath; while ((*end = *pathcomp++) != '\0') ++end; } else if (rootlen > 0) { strcpy(buildpath, rootpath); end = buildpath + rootlen; } else { *buildpath = '\0'; end = buildpath; } Trace((stderr, "[%s]\n", FnFilter1(buildpath))); return 0; }/*--------------------------------------------------------------------------- ROOT: if appropriate, store the path in rootpath and create it if neces- sary; else assume it's a zipfile member and return. This path segment gets used in extracting all members from every zipfile specified on the command line. Note that under OS/2 and MS-DOS, if a candidate extract-to directory specification includes a drive letter (leading "x:"), it is treated just as if it had a trailing '/'--that is, one directory level will be created if the path doesn't exist, unless this is otherwise pro- hibited (e.g., freshening). ---------------------------------------------------------------------------*/#if (!defined(SFX) || defined(SFX_EXDIR)) if (FUNCTION == ROOT) { Trace((stderr, "initializing root path to [%s]\n", FnFilter1(pathcomp))); if (pathcomp == (char *)NULL) { rootlen = 0; return 0; } if ((rootlen = strlen(pathcomp)) > 0) { int had_trailing_pathsep=FALSE, has_drive=FALSE, xtra=2; if (isalpha(pathcomp[0]) && pathcomp[1] == ':') has_drive = TRUE; /* drive designator */ if (pathcomp[rootlen-1] == '/' || pathcomp[rootlen-1] == '\\') { pathcomp[--rootlen] = '\0'; had_trailing_pathsep = TRUE; } if (has_drive && (rootlen == 2)) { if (!had_trailing_pathsep) /* i.e., original wasn't "x:/" */ xtra = 3; /* room for '.' + '/' + 0 at end of "x:" */ } else if (rootlen > 0) { /* need not check "x:." and "x:/" */#ifdef MSC /* MSC 6.00 bug: stat(non-existent-dir) == 0 [exists!] */ if (_dos_getfileattr(pathcomp, &attrs) || SSTAT(pathcomp,&G.statbuf) || !S_ISDIR(G.statbuf.st_mode))#else if (SSTAT(pathcomp,&G.statbuf) || !S_ISDIR(G.statbuf.st_mode))#endif { /* path does not exist */ if (!G.create_dirs /* || iswild(pathcomp) */ ) { rootlen = 0; return 2; /* treat as stored file */ }/* GRR: scan for wildcard characters? OS-dependent... if find any, return 2: * treat as stored file(s) */ /* create directory (could add loop here to scan pathcomp * and create more than one level, but really necessary?) */ if (MKDIR(pathcomp, 0777) == -1) { Info(slide, 1, ((char *)slide, LoadFarString(CantCreateExtractDir), FnFilter1(pathcomp))); rootlen = 0; /* path didn't exist, tried to create, */ return 3; /* failed: file exists, or need 2+ levels */ } } } if ((rootpath = (char *)malloc(rootlen+xtra)) == (char *)NULL) { rootlen = 0; return 10; } strcpy(rootpath, pathcomp); if (xtra == 3) /* had just "x:", make "x:." */ rootpath[rootlen++] = '.'; rootpath[rootlen++] = '/'; rootpath[rootlen] = '\0'; Trace((stderr, "rootpath now = [%s]\n", FnFilter1(rootpath))); } return 0; }#endif /* !SFX || SFX_EXDIR *//*--------------------------------------------------------------------------- END: free rootpath, immediately prior to program exit. ---------------------------------------------------------------------------*/ if (FUNCTION == END) { Trace((stderr, "freeing rootpath\n")); if (rootlen > 0) { free(rootpath); rootlen = 0; } return 0; } return 99; /* should never reach */} /* end function checkdir() *//***********************//* Function isfloppy() *//***********************/static int isfloppy(nDrive) /* more precisely, is it removable? */ int nDrive;{ union REGS regs; regs.h.ah = 0x44; regs.h.al = 0x08; regs.h.bl = (uch)nDrive;#ifdef __EMX__ _int86(0x21, ®s, ®s); if (WREGS(regs,flags) & 1)#else intdos(®s, ®s); if (WREGS(regs,cflag)) /* error: do default a/b check instead */#endif { Trace((stderr, "error in DOS function 0x44 (AX = 0x%04x): guessing instead...\n", WREGS(regs,ax))); return (nDrive == 1 || nDrive == 2)? TRUE : FALSE; } else return WREGS(regs,ax)? FALSE : TRUE;}/**************************//* Function z_dos_chmod() *//**************************/static int z_dos_chmod(__G__ fname, attributes) __GDEF ZCONST char *fname; int attributes;{ char *name; unsigned fnamelength; int errv; /* set file attributes: The DOS `chmod' system call requires to mask out the directory and volume_label attribute bits. And, a trailing '/' has to be removed from the directory name, the DOS `chmod' system call does not accept it. */ fnamelength = strlen(fname); if (fnamelength > 1 && fname[fnamelength-1] == '/' && fname[fnamelength-2] != ':' && (name = (char *)malloc(fnamelength)) != (char *)NULL) { strncpy(name, fname, fnamelength-1); name[fnamelength-1] = '\0'; } else { name = (char *)fname; fnamelength = 0; }#if defined(__TURBOC__) || (defined(__DJGPP__) && (__DJGPP__ >= 2))# if (defined(__BORLANDC__) && (__BORLANDC__ >= 0x0452))# define Chmod _rtl_chmod# else# define Chmod _chmod# endif errv = (Chmod(name, 1, attributes & (~FSUBDIR & ~FVOLID)) != (attributes & (~FSUBDIR & ~FVOLID)));# undef Chmod#else /* !(__TURBOC__ || (__DJGPP__ && __DJGPP__ >= 2)) */ errv = (_dos_setfileattr(name, attributes & (~FSUBDIR & ~FVOLID)) != 0);#endif /* ?(__TURBOC__ || (__DJGPP__ && __DJGPP__ >= 2)) */ if (errv) Info(slide, 1, ((char *)slide, LoadFarString(AttribsMayBeWrong))); if (fnamelength > 0) free(name); return errv;} /* end function z_dos_chmod() */#if (!defined(__GO32__) && !defined(__EMX__))typedef struct dosfcb { uch flag; /* ff to indicate extended FCB */ char res[5]; /* reserved */ uch vattr; /* attribute */ uch drive; /* drive (1=A, 2=B, ...) */ uch vn[11]; /* file or volume name */ char dmmy[5]; uch nn[11]; /* holds new name if renaming (else reserved) */ char dmmy2[9];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -