📄 util.c
字号:
#if defined(ATARI) || defined(CMS_MVS)#include <ctype.h>/* this should be valid for all other platforms too. (HD 11/11/95) */ for (c = 0; c< sizeof(upper); c++) { upper[c] = islower(c) ? toupper(c) : c; lower[c] = isupper(c) ? tolower(c) : c; }#else for (c = 0; c < sizeof(upper); c++) upper[c] = lower[c] = (uch)c; for (c = 'a'; c <= 'z'; c++) upper[c] = (uch)(c - 'a' + 'A'); for (c = 'A'; c <= 'Z'; c++) lower[c] = (uch)(c - 'A' + 'a');#endif}# endif /* !OS2 */#endif /* ?MSDOS16 */int namecmp(string1, string2) ZCONST char *string1, *string2;/* Compare the two strings ignoring case, and correctly taking into * account national language characters. For operating systems with * case sensitive file names, this function is equivalent to strcmp. */{ int d; for (;;) { d = (int) (uch) case_map(*string1) - (int) (uch) case_map(*string2); if (d || *string1 == 0 || *string2 == 0) return d; string1++; string2++; }}#ifdef EBCDICchar *strtoasc(char *str1, ZCONST char *str2){ char *old; old = str1; while (*str1++ = (char)ascii[(uch)(*str2++)]); return old;}char *strtoebc(char *str1, ZCONST char *str2){ char *old; old = str1; while (*str1++ = (char)ebcdic[(uch)(*str2++)]); return old;}char *memtoasc(char *mem1, ZCONST char *mem2, unsigned len){ char *old; old = mem1; while (len--) *mem1++ = (char)ascii[(uch)(*mem2++)]; return old;}char *memtoebc(char *mem1, ZCONST char *mem2, unsigned len){ char *old; old = mem1; while (len--) *mem1++ = (char)ebcdic[(uch)(*mem2++)]; return old;}#endif /* EBCDIC */#ifdef IZ_ISO2OEM_ARRAYchar *str_iso_to_oem(dst, src) ZCONST char *src; char *dst;{ char *dest_start = dst; while (*dst++ = (char)iso2oem[(uch)(*src++)]); return dest_start;}#endif#ifdef IZ_OEM2ISO_ARRAYchar *str_oem_to_iso(dst, src) ZCONST char *src; char *dst;{ char *dest_start = dst; while (*dst++ = (char)oem2iso[(uch)(*src++)]); return dest_start;}#endif/* DBCS support for Info-ZIP's zip (mainly for japanese (-: ) * by Yoshioka Tsuneo (QWF00133@nifty.ne.jp,tsuneo-y@is.aist-nara.ac.jp) * This code is public domain! Date: 1998/12/20 */#ifdef _MBCSchar *___tmp_ptr;int lastchar(ptr) ZCONST char *ptr;{ ZCONST char *oldptr = ptr; while(*ptr != '\0'){ oldptr = ptr; INCSTR(ptr); } return (int)(unsigned)*oldptr;}unsigned char *zmbschr(str, c) ZCONST unsigned char *str; unsigned int c;{ while(*str != '\0'){ if (*str == c) {return (char*)str;} INCSTR(str); } return NULL;}unsigned char *zmbsrchr(str, c) ZCONST unsigned char *str; unsigned int c;{ unsigned char *match = NULL; while(*str != '\0'){ if (*str == c) {match = (char*)str;} INCSTR(str); } return match;}#endif /* _MBCS */#ifndef UTIL/***************************************************************** | envargs - add default options from environment to command line |---------------------------------------------------------------- | Author: Bill Davidsen, original 10/13/91, revised 23 Oct 1991. | This program is in the public domain. |---------------------------------------------------------------- | Minor program notes: | 1. Yes, the indirection is a tad complex | 2. Parenthesis were added where not needed in some cases | to make the action of the code less obscure. ****************************************************************/void envargs(Pargc, Pargv, envstr, envstr2) int *Pargc; char ***Pargv; char *envstr; char *envstr2;{ char *envptr; /* value returned by getenv */ char *bufptr; /* copy of env info */ int argc; /* internal arg count */ register int ch; /* spare temp value */ char **argv; /* internal arg vector */ char **argvect; /* copy of vector address */ /* see if anything in the environment */ envptr = getenv(envstr); if (envptr != NULL) /* usual var */ while (isspace((uch)*envptr)) /* we must discard leading spaces */ envptr++; if (envptr == NULL || *envptr == '\0') if ((envptr = getenv(envstr2)) != NULL) /* alternate */ while (isspace((uch)*envptr)) envptr++; if (envptr == NULL || *envptr == '\0') return; /* count the args so we can allocate room for them */ argc = count_args(envptr); bufptr = malloc(1 + strlen(envptr)); if (bufptr == NULL) ziperr(ZE_MEM, "Can't get memory for arguments"); strcpy(bufptr, envptr); /* allocate a vector large enough for all args */ argv = (char **)malloc((argc + *Pargc + 1) * sizeof(char *)); if (argv == NULL) { free(bufptr); ziperr(ZE_MEM, "Can't get memory for arguments"); } argvect = argv; /* copy the program name first, that's always true */ *(argv++) = *((*Pargv)++); /* copy the environment args first, may be changed */ do {#if defined(AMIGA) || defined(UNIX) if (*bufptr == '"') { char *argstart = ++bufptr; *(argv++) = argstart; for (ch = *bufptr; ch != '\0' && ch != '\"'; ch = *PREINCSTR(bufptr)) if (ch == '\\' && bufptr[1] != '\0') ++bufptr; /* skip to char after backslash */ if (ch != '\0') /* overwrite trailing '"' */ *(bufptr++) = '\0'; /* remove escape characters */ while ((argstart = MBSCHR(argstart, '\\')) != NULL) { strcpy(argstart, argstart + 1); if (*argstart) ++argstart; } } else { *(argv++) = bufptr; while ((ch = *bufptr) != '\0' && !isspace((uch)ch)) INCSTR(bufptr); if (ch != '\0') *(bufptr++) = '\0'; }#else# ifdef WIN32 /* We do not support backslash-quoting of quotes in quoted */ /* strings under Win32, because backslashes are directory */ /* separators and double quotes are illegal in filenames. */ if (*bufptr == '"') { *(argv++) = ++bufptr; while ((ch = *bufptr) != '\0' && ch != '\"') INCSTR(bufptr); if (ch != '\0') *(bufptr++) = '\0'; } else { *(argv++) = bufptr; while ((ch = *bufptr) != '\0' && !isspace((uch)ch)) INCSTR(bufptr); if (ch != '\0') *(bufptr++) = '\0'; }# else *(argv++) = bufptr; while ((ch = *bufptr) != '\0' && !isspace((uch)ch)) INCSTR(bufptr); if (ch != '\0') *(bufptr++) = '\0';# endif#endif /* ?(AMIGA || UNIX) */ while ((ch = *bufptr) != '\0' && isspace((uch)ch)) INCSTR(bufptr); } while (ch); /* now save old argc and copy in the old args */ argc += *Pargc; while (--(*Pargc)) *(argv++) = *((*Pargv)++); /* finally, add a NULL after the last arg, like UNIX */ *argv = NULL; /* save the values and return */ *Pargv = argvect; *Pargc = argc;}static int count_args(s)char *s;{ int count = 0; char ch; do { /* count and skip args */ ++count;#if defined(AMIGA) || defined(UNIX) if (*s == '\"') { for (ch = *PREINCSTR(s); ch != '\0' && ch != '\"'; ch = *PREINCSTR(s)) if (ch == '\\' && s[1] != '\0') INCSTR(s); if (*s) INCSTR(s); /* trailing quote */ } else while ((ch = *s) != '\0' && !isspace((uch)ch)) INCSTR(s);#else# ifdef WIN32 if (*s == '\"') { ++s; /* leading quote */ while ((ch = *s) != '\0' && ch != '\"') INCSTR(s); if (*s) INCSTR(s); /* trailing quote */ } else while ((ch = *s) != '\0' && !isspace((uch)ch)) INCSTR(s);# else while ((ch = *s) != '\0' && !isspace((uch)ch)) INCSTR(s);# endif#endif /* ?(AMIGA || UNIX) */ while ((ch = *s) != '\0' && isspace((uch)ch)) INCSTR(s); } while (ch); return(count);}/* Extended argument processing -- by Rich Wales * This function currently deals only with the MKS shell, but could be * extended later to understand other conventions. * * void expand_args(int *argcp, char ***argvp) * * Substitutes the extended command line argument list produced by * the MKS Korn Shell in place of the command line info from DOS. * * The MKS shell gets around DOS's 128-byte limit on the length of * a command line by passing the "real" command line in the envi- * ronment. The "real" arguments are flagged by prepending a tilde * (~) to each one. * * This "expand_args" routine creates a new argument list by scanning * the environment from the beginning, looking for strings begin- * ning with a tilde character. The new list replaces the original * "argv" (pointed to by "argvp"), and the number of arguments * in the new list replaces the original "argc" (pointed to by * "argcp"). */void expand_args(argcp, argvp) int *argcp; char ***argvp;{#ifdef DOS/* Do NEVER include (re)definiton of `environ' variable with any version of MSC or BORLAND/Turbo C. These compilers supply an incompatible definition in <stdlib.h>. */#if defined(__GO32__) || defined(__EMX__) extern char **environ; /* environment */#endif /* __GO32__ || __EMX__ */ char **envp; /* pointer into environment */ char **newargv; /* new argument list */ char **argp; /* pointer into new arg list */ int newargc; /* new argument count */ /* sanity check */ if (environ == NULL || argcp == NULL || argvp == NULL || *argvp == NULL) return; /* find out how many environment arguments there are */ for (envp = environ, newargc = 0; *envp != NULL && (*envp)[0] == '~'; envp++, newargc++) ; if (newargc == 0) return; /* no environment arguments */ /* set up new argument list */ newargv = (char **) malloc(sizeof(char **) * (newargc+1)); if (newargv == NULL) return; /* malloc failed */ for (argp = newargv, envp = environ; *envp != NULL && (*envp)[0] == '~'; *argp++ = &(*envp++)[1]) ; *argp = NULL; /* null-terminate the list */ /* substitute new argument list in place of old one */ *argcp = newargc; *argvp = newargv;#else /* !DOS */ if (argcp || argvp) return;#endif /* ?DOS */}#endif /* UTIL */#ifdef DEBUGNAMES#undef freeint Free(x)void *x;{ if (x == (void *) 0xdeadbeef) exit(-1); free(x); return 0;}int printnames(){ struct zlist far *z; for (z = zfiles; z != NULL; z = z->nxt) fprintf(stderr, "%s %s %s %p %p %p %08x %08x %08x\n", z->name, z->zname, z->iname, z->name, z->zname, z->iname, *((int *) z->name), *((int *) z->zname), *((int *) z->iname)); return 0;}#endif /* DEBUGNAMES */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -