char * mktemp (char *_template) { static int count = 0; char *cp, *dp; int i, len, xcount, loopcnt; /* Reinitialize counter if we were restarted (emacs). */ if (__bss_count != mktemp_count) { mktemp_count = __bss_count; count = 0; } len = strlen (_template); cp = _template + len; xcount = 0; while (xcount < 6 && cp > _template && cp[-1] == 'X') xcount++, cp--; if (xcount) { dp = cp; while (dp > _template && dp[-1] != '/' && dp[-1] != '\\' && dp[-1] != ':') dp--; /* Keep the first characters of the template, but turn the rest into Xs. */ while (cp > dp + 8 - xcount) { *--cp = 'X'; xcount = (xcount >= 6) ? 6 : 1 + xcount; } /* If dots occur too early -- squash them. */ while (dp < cp) { if (*dp == '.') *dp = 'a'; dp++; } /* Try to add ".tmp" to the filename. Truncate unused Xs. */ if (cp + xcount + 3 < _template + len) strcpy (cp + xcount, ".tmp"); else cp[xcount] = 0; /* This loop can run up to 2<<(5*6) times, or about 10^9 times. */ for (loopcnt = 0; loopcnt < (1 << (5 * xcount)); loopcnt++) { int c = count++; for (i = 0; i < xcount; i++, c >>= 5) cp[i] = "abcdefghijklmnopqrstuvwxyz012345"[c & 0x1f]; if (!__file_exists(_template)) return _template; } } /* Failure: truncate the template and return NULL. */ *_template = 0; return 0; }
static int glob_dirs(const char *rest, char *epathbuf, int first) /* rest is ptr to null or ptr after slash, bp after slash */ { struct ffblk ff; int done; /* printf("glob_dirs[%d]: rest=`%s' %c epathbuf=`%s' %c pathbuf=`%s'\n", wildcard_nesting, rest, *rest, epathbuf, *epathbuf, pathbuf); */ if (first) { if (*rest) { glob2(rest, epathbuf); } else { char sl = epathbuf[-1]; *epathbuf = 0; /* printf("end, checking `%s'\n", pathbuf); */ if (epathbuf == pathbuf) { epathbuf[0] = '.'; epathbuf[1] = 0; } else epathbuf[-1] = 0; if (__file_exists(pathbuf)) add(pathbuf); epathbuf[-1] = sl; } } strcpy(epathbuf, "*.*"); done = findfirst(pathbuf, &ff, FA_DIREC); while (!done) { if ((ff.ff_name[0] != '.') && (ff.ff_attrib & FA_DIREC)) { int i; char *tp; if (use_lower_case) for (i=0; ff.ff_name[i] && i<13; i++) ff.ff_name[i] = tolower(ff.ff_name[i]); /* printf("found `%s' `%s'\n", pathbuf, ff.ff_name); */ strcpy(epathbuf, ff.ff_name); tp = epathbuf + strlen(epathbuf); *tp++ = slash; *tp = 0; wildcard_nesting++; if (*rest) { glob2(rest, tp); } else { if (!(flags & GLOB_MARK)) tp[-1] = 0; add(pathbuf); tp[-1] = slash; } *tp = 0; glob_dirs(rest, tp, 0); wildcard_nesting--; } done = findnext(&ff); } return 0; }
int main (int argc, char **argv) { if (__common (&argv)) { return 1; } __OUT = __LIB_ARGS = __BSCMAKE_ARGS = __BROWSE_FILES = ""; __LIB_VERSION = __BSCMAKE_VERSION = "/nologo"; char *out_listing = 0; while (argv[1]) { const char *__ARG = argv[1]; ++argv; if (__matches (__ARG, "cru") || __matches (__ARG, "rcs") || __matches (__ARG, "rc")) { __OUT = __concat (argv[1]); __LIB_ARGS = __concat (__LIB_ARGS, " /OUT:", __OUT); out_listing = __concat (__chop_off_ext (__OUT), ".bsc.listing"); _unlink (out_listing); ++argv; continue; } if (__matches (__ARG, "--version") || __matches (__ARG, "-V")) { __LIB_VERSION = ""; continue; } if (__matches (__ARG, "--help")) { __LIB_ARGS = __concat (__LIB_ARGS, " /?"); continue; } if (__matches (__ARG, "-v") || __matches (__ARG, "--verbose")) { __LIB_ARGS = __concat (__LIB_ARGS, " /VERBOSE"); __BSCMAKE_ARGS = __concat (__BSCMAKE_ARGS, "/v"); __VERBOSE = 1; continue; } if (__ends_with (__ARG, ".o")) { __LIB_ARGS = __concat (__LIB_ARGS, " ", __ARG); char *sbr = __concat (__chop_off_last (__ARG, '.'), ".sbr"); if (out_listing && __file_exists (sbr)) { __BROWSE_FILES = __concat (__BROWSE_FILES, " ", sbr); sbr = __concat (" ", __realpath (sbr)); __append_file (out_listing, sbr); } continue; } /* Pass it through by default */ __LIB_ARGS = __concat (__LIB_ARGS, " ", __ARG); } if (__VERBOSE) { __LIB_VERSION = __BSCMAKE_VERSION = ""; } __CMD = __concat (__LIB, " ", __LIB_VERSION, " ", __LIB_ARGS); /* Run command */ int __RET = __invoke_cmd (__CMD); /* If object files were involved, also gather corresponding browse info. */ if (!__RET && !__empty_string (__BROWSE_FILES)) { __RET = __build_browse_info (); } return __RET; }
static int glob2(const char *pattern, char *epathbuf) /* both point *after* the slash */ { const char *pp, *pslash; char *bp; struct ffblk ff; char *my_pattern; int done; if (strcmp(pattern, "...") == 0) { return glob_dirs(pattern+3, epathbuf, 1); } if (strncmp(pattern, "...", 3) == 0 && (pattern[3] == '\\' || pattern[3] == '/')) { slash = pattern[3]; return glob_dirs(pattern+4, epathbuf, 1); } *epathbuf = 0; /* copy as many non-wildcard segments as possible */ pp = pattern; bp = epathbuf; pslash = bp-1; while (1) { if (*pp == ':' || *pp == '\\' || *pp == '/') { pslash = bp; if (strcmp(pp+1, "...") == 0 || (strncmp(pp+1, "...", 3) == 0 && (pp[4] == '/' || pp[4] == '\\'))) { if (*pp != ':') slash = *pp; /* printf("glob2: dots at `%s'\n", pp); */ *bp++ = *pp++; break; } } else if (*pp == '*' || *pp == '?' || *pp == '[') { if (pslash > pathbuf) strncpy(epathbuf, pattern, pslash - pathbuf); pp = pattern + (pslash - epathbuf) + 1; bp = epathbuf + (pslash - epathbuf) + 1; break; } else if (*pp == 0) { break; } else if (islower(*pp)) use_lower_case = 1; else if (isupper(*pp)) use_lower_case = 0; *bp++ = *pp++; } *bp = 0; if (*pp == 0) /* end of pattern? */ { if (wildcard_nesting==0 || __file_exists(pathbuf)) add(pathbuf); return 0; } /* printf("glob2: initial segment is `%s'\n", pathbuf); */ if (wildcard_nesting) { char s = bp[-1]; bp[-1] = 0; if (!__file_exists(pathbuf)) return 0; bp[-1] = s; } for (pslash = pp; *pslash && *pslash != '\\' && *pslash != '/'; pslash++) { if (islower(*pslash)) use_lower_case = 1; else if (isupper(*pslash)) use_lower_case = 0; } if (*pslash) slash = *pslash; my_pattern = (char *)alloca(pslash - pp + 1); if (my_pattern == 0) return 0; strncpy(my_pattern, pp, pslash - pp); my_pattern[pslash-pp] = 0; /* printf("glob2: `%s' `%s'\n", pathbuf, my_pattern); */ if (strcmp(my_pattern, "...") == 0) { glob_dirs(*pslash ? pslash+1 : pslash, bp, 1); return 0; } strcpy(bp, "*.*"); done = findfirst(pathbuf, &ff, FA_RDONLY|FA_SYSTEM|FA_DIREC|FA_ARCH); while (!done) { int i; if (ff.ff_name[0] != '.') { if (use_lower_case) for (i=0; ff.ff_name[i] && i<13; i++) ff.ff_name[i] = tolower(ff.ff_name[i]); if (fnmatch(my_pattern, ff.ff_name, FNM_NOESCAPE|FNM_PATHNAME|FNM_NOCASE) == 0) { strcpy(bp, ff.ff_name); if (*pslash) { char *tp = bp + strlen(bp); *tp++ = *pslash; *tp = 0; /* printf("nest: `%s' `%s'\n", pslash+1, pathbuf); */ wildcard_nesting++; glob2(pslash+1, tp); wildcard_nesting--; } else { /* printf("ffmatch: `%s' matching `%s', add `%s'\n", ff.ff_name, my_pattern, pathbuf); */ if (ff.ff_attrib & FA_DIREC & (flags & GLOB_MARK)) { bp[strlen(bp)] = slash; bp[strlen(bp)+1] = 0; } add(pathbuf); } } } done = findnext(&ff); } return 0; }
char * searchpath(const char *file) { static char found[PATH_MAX]; static char *path; memset(found, 0, sizeof(found)); /* Get the PATH and store it for reuse. */ if (path == 0) { char *p = getenv("PATH"); path = (char *)calloc(p ? strlen(p) + 3 : 2, sizeof(char)); if (path == (char *)0) return (char *)0; /* Prepend `.' to the PATH, so current directory is always searched. */ path[0] = '.'; if (p) { register char *s; path[1] = ';'; strcpy(path+2, p); /* Convert to more plausible form. */ for (s = path; *s; ++s) { if (*s == '\\') *s = '/'; if (isupper(*s)) *s = tolower(*s); } } else path[1] = 0; } if (strpbrk (file, "/\\:") != 0) { strcpy(found, file); return found; } else { char *test_dir = path; do { char *dp; dp = strchr(test_dir, ';'); if (dp == (char *)0) dp = test_dir + strlen(test_dir); if (dp == test_dir) strcpy(found, file); else { strncpy(found, test_dir, dp - test_dir); found[dp - test_dir] = '/'; strcpy(found + (dp - test_dir) + 1, file); } if (__file_exists(found)) return found; if (*dp == 0) break; test_dir = dp + 1; } while (*test_dir != 0); } return NULL; }