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.

446 lines
9.6 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1993 **/
  4. /**********************************************************************/
  5. /*
  6. utility.cxx
  7. This module contains routines of general utility.
  8. Functions exported by this module:
  9. TransferType
  10. TransferMode
  11. DisplayBool
  12. IsDecimalNumber
  13. AllocErrorText
  14. FreeErrorText
  15. OpenDosPath
  16. FlipSlashes
  17. OpenLogFile
  18. P_strncpy
  19. FILE HISTORY:
  20. KeithMo 17-Mar-1993 Created.
  21. */
  22. #include "ftpdp.hxx"
  23. extern "C" {
  24. # include <ntlsa.h>
  25. };
  26. //
  27. // Public functions.
  28. //
  29. /*******************************************************************
  30. NAME: TransferType
  31. SYNOPSIS: Generates printable form of a transfer type.
  32. ENTRY: type - From the XFER_TYPE enumerator.
  33. RETURNS: CHAR * - "ASCII", "BINARY", etc.
  34. HISTORY:
  35. KeithMo 12-Mar-1993 Created.
  36. ********************************************************************/
  37. CHAR *
  38. TransferType(
  39. XFER_TYPE type
  40. )
  41. {
  42. CHAR * pszResult = NULL;
  43. switch( type )
  44. {
  45. case XferTypeAscii :
  46. pszResult = "ASCII";
  47. break;
  48. case XferTypeBinary :
  49. pszResult = "BINARY";
  50. break;
  51. default :
  52. DBGPRINTF(( DBG_CONTEXT,
  53. "invalid transfer type %d\n",
  54. type ));
  55. DBG_ASSERT( FALSE );
  56. pszResult = "ASCII";
  57. break;
  58. }
  59. DBG_ASSERT( pszResult != NULL );
  60. return pszResult;
  61. } // TransferType
  62. /*******************************************************************
  63. NAME: TransferMode
  64. SYNOPSIS: Generates printable form of a transfer mode.
  65. ENTRY: mode - From the XFER_MODE enumerator.
  66. RETURNS: CHAR * - "STREAM", "BLOCK", etc.
  67. NOTES: Currently, only the STREAM mode is suppored.
  68. HISTORY:
  69. KeithMo 12-Mar-1993 Created.
  70. ********************************************************************/
  71. CHAR *
  72. TransferMode(
  73. XFER_MODE mode
  74. )
  75. {
  76. CHAR * pszResult = NULL;
  77. switch( mode )
  78. {
  79. case XferModeStream :
  80. pszResult = "STREAM";
  81. break;
  82. case XferModeBlock :
  83. pszResult = "BLOCK";
  84. break;
  85. default :
  86. DBGPRINTF(( DBG_CONTEXT,
  87. "invalid transfer mode %d\n",
  88. mode ));
  89. DBG_ASSERT( FALSE );
  90. pszResult = "STREAM";
  91. break;
  92. }
  93. DBG_ASSERT( pszResult != NULL );
  94. return pszResult;
  95. } // TransferMode
  96. /*******************************************************************
  97. NAME: DisplayBool
  98. SYNOPSIS: Generates printable form of a boolean.
  99. ENTRY: fFlag - The BOOL to display.
  100. RETURNS: CHAR * - "TRUE" or "FALSE".
  101. HISTORY:
  102. KeithMo 17-Mar-1993 Created.
  103. ********************************************************************/
  104. CHAR *
  105. DisplayBool(
  106. BOOL fFlag
  107. )
  108. {
  109. return fFlag ? "TRUE" : "FALSE";
  110. } // DisplayBool
  111. /*******************************************************************
  112. NAME: IsDecimalNumber
  113. SYNOPSIS: Determines if a given string represents a decimal
  114. number.
  115. ENTRY: psz - The string to scan.
  116. RETURNS: BOOL - TRUE if this is a decimal number, FALSE
  117. otherwise.
  118. HISTORY:
  119. KeithMo 12-Mar-1993 Created.
  120. ********************************************************************/
  121. BOOL
  122. IsDecimalNumber(
  123. CHAR * psz
  124. )
  125. {
  126. BOOL fResult = ( *psz != '\0' );
  127. CHAR ch;
  128. while( ch = *psz++ )
  129. {
  130. if( ( ch < '0' ) || ( ch > '9' ) )
  131. {
  132. fResult = FALSE;
  133. break;
  134. }
  135. }
  136. return fResult;
  137. } // IsDecimalNumber
  138. /*******************************************************************
  139. NAME: AllocErrorText
  140. SYNOPSIS: Maps a specified Win32 error code to a textual
  141. description. In the interest of multithreaded
  142. safety, this routine will allocate a block of memory
  143. to contain the text and return a pointer to that
  144. block. It is up to the caller to free the block
  145. with FreeErrorText.
  146. ENTRY: err - The error to map.
  147. RETURNS: CHAR * - A textual description of err. Will be NULL
  148. if an error occurred while mapping err to text.
  149. HISTORY:
  150. KeithMo 27-Apr-1993 Created.
  151. ********************************************************************/
  152. CHAR *
  153. AllocErrorText(
  154. APIERR err
  155. )
  156. {
  157. APIERR fmerr = NO_ERROR;
  158. CHAR * pszText = NULL;
  159. if( FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
  160. | FORMAT_MESSAGE_IGNORE_INSERTS
  161. | FORMAT_MESSAGE_FROM_SYSTEM
  162. | FORMAT_MESSAGE_MAX_WIDTH_MASK,
  163. NULL,
  164. (DWORD)err,
  165. g_pInetSvc->IsSystemDBCS() // always use english for
  166. ? 0x409 // FarEast NT system
  167. : 0,
  168. (LPTSTR)&pszText,
  169. 0,
  170. NULL ) == 0 )
  171. {
  172. fmerr = GetLastError();
  173. }
  174. else
  175. {
  176. }
  177. IF_DEBUG( COMMANDS )
  178. {
  179. if( fmerr == NO_ERROR )
  180. {
  181. DBGPRINTF(( DBG_CONTEXT,
  182. "mapped error %lu to %s\n",
  183. err,
  184. pszText ));
  185. }
  186. else
  187. {
  188. DBGPRINTF(( DBG_CONTEXT,
  189. "cannot map error %lu to text, error %lu\n",
  190. err,
  191. fmerr ));
  192. }
  193. }
  194. return pszText;
  195. } // AllocErrorText
  196. /*******************************************************************
  197. NAME: FreeErrorText
  198. SYNOPSIS: Frees the pointer returned by AllocErrorText.
  199. ENTRY: pszText - The text to free. Must be a pointer
  200. returned by AllocErrorText.
  201. HISTORY:
  202. KeithMo 27-Apr-1993 Created.
  203. ********************************************************************/
  204. VOID
  205. FreeErrorText(
  206. CHAR * pszText
  207. )
  208. {
  209. LocalFree( (HLOCAL)pszText );
  210. } // FreeErrorText
  211. DWORD
  212. OpenPathForAccess(
  213. LPHANDLE phFile,
  214. LPSTR pszPath,
  215. ULONG DesiredAccess,
  216. ULONG ShareAccess,
  217. HANDLE hImpersonation
  218. )
  219. /*++
  220. This function opens a path for access to do some verification
  221. or holding on to the file/directory when a user is logged on.
  222. Arguments:
  223. phFile - pointer to handle, where a handle is stored on
  224. successful return.
  225. pszPath - pointer to null terminated string containing the path
  226. for path to be opened.
  227. DesiredAccess - Access type to the file.
  228. ShareAccess - access flags for shared opens.
  229. hImpersonation - Impersonation token for this user - used for
  230. long filename check
  231. Returns:
  232. Win32 error code - NO_ERROR on success
  233. Author:
  234. MuraliK 14-Nov-1995
  235. --*/
  236. {
  237. DWORD dwError = NO_ERROR;
  238. if ( phFile == NULL) {
  239. return ( ERROR_INVALID_PARAMETER);
  240. }
  241. *phFile = CreateFile( pszPath, // path for the file
  242. DesiredAccess, // fdwAccess
  243. ShareAccess, // fdwShareMode
  244. NULL, // Security attributes
  245. OPEN_EXISTING, // fdwCreate
  246. FILE_FLAG_BACKUP_SEMANTICS, // fdwAttrsAndFlags
  247. NULL ); // hTemplateFile
  248. if ( *phFile == INVALID_HANDLE_VALUE) {
  249. dwError = GetLastError();
  250. }
  251. else {
  252. if ( strchr( pszPath, '~' )) {
  253. BOOL fShort;
  254. RevertToSelf();
  255. dwError = CheckIfShortFileName( (UCHAR *) pszPath,
  256. hImpersonation,
  257. &fShort );
  258. if ( !dwError && fShort )
  259. {
  260. dwError = ERROR_FILE_NOT_FOUND;
  261. DBG_REQUIRE( CloseHandle( *phFile ));
  262. *phFile = INVALID_HANDLE_VALUE;
  263. }
  264. }
  265. }
  266. return ( dwError);
  267. } // OpenPathForAccess()
  268. /*******************************************************************
  269. NAME: FlipSlashes
  270. SYNOPSIS: Flips the DOS-ish backslashes ('\') into Unix-ish
  271. forward slashes ('/').
  272. ENTRY: pszPath - The path to munge.
  273. RETURNS: CHAR * - pszPath.
  274. HISTORY:
  275. KeithMo 04-Jun-1993 Created.
  276. ********************************************************************/
  277. CHAR *
  278. FlipSlashes(
  279. CHAR * pszPath
  280. )
  281. {
  282. CHAR ch;
  283. CHAR * pszScan = pszPath;
  284. while( ( ch = *pszScan ) != '\0' )
  285. {
  286. //
  287. // skip DBCS character
  288. //
  289. if ( IsDBCSLeadByte( ch ) && *(pszScan+1) )
  290. {
  291. pszScan++;
  292. }
  293. else
  294. if( ch == '\\' )
  295. {
  296. *pszScan = '/';
  297. }
  298. pszScan++;
  299. }
  300. return pszPath;
  301. } // FlipSlashes
  302. //
  303. // Private functions.
  304. //
  305. /*******************************************************************
  306. NAME:
  307. SYNOPSIS: strncpy, that always terminates with a '\0'
  308. ENTRY: same as strncpy.
  309. RETURNS: same as strncpy.
  310. HISTORY:
  311. RobSol 02-Apr-2001 Created.
  312. ********************************************************************/
  313. PSTR
  314. P_strncpy(
  315. PSTR dst,
  316. PCSTR src,
  317. DWORD cnt)
  318. {
  319. strncpy( dst, src, cnt);
  320. dst[ cnt - 1] = '\0';
  321. return dst;
  322. }