Leaked source code of windows server 2003
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.

192 lines
4.7 KiB

  1. #include "pch.h"
  2. #include <win16def.h>
  3. #include <win32fn.h>
  4. #include <fcntl.h>
  5. #include <sys\types.h>
  6. #include <sys\stat.h>
  7. #include <io.h>
  8. //extern LPSTR g_lpszCmdLine;
  9. HANDLE CreateFile(
  10. LPCTSTR lpFileName, // pointer to name of the file
  11. DWORD dwDesiredAccess, // access (read-write) mode
  12. DWORD dwShareMode, // share mode
  13. LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security descriptor
  14. DWORD dwCreationDistribution, // how to create
  15. DWORD dwFlagsAndAttributes, // file attributes
  16. HANDLE hTemplateFile // handle to file with attributes to copy
  17. )
  18. {
  19. int oflag = 0, pmode = 0, iHandle = -1;
  20. if ((dwDesiredAccess & GENERIC_READ) && (dwDesiredAccess & GENERIC_WRITE))
  21. oflag = _O_RDWR;
  22. else if (dwDesiredAccess & GENERIC_WRITE)
  23. oflag = _O_WRONLY;
  24. else
  25. oflag = _O_RDONLY;
  26. switch (dwCreationDistribution)
  27. {
  28. case CREATE_NEW:
  29. oflag |= (_O_CREAT | _O_EXCL);
  30. break;
  31. case CREATE_ALWAYS:
  32. case TRUNCATE_EXISTING:
  33. oflag |= _O_TRUNC;
  34. break;
  35. case OPEN_ALWAYS:
  36. oflag |= _O_CREAT;
  37. }
  38. if (dwShareMode & FILE_SHARE_READ)
  39. pmode |= _S_IREAD;
  40. if (dwShareMode & FILE_SHARE_WRITE)
  41. pmode |= _S_IWRITE;
  42. iHandle = _open(lpFileName, oflag, pmode);
  43. if (-1 == iHandle)
  44. return (HANDLE) INVALID_HANDLE_VALUE;
  45. else
  46. return (HANDLE) iHandle;
  47. }
  48. BOOL WriteFile(
  49. HANDLE hFile, // handle to file to write to
  50. LPCVOID lpBuffer, // pointer to data to write to file
  51. DWORD nNumberOfBytesToWrite, // number of bytes to write
  52. LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
  53. LPOVERLAPPED lpOverlapped // pointer to structure needed for overlapped I/O
  54. )
  55. {
  56. *lpNumberOfBytesWritten = (DWORD) _write(hFile, lpBuffer,
  57. (unsigned int)nNumberOfBytesToWrite);
  58. return (*lpNumberOfBytesWritten == nNumberOfBytesToWrite);
  59. }
  60. BOOL MoveFileEx(
  61. LPCTSTR lpExistingFileName, // address of name of the existing file
  62. LPCTSTR lpNewFileName, // address of new name for the file
  63. DWORD dwFlags // flag to determine how to move file
  64. )
  65. {
  66. //
  67. // BUGBUG: Try renaming first and then delete file
  68. //
  69. if (dwFlags & MOVEFILE_REPLACE_EXISTING)
  70. {
  71. if (_access(lpNewFileName, 0) == 0)
  72. remove(lpNewFileName);
  73. }
  74. return (rename(lpExistingFileName, lpNewFileName) == 0);
  75. }
  76. BOOL CloseHandle(
  77. HANDLE hObject // handle to object to close
  78. )
  79. {
  80. // We should check if this is really a file hande...
  81. return (!_close(hObject));
  82. }
  83. #if 0
  84. DWORD SearchPath(
  85. LPCTSTR lpPath, // address of search path
  86. LPCTSTR lpFileName, // address of filename
  87. LPCTSTR lpExtension, // address of extension
  88. DWORD nBufferLength, // size, in characters, of buffer
  89. LPTSTR lpBuffer, // address of buffer for found filename
  90. LPTSTR far *lpFilePart // address of pointer to file component
  91. )
  92. {
  93. LPSTR pszPath;
  94. LPSTR pszFile;
  95. LPSTR pEnv;
  96. int len = 0, prevlen;
  97. pszPath = (LPSTR)_fcalloc(1, MAX_PATH*3);
  98. pszFile = (LPSTR)_fcalloc(1, MAX_PATH);
  99. //
  100. // Create an environment variable for searchenv to use.
  101. //
  102. strcpy(pszPath, ICW_PATH);
  103. strcat(pszPath, "=");
  104. len = strlen(pszPath);
  105. if (NULL == lpPath)
  106. {
  107. //
  108. // Directory from which the application laoded
  109. //
  110. /* prevlen = len;
  111. _fstrcpy(szPath+len, g_lpszCmdLine);
  112. for ( ; szPath[len] != ' ' && szPath[len] != '\0'; len++) ;
  113. for ( ; len > prevlen+1 && szPath[len] != '\\'; len--) ;
  114. szPath[len++] = ';';
  115. */
  116. //
  117. // Windows system directory
  118. //
  119. len += GetSystemDirectory(pszPath+len, MAX_PATH);
  120. pszPath[len++] = ';';
  121. //
  122. // Windows directory
  123. //
  124. len += GetWindowsDirectory(pszPath+len, MAX_PATH);
  125. //
  126. // PATH environment variable
  127. //
  128. if ((pEnv = getenv("PATH")) != NULL)
  129. {
  130. pszPath[len++] = ';';
  131. for ( ; *pEnv; pEnv++) pszPath[len++] = *pEnv;
  132. }
  133. pszPath[len] = '\0';
  134. }
  135. else
  136. {
  137. lstrcpy(pszPath+len, lpPath);
  138. }
  139. //
  140. // Set the environment variable so _searchenv can use it
  141. //
  142. _putenv(pszPath);
  143. //
  144. // Append the extension to the file, if necessary
  145. //
  146. lstrcpy(pszFile, lpFileName);
  147. len = lstrlen(pszFile);
  148. if ((pszFile[len] != '.') && (lpExtension != NULL))
  149. lstrcat(pszFile, lpExtension);
  150. _searchenv(pszFile, ICW_PATH, lpBuffer);
  151. //
  152. // Clear the temporary environment variable before freeing the memory
  153. //
  154. lstrcpy(pszFile, ICW_PATH);
  155. lstrcat(pszFile, "=");
  156. _putenv(pszFile);
  157. _ffree(pszFile);
  158. _ffree(pszPath);
  159. return (lstrlen(lpBuffer));
  160. }
  161. #endif //0