Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1593 lines
44 KiB

  1. // Exec.C - Contains routines that have do to with execing programs
  2. //
  3. // Copyright (c) 1988-1991, Microsoft Corporation. All Rights Reserved.
  4. //
  5. // Purpose:
  6. // Contains routines that spawn programs ...
  7. //
  8. // Revision History:
  9. // 04-Feb-2000 BTF Ported to Win64
  10. // 15-Nov-1993 JdR Major speed improvements
  11. // 15-Oct-1993 HV Use tchar.h instead of mbstring.h directly, change STR*() to _ftcs*()
  12. // 10-May-1993 HV Add include file mbstring.h
  13. // Change the str* functions to STR*
  14. // 06-Oct-1992 GBS Removed extern for _pgmptr
  15. // 10-Aug-1992 GBS Change file parsing in execLine to use splitpath
  16. // 19-Aug-1992 SS Remove Quotes from cd argument.
  17. // 08-Jun-1992 SS add IDE feedback support
  18. // 08-Jun-1992 SS Port to DOSX32
  19. // 16-May-1991 SB Created from routines that existed elsewhere
  20. #include "precomp.h"
  21. #pragma hdrstop
  22. #define SLASH '\\'
  23. #define PUBLIC
  24. #define QUOTE '\"'
  25. extern BOOL processInline(char *, char **, STRINGLIST **, BOOL);
  26. #ifdef _M_IX86
  27. extern UCHAR fRunningUnderChicago;
  28. #else
  29. #define fRunningUnderChicago FALSE
  30. #endif
  31. char * getComSpec(void);
  32. BOOL iterateCommand(char*, STRINGLIST*, UCHAR, UCHAR, char *, unsigned*);
  33. void removeQuotes(int, char **);
  34. void touch(char*, BOOL);
  35. //buffer for path of .cmd/.bat
  36. extern char * makeStr;
  37. extern char * shellName;
  38. char szCmdLineBuf[MAXCMDLINELENGTH];
  39. char *szNmakeProgName;
  40. // buildArgumentVector -- builds an argument vector from a command line
  41. //
  42. // Scope:
  43. // Local.
  44. //
  45. // Purpose:
  46. // It builds an argument vector for a command line. This argument vector can
  47. // be used by spawnvX routines. The algorithm is explained in the notes below.
  48. //
  49. // Input:
  50. // argc -- The number of arguments created in the argument vector
  51. // argv -- The actual argument vector created
  52. // (Ignored if NULL)
  53. // cmdline -- The command line whose vector is required
  54. //
  55. // Output:
  56. // Returns the number of arguments and the argument vector as parameters
  57. //
  58. // Errors/Warnings:
  59. // Assumes:
  60. // That the behaviour of cmd.exe i.e. parses quotes but does not disturb them.
  61. // Assumes that the SpawnVX routines will handle quotes as well as escaped
  62. // chars.
  63. //
  64. // Modifies Globals:
  65. // Uses Globals:
  66. // Notes:
  67. // Scan the cmdline from left to the end building the argument vector along
  68. // the way. Whitespace delimits arguments except for the first argument for
  69. // which the switch char '/' is also allowed. Backslash can be used to escape
  70. // a char and so ignore the character following it. Parse the quotes along
  71. // the way. If an argument begins with a double-quote then all characters till
  72. // an unescaped double-quote are part of that argument. Likewise, if an
  73. // unescaped Doublequote occurs within an argument then the above follows. If
  74. // the end of the command line comes before the closing quote then the
  75. // argument goes as far as that.
  76. void
  77. buildArgumentVector(
  78. unsigned *argc,
  79. char **argv,
  80. char *cmdline
  81. )
  82. {
  83. char *p; // current loc in cmdline
  84. char *end; // end of command line
  85. BOOL fFirstTime = TRUE; // true if 1st argument
  86. // 11-May-1993 HV _mbschr() bug: return NULL
  87. // end = _tcschr(p = cmdline, '\0');
  88. // Work around:
  89. end = p = cmdline;
  90. while (*end)
  91. end++;
  92. for (*argc = 0; p < end; ++*argc) {
  93. p += _tcsspn(p, " \t"); // skip whitespace
  94. if (p >= end)
  95. break;
  96. if (argv)
  97. *argv++ = p;
  98. if (*p == '\"') {
  99. // If the word begins with double-quote, find the next
  100. // occurrence of double-quote which is not preceded by backslash
  101. // (same escape as C runtime), or end of string, whichever is
  102. // first. From there, find the next whitespace character.
  103. for (++p; p < end; p = _tcsinc(p)) {
  104. if (*p == '\\')
  105. ++p; // skip escaped character
  106. else if (*p == '\"')
  107. break;
  108. }
  109. if (p >= end)
  110. continue;
  111. ++p;
  112. p = _tcspbrk(p, " \t");
  113. } else {
  114. // For the first word on the command line, accept the switch
  115. // character and whitespace as terminators. Otherwise, just
  116. // whitespace.
  117. p = _tcspbrk(p, " \t\"/");
  118. for (;p && p < end;p = _tcspbrk(p+1, " \t\"/")) {
  119. if (*p == '/' && !fFirstTime)
  120. continue; // after 1st word '/' is !terminator
  121. else break;
  122. }
  123. if (p && *p == '\"') {
  124. for (p++;p < end;p++) { // inside quote so skip to next one
  125. if (*p == '\"')
  126. break;
  127. }
  128. p = _tcspbrk(p, " \t"); // after quote go to first whitespace
  129. }
  130. if (fFirstTime) {
  131. fFirstTime = FALSE;
  132. // If switch char terminates the word, replace it with 0,
  133. // re-allocate the word on the heap, restore the switch and set
  134. // p just before the switch. It would be easier to shift
  135. // everything right but then we have to worry about overflow.
  136. if (p && *p == '/' && argv) {
  137. *p = '\0';
  138. argv[-1] = makeString(argv[-1]);
  139. *p-- = '/';
  140. }
  141. }
  142. }
  143. if (!p)
  144. p = end;
  145. // Now, p points to end of command line argument
  146. if (argv)
  147. *p++ = '\0';
  148. }
  149. if (argv)
  150. *argv = NULL;
  151. }
  152. PUBLIC int
  153. doCommands(
  154. char *name,
  155. STRINGLIST *s,
  156. STRINGLIST *t,
  157. UCHAR buildFlags,
  158. char *pFirstDep
  159. )
  160. {
  161. STRINGLIST *temp;
  162. int rc;
  163. temp = makeNewStrListElement();
  164. temp->text = makeString(name);
  165. rc = doCommandsEx (temp, s, t, buildFlags, pFirstDep);
  166. free_stringlist(temp);
  167. return rc;
  168. }
  169. PUBLIC int
  170. doCommandsEx(
  171. STRINGLIST *nameList,
  172. STRINGLIST *s,
  173. STRINGLIST *t,
  174. UCHAR buildFlags,
  175. char *pFirstDep
  176. )
  177. {
  178. char *u, *v;
  179. UCHAR cFlags;
  180. unsigned status = 0;
  181. int retryCount = 0;
  182. char c;
  183. char *Cmd;
  184. char *pLine;
  185. BOOL fExpanded;
  186. char *pCmd;
  187. size_t cbLine;
  188. #ifdef DEBUG_ALL
  189. if (fDebug) {
  190. printf("* doCommands:");
  191. DumpList(nameList);
  192. DumpList(s);
  193. DumpList(t);
  194. }
  195. #endif
  196. #ifdef DEBUG_ALL
  197. printf("DEBUG: doCommands 1\n");
  198. #endif
  199. ++numCommands;
  200. if (ON(gFlags, F1_QUESTION_STATUS))
  201. return(0);
  202. if (ON(gFlags, F1_TOUCH_TARGETS)) {
  203. STRINGLIST *pName;
  204. for (pName = nameList; pName; pName = pName->next) {
  205. touch(pName->text, (USHORT) ON(buildFlags, F2_NO_EXECUTE));
  206. }
  207. return(0);
  208. }
  209. #ifdef DEBUG_ALL
  210. printf("DEBUG: doCommands 2\n");
  211. #endif
  212. for (; s; s = s->next) {
  213. fExpanded = processInline(s->text, &Cmd, &t,
  214. ON(buildFlags, F2_DUMP_INLINE));
  215. cFlags = 0;
  216. errorLevel = 0;
  217. u = Cmd;
  218. for (v = u; *v; v = _tcsinc(v)) {
  219. if (*v == ESCH) ++v;
  220. else if (*v == '$') {
  221. if (*++v == '$')
  222. continue;
  223. // commented out 15-Apr-93 by JonM. This code forces recursive nmake to be
  224. // executed even if -n, but it's hosed (the -n is not passed to the recursive
  225. // nmake), and the whole thing sounds like a bad idea anyway, so I'm going to
  226. // turn it off.
  227. // if (!_tcsncmp(v, "(MAKE)", 6)) {
  228. // SET(cFlags, C_EXECUTE);
  229. // break;
  230. // }
  231. }
  232. }
  233. #ifdef DEBUG_ALL
  234. printf("DEBUG: doCommands 2.1\n");
  235. #endif
  236. for (c = *u; c == '!' ||
  237. c == '-' ||
  238. c == '@' ||
  239. c == ESCH ||
  240. WHITESPACE(c); u = _tcsinc(u), c = *u) {
  241. switch (c) {
  242. case ESCH:
  243. if (c = *++u, WHITESPACE(c))
  244. c = ' '; // keep going
  245. else
  246. c = ESCH;
  247. break;
  248. case '!':
  249. SET(cFlags, C_ITERATE);
  250. break;
  251. case '-':
  252. SET(cFlags, C_IGNORE);
  253. ++u;
  254. if (_istdigit(*u)) {
  255. char *pNumber = u;
  256. errorLevel = _tcstoul(u, &u, 10);
  257. if (errno == ERANGE) {
  258. *u = '\0';
  259. makeError(line, CONST_TOO_BIG, pNumber);
  260. }
  261. while(_istspace(*u))
  262. u++;
  263. } else
  264. errorLevel = UINT_MAX;
  265. --u;
  266. break;
  267. case '@':
  268. if (
  269. OFF(flags, F2_NO_EXECUTE)) {
  270. SET(cFlags, C_SILENT);
  271. }
  272. break;
  273. }
  274. if (c == ESCH)
  275. break; // stop parsing for cmd-line options
  276. }
  277. #ifdef DEBUG_ALL
  278. printf("DEBUG: doCommands 2.2\n");
  279. #endif
  280. if (ON(cFlags, C_ITERATE) &&
  281. iterateCommand(u, t, buildFlags, cFlags, pFirstDep, &status)
  282. ) {
  283. // The macros used by the command have to be freed & so we do so
  284. v = u;
  285. #ifdef DEBUG_ALL
  286. printf("DEBUG: doCommands 2.21\n");
  287. #endif
  288. if (_tcschr(u, '$'))
  289. u = expandMacros(u, &t);
  290. #ifdef DEBUG_ALL
  291. printf("DEBUG: doCommands 2.22\n");
  292. #endif
  293. if (v != u)
  294. FREE(u);
  295. if (OFF(buildFlags, F2_IGNORE_EXIT_CODES) &&
  296. fOptionK &&
  297. status &&
  298. status > errorLevel)
  299. {
  300. break;
  301. }
  302. continue;
  303. }
  304. v = u;
  305. #ifdef DEBUG_ALL
  306. printf("DEBUG: doCommands 2.23\n");
  307. #endif
  308. if (!fExpanded && _tcschr(u, '$'))
  309. u = expandMacros(u, &t);
  310. #ifdef DEBUG_ALL
  311. printf("DEBUG: doCommands 2.24\n");
  312. #endif
  313. cbLine = _tcslen(u) + 1;
  314. pLine = (char *) rallocate (__max(cbLine, MAXCMDLINELENGTH));
  315. _tcscpy(pLine, u);
  316. // by this time $< has already been expanded.
  317. // in order to allow processing of long commands that are due to
  318. // batch-mode rules, use a buffer that may be larger than MAXCMDLINELENGTH
  319. // Later we'll attempt to execute the long command directly, instead of
  320. // passing it to the shell.
  321. // Note: the macros expanded by ZFormat are not normally found in the
  322. // command block of a batch-mode rule, so it should be safe to use
  323. // max(cbLine, MAXCMDLINELENGTH) as a limit for ZFormat
  324. if (ZFormat (pLine, __max(cbLine, MAXCMDLINELENGTH), u, pFirstDep))
  325. makeError(0, COMMAND_TOO_LONG, u);
  326. retry:
  327. status = execLine(pLine,
  328. (BOOL)(ON(buildFlags, F2_NO_EXECUTE)
  329. || (OFF(buildFlags,F2_NO_ECHO)
  330. && OFF(cFlags,C_SILENT))),
  331. (BOOL)((OFF(buildFlags, F2_NO_EXECUTE)
  332. )
  333. || ON(cFlags, C_EXECUTE)),
  334. (BOOL)ON(cFlags, C_IGNORE), &pCmd);
  335. if (OFF(buildFlags, F2_IGNORE_EXIT_CODES)) {
  336. if (status == STATUS_PENDING) {
  337. // Hack for ntvdm problem returning correct error code.
  338. if (retryCount < 10) {
  339. retryCount++;
  340. goto retry;
  341. }
  342. }
  343. if (status && status > errorLevel) {
  344. if (!fOptionK)
  345. makeError(0, BAD_RETURN_CODE, pCmd, status);
  346. }
  347. }
  348. if (v != u)
  349. FREE(u);
  350. FREE(Cmd);
  351. FREE(pLine);
  352. if (OFF(buildFlags, F2_IGNORE_EXIT_CODES) &&
  353. fOptionK &&
  354. status &&
  355. status > errorLevel)
  356. {
  357. break;
  358. }
  359. }
  360. #ifdef DEBUG_ALL
  361. printf("DEBUG: doCommands 3\n");
  362. #endif
  363. if (OFF(buildFlags, F2_IGNORE_EXIT_CODES) && fOptionK &&
  364. (status > errorLevel))
  365. return(status);
  366. else
  367. return(0);
  368. }
  369. // expandCommandLine -- expands %name% strings in the Command Line
  370. //
  371. // Purpose:
  372. // The function expands '%name%' type strings in the Command Line. Its main
  373. // job is to assist FEmulateCommand() in emulating set for OS/2.
  374. //
  375. // Modifies: buf -- The Command Line available globally
  376. //
  377. // Output:
  378. // Returns -- the position of 'name=value' part in the Command Line.
  379. // -- Null when no '=' is found so that FEmulateCommand() can pass the
  380. // line to the shell to signal syntax error.
  381. // Note:
  382. // The shell does not give a syntax error for unmatched '%' and assumes it
  383. // as just another character in this case. This behaviour is duplicated
  384. // by expandCommandLine()
  385. char *
  386. expandCommandLine(
  387. void
  388. )
  389. {
  390. char Buf[MAXCMDLINELENGTH]; // Buffer for expanded string
  391. char *pBuf;
  392. char EnvBuf[MAXCMDLINELENGTH]; // getenv returned string copy
  393. char *posName, // position of 'name=string' in Buf or buf
  394. *p, // points into buf
  395. *pEnv; // points into Env
  396. char ExpandName[MAXNAME]; // %name% string
  397. char *pExpandName;
  398. pBuf = Buf;
  399. _tcscpy(pBuf, "set");
  400. p = szCmdLineBuf + 3; // go beyond 'set'
  401. pBuf +=3;
  402. /* Skip whitespace */
  403. for (;;p++) {
  404. if (!(WHITESPACE(*p)))
  405. break; // argc>1 � this will happen
  406. else *pBuf++ = *p;
  407. }
  408. if (!_tcschr(p, '='))
  409. return(""); // Syntax error so pass to the shell
  410. else
  411. posName = pBuf; // fixes position of Name in Buf
  412. // Now we look for environment variables and expand if required
  413. for (;*p != '=';p++)
  414. *pBuf++ = *p;
  415. for (;*p;) {
  416. if (*p == '%') {
  417. pExpandName = &ExpandName[0];
  418. while (*++p != '%' && *p)
  419. *pExpandName++ = *p;
  420. *pExpandName = '\0';
  421. if (!*p++) { // unmatched %;so don't expand
  422. *pBuf='\0'; // from the environment; like set
  423. _tcscat(Buf, ExpandName);
  424. pBuf += _tcslen(ExpandName);
  425. break; // Done precessing quit #43290
  426. } else { // matched %;so expand from the environment
  427. EnvBuf[0] = '\0';
  428. if ((pEnv = getenv(ExpandName)) != (char *)NULL) {
  429. *pBuf='\0';
  430. // If the expanded command line is too long
  431. // just say that we can't expand it!!! #43290
  432. size_t len = _tcslen(pEnv) + _tcslen(Buf);
  433. if (len > MAXCMDLINELENGTH)
  434. return NULL;
  435. _tcscat(EnvBuf, pEnv);
  436. _tcscat(Buf,EnvBuf);
  437. pBuf += _tcslen(EnvBuf);
  438. }
  439. }
  440. } else
  441. *pBuf++ = *p++;
  442. }
  443. *pBuf = '\0';
  444. _tcscpy(szCmdLineBuf, Buf);
  445. *posName = '\0';
  446. posName = szCmdLineBuf + _tcslen(Buf); // Offset into buf
  447. return(posName);
  448. }
  449. // expandEnvVars -- expands %name% strings in szArg
  450. //
  451. // Returns -- szNew: the resulting expanded string
  452. // (szNew should be FREEd by the caller)
  453. //
  454. char *
  455. expandEnvVars(
  456. char *szArg
  457. )
  458. {
  459. char *pchLeft = NULL;
  460. char *pchRight = NULL;
  461. char *pchStart = szArg;
  462. char *szNew = makeString("");
  463. while (*pchStart) {
  464. pchLeft = _tcschr(pchStart, '%');
  465. if (pchLeft) {
  466. pchRight = _tcschr(pchLeft + 1, '%');
  467. }
  468. if (pchLeft && pchRight) {
  469. char *szEnv;
  470. *pchLeft = '\0';
  471. *pchRight = '\0';
  472. szNew = reallocString(szNew, pchStart);
  473. if (szEnv = getenv(pchLeft + 1)) {
  474. szNew = reallocString(szNew, szEnv);
  475. }
  476. else {
  477. // no matching env var was found
  478. // append the %..% string literary
  479. *pchLeft = '%';
  480. szNew = reallocString(szNew, pchLeft);
  481. szNew = reallocString(szNew, "%");
  482. }
  483. *pchLeft = '%';
  484. *pchRight = '%';
  485. pchStart = pchRight + 1;
  486. pchLeft = NULL;
  487. pchRight = NULL;
  488. }
  489. else {
  490. szNew = reallocString(szNew, pchStart);
  491. pchStart += _tcslen(pchStart);
  492. }
  493. }
  494. return szNew;
  495. }
  496. // FEmulateCommand - look for certain commands and emulate them
  497. //
  498. // Emulate $(MAKE), cd, chdir, and <drive letter>:.
  499. // Also emulates 'set'.
  500. //
  501. // RETURNS: TRUE if command emulated, FALSE if not.
  502. //
  503. // Note:
  504. // In set emulation if a syntax error is discovered then it lets the
  505. // shell handle it. It does this by returning FALSE.
  506. BOOL
  507. FEmulateCommand(
  508. int argc,
  509. char **argv,
  510. int *pStatus
  511. )
  512. {
  513. char *pArg0 = argv[0];
  514. char *pArg1 = argv[1];
  515. if (_istalpha(*pArg0) && pArg0[1] == ':' && !pArg0[2]) {
  516. // If "<drive letter>:" then change drives. Ignore everything after
  517. // the drive letter, just like the shell does.
  518. _chdrive(_totupper(*pArg0) - 'A' + 1);
  519. *pStatus = 0;
  520. return(TRUE);
  521. }
  522. if (!_tcsicmp(pArg0, "set")) {
  523. char *pNameVal; // the "name=value" string
  524. // If "set" then pass it to the shell and if "set string" then put it
  525. // into the environment. Let the shell handle the syntax errors.
  526. if (argc == 1) {
  527. return(FALSE); // pass it to the shell
  528. }
  529. // expandCommandLine cannot handle lines > MAXCMDLINELENGTH
  530. // In that case szCmdLineBuf will be empty
  531. if (!szCmdLineBuf[0])
  532. return (FALSE);
  533. pNameVal = expandCommandLine();
  534. if (pNameVal == NULL)
  535. {
  536. // Expanded commad line too long
  537. return FALSE;
  538. }
  539. if (!*pNameVal) {
  540. // If there is a syntax error let the shell handle it
  541. return(FALSE);
  542. }
  543. if ((*pStatus = PutEnv(makeString(pNameVal))) == -1) {
  544. makeError(currentLine, OUT_OF_ENV_SPACE);
  545. }
  546. } else {
  547. // If "cd foo" or "chdir foo", do a chdir() else in protect mode this
  548. // would be a no-op. Ignore everything after 1st arg, just like the
  549. // shell does.
  550. char *szArg;
  551. if (!_tcsnicmp(pArg0, "cd", 2)) {
  552. pArg0 += 2;
  553. } else if (!_tcsnicmp(pArg0, "chdir", 5)) {
  554. pArg0 += 5;
  555. } else {
  556. return(FALSE);
  557. }
  558. // At this point, a prefix of argv[0] matches cd or chdir and pArg0
  559. // points to the next char. Check for a path separator in argv[0]
  560. // (e.g., cd..\foo) or else use the next arg if present.
  561. // if there are more than two arguments then let the shell handle it
  562. if (argc > 2) {
  563. return(FALSE);
  564. }
  565. // Remove quotes, if any from the argument
  566. removeQuotes(argc, argv);
  567. if (!*pArg0 && pArg1) {
  568. // Under certain circumstances the C RunTime does not help us
  569. // e.g. 'd:', in this case let the shell do it ...
  570. if (isalpha(*pArg1) && pArg1[1] == ':' && !pArg1[2]) {
  571. return(FALSE);
  572. }
  573. szArg = expandEnvVars(pArg1); // [VS98 2251]
  574. *pStatus = _chdir(szArg);
  575. FREE (szArg);
  576. } else if (*pArg0 == '.' || PATH_SEPARATOR(*pArg0)) {
  577. szArg = expandEnvVars(pArg0); // [VS98 2251]
  578. *pStatus = _chdir(szArg);
  579. FREE (szArg);
  580. } else {
  581. // Unrecognized syntax--we can't emulate.
  582. return(FALSE);
  583. }
  584. }
  585. // If error, simulate a return code of 1.
  586. if (*pStatus != 0) {
  587. *pStatus = 1;
  588. }
  589. return(TRUE);
  590. }
  591. #ifdef WIN95
  592. int __cdecl
  593. cmpSzPsz(
  594. const void *sz,
  595. const void *psz
  596. )
  597. {
  598. const char *sz1 = (char *) sz;
  599. const char *sz2 = *(char **) psz;
  600. return(_tcsicmp(sz1, sz2));
  601. }
  602. BOOL
  603. FInternalCommand(
  604. const char *szName
  605. )
  606. {
  607. const char * const *pszInternal;
  608. static const char * const rgszInternal[] =
  609. {
  610. "BREAK",
  611. "CALL",
  612. "CD",
  613. "CHDIR",
  614. "CLS",
  615. "COPY",
  616. "CTTY",
  617. "DATE",
  618. "DEL",
  619. "DIR",
  620. "DIR.",
  621. "ECHO",
  622. "ECHO.",
  623. "ERASE",
  624. "EXIT",
  625. "FOR",
  626. "GOTO",
  627. "IF",
  628. "MD",
  629. "MKDIR",
  630. "PATH",
  631. "PAUSE",
  632. "PROMPT",
  633. "RD",
  634. "REM",
  635. "REN",
  636. "RENAME",
  637. "RMDIR",
  638. "SET",
  639. "SHIFT",
  640. "TIME",
  641. "TYPE",
  642. "VER",
  643. "VERIFY",
  644. "VOL"
  645. };
  646. pszInternal = (const char * const *) bsearch(szName,
  647. rgszInternal,
  648. sizeof(rgszInternal) / sizeof(rgszInternal[0]),
  649. sizeof(rgszInternal[0]),
  650. &cmpSzPsz);
  651. return(pszInternal != NULL);
  652. }
  653. #endif // WIN95
  654. // redirect -- handles redirection of input or output.
  655. //
  656. // arguments: dir - READ => input,
  657. // WRITE => output,
  658. // APPEND => append to end of the file.
  659. //
  660. // p - pointer to buffer that has the filename as
  661. // well as the rest of the command string.
  662. //
  663. // return value FALSE => error (freopen fails)
  664. // TRUE => normal return.
  665. //
  666. // the freopen() call sets up the redirection. the rest of the
  667. // command string is then copied forward.
  668. BOOL
  669. redirect(
  670. char *name,
  671. unsigned which
  672. )
  673. {
  674. char *p;
  675. char c = '\0';
  676. BOOL fStatus;
  677. char *mode;
  678. FILE *stream;
  679. FILE *newFile;
  680. while (WHITESPACE(*name)) {
  681. name++;
  682. }
  683. if (p = _tcspbrk(name, " \t<>\r")) {
  684. c = *p;
  685. *p = '\0';
  686. }
  687. if (which == READ) {
  688. mode = "r";
  689. stream = stdin;
  690. } else {
  691. stream = stdout;
  692. if (which == WRITE) {
  693. mode = "w";
  694. } else {
  695. mode = "a";
  696. }
  697. }
  698. newFile = freopen(name, mode, stream);
  699. fStatus = (newFile != NULL);
  700. if (fStatus && (which == APPEND)) {
  701. _lseek(_fileno(newFile), 0L, SEEK_END);
  702. }
  703. while (*name) {
  704. *name++ = ' ';
  705. }
  706. if (p) {
  707. *p = c;
  708. }
  709. return(fStatus);
  710. }
  711. BOOL
  712. FDoRedirection(
  713. char *p,
  714. int *oldIn,
  715. int *oldOut
  716. )
  717. {
  718. BOOL in = FALSE;
  719. BOOL out = FALSE;
  720. BOOL fReturn = FALSE;
  721. char *q;
  722. unsigned which;
  723. char *save = NULL;
  724. while (q = _tcspbrk(p, "<>|")) {
  725. switch (*q) {
  726. case '<':
  727. if (in) {
  728. fReturn = TRUE;
  729. break;
  730. }
  731. if (!save) {
  732. save = makeString(p);
  733. }
  734. *q++ = ' ';
  735. p = q;
  736. in = TRUE;
  737. *oldIn = _dup(_fileno(stdin));
  738. if ((*oldIn == -1) || !redirect(q, READ)) {
  739. fReturn = TRUE;
  740. break;
  741. }
  742. break;
  743. case '>':
  744. if (out) {
  745. fReturn = TRUE;
  746. break;
  747. }
  748. if (!save) {
  749. save = makeString(p);
  750. }
  751. *q++ = ' ';
  752. p = q;
  753. out = TRUE;
  754. if (*q == '>') {
  755. *q++ = ' ';
  756. which = APPEND;
  757. } else {
  758. which = WRITE;
  759. }
  760. *oldOut = _dup(_fileno(stdout));
  761. if ((*oldOut == -1) || !redirect(q, which)) {
  762. fReturn = TRUE;
  763. break;
  764. }
  765. break;
  766. case '|':
  767. fReturn = TRUE;
  768. break;
  769. default :
  770. makeError(0, BUILD_INTERNAL);
  771. }
  772. if (fReturn) {
  773. break;
  774. }
  775. }
  776. if (fReturn) {
  777. if (save != NULL) {
  778. _tcscpy(p, save);
  779. FREE(save);
  780. }
  781. if (in && (*oldIn != -1)) {
  782. if (_dup2(*oldIn, _fileno(stdin)) == -1) {
  783. makeError(0, BUILD_INTERNAL);
  784. }
  785. _close(*oldIn);
  786. *oldIn = -1;
  787. }
  788. if (out && (*oldOut != -1)) {
  789. if (_dup2(*oldOut, _fileno(stdout)) == -1) {
  790. makeError(0, BUILD_INTERNAL);
  791. }
  792. _close(*oldOut);
  793. *oldOut = -1;
  794. }
  795. }
  796. return(fReturn);
  797. }
  798. BOOL
  799. FSearchForExecutableExt(
  800. const char *szFilename,
  801. const char *szExt,
  802. BOOL fHasPath,
  803. char *szPath
  804. )
  805. {
  806. char szFullName[_MAX_PATH];
  807. _tcscpy(szFullName, szFilename);
  808. _tcscat(szFullName, szExt);
  809. if (fHasPath) {
  810. if (_access(szFullName, 0) == 0) {
  811. _tcscpy(szPath, szFullName);
  812. return(TRUE);
  813. }
  814. return(FALSE);
  815. }
  816. _searchenv(szFullName, "PATH", szPath);
  817. return(szPath[0] != '\0');
  818. }
  819. BOOL
  820. FSearchForExecutable(char *szFullName, char *szPath, BOOL *fBat)
  821. {
  822. char szDrive[_MAX_DRIVE];
  823. char szDir[_MAX_DIR];
  824. char szFileName[_MAX_FNAME];
  825. char szNoExt[_MAX_PATH];
  826. BOOL fHasPath;
  827. char *szEndQuote;
  828. BOOL fHasQuotes = FALSE;
  829. // Ignore any given extension. This is what COMMAND.COM does,
  830. char *szToPass = szFullName;
  831. if (*szFullName == QUOTE) {
  832. // get rid of any number of quotes at the beginning and at the end of the
  833. // string. This allows handling names enclosed in multiple quotes that are
  834. // accepted by the shell (DS 14300)
  835. szEndQuote = _tcsdec(szFullName, szFullName + _tcslen(szFullName));
  836. if (QUOTE == *szEndQuote) {
  837. fHasQuotes = TRUE;
  838. while (QUOTE == *szToPass)
  839. szToPass ++;
  840. while (szEndQuote > szToPass) {
  841. char *szPrev = _tcsdec (szToPass, szEndQuote);
  842. if (QUOTE != *szPrev)
  843. break;
  844. szEndQuote = szPrev;
  845. }
  846. *szEndQuote = '\0';
  847. }
  848. }
  849. _splitpath(szToPass, szDrive, szDir, szFileName, NULL);
  850. _makepath(szNoExt, szDrive, szDir, szFileName, NULL);
  851. fHasPath = (szDrive[0] != '\0') || (szDir[0] != '\0');
  852. *fBat = FALSE;
  853. // Search for .COM file
  854. if (FSearchForExecutableExt(szNoExt, ".com", fHasPath, szPath)) {
  855. goto success;
  856. }
  857. // Search for .EXE file
  858. if (FSearchForExecutableExt(szNoExt, ".exe", fHasPath, szPath)) {
  859. goto success;
  860. }
  861. // Search for .BAT file
  862. if (FSearchForExecutableExt(szNoExt, ".bat", fHasPath, szPath)) {
  863. *fBat = TRUE;
  864. goto success;
  865. }
  866. return(FALSE);
  867. success:
  868. if (fHasQuotes) {
  869. size_t size = _tcslen(szPath);
  870. memmove(szPath+1, szPath, size);
  871. *szPath = '"';
  872. *(szPath + size + 1) = '"';
  873. *(szPath + size + 2) = '\0';
  874. *szEndQuote = '"';
  875. }
  876. return TRUE;
  877. }
  878. // execLine -- execute a command line
  879. //
  880. // Scope: Global (build.c, rpn.c)
  881. //
  882. // Purpose:
  883. // Parses the command line for redirection characters and redirects stdin and
  884. // stdout if "<", ">", or ">>" are seen. If any of the following occur,
  885. // restore the original stdin and stdout, pass the command to the shell, and
  886. // invoke the shell:
  887. // - the command line contains "|" (pipe)
  888. // - a syntax error occurs in parsing the command line
  889. // - an error occurs in redirection
  890. // Otherwise, attempt to invoke the command directly, then restore the
  891. // original stdin and stdout. If this invocation failed because of
  892. // file-not-found then pass the command to the shell and invoke the shell.
  893. //
  894. // Input: line -- The command line to be executed
  895. // echoCmd -- determines if the command line is to be echoed
  896. // doCmd -- determines if the command is to be actually executed
  897. // ignoreReturn -- determines if NMAKE is to ignore the return code on
  898. // execution
  899. // ppCmd -- if non-null then on error returns command executed
  900. //
  901. // Output: Returns ... return code from child process
  902. // ... -1 if error occurs
  903. //
  904. // Notes:
  905. // 1/ Quoted strings can have redir chars "<>" which will be skipped over.
  906. // 2/ Unmatched quotes cause error; redir chars are replaced by space char.
  907. // 3/ Dup stdin file handle then redirect it. If we have to use the shell,
  908. // restore the original command line.
  909. // 4/ Emulate certain commands such as "cd" to help prevent some makefiles
  910. // from breaking when ported from DOS to OS/2.
  911. //
  912. // Algorithm for spawning commands:
  913. // If we can't handle the syntax, let the shell do everything. Otherwise,
  914. // first check to see if the command (without extension) is a DOS built-in &
  915. // if it is, call the shell to execute it (this is how cmd.exe behaves)
  916. // If it's not a built-in, we check to see if it has a .cmd or a .bat
  917. // extension (depending on whether we're in DOS or OS/2). If it does, we
  918. // call system() to execute it.
  919. // If it has some other extension, we ignore the extension and go looking for
  920. // a .cmd or .bat file. If we find it, we execute it with system().
  921. // Otherwise, we try to spawn it (without extension). If the spawn fails,
  922. // we issue an unknown program error.
  923. int
  924. execLine(
  925. char *line,
  926. BOOL echoCmd,
  927. BOOL doCmd,
  928. BOOL ignoreReturn,
  929. char **ppCmd
  930. )
  931. {
  932. char **argv;
  933. BOOL fUseShell;
  934. BOOL fLongCommand;
  935. int status;
  936. unsigned argc;
  937. if (!shellName) {
  938. shellName = getComSpec();
  939. }
  940. switch (*line) {
  941. case '@':
  942. // Turn off echo if it was on. This handles the case where the "@"
  943. // was in a macro.
  944. //
  945. line++;
  946. if (doCmd)
  947. echoCmd = 0;
  948. break;
  949. case '-':
  950. ignoreReturn = TRUE;
  951. ++line;
  952. if (_istdigit(*line)) {
  953. char * pNumber = line;
  954. errorLevel = _tcstoul(line, &line, 10);
  955. if (errno == ERANGE) {
  956. *line = '\0';
  957. makeError(0, CONST_TOO_BIG, pNumber); // Todo: replace 0 with line number
  958. }
  959. while(_istspace(*line))
  960. line++;
  961. } else
  962. errorLevel = UINT_MAX;
  963. break;
  964. }
  965. // handle null command ...
  966. if (!line[0])
  967. return(0);
  968. #if 0
  969. // 10/10/96: disabled to allow execution of long
  970. // commands that are produced by batch-mode rules
  971. // copy command line into buffer
  972. if (_tcslen(line) < MAXCMDLINELENGTH)
  973. _tcscpy(szCmdLineBuf, line);
  974. else
  975. makeError(0, COMMAND_TOO_LONG, line);
  976. #endif
  977. fLongCommand = _tcslen(line) >= MAXCMDLINELENGTH;
  978. if (!fLongCommand)
  979. _tcscpy(szCmdLineBuf, line);
  980. else
  981. *szCmdLineBuf = '\0';
  982. // Allocate a copy of the command line on the heap because in a
  983. // recursive call to doMake(), argv pointers will be allocated from
  984. // the static buffer which will then be trashed. For buildArg...().
  985. pCmdLineCopy = makeString(line);
  986. // If -n then echo command if not '$(MAKE)'
  987. if (echoCmd) {
  988. printf("\t%s\n", pCmdLineCopy);
  989. fflush(stdout);
  990. }
  991. // Build arg vector. This is a waste on Windows NT since we're probably
  992. // going to use the shell, except we have to check for cd, $(MAKE),
  993. // etc. so we take advantage of the parsing code.
  994. buildArgumentVector(&argc, NULL, pCmdLineCopy);
  995. if (argc == 0) {
  996. return(0); // for case when macro command is null
  997. }
  998. // allocate argv. Leave space for extra arguments
  999. // (like "cmd", "/k", quotes) that may be added later
  1000. argv = (char **) rallocate((argc + 5) * sizeof (char *));
  1001. buildArgumentVector(&argc, argv, pCmdLineCopy);
  1002. // 11-May-1993 HV The _mbsicmp() does not like NULL pointer
  1003. // so I have to check before calling it.
  1004. if (argv[0] && makeStr && !_tcsicmp(argv[0], makeStr)) {
  1005. if(!szNmakeProgName) {
  1006. szNmakeProgName = _pgmptr;
  1007. if( _tcspbrk( szNmakeProgName," " )) { // If the program name has an embedded space in it
  1008. // Let's put quotes around it
  1009. szNmakeProgName = (char *)rallocate(_tcslen(szNmakeProgName)+3);
  1010. *szNmakeProgName = QUOTE; // First quote
  1011. *(szNmakeProgName+1) = '\0';
  1012. _tcscat( szNmakeProgName, _pgmptr ); // copy the full program name (self)
  1013. _tcscat( szNmakeProgName, "\""); // Final quote and \0
  1014. }
  1015. }
  1016. argv[0]=szNmakeProgName;
  1017. }
  1018. if (!doCmd) { // don't execute command if doCmd false
  1019. // For -n, emulate if possible.
  1020. if (FEmulateCommand(argc, argv, &status)) {
  1021. if (status && ppCmd) {
  1022. *ppCmd = makeString(*argv);
  1023. }
  1024. return(status); // return status
  1025. }
  1026. return(0);
  1027. }
  1028. // Try emulating the command if appropriate. If not, and we should not
  1029. // use the shell, try spawning command directly.
  1030. // Check status when emulating
  1031. if (FEmulateCommand(argc, argv, &status)) {
  1032. // Command has been emulated. Don't execute it again.
  1033. fUseShell = FALSE;
  1034. } else if (!fRunningUnderChicago && !fLongCommand) {
  1035. // Use the shell for Windows NT unless the command is too long
  1036. fUseShell = TRUE;
  1037. #ifdef WIN95
  1038. } else if (fRunningUnderChicago && FInternalCommand(argv[0])) {
  1039. // Under Windows 95 or MS-DOS, use the shell for internal commands
  1040. fUseShell = TRUE;
  1041. #endif // WIN95
  1042. } else {
  1043. int oldIn = -1; // Old stdin file handle
  1044. int oldOut = -1; // Old stdout file handle
  1045. // Under Windows 95 or MS-DOS, COMMAND.COM doesn't return child return
  1046. // codes. Try spawning the child application directly.
  1047. // This code is also now used if the line is too long to be handled by
  1048. // the NT command interpreter.
  1049. fUseShell = FDoRedirection(line, &oldIn, &oldOut);
  1050. if (!fUseShell) {
  1051. char szPath[_MAX_PATH];
  1052. char szQuotedPath[_MAX_PATH];
  1053. BOOL fBat;
  1054. if (oldIn != -1 || oldOut != -1) { // If there was a redirection
  1055. // Need to re-build the argument vector without the
  1056. // redirection characters
  1057. FREE(pCmdLineCopy);
  1058. pCmdLineCopy = makeString(line);
  1059. buildArgumentVector(&argc, argv, pCmdLineCopy);
  1060. }
  1061. if (!FSearchForExecutable(argv[0], szPath, &fBat)) {
  1062. /* If not found, set up an error since COMMAND will
  1063. * return 0. This risks future incompatibility if new
  1064. * COMMAND.COM internal commands are added.
  1065. */
  1066. if (fRunningUnderChicago) {
  1067. errno = ENOENT;
  1068. status = -1;
  1069. } else {
  1070. fUseShell = TRUE;
  1071. }
  1072. } else if (fBat) {
  1073. // If .bat extension, use COMMAND.COM.
  1074. // UNDONE: CreateProcess is supposed to handle this. Try it.
  1075. fUseShell = TRUE;
  1076. } else {
  1077. // Spawn command directly.
  1078. // DevStudio#8911, cannot use quotes in szPath
  1079. if (*szPath == QUOTE && *(szPath + _tcslen(szPath) - 1) == QUOTE) {
  1080. // unquote the path.
  1081. size_t cb = _tcslen(szPath);
  1082. memmove(szPath, szPath + 1, cb);
  1083. *(szPath + cb - 2) = '\0';
  1084. }
  1085. #if 0
  1086. {
  1087. int i;
  1088. printf("Spawning \"%s\" directly\n", szPath);
  1089. for (i = 0; i < argc; i++) {
  1090. printf ( "Arg[%d] = \"%s\"\n", i, argv[i] );
  1091. }
  1092. }
  1093. #endif
  1094. // DS 14300: Use full path for argv[0]
  1095. // otherwise a shell command may be invoked
  1096. // instead of an intended executable with the
  1097. // same name. Enclosing quotes are needed if
  1098. // string has embedded spaces
  1099. argv[0] = szPath;
  1100. if (_tcschr (argv[0], ' ')) {
  1101. *szQuotedPath = QUOTE;
  1102. _tcscpy (szQuotedPath+1, szPath);
  1103. _tcscat (szQuotedPath, "\"");
  1104. argv[0] = szQuotedPath;
  1105. }
  1106. status = (int)_spawnvp(P_WAIT, szPath, argv); // REVIEW:WIN64 cast
  1107. }
  1108. }
  1109. if (oldIn != -1) {
  1110. if (_dup2(oldIn, _fileno(stdin)) == -1) {
  1111. makeError(0, BUILD_INTERNAL);
  1112. }
  1113. _close(oldIn);
  1114. }
  1115. if (oldOut != -1) {
  1116. if (_dup2(oldOut, _fileno(stdout)) == -1) {
  1117. makeError(0, BUILD_INTERNAL);
  1118. }
  1119. _close(oldOut);
  1120. }
  1121. }
  1122. if (fUseShell) {
  1123. int i;
  1124. BOOL fExtraQuote = FALSE;
  1125. // copy command line into buffer
  1126. if (_tcslen(line) < MAXCMDLINELENGTH)
  1127. _tcscpy(szCmdLineBuf, line);
  1128. else
  1129. makeError(0, COMMAND_TOO_LONG, line);
  1130. // Workaround for cmd bug (DevStudio #11253):
  1131. // IF argv[0] (before we rearrange with cmd.exe /c) is quoted AND
  1132. // any of the other argv[1...n] args have quotes AND
  1133. // running on NT
  1134. // THEN we add an extra quote before argv[0] and one after argv[n].
  1135. if ((*argv[0] == QUOTE) &&
  1136. (*(argv[0] + _tcslen(argv[0]) - 1) == QUOTE) &&
  1137. !fRunningUnderChicago) {
  1138. for (i = argc - 1; i >= 1; i--) {
  1139. if( _tcspbrk( argv[i],"\"" )) {
  1140. fExtraQuote = TRUE;
  1141. break;
  1142. }
  1143. }
  1144. }
  1145. if (fExtraQuote) {
  1146. argv[argc++] = "\"";
  1147. argv[argc] = NULL;
  1148. }
  1149. for (i = argc; i >= 0; i--) {
  1150. argv[i+2] = argv[i];
  1151. }
  1152. argv[0] = shellName;
  1153. argv[1] = fExtraQuote ? "/c \"" : "/c";
  1154. #if 0
  1155. printf("Shelling \"%s\"\n", szCmdLineBuf);
  1156. for (i = 0; i < argc + 2; i++) {
  1157. printf ( "Arg[%d] = \"%s\"\n", i, argv[i] );
  1158. }
  1159. #endif
  1160. status = (int)_spawnvp(P_WAIT, argv[0], (const char * const *) argv); // REVIEW:WIN64 cast
  1161. }
  1162. // Check for errors spawning command (distinct from errors *returned*
  1163. // from a successfully spawned command).
  1164. if (status == -1) {
  1165. if (ignoreReturn) {
  1166. status = 0;
  1167. } else {
  1168. switch (errno) {
  1169. case 0:
  1170. // We (ie: nmake) didn't fail, but the spawned program did.
  1171. break;
  1172. case ENOENT:
  1173. makeError(0, CANT_FIND_PROGRAM, argv[0]);
  1174. break;
  1175. case ENOMEM:
  1176. makeError(0, EXEC_NO_MEM, fUseShell ? argv[2] : argv[0]);
  1177. break;
  1178. default:
  1179. // Done to flag possibly erroneous decision made here [SB]
  1180. makeError(0, SPAWN_FAILED_ERROR, _strerror(NULL));
  1181. }
  1182. }
  1183. }
  1184. if (status && ppCmd) {
  1185. *ppCmd = makeString(fUseShell ? argv[2] : argv[0]);
  1186. }
  1187. FREE(argv);
  1188. FREE(pCmdLineCopy);
  1189. return(status);
  1190. }
  1191. // getComSpec()
  1192. //
  1193. // actions: Attempts to find system shell.
  1194. //
  1195. // First look for COMSPEC. If not found, look for COMMAND.COM or CMD.EXE
  1196. // in the current directory then the path. If not found, fatal error.
  1197. // It would make sense to give an error if COMSPEC is not defined but
  1198. // test suites are easier if no user-defined environment variables are
  1199. // required.
  1200. char *
  1201. getComSpec()
  1202. {
  1203. char *szShell;
  1204. char szPath[_MAX_PATH];
  1205. if ((szShell = getenv("COMSPEC")) != NULL) {
  1206. return(szShell);
  1207. }
  1208. if (fRunningUnderChicago) {
  1209. szShell = "COMMAND.COM";
  1210. } else {
  1211. szShell = "CMD.EXE";
  1212. }
  1213. _searchenv(szShell, "PATH", szPath);
  1214. if (szPath[0] == '\0') {
  1215. makeError(0, NO_COMMAND_COM);
  1216. }
  1217. return(makeString(szPath));
  1218. }
  1219. BOOL
  1220. iterateCommand(
  1221. char *u,
  1222. STRINGLIST *t,
  1223. UCHAR buildFlags,
  1224. UCHAR cFlags,
  1225. char *pFirstDep,
  1226. unsigned *status
  1227. )
  1228. {
  1229. BOOL parens;
  1230. char c = '\0';
  1231. char *v;
  1232. STRINGLIST *p = NULL,
  1233. *q;
  1234. char *pLine;
  1235. char *pCmd;
  1236. for (v = u; *v ; ++v) {
  1237. parens = FALSE;
  1238. if (*v == '$') {
  1239. if (*(v+1) == '(') {
  1240. ++v;
  1241. parens = TRUE;
  1242. }
  1243. if (*(v+1) == '?') {
  1244. if (parens
  1245. && !(_tcschr("DFBR", *(v+2)) && *(v+3) == ')')
  1246. && *(v+2) != ')')
  1247. continue;
  1248. p = dollarQuestion;
  1249. c = '?';
  1250. break;
  1251. }
  1252. if (*++v == '*' && *(v+1) == '*') {
  1253. if (parens
  1254. && !(_tcschr("DFBR", *(v+2)) && *(v+3) == ')')
  1255. && *(v+2) != ')')
  1256. continue;
  1257. p = dollarStarStar;
  1258. c = '*';
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. if (!*v) {
  1264. return(FALSE);
  1265. }
  1266. v = u;
  1267. q = p;
  1268. while (p) {
  1269. macros = t;
  1270. if (c == '*') {
  1271. p = dollarStarStar->next;
  1272. dollarStarStar->next = NULL;
  1273. } else {
  1274. p = dollarQuestion->next;
  1275. dollarQuestion->next = NULL;
  1276. }
  1277. u = expandMacros(v, &macros);
  1278. expandExtmake(CmdLine, u, pFirstDep);
  1279. pLine = CmdLine;
  1280. *status = execLine(pLine,
  1281. (BOOL)(ON(buildFlags, F2_NO_EXECUTE)
  1282. || (OFF(buildFlags,F2_NO_ECHO)
  1283. && OFF(cFlags,C_SILENT))),
  1284. (BOOL)((OFF(buildFlags, F2_NO_EXECUTE)
  1285. )
  1286. || ON(cFlags, C_EXECUTE)),
  1287. (BOOL)ON(cFlags, C_IGNORE), &pCmd);
  1288. if (OFF(buildFlags, F2_IGNORE_EXIT_CODES)) {
  1289. if (*status && *status > errorLevel)
  1290. if (!fOptionK)
  1291. makeError(0, BAD_RETURN_CODE, pCmd, *status);
  1292. }
  1293. if (c == '*')
  1294. dollarStarStar = dollarStarStar->next = p;
  1295. else
  1296. dollarQuestion = dollarQuestion->next = p;
  1297. FREE(u);
  1298. if (OFF(buildFlags, F2_IGNORE_EXIT_CODES) &&
  1299. fOptionK &&
  1300. *status &&
  1301. *status > errorLevel)
  1302. {
  1303. break;
  1304. }
  1305. }
  1306. if (c == '*')
  1307. dollarStarStar = q;
  1308. else
  1309. dollarQuestion = q;
  1310. return(TRUE);
  1311. }
  1312. void
  1313. removeQuotes(
  1314. int argc,
  1315. char **argv
  1316. )
  1317. {
  1318. char *t,
  1319. *string;
  1320. for (; argc--; argv++) {
  1321. string = *argv;
  1322. for (t = string; *t;) {
  1323. if (*t == SLASH || *t == ESCH) {
  1324. if (t[1] == QUOTE)
  1325. *(string)++ = *(t++);
  1326. *(string++) = *(t++);
  1327. continue;
  1328. }
  1329. if (*t == QUOTE)
  1330. ++t;
  1331. else {
  1332. if (_istlead(* (unsigned char *)t))
  1333. *(string++) = *(t++);
  1334. *(string++) = *(t++);
  1335. }
  1336. }
  1337. *string = '\0';
  1338. }
  1339. }
  1340. void
  1341. touch(
  1342. char *s,
  1343. BOOL minusN
  1344. )
  1345. {
  1346. int fd;
  1347. char c;
  1348. FILE * file;
  1349. makeMessage(TOUCHING_TARGET, s);
  1350. if (!minusN &&
  1351. ((file = FILEOPEN(s, "r+b")) != NULL)) {
  1352. fd = _fileno(file);
  1353. if (_read(fd, &c, 1) > 0) {
  1354. _lseek(fd, 0L, SEEK_SET);
  1355. _write(fd, &c, 1);
  1356. }
  1357. _close(fd);
  1358. }
  1359. }