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.

293 lines
7.5 KiB

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