cmp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. cmp.c
  5. Abstract:
  6. This module implements the cmp (compare utility).
  7. Author:
  8. Evan Green 4-Sep-2013
  9. Environment:
  10. POSIX
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/lib/types.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <getopt.h>
  19. #include <libgen.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include "swlib.h"
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. #define CMP_VERSION_MAJOR 1
  28. #define CMP_VERSION_MINOR 0
  29. #define CMP_USAGE \
  30. "usage: cmp [-l | -s] file1 file2\n" \
  31. "The cmp utility compares two files. It writes no output if both files \n" \
  32. "are the same. Under default options, it writes the byte and line \n" \
  33. "number at which the first difference occurred. Options are:\n" \
  34. " -l, --verbose -- Write the byte number (decimal) and the differing \n" \
  35. " bytes (octal) for each difference.\n" \
  36. " -s, --quiet, --silent -- Write nothing for differing files. Return \n" \
  37. " exit status only.\n" \
  38. " --help -- Show this help text and exit.\n" \
  39. " --version -- Show the application version information and exit.\n\n" \
  40. "The operands are paths to files to compare. If - is supplied for \n" \
  41. "either file, standard in will be used.\n" \
  42. "The cmp utility returns 0 if the files are identical, 1 if the files \n" \
  43. "are different or of different size, and >1 if an error occurred.\n\n"
  44. #define CMP_OPTIONS_STRING "ls"
  45. //
  46. // Define cmp options.
  47. //
  48. //
  49. // Set this option to print each byte difference.
  50. //
  51. #define CMP_OPTION_VERBOSE 0x00000001
  52. //
  53. // Set this option to print nothing.
  54. //
  55. #define CMP_OPTION_SILENT 0x00000002
  56. //
  57. // ------------------------------------------------------ Data Type Definitions
  58. //
  59. //
  60. // ----------------------------------------------- Internal Function Prototypes
  61. //
  62. //
  63. // -------------------------------------------------------------------- Globals
  64. //
  65. struct option CmpLongOptions[] = {
  66. {"verbose", no_argument, 0, 'l'},
  67. {"quiet", no_argument, 0, 's'},
  68. {"silent", no_argument, 0, 's'},
  69. {"help", no_argument, 0, 'h'},
  70. {"version", no_argument, 0, 'V'},
  71. {NULL, 0, 0, 0},
  72. };
  73. //
  74. // ------------------------------------------------------------------ Functions
  75. //
  76. INT
  77. CmpMain (
  78. INT ArgumentCount,
  79. CHAR **Arguments
  80. )
  81. /*++
  82. Routine Description:
  83. This routine is the main entry point for the cmp (compare) utility.
  84. Arguments:
  85. ArgumentCount - Supplies the number of command line arguments the program
  86. was invoked with.
  87. Arguments - Supplies a tokenized array of command line arguments.
  88. Return Value:
  89. Returns an integer exit code. 0 for success, nonzero otherwise.
  90. --*/
  91. {
  92. ULONG ArgumentIndex;
  93. INT Character1;
  94. INT Character2;
  95. ULONGLONG CharacterNumber;
  96. FILE *File1;
  97. FILE *File2;
  98. ULONGLONG LineNumber;
  99. INT Option;
  100. ULONG Options;
  101. PSTR Path1;
  102. PSTR Path2;
  103. ULONG SourceCount;
  104. int Status;
  105. File1 = NULL;
  106. File2 = NULL;
  107. SourceCount = 0;
  108. Options = COPY_OPTION_FOLLOW_OPERAND_LINKS;
  109. //
  110. // Process the control arguments.
  111. //
  112. while (TRUE) {
  113. Option = getopt_long(ArgumentCount,
  114. Arguments,
  115. CMP_OPTIONS_STRING,
  116. CmpLongOptions,
  117. NULL);
  118. if (Option == -1) {
  119. break;
  120. }
  121. if ((Option == '?') || (Option == ':')) {
  122. Status = 2;
  123. goto MainEnd;
  124. }
  125. switch (Option) {
  126. case 'l':
  127. Options |= CMP_OPTION_VERBOSE;
  128. Options &= ~CMP_OPTION_SILENT;
  129. break;
  130. case 's':
  131. Options |= CMP_OPTION_SILENT;
  132. Options &= ~CMP_OPTION_VERBOSE;
  133. break;
  134. case 'V':
  135. SwPrintVersion(CMP_VERSION_MAJOR, CMP_VERSION_MINOR);
  136. return 2;
  137. case 'h':
  138. printf(CMP_USAGE);
  139. return 2;
  140. default:
  141. assert(FALSE);
  142. Status = 2;
  143. goto MainEnd;
  144. }
  145. }
  146. ArgumentIndex = optind;
  147. if (ArgumentIndex > ArgumentCount) {
  148. ArgumentIndex = ArgumentCount;
  149. }
  150. SourceCount = ArgumentCount - ArgumentIndex;
  151. //
  152. // Fail if there were not enough arguments.
  153. //
  154. if ((SourceCount != 2) && (SourceCount != 1)) {
  155. SwPrintError(0,
  156. NULL,
  157. "One or two arguments expected. Try --help for usage");
  158. return 2;
  159. }
  160. Path1 = Arguments[ArgumentIndex];
  161. Path2 = NULL;
  162. if (SourceCount == 2) {
  163. Path2 = Arguments[ArgumentIndex + 1];
  164. }
  165. //
  166. // Open up the files.
  167. //
  168. if (strcmp(Path1, "-") == 0) {
  169. Path1 = "<stdin>";
  170. File1 = stdin;
  171. } else {
  172. File1 = fopen(Path1, "rb");
  173. if (File1 == NULL) {
  174. Status = errno;
  175. SwPrintError(Status, Path1, "Unable to open");
  176. goto MainEnd;
  177. }
  178. }
  179. if ((Path2 == NULL) || (strcmp(Path2, "-") == 0)) {
  180. Path2 = "<stdin>";
  181. File2 = stdin;
  182. } else {
  183. File2 = fopen(Path2, "rb");
  184. if (File2 == NULL) {
  185. Status = errno;
  186. SwPrintError(Status, Path2, "Unable to open");
  187. goto MainEnd;
  188. }
  189. }
  190. //
  191. // If standard in was selected for anything, then change standard in to
  192. // binary mode.
  193. //
  194. if ((File1 == stdin) || (File2 == stdin)) {
  195. Status = SwSetBinaryMode(fileno(stdin), TRUE);
  196. if (Status != 0) {
  197. SwPrintError(Status, NULL, "Failed to set stdin binary mode.");
  198. goto MainEnd;
  199. }
  200. }
  201. //
  202. // Perform the comparison.
  203. //
  204. CharacterNumber = 1;
  205. LineNumber = 1;
  206. Status = 0;
  207. while (TRUE) {
  208. Character1 = fgetc(File1);
  209. Character2 = fgetc(File2);
  210. //
  211. // Handle one or both of the files ending.
  212. //
  213. if (Character1 == EOF) {
  214. if (Character2 != EOF) {
  215. if ((Options & CMP_OPTION_SILENT) == 0) {
  216. fprintf(stderr, "cmp: EOF on %s\n", Path1);
  217. }
  218. Status = 1;
  219. }
  220. break;
  221. } else if (Character2 == EOF) {
  222. if ((Options & CMP_OPTION_SILENT) == 0) {
  223. fprintf(stderr, "cmp: EOF on %s\n", Path2);
  224. }
  225. Status = 1;
  226. break;
  227. }
  228. //
  229. // Neither character is EOF. If they're different, report it.
  230. //
  231. if (Character1 != Character2) {
  232. Status = 1;
  233. if ((Options & CMP_OPTION_VERBOSE) != 0) {
  234. printf("%I64d %o %o\n",
  235. CharacterNumber,
  236. Character1,
  237. Character2);
  238. } else {
  239. if ((Options & CMP_OPTION_SILENT) == 0) {
  240. printf("%s %s differ: char %I64d, line %I64d\n",
  241. Path1,
  242. Path2,
  243. CharacterNumber,
  244. LineNumber);
  245. }
  246. break;
  247. }
  248. }
  249. //
  250. // Advance the character and line numbers.
  251. //
  252. CharacterNumber += 1;
  253. if (Character1 == '\n') {
  254. LineNumber += 1;
  255. }
  256. }
  257. //
  258. // Return an error if either of the streams are funky.
  259. //
  260. if ((ferror(File1) != 0) || (ferror(File2) != 0)) {
  261. Status = errno;
  262. if (Status == 0) {
  263. Status = 2;
  264. }
  265. }
  266. MainEnd:
  267. if ((File1 != NULL) && (File1 != stdin)) {
  268. fclose(File1);
  269. }
  270. if ((File2 != NULL) && (File2 != stdin)) {
  271. fclose(File2);
  272. }
  273. return Status;
  274. }
  275. //
  276. // --------------------------------------------------------- Internal Functions
  277. //