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.

231 lines
7.2 KiB

  1. // INIT.C -- routines to handle TOOLS.INI
  2. //
  3. // Copyright (c) 1988-1990, Microsoft Corporation. All rights reserved.
  4. //
  5. // Purpose:
  6. // Module contains routines to deal with TOOLS.INI file. Functions in TOOLS.LIB
  7. // have not been used because NMAKE needs to be small and the overhead is too
  8. // much.
  9. //
  10. // Revision History:
  11. // 04-Feb-2000 BTF Ported to Win64
  12. // 15-Oct-1993 HV Use tchar.h instead of mbstring.h directly, change STR*() to _ftcs*()
  13. // 10-May-1993 HV Add include file mbstring.h
  14. // Change the str* functions to STR*
  15. // 10-May-1993 HV Revise SearchFileInEnv to take care of cases when path
  16. // characters (\,/,:) are used. This fixed the recursive
  17. // problem.
  18. // 22-Apr-1993 HV Rewrite SearchRunPath() to use _makepath(), _searchenv()
  19. // Add SearchFileInEnv() helper for SearchRunPath()
  20. // 08-Jun-1992 SS Port to DOSX32
  21. // 02-Feb-1990 SB Replace fopen() by FILEOPEN
  22. // 22-Nov-1989 SB Changed free() to FREE()
  23. // 19-Oct-1989 SB searchHandle passed around as extra param
  24. // 16-Aug-1989 SB error check for fclose() added
  25. // 24-Apr-1989 SB made FILEINFO as void * for OS/2 1.2 support
  26. // 05-Apr-1989 SB made all funcs NEAR; Reqd to make all function calls NEAR
  27. // 20-Sep-1988 RB Add SearchRunPath().
  28. // Remove TOOLS.INI warning.
  29. // 17-Aug-1988 RB Clean up.
  30. // 10-May-1988 rb Find tools.ini in current directory first.
  31. // 27-May-1988 rb Remove NO_INIT_ENTRY warning because of built-ins.
  32. #include "precomp.h"
  33. #pragma hdrstop
  34. // findTag()
  35. //
  36. // arguments: tag pointer to tag name to be searched for
  37. //
  38. // actions: reads tokens from file
  39. // whenever it sees a newline, checks the next token
  40. // to see if 1st char is opening paren
  41. // if no, reads and discards rest of line and
  42. // checks next token to see if it's newline or EOF
  43. // and if newline loops to check next token . . .
  44. // if yes ('[' found), looks on line for tag
  45. // if tag found, looks for closing paren
  46. // if ']' found, discards rest of line and returns
  47. // else keeps looking until end of file or error
  48. //
  49. // returns: if successful, returns TRUE
  50. // if tag never found, returns FALSE
  51. BOOL
  52. findTag(
  53. char *tag
  54. )
  55. {
  56. BOOL endTag; // TRUE when find [...]
  57. size_t n;
  58. char *s;
  59. for (line = 0; fgets(buf, MAXBUF, file); ++line) {
  60. if (*buf == '[') {
  61. endTag = FALSE;
  62. for (s = _tcstok(buf+1," \t\n");
  63. s && !endTag;
  64. s = _tcstok(NULL," \t\n")
  65. ) {
  66. n = _tcslen(s) - 1;
  67. if (s[n] == ']') {
  68. endTag = TRUE;
  69. s[n] = '\0';
  70. }
  71. if (!_tcsicmp(s,tag)) {
  72. return(TRUE);
  73. }
  74. }
  75. }
  76. }
  77. if (!feof(file)) {
  78. currentLine = line;
  79. makeError(0, CANT_READ_FILE);
  80. }
  81. return(FALSE);
  82. }
  83. // tagOpen()
  84. //
  85. // arguments: where pointer to name of environment variable
  86. // containing path to search
  87. // name pointer to name of initialization file
  88. // tag pointer to name of tag to find in file
  89. //
  90. // actions: looks for file in current directory
  91. // if not found, looks in each dir in path (semicolons
  92. // separate each path from the next in the string)
  93. // if file is found and opened, looks for the given tag
  94. //
  95. // (if ported to xenix, tagOpen() and searchPath()
  96. // should probably use access() and not findFirst().)
  97. //
  98. // returns: if file and tag are found, returns pointer to file,
  99. // opened for reading and positioned at the line
  100. // following the tag line
  101. // else returns NULL
  102. BOOL
  103. tagOpen(
  104. char *where,
  105. char *name,
  106. char *tag
  107. )
  108. {
  109. char szPath[_MAX_PATH];
  110. // Look for 'name' in current directory then path.
  111. _searchenv(name, where, szPath);
  112. if (szPath[0] == '\0') {
  113. return(FALSE);
  114. }
  115. if (!(file = FILEOPEN(szPath, "rt"))) {
  116. makeError(0, CANT_READ_FILE, szPath);
  117. }
  118. if (findTag(tag)) {
  119. return(TRUE); // look for tag in file
  120. }
  121. if (fclose(file) == EOF) { // if tag not found, close
  122. makeError(0, ERROR_CLOSING_FILE, szPath);
  123. }
  124. return(FALSE); // file and pretend file not found
  125. }
  126. // searchPath()
  127. //
  128. // arguments: p pointer to string of paths to be searched
  129. // name name of file being searched for
  130. //
  131. // actions: looks for name in current directory, then each
  132. // directory listed in string.
  133. //
  134. // returns: pointer to path spec of file found, else NULL
  135. //
  136. // I don't use _tcstok() here because that modifies the string that it "token-
  137. // izes" and we cannot modify the environment-variable string. I'd have to
  138. // make a local copy of the whole string, and then make another copy of each
  139. // directory to which I concatenate the filename to in order to test for the
  140. // file's existence.
  141. char *
  142. searchPath(
  143. char *p,
  144. char *name,
  145. void *findBuf,
  146. NMHANDLE *searchHandle
  147. )
  148. {
  149. char *s; // since it's not in use
  150. // CONSIDER: Why aren't we using access() here? FindFirst has problems
  151. // CONSIDER: with networks and DOS 2.x. Also maybe cheaper. [RLB]. */
  152. // We use FindFirst() because the dateTime of file matters to us
  153. // We don't need it always but then access() probably uses findFirst()
  154. // -Sundeep-
  155. if (findFirst(name, findBuf, searchHandle)) { // check current dir first
  156. return(makeString(name));
  157. }
  158. // Check if environment string is NULL. Unnecessary if check is done
  159. // elsewhere, but it's more convenient and safer to do it here.
  160. if (p == NULL) {
  161. return(NULL);
  162. }
  163. for (s = buf; ;) {
  164. while (*p && '\"' == *p) {
  165. // Quotes should not be used in search paths. If we find any,
  166. // we ignore them. This way we can form the full path and the
  167. // filename without quotes and add an enclosing pair of quotes
  168. // later, if necessary. [DS 14575]
  169. p++;
  170. }
  171. if (!*p || (*s = *p++) == ';') { // found a dir separator
  172. if (s == buf) { // ignore ; w/out name
  173. if (*p) {
  174. continue;
  175. }
  176. return(NULL); // list exhausted ...
  177. }
  178. if (*(s-1) != '\\' && *(s-1) != '/') { // append path separator
  179. *s++ = '\\';
  180. }
  181. *s = '\0';
  182. if (_tcspbrk(buf,"*?")) { // wildcards not allowed
  183. s = buf;
  184. continue;
  185. }
  186. _tcscpy(s, name); // append file name, zap
  187. if (findFirst(buf, findBuf, searchHandle)) {
  188. return(makeString(buf));
  189. }
  190. s = buf; // reset ptr to begin of
  191. } // buf and check next dir
  192. else {
  193. ++s; // we keep copying chars
  194. } // until find ';' or '\0'
  195. }
  196. }