comm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* `comm' compare two sorted files line by line.
  2. Copyright (C) 1986 Richard M. Stallman
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously
  27. and appropriately publish on each copy a valid copyright notice
  28. "Copyright (C) 1986 Richard M. Stallman"; and include following the
  29. copyright notice a verbatim copy of the above disclaimer of warranty
  30. and of this License.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be freely distributed
  39. and licensed to all third parties on terms identical to those
  40. contained in this License Agreement (except that you may choose
  41. to grant more extensive warranty protection to third parties,
  42. at your option).
  43. 3. You may copy and distribute this program or any portion of it in
  44. compiled, executable or object code form under the terms of Paragraphs
  45. 1 and 2 above provided that you do the following:
  46. a) cause each such copy to be accompanied by the
  47. corresponding machine-readable source code, which must
  48. be distributed under the terms of Paragraphs 1 and 2 above; or,
  49. b) cause each such copy to be accompanied by a
  50. written offer, with no time limit, to give any third party
  51. free (except for a nominal shipping charge) a machine readable
  52. copy of the corresponding source code, to be distributed
  53. under the terms of Paragraphs 1 and 2 above; or,
  54. c) in the case of a recipient of this program in compiled, executable
  55. or object code form (without the corresponding source code) you
  56. shall cause copies you distribute to be accompanied by a copy
  57. of the written offer of source code which you received along
  58. with the copy you received.
  59. 4. You may not copy, sublicense, distribute or transfer this program
  60. except as expressly provided under this License Agreement. Any attempt
  61. otherwise to copy, sublicense, distribute or transfer this program is void and
  62. your rights to use the program under this License agreement shall be
  63. automatically terminated. However, parties who have received computer
  64. software programs from you with this License Agreement will not have
  65. their licenses terminated so long as such parties remain in full compliance.
  66. In other words, you are welcome to use, share and improve this program.
  67. You are forbidden to forbid anyone else to use, share and improve
  68. what you give them. Help stamp out software-hoarding! */
  69. #include <stdio.h>
  70. /* A `struct linebuffer' is a structure which holds a line of text.
  71. `readline' reads a line from a stream into a linebuffer
  72. and works regardless of the length of the line. */
  73. struct linebuffer
  74. {
  75. long size;
  76. char *buffer;
  77. };
  78. /* Buffers for next available line of file1, file2 */
  79. struct linebuffer lb1, lb2;
  80. /* If nonzero, print lines that are found only in file 1 */
  81. int only_file_1;
  82. /* If nonzero, print lines that are found only in file 2 */
  83. int only_file_2;
  84. /* If nonzero, print lines that are found in both files */
  85. int both;
  86. void initbuffer ();
  87. struct linebuffer *readline ();
  88. void writeline ();
  89. char *concat ();
  90. main(argc, argv)
  91. int argc;
  92. char *argv[];
  93. {
  94. int i;
  95. char *file1, *file2;
  96. only_file_1 = 1;
  97. only_file_2 = 1;
  98. both = 1;
  99. for (i = 1; i < argc; i++)
  100. {
  101. if (argv[i][0] == '-')
  102. {
  103. char *p = argv[i] + 1;
  104. char c;
  105. while (c = *p++)
  106. switch (c)
  107. {
  108. case '1':
  109. only_file_1 = 0;
  110. break;
  111. case '2':
  112. only_file_2 = 0;
  113. break;
  114. case '3':
  115. both = 0;
  116. break;
  117. default:
  118. error ("unrecognized switch %s", argv[i]);
  119. goto argdone;
  120. }
  121. argdone: ;
  122. }
  123. else if (!file1)
  124. file1 = argv[i];
  125. else if (!file2)
  126. file2 = argv[i];
  127. else
  128. {
  129. error ("too many arguments", 0);
  130. break;
  131. }
  132. }
  133. if (!file2)
  134. fatal ("no files specified", 0);
  135. compare_files (file1, file2);
  136. }
  137. /* Initialize a linebuffer for use */
  138. void
  139. initbuffer (linebuffer)
  140. struct linebuffer *linebuffer;
  141. {
  142. linebuffer->size = 200;
  143. linebuffer->buffer = (char *) xmalloc (200);
  144. }
  145. /* Read a line of text from `stream' into `linebuffer'.
  146. Return `linebuffer', except if there is no line to be read
  147. because we are at end of file, return 0. */
  148. struct linebuffer *
  149. readline (linebuffer, stream)
  150. struct linebuffer *linebuffer;
  151. FILE *stream;
  152. {
  153. char *buffer = linebuffer->buffer;
  154. char *p = linebuffer->buffer;
  155. char *end = p + linebuffer->size;
  156. if (feof (stream)) return 0;
  157. while (1)
  158. {
  159. int c = getc (stream);
  160. if (p == end)
  161. {
  162. buffer = (char *) xrealloc (buffer, linebuffer->size *= 2);
  163. p += buffer - linebuffer->buffer;
  164. end += buffer - linebuffer->buffer;
  165. linebuffer->buffer = buffer;
  166. }
  167. if (c < 0 || c == '\n')
  168. {
  169. *p = 0;
  170. break;
  171. }
  172. *p++ = c;
  173. }
  174. if (feof (stream) && p == buffer) return 0;
  175. return linebuffer;
  176. }
  177. /* Output the line in the specified linebuffer
  178. provided the switches say it should be output.
  179. `class' is 1 for a line found only in file 1,
  180. 2 for a line only in file 2, 3 for a line in both. */
  181. void
  182. writeline (line, stream, class)
  183. struct linebuffer *line;
  184. FILE *stream;
  185. int class;
  186. {
  187. switch (class)
  188. {
  189. case 1:
  190. if (!only_file_1) return;
  191. break;
  192. case 2:
  193. if (!only_file_2) return;
  194. /* Skip the tab stop for case 1, if we are printing case 1. */
  195. if (only_file_1)
  196. putc ('\t', stream);
  197. break;
  198. case 3:
  199. if (!both) return;
  200. /* Skip the tab stop for case 1, if we are printing case 1. */
  201. if (only_file_1)
  202. putc ('\t', stream);
  203. /* Skip the tab stop for case 2, if we are printing case 2. */
  204. if (only_file_2)
  205. putc ('\t', stream);
  206. break;
  207. }
  208. fputs (line->buffer, stream);
  209. putc ('\n', stream);
  210. }
  211. /* Assume that each input file is sorted;
  212. merge them and output the result. */
  213. int
  214. compare_files (file1, file2)
  215. char *file1, *file2;
  216. {
  217. struct linebuffer lb1[2];
  218. struct linebuffer *thisline[2];
  219. FILE *streams[2];
  220. char **infiles = &file1;
  221. int i;
  222. /* For each file, we have one linebuffer in lb1. */
  223. /* thisline[i] points to the linebuffer holding the next available line in file i,
  224. or is zero if there are no lines left in that file. */
  225. /* streams[i] holds the input stream for file i. */
  226. /* infiles[i] is the name of file i. */
  227. /* Initialize all that storage */
  228. for (i = 0; i < 2; i++)
  229. {
  230. initbuffer (&lb1[i]);
  231. thisline[i] = &lb1[i];
  232. streams[i] = infiles[i] ? fopen (infiles[i], "r") : stdin;
  233. if (!streams[i])
  234. pfatal_with_name (infiles[i]);
  235. thisline[i] = readline (thisline[i], streams[i]);
  236. }
  237. while (thisline[0] || thisline[1])
  238. {
  239. int order;
  240. /* Compare the next avail lines of the two files. */
  241. if (!thisline[0])
  242. order = 1;
  243. else if (!thisline[1])
  244. order = -1;
  245. else
  246. order = strcmp (thisline[0]->buffer, thisline[1]->buffer);
  247. /* Output the line that is lesser */
  248. if (order == 0)
  249. writeline (thisline[1], stdout, 3);
  250. else if (order > 0)
  251. writeline (thisline[1], stdout, 2);
  252. else
  253. writeline (thisline[0], stdout, 1);
  254. /* Step the file the line came from. If the files match, step both files. */
  255. if (order >= 0)
  256. thisline[1] = readline (thisline[1], streams[1]);
  257. if (order <= 0)
  258. thisline[0] = readline (thisline[0], streams[0]);
  259. }
  260. /* Free all storage and close all input streams */
  261. for (i = 0; i < 2; i++)
  262. {
  263. if (infiles[i])
  264. fclose (streams[i]);
  265. free (lb1[i].buffer);
  266. }
  267. }
  268. /* Print error message and exit. */
  269. fatal (s1, s2)
  270. char *s1, *s2;
  271. {
  272. error (s1, s2);
  273. exit (1);
  274. }
  275. /* Print error message. `s1' is printf control string, `s2' is arg for it. */
  276. error (s1, s2)
  277. char *s1, *s2;
  278. {
  279. printf ("comm: ");
  280. printf (s1, s2);
  281. printf ("\n");
  282. }
  283. pfatal_with_name (name)
  284. char *name;
  285. {
  286. extern int errno, sys_nerr;
  287. extern char *sys_errlist[];
  288. char *s;
  289. if (errno < sys_nerr)
  290. s = concat ("", sys_errlist[errno], " for %s");
  291. else
  292. s = "cannot open %s";
  293. fatal (s, name);
  294. }
  295. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
  296. char *
  297. concat (s1, s2, s3)
  298. char *s1, *s2, *s3;
  299. {
  300. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  301. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  302. strcpy (result, s1);
  303. strcpy (result + len1, s2);
  304. strcpy (result + len1 + len2, s3);
  305. *(result + len1 + len2 + len3) = 0;
  306. return result;
  307. }
  308. /* Like malloc but get fatal error if memory is exhausted. */
  309. int
  310. xmalloc (size)
  311. int size;
  312. {
  313. int result = malloc (size);
  314. if (!result)
  315. fatal ("virtual memory exhausted", 0);
  316. return result;
  317. }
  318. int
  319. xrealloc (ptr, size)
  320. char *ptr;
  321. int size;
  322. {
  323. int result = realloc (ptr, size);
  324. if (!result)
  325. fatal ("virtual memory exhausted");
  326. return result;
  327. }