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.

264 lines
7.5 KiB

  1. // UTILB.C -- Data structure manipulation functions specific to OS/2
  2. //
  3. // Copyright (c) 1988-1990, Microsoft Corporation. All rights reserved.
  4. //
  5. // Purpose:
  6. // This file was created from functions in util.c & esetdrv.c which were system
  7. // dependent. This was done so that the build of the project became simpler and
  8. // there was a clear flow in the build process.
  9. //
  10. // Method of Creation:
  11. // 1. Identified all functions having mixed mode code.
  12. // 2. Deleted all code blocked out by '#ifndef BOUND' preprocessor directives
  13. // in these functions
  14. // 3. Deleted all local function & their prototypes not referred by these
  15. // 4. Deleted all global data unreferenced by these, including data blocked
  16. // of by '#ifdef DEBUG'
  17. //
  18. // Revision History:
  19. // 21-Feb-1994 HV Get rid of _alloca in findFirst: it confuses the compiler's
  20. // backend scheduler (PhilLu).
  21. // 15-Nov-1993 JdR Major speed improvements
  22. // 15-Oct-1993 HV Use tchar.h instead of mbstring.h directly, change STR*() to _ftcs*()
  23. // 15-Jun-1993 HV No longer display warning about filenames being longer than
  24. // 8.3. Decision made by EmerickF, see Ikura bug #86 for more
  25. // details.
  26. // 03-Jun-1993 HV Fixed findFirst's pathname truncation (Ikura bug #86)
  27. // 10-May-1993 HV Add include file mbstring.h
  28. // Change the str* functions to STR*
  29. // 08-Jun-1992 SS Port to DOSX32
  30. // 10-Apr-1990 SB removed if _osmode stuff, not needed in protect only version.
  31. // 04-Dec-1989 SB removed unreferenced local variables in findFirst()
  32. // 01-Dec-1989 SB changed a remaining free() to FREE(); now FREE()'ing all
  33. // allocated stuff from findFirst() on exit
  34. // 22-Nov-1989 SB Add #ifdef DEBUG_FIND to debug FIND_FIRST, etc.
  35. // 13-Nov-1989 SB Define INCL_NOPM to exclude <pm.h>
  36. // 19-Oct-1989 SB findFirst() and findNext() get extra parameter
  37. // 08-Oct-1989 SB remove quotes around a name before making system call
  38. // 02-Oct-1989 SB setdrive() proto change
  39. // 04-Sep-1989 SB Add DOSFINDCLOSE calls is findFirst and QueryFileInfo
  40. // 05-Jul-1989 SB add curTime() to get current time. (C Runtime function
  41. // differs from DOS time and so time() is no good
  42. // 05-Jun-1989 SB call DosFindClose if DosFindNext returns an error
  43. // 28-May-1989 SB Add getCurDir() to initialize MAKEDIR macro
  44. // 24-Apr-1989 SB made FILEINFO a thing of the past. Replace by void *
  45. // added OS/2 ver 1.2 support
  46. // 05-Apr-1989 SB made all funcs NEAR; Reqd to make all function calls NEAR
  47. // 09-Mar-1989 SB Added function QueryFileInfo() because DosFindFirst has FAPI
  48. // restrictions. ResultBuf was being allocated on the heap but
  49. // not being freed. This saves about 36 bytes for every call to
  50. // findAFile i.e. to findFirst(), findNext() or expandWildCards
  51. // 09-Nov-1988 SB Created
  52. #include "precomp.h"
  53. #pragma hdrstop
  54. #include <io.h>
  55. #include <direct.h>
  56. #include <time.h>
  57. STRINGLIST *
  58. expandWildCards(
  59. char *s // text to expand
  60. )
  61. {
  62. struct _finddata_t finddata;
  63. NMHANDLE searchHandle;
  64. STRINGLIST *xlist, // list of expanded names
  65. *p;
  66. char *namestr;
  67. if (!(namestr = findFirst(s, &finddata, &searchHandle))) {
  68. return(NULL);
  69. }
  70. xlist = makeNewStrListElement();
  71. xlist->text = prependPath(s, namestr);
  72. while (namestr = findNext(&finddata, searchHandle)) {
  73. p = makeNewStrListElement();
  74. p->text = prependPath(s, namestr);
  75. prependItem(&xlist, p);
  76. }
  77. return(xlist);
  78. }
  79. // QueryFileInfo -- it does a DosFindFirst which circumvents FAPI restrictions
  80. //
  81. // Scope: Global (used by Build.c also)
  82. //
  83. // Purpose:
  84. // DosFindFirst() has a FAPI restriction in Real mode. You cannot ask it give
  85. // you a handle to a DTA structure other than the default handle. This function
  86. // calls C Library Function _dos_findfirst in real mode (which sets the DTA) and
  87. // does the job. In protect mode it asks OS/2 for a new handle.
  88. //
  89. // Input:
  90. // file -- the file to be searched for
  91. // dta -- the struct containing result of the search
  92. //
  93. // Output: Returns a pointer to the filename found (if any)
  94. //
  95. // Assumes: That dta points to a structure which has been allocated enough memory
  96. //
  97. // Uses Globals:
  98. // _osmode -- to determine whether in Real or Bound mode
  99. char *
  100. QueryFileInfo(
  101. char *file,
  102. void **dta
  103. )
  104. {
  105. NMHANDLE hDir;
  106. char *t;
  107. // Remove Quotes around filename, if existing
  108. t = file + _tcslen(file) - 1;
  109. if (*file == '"' && *t == '"') {
  110. file = unQuote(file); // file is quoted, so remove quote
  111. }
  112. #if defined(DEBUG_FIND)
  113. printf("QueryFileInfo file: %s\n", file);
  114. #endif
  115. if ((hDir = _findfirst(file, (struct _finddata_t *) dta)) == -1) {
  116. return(NULL);
  117. }
  118. _findclose(hDir);
  119. return(((struct _finddata_t *) dta)->name);
  120. }
  121. //
  122. // Truncate filename to system limits
  123. //
  124. void
  125. truncateFilename(
  126. char * s
  127. )
  128. {
  129. char szDrive[_MAX_DRIVE];
  130. char szDir[_MAX_DIR];
  131. char szName[_MAX_FNAME];
  132. char szExtension[_MAX_EXT];
  133. // Ikura bug #86: pathname incorrectly truncated. Solution: first parse it
  134. // using _splitpath(), then truncate the filename and extension part.
  135. // Finally reconstruct the pathname by calling _makepath().
  136. _splitpath(s, szDrive, szDir, szName, szExtension);
  137. _makepath(s, szDrive, szDir, szName, szExtension);
  138. }
  139. char *
  140. findFirst(
  141. char *s, // text to expand
  142. void *dta,
  143. NMHANDLE *dirHandle
  144. )
  145. {
  146. BOOL anyspecial; // flag set if s contains special characters.
  147. char buf[_MAX_PATH]; // buffer for removing ESCH
  148. // Check if name contains any special characters
  149. anyspecial = (_tcspbrk(s, "\"^*?") != NULL);
  150. if (anyspecial) {
  151. char *t;
  152. char *x; // Pointers for truncation, walking for ESCH
  153. t = s + _tcslen(s) - 1;
  154. // Copy pathname, skipping ESCHs and quotes
  155. x = buf;
  156. while( *s ) {
  157. if (*s == '^' || *s == '"') {
  158. s++;
  159. }
  160. else {
  161. if (_istlead(*(unsigned char *)s))
  162. *x++ = *s++;
  163. *x++ = *s++;
  164. }
  165. }
  166. *x = '\0';
  167. s = buf; // only elide ESCH the first time!
  168. }
  169. truncateFilename(s);
  170. if ((*dirHandle = _findfirst(s, (struct _finddata_t *) dta)) == -1) {
  171. // BUGBUG Use GetLastError to get details
  172. return(NULL);
  173. }
  174. // If it had no wildcard then close the search handle
  175. if (!anyspecial || (!_tcschr(s, '*') && !_tcschr(s, '?'))) {
  176. _findclose(*dirHandle);
  177. }
  178. return(((struct _finddata_t *) dta)->name);
  179. }
  180. char *
  181. findNext(
  182. void *dta,
  183. NMHANDLE dirHandle
  184. )
  185. {
  186. if (_findnext(dirHandle, (struct _finddata_t *) dta)) {
  187. _findclose(dirHandle);
  188. return(NULL);
  189. }
  190. return(((struct _finddata_t *) dta)->name);
  191. }
  192. char *
  193. getCurDir(void)
  194. {
  195. // Convert $ to $$ before returning current dir
  196. // [DS 14983]. This allows $(MAKEDIR) to work properly in
  197. // case the current path contains a $ sign.
  198. //
  199. char *pszPath;
  200. char pbPath[_MAX_DIR+1];
  201. char *pchSrc = pbPath;
  202. char *pchDst;
  203. char ch;
  204. pszPath = (char *) rallocate(2 * _tcslen(_getcwd(pbPath, _MAX_DIR+1)) + 1);
  205. pchDst = pszPath;
  206. // non-MBCS aware implementation ('$' can't be a trailbyte)
  207. while (ch = *pchSrc) {
  208. *pchDst++ = *pchSrc++;
  209. if ('$' == ch)
  210. *pchDst++ = ch;
  211. }
  212. *pchDst = '\0';
  213. return(pszPath);
  214. }
  215. void
  216. curTime(
  217. time_t *plTime
  218. )
  219. {
  220. time(plTime);
  221. }