Windows NT 4.0 source code leak
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.

271 lines
6.5 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. uuidgen.c {v1.00}
  5. Abstract:
  6. usage: uuidgen [-isonvh?]
  7. i - Output UUID in an IDL interface template
  8. s - Output UUID as an initialized C struct
  9. o<filename> - redirect output to a file, specified immediately after o
  10. n<number> - Number of UUIDs to generate, specified immediately after n
  11. v - display version information about uuidgen
  12. h,? - Display command option summary
  13. This command-line program simply uses the procedure I_UuidStringGenerate
  14. to output one or more UUIDs in one of the formats normal, IDL, or C struct.
  15. This code should build under nt to nt and nmake under os2 to dos.
  16. Requires uuidfmt.c.
  17. Author:
  18. Joev Dubach (t-joevd) 6/11/92
  19. Revision History:
  20. --*/
  21. //
  22. // Defines
  23. //
  24. #define IDL_BIT_FLAG 0x01
  25. #define CSTRUCT_BIT_FLAG 0x02
  26. #define BAD_SWITCH_STR "Invalid Switch Usage: %s\n\n"
  27. #define CANNOT_OPEN_STR "Cannot open output file: %s\n\n"
  28. #define TOO_SMALL_STR "Argument to %s must be an integer greater than 0.\n\n"
  29. #define VERSION_INFO_STR \
  30. "Microsoft UUID Generator (c) 1992. v1.00\n\n"
  31. #define BAD_ARGS_STR "The arguments i and s are mutually exclusive.\n\n"
  32. #define NO_ADDRESS_STR \
  33. "Cannot find the RPC End Point Mapper (RPCSS.EXE); Unable to allocate UUIDs.\n\n"
  34. #define OUT_OF_MEMORY_STR \
  35. "Unable to allocate enough memory to create string.\n\n"
  36. #define LOCAL_ONLY_STR \
  37. "Warning: Unable to determine your network address. The UUID generated is\n" \
  38. "unique on this computer only. It should not be used on another computer.\n"
  39. //
  40. // Inclusions
  41. //
  42. // The order in which these includes are performed seems, after
  43. // extensive testing and analysis, to be highly crucial to an
  44. // NT build.
  45. #include <sysinc.h>
  46. #include <rpc.h>
  47. #include <uuidfmt.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. //
  51. // Function prototypes.
  52. //
  53. #ifdef NTENV
  54. int _CRTAPI1
  55. #else // NTENV
  56. int
  57. #endif // NTENV
  58. main(
  59. int argc,
  60. char **argv
  61. );
  62. void Usage(void);
  63. void ErrorUsageAndExit(void);
  64. void NoErrorUsageAndExit(void);
  65. //
  66. // Global variables
  67. //
  68. FILE * OutputFile = stdout;
  69. #ifdef NTENV
  70. int _CRTAPI1
  71. #else // NTENV
  72. int
  73. #endif // NTENV
  74. main(
  75. int argc,
  76. char **argv
  77. )
  78. {
  79. int ConditionFlags = 0; // Holds conditions IDL_BIT_FLAG and
  80. // CSTRUCT_BIT_FLAG.
  81. char MyUuidString[255]; // The UUID string returned by
  82. // I_UuidStringGenerate.
  83. int NumberOfUuids = 1; // How many to make.
  84. int i; // Current arg#.
  85. int Flag; // UUID Format requested.
  86. RPC_STATUS Result;
  87. int FirstTime = 1;
  88. //
  89. // Parse the command line.
  90. //
  91. for (i=1;argc-i;i++)
  92. {
  93. //
  94. // Make sure arg is in proper format.
  95. //
  96. if ( (argv[i][0] != '/') && (argv[i][0] != '-') )
  97. {
  98. fprintf(stderr, BAD_SWITCH_STR, argv[i]);
  99. ErrorUsageAndExit;
  100. }
  101. //
  102. // Which arg is it?
  103. //
  104. switch (argv[i][1])
  105. {
  106. case 'I':
  107. case 'i':
  108. ConditionFlags |= IDL_BIT_FLAG;
  109. break;
  110. case 'S':
  111. case 's':
  112. ConditionFlags |= CSTRUCT_BIT_FLAG;
  113. break;
  114. case 'O':
  115. case 'o':
  116. OutputFile = fopen(argv[i]+2, "wt");
  117. if (OutputFile == NULL)
  118. {
  119. fprintf(stderr,CANNOT_OPEN_STR, argv[i]+2);
  120. exit(1);
  121. }
  122. break;
  123. case 'N':
  124. case 'n':
  125. NumberOfUuids = atoi(argv[i]+2);
  126. if (NumberOfUuids <= 0)
  127. {
  128. fprintf(stderr, TOO_SMALL_STR, argv[i-1]);
  129. ErrorUsageAndExit();
  130. }
  131. break;
  132. case 'V':
  133. case 'v':
  134. fprintf(OutputFile, VERSION_INFO_STR);
  135. exit(0);
  136. case 'H':
  137. case 'h':
  138. case '?':
  139. NoErrorUsageAndExit();
  140. default:
  141. fprintf(stderr, BAD_SWITCH_STR, argv[i]);
  142. ErrorUsageAndExit();
  143. } // switch
  144. } // for
  145. //
  146. // Fulfill the user's request
  147. //
  148. if ((ConditionFlags & IDL_BIT_FLAG) && (ConditionFlags & CSTRUCT_BIT_FLAG))
  149. {
  150. fprintf(stderr,BAD_ARGS_STR);
  151. ErrorUsageAndExit();
  152. }
  153. if (ConditionFlags & IDL_BIT_FLAG)
  154. {
  155. Flag = UUIDGEN_FORMAT_IDL;
  156. }
  157. else if (ConditionFlags & CSTRUCT_BIT_FLAG)
  158. {
  159. Flag = UUIDGEN_FORMAT_CSTRUCT;
  160. }
  161. else
  162. {
  163. Flag = UUIDGEN_FORMAT_PLAIN;
  164. }
  165. for (;NumberOfUuids;NumberOfUuids--)
  166. {
  167. Result = I_UuidStringGenerate(
  168. Flag,
  169. MyUuidString,
  170. "INTERFACENAME"
  171. );
  172. ASSERT((Result == RPC_S_OK)
  173. || (Result == RPC_S_UUID_LOCAL_ONLY)
  174. || (Result == RPC_S_OUT_OF_MEMORY)
  175. || (Result == RPC_S_UUID_NO_ADDRESS));
  176. switch(Result)
  177. {
  178. case RPC_S_OUT_OF_MEMORY:
  179. fprintf(stderr,OUT_OF_MEMORY_STR);
  180. exit(1);
  181. case RPC_S_UUID_NO_ADDRESS:
  182. fprintf(stderr,NO_ADDRESS_STR);
  183. exit(1);
  184. case RPC_S_UUID_LOCAL_ONLY:
  185. if (FirstTime)
  186. {
  187. fprintf(stderr,LOCAL_ONLY_STR);
  188. FirstTime = 0;
  189. }
  190. // Fall through to the valid case.
  191. case RPC_S_OK:
  192. fprintf(
  193. OutputFile,
  194. MyUuidString
  195. );
  196. } // end switch
  197. } // end for
  198. return(0);
  199. }
  200. void Usage(void)
  201. {
  202. fprintf(OutputFile,"usage: uuidgen [-isonvh?]\n");
  203. fprintf(OutputFile,"i - Output UUID in an IDL interface template\n");
  204. fprintf(OutputFile,"s - Output UUID as an initialized C struct\n");
  205. fprintf(OutputFile,
  206. "o<filename> - redirect output to a file, specified immediately after o\n");
  207. fprintf(OutputFile,
  208. "n<number> - Number of UUIDs to generate, specified immediately after n\n");
  209. fprintf(OutputFile,"v - display version information about uuidgen\n");
  210. fprintf(OutputFile,"h,? - Display command option summary\n");
  211. }
  212. void ErrorUsageAndExit(void)
  213. {
  214. OutputFile = stderr;
  215. Usage();
  216. exit(1);
  217. }
  218. void NoErrorUsageAndExit(void)
  219. {
  220. Usage();
  221. exit(0);
  222. }