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.

511 lines
13 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. alias.c
  5. Abstract:
  6. alias utility
  7. Author:
  8. Therese Stowell (thereses) 22-Mar-1990
  9. Revision History:
  10. --*/
  11. #include <windows.h>
  12. #include <conapi.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <cvtoem.h>
  17. BOOL fVerbose;
  18. DWORD
  19. DisplayAliases(
  20. char *ExeName
  21. );
  22. DWORD
  23. DisplayAlias(
  24. LPSTR AliasName,
  25. LPSTR ExeName
  26. );
  27. DWORD
  28. DoAliasFile(
  29. char * FileName,
  30. char * ExeName,
  31. BOOL fDelete
  32. );
  33. DWORD
  34. DoAlias(
  35. char *Source,
  36. char *Target,
  37. char *ExeName
  38. );
  39. void
  40. usage( void )
  41. {
  42. fprintf( stderr, "Usage: ALIAS [-v] [-p programName] [-f filespec] [<source> <target>]\n" );
  43. fprintf( stderr, " [-v] means verbose output.\n" );
  44. fprintf( stderr, " [-d] means delete aliases.\n" );
  45. fprintf( stderr, " [-p programName] specifies which image file name these alias\n" );
  46. fprintf( stderr, " definitions are for. Default is CMD.EXE\n" );
  47. fprintf( stderr, " [-f filespec] specifies a file which contains the aliases.\n" );
  48. exit( 1 );
  49. }
  50. DWORD
  51. DoAlias(
  52. char *Source,
  53. char *Target,
  54. char *ExeName
  55. )
  56. {
  57. if (!AddConsoleAlias( Source, Target, ExeName )) {
  58. if (!Target) {
  59. fprintf( stderr,
  60. "ALIAS: Unable to delete alias - %s\n",
  61. Source,
  62. Target
  63. );
  64. }
  65. else {
  66. fprintf( stderr,
  67. "ALIAS: Unable to add alias - %s = %s\n",
  68. Source,
  69. Target
  70. );
  71. }
  72. return ERROR_NOT_ENOUGH_MEMORY;
  73. }
  74. else
  75. if (fVerbose) {
  76. if (!Target) {
  77. fprintf( stderr, "Deleted alias - %s\n", Source );
  78. }
  79. else {
  80. fprintf( stderr, "Added alias - %s = %s\n", Source, Target );
  81. }
  82. }
  83. return NO_ERROR;
  84. }
  85. DWORD
  86. DoAliasFile(
  87. char * FileName,
  88. char * ExeName,
  89. BOOL fDelete
  90. )
  91. {
  92. DWORD rc;
  93. FILE *fh;
  94. char LineBuffer[ 256 ], *Source, *Target, *s;
  95. if (!(fh = fopen( FileName, "rt" ))) {
  96. fprintf( stderr, "ALIAS: Unable to open file - %s\n", FileName );
  97. return ERROR_FILE_NOT_FOUND;
  98. }
  99. if (fVerbose) {
  100. fprintf( stderr,
  101. "ALIAS: %s aliases defined in %s\n",
  102. fDelete ? "Deleting" : "Loading",
  103. FileName
  104. );
  105. }
  106. while (s = fgets( LineBuffer, sizeof( LineBuffer ), fh )) {
  107. while (*s <= ' ') {
  108. if (!*s) {
  109. break;
  110. }
  111. s++;
  112. }
  113. if (!*s || (*s == '/' && s[1] == '/')) {
  114. continue;
  115. }
  116. Source = s;
  117. while (*s > ' ') {
  118. s++;
  119. }
  120. *s++ = '\0';
  121. while (*s <= ' ') {
  122. if (!*s) {
  123. break;
  124. }
  125. s++;
  126. }
  127. Target = s;
  128. s += strlen( s );
  129. while (*s <= ' ') {
  130. *s-- = '\0';
  131. if (s < Target) {
  132. break;
  133. }
  134. }
  135. rc = DoAlias( Source, fDelete ? NULL : Target, ExeName );
  136. if (rc != NO_ERROR) {
  137. break;
  138. }
  139. }
  140. return rc;
  141. }
  142. DWORD
  143. DisplayAlias(
  144. LPSTR AliasName,
  145. LPSTR ExeName
  146. )
  147. {
  148. DWORD cb;
  149. CHAR AliasBuffer[512];
  150. if (cb = GetConsoleAlias( AliasName, AliasBuffer, sizeof( AliasBuffer ), ExeName )) {
  151. printf( "%-16s=%s\n", AliasName, AliasBuffer );
  152. return NO_ERROR;
  153. }
  154. else {
  155. printf( "%-16s *** Unable to read value of alias ***\n",
  156. AliasName
  157. );
  158. return ERROR_ENVVAR_NOT_FOUND;
  159. }
  160. }
  161. int __cdecl
  162. CmpNamesRoutine(
  163. const VOID *Element1,
  164. const VOID *Element2
  165. )
  166. {
  167. return( strcmp( *(LPSTR *)Element1, *(LPSTR *)Element2 ) );
  168. }
  169. DWORD
  170. DisplayAliases(
  171. char *ExeName
  172. )
  173. {
  174. DWORD cb, rc, nExeNames, nAliases, iExeName, iAlias;
  175. LPSTR FreeMem1, FreeMem2, AliasName, AliasValue, s, *SortedExeNames, *SortedAliasNames;
  176. if (ExeName == NULL) {
  177. cb = GetConsoleAliasExesLength();
  178. if (cb == 0) {
  179. return ERROR_ENVVAR_NOT_FOUND;
  180. }
  181. if (!(FreeMem1 = malloc( cb+2 ))) {
  182. fprintf( stderr, "ALIAS: Not enough memory for EXE names.\n" );
  183. return ERROR_NOT_ENOUGH_MEMORY;
  184. }
  185. ExeName = FreeMem1;
  186. if (!GetConsoleAliasExes( ExeName, cb )) {
  187. fprintf( stderr, "ALIAS: Unable to read alias EXE names.\n" );
  188. return ERROR_ENVVAR_NOT_FOUND;
  189. }
  190. ExeName[ cb ] = '\0';
  191. ExeName[ cb+1 ] = '\0';
  192. nExeNames = 0;
  193. s = ExeName;
  194. while (*s) {
  195. _strupr( s );
  196. nExeNames++;
  197. while (*s++) {
  198. }
  199. }
  200. SortedExeNames = malloc( nExeNames * sizeof( LPSTR ) );
  201. if (SortedExeNames == NULL) {
  202. fprintf( stderr, "ALIAS: Not enough memory to sort .EXE names.\n" );
  203. }
  204. else {
  205. iExeName = 0;
  206. s = ExeName;
  207. while (*s) {
  208. SortedExeNames[ iExeName++ ] = s;
  209. while (*s++) {
  210. }
  211. }
  212. qsort( SortedExeNames,
  213. nExeNames,
  214. sizeof( LPSTR ),
  215. CmpNamesRoutine
  216. );
  217. iExeName = 0;
  218. }
  219. }
  220. else {
  221. SortedExeNames = NULL;
  222. FreeMem1 = NULL;
  223. }
  224. rc = NO_ERROR;
  225. while (rc == NO_ERROR && *ExeName) {
  226. if (SortedExeNames != NULL) {
  227. ExeName = SortedExeNames[ iExeName++ ];
  228. }
  229. cb = GetConsoleAliasesLength(ExeName);
  230. if (cb == 0) {
  231. printf( "No aliases defined for %s in current console.\n", ExeName );
  232. }
  233. else {
  234. if (!(FreeMem2 = malloc( cb+2 ))) {
  235. fprintf( stderr, "ALIAS: Not enough memory for alias names.\n" );
  236. rc = ERROR_NOT_ENOUGH_MEMORY;
  237. break;
  238. }
  239. SortedAliasNames = NULL;
  240. AliasName = FreeMem2;
  241. if (GetConsoleAliases( AliasName, cb, ExeName )) {
  242. AliasName[ cb ] = '\0';
  243. AliasName[ cb+1 ] = '\0';
  244. nAliases = 0;
  245. s = AliasName;
  246. while (*s) {
  247. nAliases++;
  248. while (*s++) {
  249. }
  250. }
  251. SortedAliasNames = malloc( nAliases * sizeof( LPSTR ) );
  252. if (SortedAliasNames == NULL) {
  253. fprintf( stderr, "ALIAS: Not enough memory to sort alias names.\n" );
  254. }
  255. else {
  256. iAlias = 0;
  257. s = AliasName;
  258. while (*s) {
  259. SortedAliasNames[ iAlias++ ] = s;
  260. while (*s++) {
  261. }
  262. }
  263. qsort( SortedAliasNames,
  264. nAliases,
  265. sizeof( LPSTR ),
  266. CmpNamesRoutine
  267. );
  268. iAlias = 0;
  269. }
  270. printf( "Dumping all defined aliases for %s.\n", ExeName );
  271. while (*AliasName) {
  272. if (SortedAliasNames != NULL) {
  273. AliasName = SortedAliasNames[ iAlias++ ];
  274. }
  275. AliasValue = AliasName;
  276. while (*AliasValue) {
  277. if (*AliasValue == '=') {
  278. *AliasValue++ = '\0';
  279. break;
  280. }
  281. else {
  282. AliasValue++;
  283. }
  284. }
  285. printf( " %-16s=%s\n", AliasName, AliasValue );
  286. if (SortedAliasNames != NULL) {
  287. if (iAlias < nAliases) {
  288. AliasName = " ";
  289. }
  290. else {
  291. AliasName = "";
  292. }
  293. }
  294. else {
  295. AliasName = AliasValue;
  296. while (*AliasName++) {
  297. ;
  298. }
  299. }
  300. }
  301. }
  302. else {
  303. fprintf( stderr, "ALIAS: unable to read aliases for %s.\n", ExeName );
  304. rc = ERROR_ENVVAR_NOT_FOUND;
  305. }
  306. free( FreeMem2 );
  307. if (SortedAliasNames != NULL) {
  308. free( SortedAliasNames );
  309. }
  310. }
  311. if (SortedExeNames != NULL) {
  312. if (iExeName < nExeNames) {
  313. ExeName = " ";
  314. }
  315. else {
  316. ExeName = "";
  317. }
  318. }
  319. else {
  320. while (*ExeName++) {
  321. ;
  322. }
  323. }
  324. }
  325. if (SortedExeNames != NULL) {
  326. free( SortedExeNames );
  327. }
  328. if (FreeMem1) {
  329. free( FreeMem1 );
  330. }
  331. return rc;
  332. }
  333. DWORD __cdecl
  334. main(
  335. int argc,
  336. char *argv[]
  337. )
  338. {
  339. DWORD rc;
  340. char *s, *s1, *AliasName;
  341. char *ExeName;
  342. BOOL fDelete;
  343. BOOL DisplayAllAliases;
  344. ConvertAppToOem( argc,argv );
  345. AliasName = NULL;
  346. ExeName = NULL;
  347. fVerbose = FALSE;
  348. fDelete = FALSE;
  349. DisplayAllAliases = TRUE;
  350. rc = NO_ERROR;
  351. while (rc == NO_ERROR && --argc) {
  352. s = *++argv;
  353. if (*s == '-' || *s == '/') {
  354. while (*++s) {
  355. switch( *s ) {
  356. case '?':
  357. case 'h':
  358. case 'H':
  359. usage();
  360. break;
  361. case 'd':
  362. case 'D':
  363. fDelete = TRUE;
  364. break;
  365. case 'v':
  366. case 'V':
  367. fVerbose = TRUE;
  368. break;
  369. case 'p':
  370. case 'P':
  371. if (!--argc) {
  372. fprintf( stderr, "ALIAS: Argument to -p switch missing.\n" );
  373. usage();
  374. }
  375. if (ExeName != NULL) {
  376. free( ExeName );
  377. ExeName = NULL;
  378. }
  379. s1 = *++argv;
  380. ExeName = calloc( 1, strlen( s1 )+2 );
  381. if (ExeName) {
  382. strcpy( ExeName, s1 );
  383. }
  384. break;
  385. case 'f':
  386. case 'F':
  387. if (!--argc) {
  388. fprintf( stderr, "ALIAS: Argument to -f switch missing.\n" );
  389. usage();
  390. }
  391. DisplayAllAliases = FALSE;
  392. rc = DoAliasFile( *++argv, ExeName ? ExeName : "CMD.EXE", fDelete );
  393. break;
  394. default:
  395. fprintf( stderr, "ALIAS: invalid switch /%c\n", *s );
  396. usage();
  397. break;
  398. }
  399. }
  400. }
  401. else {
  402. DisplayAllAliases = FALSE;
  403. if (AliasName == NULL) {
  404. if (fDelete) {
  405. rc = DoAlias( s, NULL, ExeName ? ExeName : "CMD.EXE" );
  406. }
  407. else {
  408. AliasName = s;
  409. }
  410. }
  411. else {
  412. if (fDelete) {
  413. rc = DoAlias( AliasName, NULL, ExeName ? ExeName : "CMD.EXE" );
  414. AliasName = s;
  415. }
  416. else {
  417. rc = DoAlias( AliasName, s, ExeName ? ExeName : "CMD.EXE" );
  418. AliasName = NULL;
  419. }
  420. }
  421. }
  422. }
  423. if (rc == NO_ERROR) {
  424. if (AliasName != NULL) {
  425. if (fDelete) {
  426. rc = DoAlias( AliasName, NULL, ExeName ? ExeName : "CMD.EXE" );
  427. }
  428. else {
  429. rc = DisplayAlias( AliasName, ExeName ? ExeName : "CMD.EXE" );
  430. }
  431. }
  432. else
  433. if (DisplayAllAliases) {
  434. rc = DisplayAliases( ExeName );
  435. }
  436. }
  437. if (ExeName != NULL) {
  438. free( ExeName );
  439. ExeName = NULL;
  440. }
  441. exit( rc );
  442. return rc;
  443. }