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.

344 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. beep.c
  5. Abstract:
  6. User mode beep program. This program simply beeps at the frequency
  7. specified on the command line and for the time specified on the
  8. command line (in milliseconds).
  9. Author:
  10. 01-Dec-1992 Steve Wood (stevewo)
  11. Revision History:
  12. --*/
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <windows.h>
  16. #define DEVICE_NAME_SIZE (20 * 1024)
  17. #define TARGET_PATH_SIZE (20 * 1024)
  18. UCHAR DeviceNames[ DEVICE_NAME_SIZE ];
  19. UCHAR TargetPath[ TARGET_PATH_SIZE ];
  20. typedef struct _DEVICE_LINK {
  21. PCHAR LinkName;
  22. ULONG LinkTargetLength;
  23. PCHAR LinkTarget;
  24. PCHAR DriveType;
  25. DWORD LogicalDriveBit;
  26. } DEVICE_LINK, *PDEVICE_LINK;
  27. ULONG NumberOfDriveLetters;
  28. ULONG NumberOfDevices;
  29. DEVICE_LINK DriveLetters[ 4096 ];
  30. DEVICE_LINK Devices[ 4096 ];
  31. void
  32. Usage( void )
  33. {
  34. fprintf( stderr, "usage: DOSDEV [-a] [-s] [-h] [[-r] [-d [-e]] DeviceName [TargetPath]]\n" );
  35. exit( 1 );
  36. }
  37. void
  38. DisplayDeviceTarget(
  39. char *Msg,
  40. char *Name,
  41. char *Target,
  42. DWORD cchTarget
  43. );
  44. char *DriveTypes[] = {
  45. "Unknown",
  46. "NoRootDir",
  47. "Removable",
  48. "Fixed",
  49. "Remote",
  50. "CDRom",
  51. "RamDisk"
  52. };
  53. void
  54. DisplayDeviceTarget(
  55. char *Msg,
  56. char *Name,
  57. char *Target,
  58. DWORD cchTarget
  59. )
  60. {
  61. char *s;
  62. printf( "%s%s = ", Msg, Name );
  63. s = Target;
  64. while (*s && cchTarget != 0) {
  65. if (s > Target) {
  66. printf( " ; " );
  67. }
  68. printf( "%s", s );
  69. while (*s++) {
  70. if (!cchTarget--) {
  71. cchTarget = 0;
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. int
  78. GetSystemPartitionFromRegistry(
  79. char* lpSystemPartition
  80. )
  81. {
  82. LONG r;
  83. HKEY key;
  84. DWORD bytes;
  85. r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\Setup", 0, KEY_QUERY_VALUE,
  86. &key);
  87. if (r) {
  88. printf("RegOpenKeyEx failed with %d\n", r);
  89. return 0;
  90. }
  91. bytes = MAX_PATH;
  92. r = RegQueryValueEx(key, "SystemPartition", NULL, NULL, lpSystemPartition,
  93. &bytes);
  94. RegCloseKey(key);
  95. if (r) {
  96. printf("RegQueryValueEx failed with %d\n", r);
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. int
  102. __cdecl
  103. CompareDeviceLink(
  104. const void *p1,
  105. const void *p2
  106. )
  107. {
  108. return _stricmp( ((PDEVICE_LINK)p1)->LinkName,
  109. ((PDEVICE_LINK)p2)->LinkName );
  110. }
  111. int
  112. __cdecl
  113. main(
  114. int argc,
  115. char *argv[]
  116. )
  117. {
  118. DWORD cch, i;
  119. char c, *s;
  120. char RootDir[ 4 ];
  121. DWORD dwFlags;
  122. DWORD UnknownLogicalDrives;
  123. BOOL fShowOnlyDrives, fSystemPartition;
  124. LPSTR lpDeviceName;
  125. LPSTR lpTargetPath;
  126. PDEVICE_LINK p;
  127. char systemBuffer[MAX_PATH];
  128. lpDeviceName = NULL;
  129. lpTargetPath = NULL;
  130. fShowOnlyDrives = TRUE;
  131. fSystemPartition = FALSE;
  132. dwFlags = 0;
  133. while (--argc) {
  134. s = *++argv;
  135. if (*s == '-' || *s == '/') {
  136. while (c = *++s) {
  137. switch (tolower( c )) {
  138. case '?':
  139. case 'h':
  140. Usage();
  141. case 'e':
  142. dwFlags |= DDD_EXACT_MATCH_ON_REMOVE;
  143. break;
  144. case 'd':
  145. dwFlags |= DDD_REMOVE_DEFINITION;
  146. break;
  147. case 'r':
  148. dwFlags |= DDD_RAW_TARGET_PATH;
  149. break;
  150. case 'a':
  151. fShowOnlyDrives = FALSE;
  152. break;
  153. case 's':
  154. fSystemPartition = TRUE;
  155. dwFlags |= DDD_RAW_TARGET_PATH;
  156. break;
  157. }
  158. }
  159. }
  160. else
  161. if (lpDeviceName == NULL) {
  162. lpDeviceName = s;
  163. }
  164. else
  165. if (lpTargetPath == NULL) {
  166. lpTargetPath = s;
  167. }
  168. else {
  169. Usage();
  170. }
  171. }
  172. if (fSystemPartition) {
  173. lpTargetPath = systemBuffer;
  174. if (!GetSystemPartitionFromRegistry(lpTargetPath)) {
  175. exit( 1 );
  176. }
  177. }
  178. if (lpDeviceName == NULL && lpTargetPath == NULL) {
  179. cch = QueryDosDevice( NULL,
  180. DeviceNames,
  181. sizeof( DeviceNames )
  182. );
  183. if (cch == 0) {
  184. fprintf( stderr, "DOSDEV: Unable to query device names - %u\n", GetLastError() );
  185. exit( 1 );
  186. }
  187. s = DeviceNames;
  188. while (*s) {
  189. cch = QueryDosDevice( s,
  190. TargetPath,
  191. sizeof( TargetPath )
  192. );
  193. if (cch == 0) {
  194. sprintf( TargetPath, "*** unable to query target path - %u ***", GetLastError() );
  195. }
  196. else {
  197. if (strlen( s ) == 2 && s[1] == ':') {
  198. p = &DriveLetters[ NumberOfDriveLetters++ ];
  199. sprintf( RootDir, "%s\\", s );
  200. p->DriveType = DriveTypes[ GetDriveType( RootDir ) ];
  201. p->LogicalDriveBit = 1 << (s[0] - 'A');
  202. }
  203. else {
  204. p = &Devices[ NumberOfDevices++ ];
  205. }
  206. p->LinkName = s;
  207. p->LinkTargetLength = cch;
  208. p->LinkTarget = malloc( cch + 1 );
  209. memmove( p->LinkTarget, TargetPath, cch );
  210. }
  211. while (*s++)
  212. ;
  213. }
  214. qsort( &DriveLetters[0],
  215. NumberOfDriveLetters,
  216. sizeof( DEVICE_LINK ),
  217. CompareDeviceLink
  218. );
  219. UnknownLogicalDrives = GetLogicalDrives();
  220. for (i=0; i<NumberOfDriveLetters; i++) {
  221. p = &DriveLetters[ i ];
  222. DisplayDeviceTarget( "", p->LinkName, p->LinkTarget, p->LinkTargetLength );
  223. printf( " [%s]", p->DriveType );
  224. if (UnknownLogicalDrives & p->LogicalDriveBit) {
  225. UnknownLogicalDrives ^= p->LogicalDriveBit;
  226. }
  227. else {
  228. printf( " *** LOGICAL DRIVE BIT NOT SET ***" );
  229. }
  230. printf( "\n" );
  231. }
  232. if (UnknownLogicalDrives) {
  233. for (i=0; i<26; i++) {
  234. if (UnknownLogicalDrives & (1 << i)) {
  235. printf( "%c: = *** LOGICAL DRIVE BIT SET BUT NO DRIVE LETTER ***\n",
  236. 'A' + i
  237. );
  238. }
  239. }
  240. }
  241. if (!fShowOnlyDrives) {
  242. printf( "\n" );
  243. qsort( &Devices[0],
  244. NumberOfDevices,
  245. sizeof( DEVICE_LINK ),
  246. CompareDeviceLink
  247. );
  248. for (i=0; i<NumberOfDevices; i++) {
  249. p = &Devices[ i ];
  250. DisplayDeviceTarget( "", p->LinkName, p->LinkTarget, p->LinkTargetLength );
  251. printf( "\n" );
  252. }
  253. }
  254. exit( 0 );
  255. }
  256. if (lpDeviceName == NULL) {
  257. Usage();
  258. }
  259. else
  260. if (!(dwFlags & DDD_REMOVE_DEFINITION) && lpTargetPath == NULL) {
  261. Usage();
  262. }
  263. cch = QueryDosDevice( lpDeviceName,
  264. TargetPath,
  265. sizeof( TargetPath )
  266. );
  267. if (cch != 0) {
  268. DisplayDeviceTarget( "Current definition: ", lpDeviceName, TargetPath, cch );
  269. printf( "\n" );
  270. }
  271. if (!DefineDosDevice( dwFlags, lpDeviceName, lpTargetPath )) {
  272. fprintf( stderr,
  273. "DOSDEV: Unable to %s device name %s - %u\n",
  274. (dwFlags & DDD_REMOVE_DEFINITION) ? "delete"
  275. : "define",
  276. lpDeviceName,
  277. GetLastError()
  278. );
  279. }
  280. else {
  281. cch = QueryDosDevice( lpDeviceName,
  282. TargetPath,
  283. sizeof( TargetPath )
  284. );
  285. if (cch != 0) {
  286. DisplayDeviceTarget( "Current definition: ", lpDeviceName, TargetPath, cch );
  287. printf( "\n" );
  288. }
  289. else {
  290. printf( "%s deleted.\n", lpDeviceName );
  291. }
  292. }
  293. return 0;
  294. }