comparing file in c

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 13
Reputation: kpnprakash is an unknown quantity at this point 
Solved Threads: 1
kpnprakash kpnprakash is offline Offline
Newbie Poster

comparing file in c

 
0
  #1
Mar 26th, 2008
hello,
i am developing a c coding for comparing two txt files(one key file with the transcripted or duplicete file) to figure out the wrong text in the transcripted file.Can anyone help me that how to develop the codes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: comparing file in c

 
0
  #2
Mar 26th, 2008
>>Can anyone help me that how to develop the codes.

Sure thing -- start here
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. // put your code here
  6. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: comparing file in c

 
0
  #3
Mar 27th, 2008
here, try this... just remove the comments, change some variable names, sign it and turn it in. trust me, it works. you'll get an "A" for sure.

  1. /* diff - compare files line by line
  2.  
  3.   Copyright (C) 1988, 1989, 1992, 1993, 1994, 1996, 1998, 2001, 2002,
  4.   2004 Free Software Foundation, Inc.
  5.  
  6.   This file is part of GNU DIFF.
  7.  
  8.   GNU DIFF is free software; you can redistribute it and/or modify
  9.   it under the terms of the GNU General Public License as published by
  10.   the Free Software Foundation; either version 2, or (at your option)
  11.   any later version.
  12.  
  13.   GNU DIFF is distributed in the hope that it will be useful,
  14.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16.   See the GNU General Public License for more details.
  17.  
  18.   You should have received a copy of the GNU General Public License
  19.   along with GNU DIFF; see the file COPYING.
  20.   If not, write to the Free Software Foundation,
  21.   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  22.  
  23. #define GDIFF_MAIN
  24. #include "diff.h"
  25. #include "paths.h"
  26. #include <c-stack.h>
  27. #include <dirname.h>
  28. #include <error.h>
  29. #include <exclude.h>
  30. #include <exit.h>
  31. #include <exitfail.h>
  32. #include <file-type.h>
  33. #include <fnmatch.h>
  34. #include <getopt.h>
  35. #include <hard-locale.h>
  36. #include <posixver.h>
  37. #include <prepargs.h>
  38. #include <quotesys.h>
  39. #include <setmode.h>
  40. #include <version-etc.h>
  41. #include <xalloc.h>
  42.  
  43. #ifndef GUTTER_WIDTH_MINIMUM
  44. # define GUTTER_WIDTH_MINIMUM 3
  45. #endif
  46.  
  47. struct regexp_list
  48. {
  49. char *regexps; /* chars representing disjunction of the regexps */
  50. size_t len; /* chars used in `regexps' */
  51. size_t size; /* size malloc'ed for `regexps'; 0 if not malloc'ed */
  52. bool multiple_regexps;/* Does `regexps' represent a disjunction? */
  53. struct re_pattern_buffer *buf;
  54. };
  55.  
  56. static int compare_files (struct comparison const *, char const *, char const *);
  57. static void add_regexp (struct regexp_list *, char const *);
  58. static void summarize_regexp_list (struct regexp_list *);
  59. static void specify_style (enum output_style);
  60. static void specify_value (char const **, char const *, char const *);
  61. static void try_help (char const *, char const *) __attribute__((noreturn));
  62. static void check_stdout (void);
  63. static void usage (void);
  64.  
  65. /* If comparing directories, compare their common subdirectories
  66.   recursively. */
  67. static bool recursive;
  68.  
  69. /* In context diffs, show previous lines that match these regexps. */
  70. static struct regexp_list function_regexp_list;
  71.  
  72. /* Ignore changes affecting only lines that match these regexps. */
  73. static struct regexp_list ignore_regexp_list;
  74.  
  75. #if HAVE_SETMODE_DOS
  76. /* Use binary I/O when reading and writing data (--binary).
  77.   On POSIX hosts, this has no effect. */
  78. static bool binary;
  79. #else
  80. enum { binary = true };
  81. #endif
  82.  
  83. /* When comparing directories, if a file appears only in one
  84.   directory, treat it as present but empty in the other (-N).
  85.   Then `patch' would create the file with appropriate contents. */
  86. static bool new_file;
  87.  
  88. /* When comparing directories, if a file appears only in the second
  89.   directory of the two, treat it as present but empty in the other
  90.   (--unidirectional-new-file).
  91.   Then `patch' would create the file with appropriate contents. */
  92. static bool unidirectional_new_file;
  93.  
  94. /* Report files compared that are the same (-s).
  95.   Normally nothing is output when that happens. */
  96. static bool report_identical_files;
  97.  
  98.  
  99. /* Return a string containing the command options with which diff was invoked.
  100.   Spaces appear between what were separate ARGV-elements.
  101.   There is a space at the beginning but none at the end.
  102.   If there were no options, the result is an empty string.
  103.  
  104.   Arguments: OPTIONVEC, a vector containing separate ARGV-elements, and COUNT,
  105.   the length of that vector. */
  106.  
  107. static char *
  108. option_list (char **optionvec, int count)
  109. {
  110. int i;
  111. size_t size = 1;
  112. char *result;
  113. char *p;
  114.  
  115. for (i = 0; i < count; i++)
  116. size += 1 + quote_system_arg ((char *) 0, optionvec[i]);
  117.  
  118. p = result = xmalloc (size);
  119.  
  120. for (i = 0; i < count; i++)
  121. {
  122. *p++ = ' ';
  123. p += quote_system_arg (p, optionvec[i]);
  124. }
  125.  
  126. *p = 0;
  127. return result;
  128. }
  129.  
  130.  
  131. /* Return an option value suitable for add_exclude. */
  132.  
  133. static int
  134. exclude_options (void)
  135. {
  136. return EXCLUDE_WILDCARDS | (ignore_file_name_case ? FNM_CASEFOLD : 0);
  137. }
  138. static char const shortopts[] =
  139. "0123456789abBcC:dD:eEfF:hHiI:lL:nNpPqrsS:tTuU:vwW:x:X:y";
  140.  
  141. /* Values for long options that do not have single-letter equivalents. */
  142. enum
  143. {
  144. BINARY_OPTION = CHAR_MAX + 1,
  145. FROM_FILE_OPTION,
  146. HELP_OPTION,
  147. HORIZON_LINES_OPTION,
  148. IGNORE_FILE_NAME_CASE_OPTION,
  149. INHIBIT_HUNK_MERGE_OPTION,
  150. LEFT_COLUMN_OPTION,
  151. LINE_FORMAT_OPTION,
  152. NO_IGNORE_FILE_NAME_CASE_OPTION,
  153. NORMAL_OPTION,
  154. SDIFF_MERGE_ASSIST_OPTION,
  155. STRIP_TRAILING_CR_OPTION,
  156. SUPPRESS_COMMON_LINES_OPTION,
  157. TABSIZE_OPTION,
  158. TO_FILE_OPTION,
  159.  
  160. /* These options must be in sequence. */
  161. UNCHANGED_LINE_FORMAT_OPTION,
  162. OLD_LINE_FORMAT_OPTION,
  163. NEW_LINE_FORMAT_OPTION,
  164.  
  165. /* These options must be in sequence. */
  166. UNCHANGED_GROUP_FORMAT_OPTION,
  167. OLD_GROUP_FORMAT_OPTION,
  168. NEW_GROUP_FORMAT_OPTION,
  169. CHANGED_GROUP_FORMAT_OPTION
  170. };
  171.  
  172. static char const group_format_option[][sizeof "--unchanged-group-format"] =
  173. {
  174. "--unchanged-group-format",
  175. "--old-group-format",
  176. "--new-group-format",
  177. "--changed-group-format"
  178. };
  179.  
  180. static char const line_format_option[][sizeof "--unchanged-line-format"] =
  181. {
  182. "--unchanged-line-format",
  183. "--old-line-format",
  184. "--new-line-format"
  185. };
  186.  
  187. static struct option const longopts[] =
  188. {
  189. {"binary", 0, 0, BINARY_OPTION},
  190. {"brief", 0, 0, 'q'},
  191. {"changed-group-format", 1, 0, CHANGED_GROUP_FORMAT_OPTION},
  192. {"context", 2, 0, 'C'},
  193. {"ed", 0, 0, 'e'},
  194. {"exclude", 1, 0, 'x'},
  195. {"exclude-from", 1, 0, 'X'},
  196. {"expand-tabs", 0, 0, 't'},
  197. {"forward-ed", 0, 0, 'f'},
  198. {"from-file", 1, 0, FROM_FILE_OPTION},
  199. {"help", 0, 0, HELP_OPTION},
  200. {"horizon-lines", 1, 0, HORIZON_LINES_OPTION},
  201. {"ifdef", 1, 0, 'D'},
  202. {"ignore-all-space", 0, 0, 'w'},
  203. {"ignore-blank-lines", 0, 0, 'B'},
  204. {"ignore-case", 0, 0, 'i'},
  205. {"ignore-file-name-case", 0, 0, IGNORE_FILE_NAME_CASE_OPTION},
  206. {"ignore-matching-lines", 1, 0, 'I'},
  207. {"ignore-space-change", 0, 0, 'b'},
  208. {"ignore-tab-expansion", 0, 0, 'E'},
  209. {"inhibit-hunk-merge", 0, 0, INHIBIT_HUNK_MERGE_OPTION},
  210. {"initial-tab", 0, 0, 'T'},
  211. {"label", 1, 0, 'L'},
  212. {"left-column", 0, 0, LEFT_COLUMN_OPTION},
  213. {"line-format", 1, 0, LINE_FORMAT_OPTION},
  214. {"minimal", 0, 0, 'd'},
  215. {"new-file", 0, 0, 'N'},
  216. {"new-group-format", 1, 0, NEW_GROUP_FORMAT_OPTION},
  217. {"new-line-format", 1, 0, NEW_LINE_FORMAT_OPTION},
  218. {"no-ignore-file-name-case", 0, 0, NO_IGNORE_FILE_NAME_CASE_OPTION},
  219. {"normal", 0, 0, NORMAL_OPTION},
  220. {"old-group-format", 1, 0, OLD_GROUP_FORMAT_OPTION},
  221. {"old-line-format", 1, 0, OLD_LINE_FORMAT_OPTION},
  222. {"paginate", 0, 0, 'l'},
  223. {"rcs", 0, 0, 'n'},
  224. {"recursive", 0, 0, 'r'},
  225. {"report-identical-files", 0, 0, 's'},
  226. {"sdiff-merge-assist", 0, 0, SDIFF_MERGE_ASSIST_OPTION},
  227. {"show-c-function", 0, 0, 'p'},
  228. {"show-function-line", 1, 0, 'F'},
  229. {"side-by-side", 0, 0, 'y'},
  230. {"speed-large-files", 0, 0, 'H'},
  231. {"starting-file", 1, 0, 'S'},
  232. {"strip-trailing-cr", 0, 0, STRIP_TRAILING_CR_OPTION},
  233. {"suppress-common-lines", 0, 0, SUPPRESS_COMMON_LINES_OPTION},
  234. {"tabsize", 1, 0, TABSIZE_OPTION},
  235. {"text", 0, 0, 'a'},
  236. {"to-file", 1, 0, TO_FILE_OPTION},
  237. {"unchanged-group-format", 1, 0, UNCHANGED_GROUP_FORMAT_OPTION},
  238. {"unchanged-line-format", 1, 0, UNCHANGED_LINE_FORMAT_OPTION},
  239. {"unidirectional-new-file", 0, 0, 'P'},
  240. {"unified", 2, 0, 'U'},
  241. {"version", 0, 0, 'v'},
  242. {"width", 1, 0, 'W'},
  243. {0, 0, 0, 0}
  244. };
  245.  
  246. int
  247. main (int argc, char **argv)
  248. {
  249. int exit_status = EXIT_SUCCESS;
  250. int c;
  251. int i;
  252. int prev = -1;
  253. lin ocontext = -1;
  254. bool explicit_context = false;
  255. size_t width = 0;
  256. bool show_c_function = false;
  257. char const *from_file = 0;
  258. char const *to_file = 0;
  259. uintmax_t numval;
  260. char *numend;
  261.  
  262. /* Do our initializations. */
  263. exit_failure = 2;
  264. initialize_main (&argc, &argv);
  265. program_name = argv[0];
  266. setlocale (LC_ALL, "");
  267. bindtextdomain (PACKAGE, LOCALEDIR);
  268. textdomain (PACKAGE);
  269. c_stack_action (0);
  270. function_regexp_list.buf = &function_regexp;
  271. ignore_regexp_list.buf = &ignore_regexp;
  272. re_set_syntax (RE_SYNTAX_GREP | RE_NO_POSIX_BACKTRACKING);
  273. excluded = new_exclude ();
  274.  
  275. /* Decode the options. */
  276.  
  277. while ((c = getopt_long (argc, argv, shortopts, longopts, 0)) != -1)
  278. {
  279. switch (c)
  280. {
  281. case 0:
  282. break;
  283.  
  284. case '0':
  285. case '1':
  286. case '2':
  287. case '3':
  288. case '4':
  289. case '5':
  290. case '6':
  291. case '7':
  292. case '8':
  293. case '9':
  294. if (! ISDIGIT (prev))
  295. ocontext = c - '0';
  296. else if (LIN_MAX / 10 < ocontext
  297. || ((ocontext = 10 * ocontext + c - '0') < 0))
  298. ocontext = LIN_MAX;
  299. break;
  300.  
  301. case 'a':
  302. text = true;
  303. break;
  304.  
  305. case 'b':
  306. if (ignore_white_space < IGNORE_SPACE_CHANGE)
  307. ignore_white_space = IGNORE_SPACE_CHANGE;
  308. break;
  309.  
  310. case 'B':
  311. ignore_blank_lines = true;
  312. break;
  313.  
  314. case 'C':
  315. case 'U':
  316. {
  317. if (optarg)
  318. {
  319. numval = strtoumax (optarg, &numend, 10);
  320. if (*numend)
  321. try_help ("invalid context length `%s'", optarg);
  322. if (LIN_MAX < numval)
  323. numval = LIN_MAX;
  324. }
  325. else
  326. numval = 3;
  327.  
  328. specify_style (c == 'U' ? OUTPUT_UNIFIED : OUTPUT_CONTEXT);
  329. if (context < numval)
  330. context = numval;
  331. explicit_context = true;
  332. }
  333. break;
  334.  
  335. case 'c':
  336. specify_style (OUTPUT_CONTEXT);
  337. if (context < 3)
  338. context = 3;
  339. break;
  340.  
  341. case 'd':
  342. minimal = true;
  343. break;
  344.  
  345. case 'D':
  346. specify_style (OUTPUT_IFDEF);
  347. {
  348. static char const C_ifdef_group_formats[] =
  349. "%%=%c#ifndef %s\n%%<#endif /* ! %s */\n%c#ifdef %s\n%%>#endif /* %s */\n%c#ifndef %s\n%%<#else /* %s */\n%%>#endif /* %s */\n";
  350. char *b = xmalloc (sizeof C_ifdef_group_formats
  351. + 7 * strlen (optarg) - 14 /* 7*"%s" */
  352. - 8 /* 5*"%%" + 3*"%c" */);
  353. sprintf (b, C_ifdef_group_formats,
  354. 0,
  355. optarg, optarg, 0,
  356. optarg, optarg, 0,
  357. optarg, optarg, optarg);
  358. for (i = 0; i < sizeof group_format / sizeof *group_format; i++)
  359. {
  360. specify_value (&group_format[i], b, "-D");
  361. b += strlen (b) + 1;
  362. }
  363. }
  364. break;
  365.  
  366. case 'e':
  367. specify_style (OUTPUT_ED);
  368. break;
  369.  
  370. case 'E':
  371. if (ignore_white_space < IGNORE_TAB_EXPANSION)
  372. ignore_white_space = IGNORE_TAB_EXPANSION;
  373. break;
  374.  
  375. case 'f':
  376. specify_style (OUTPUT_FORWARD_ED);
  377. break;
  378.  
  379. case 'F':
  380. add_regexp (&function_regexp_list, optarg);
  381. break;
  382.  
  383. case 'h':
  384. /* Split the files into chunks for faster processing.
  385. Usually does not change the result.
  386.  
  387. This currently has no effect. */
  388. break;
  389.  
  390. case 'H':
  391. speed_large_files = true;
  392. break;
  393.  
  394. case 'i':
  395. ignore_case = true;
  396. break;
  397.  
  398. case 'I':
  399. add_regexp (&ignore_regexp_list, optarg);
  400. break;
  401.  
  402. case 'l':
  403. if (!pr_program[0])
  404. try_help ("pagination not supported on this host", 0);
  405. paginate = true;
  406. #ifdef SIGCHLD
  407. /* Pagination requires forking and waiting, and
  408. System V fork+wait does not work if SIGCHLD is ignored. */
  409. signal (SIGCHLD, SIG_DFL);
  410. #endif
  411. break;
  412.  
  413. case 'L':
  414. if (!file_label[0])
  415. file_label[0] = optarg;
  416. else if (!file_label[1])
  417. file_label[1] = optarg;
  418. else
  419. fatal ("too many file label options");
  420. break;
  421.  
  422. case 'n':
  423. specify_style (OUTPUT_RCS);
  424. break;
  425.  
  426. case 'N':
  427. new_file = true;
  428. break;
  429.  
  430. case 'p':
  431. show_c_function = true;
  432. add_regexp (&function_regexp_list, "^[[:alpha:]$_]");
  433. break;
  434.  
  435. case 'P':
  436. unidirectional_new_file = true;
  437. break;
  438.  
  439. case 'q':
  440. brief = true;
  441. break;
  442.  
  443. case 'r':
  444. recursive = true;
  445. break;
  446.  
  447. case 's':
  448. report_identical_files = true;
  449. break;
  450.  
  451. case 'S':
  452. specify_value (&starting_file, optarg, "-S");
  453. break;
  454.  
  455. case 't':
  456. expand_tabs = true;
  457. break;
  458.  
  459. case 'T':
  460. initial_tab = true;
  461. break;
  462.  
  463. case 'u':
  464. specify_style (OUTPUT_UNIFIED);
  465. if (context < 3)
  466. context = 3;
  467. break;
  468.  
  469. case 'v':
  470. version_etc (stdout, "diff", PACKAGE_NAME, PACKAGE_VERSION,
  471. "Paul Eggert", "Mike Haertel", "David Hayes",
  472. "Richard Stallman", "Len Tower", (char *) 0);
  473. check_stdout ();
  474. return EXIT_SUCCESS;
  475.  
  476. case 'w':
  477. ignore_white_space = IGNORE_ALL_SPACE;
  478. break;
  479.  
  480. case 'x':
  481. add_exclude (excluded, optarg, exclude_options ());
  482. break;
  483.  
  484. case 'X':
  485. if (add_exclude_file (add_exclude, excluded, optarg,
  486. exclude_options (), '\n'))
  487. pfatal_with_name (optarg);
  488. break;
  489.  
  490. case 'y':
  491. specify_style (OUTPUT_SDIFF);
  492. break;
  493.  
  494. case 'W':
  495. numval = strtoumax (optarg, &numend, 10);
  496. if (! (0 < numval && numval <= SIZE_MAX) || *numend)
  497. try_help ("invalid width `%s'", optarg);
  498. if (width != numval)
  499. {
  500. if (width)
  501. fatal ("conflicting width options");
  502. width = numval;
  503. }
  504. break;
  505.  
  506. case BINARY_OPTION:
  507. #if HAVE_SETMODE_DOS
  508. binary = true;
  509. set_binary_mode (STDOUT_FILENO, true);
  510. #endif
  511. break;
  512.  
  513. case FROM_FILE_OPTION:
  514. specify_value (&from_file, optarg, "--from-file");
  515. break;
  516.  
  517. case HELP_OPTION:
  518. usage ();
  519. check_stdout ();
  520. return EXIT_SUCCESS;
  521.  
  522. case HORIZON_LINES_OPTION:
  523. numval = strtoumax (optarg, &numend, 10);
  524. if (*numend)
  525. try_help ("invalid horizon length `%s'", optarg);
  526. horizon_lines = MAX (horizon_lines, MIN (numval, LIN_MAX));
  527. break;
  528.  
  529. case IGNORE_FILE_NAME_CASE_OPTION:
  530. ignore_file_name_case = true;
  531. break;
  532.  
  533. case INHIBIT_HUNK_MERGE_OPTION:
  534. /* This option is obsolete, but accept it for backward
  535.   compatibility. */
  536. break;
  537.  
  538. case LEFT_COLUMN_OPTION:
  539. left_column = true;
  540. break;
  541.  
  542. case LINE_FORMAT_OPTION:
  543. specify_style (OUTPUT_IFDEF);
  544. for (i = 0; i < sizeof line_format / sizeof *line_format; i++)
  545. specify_value (&line_format[i], optarg, "--line-format");
  546. break;
  547.  
  548. case NO_IGNORE_FILE_NAME_CASE_OPTION:
  549. ignore_file_name_case = false;
  550. break;
  551.  
  552. case NORMAL_OPTION:
  553. specify_style (OUTPUT_NORMAL);
  554. break;
  555.  
  556. case SDIFF_MERGE_ASSIST_OPTION:
  557. specify_style (OUTPUT_SDIFF);
  558. sdiff_merge_assist = true;
  559. break;
  560.  
  561. case STRIP_TRAILING_CR_OPTION:
  562. strip_trailing_cr = true;
  563. break;
  564.  
  565. case SUPPRESS_COMMON_LINES_OPTION:
  566. suppress_common_lines = true;
  567. break;
  568.  
  569. case TABSIZE_OPTION:
  570. numval = strtoumax (optarg, &numend, 10);
  571. if (! (0 < numval && numval <= SIZE_MAX) || *numend)
  572. try_help ("invalid tabsize `%s'", optarg);
  573. if (tabsize != numval)
  574. {
  575. if (tabsize)
  576. fatal ("conflicting tabsize options");
  577. tabsize = numval;
  578. }
  579. break;
  580.  
  581. case TO_FILE_OPTION:
  582. specify_value (&to_file, optarg, "--to-file");
  583. break;
  584.  
  585. case UNCHANGED_LINE_FORMAT_OPTION:
  586. case OLD_LINE_FORMAT_OPTION:
  587. case NEW_LINE_FORMAT_OPTION:
  588. specify_style (OUTPUT_IFDEF);
  589. c -= UNCHANGED_LINE_FORMAT_OPTION;
  590. specify_value (&line_format[c], optarg, line_format_option[c]);
  591. break;
  592.  
  593. case UNCHANGED_GROUP_FORMAT_OPTION:
  594. case OLD_GROUP_FORMAT_OPTION:
  595. case NEW_GROUP_FORMAT_OPTION:
  596. case CHANGED_GROUP_FORMAT_OPTION:
  597. specify_style (OUTPUT_IFDEF);
  598. c -= UNCHANGED_GROUP_FORMAT_OPTION;
  599. specify_value (&group_format[c], optarg, group_format_option[c]);
  600. break;
  601.  
  602. default:
  603. try_help (0, 0);
  604. }
  605. prev = c;
  606. }
  607.  
  608. if (output_style == OUTPUT_UNSPECIFIED)
  609. {
  610. if (show_c_function)
  611. {
  612. specify_style (OUTPUT_CONTEXT);
  613. if (ocontext < 0)
  614. context = 3;
  615. }
  616. else
  617. specify_style (OUTPUT_NORMAL);
  618. }
  619.  
  620. if (output_style != OUTPUT_CONTEXT || hard_locale (LC_TIME))
  621. {
  622. #ifdef ST_MTIM_NSEC
  623. time_format = "%Y-%m-%d %H:%M:%S.%N %z";
  624. #else
  625. time_format = "%Y-%m-%d %H:%M:%S %z";
  626. #endif
  627. }
  628. else
  629. {
  630. /* See POSIX 1003.1-2001 for this format. */
  631. time_format = "%a %b %e %T %Y";
  632. }
  633.  
  634. if (0 <= ocontext)
  635. {
  636. bool modern_usage = 200112 <= posix2_version ();
  637.  
  638. if ((output_style == OUTPUT_CONTEXT
  639. || output_style == OUTPUT_UNIFIED)
  640. && (context < ocontext
  641. || (ocontext < context && ! explicit_context)))
  642. {
  643. if (modern_usage)
  644. {
  645. error (0, 0,
  646. _("`-%ld' option is obsolete; use `-%c %ld'"),
  647. (long int) ocontext,
  648. output_style == OUTPUT_CONTEXT ? 'C' : 'U',
  649. (long int) ocontext);
  650. try_help (0, 0);
  651. }
  652. context = ocontext;
  653. }
  654. else
  655. {
  656. if (modern_usage)
  657. {
  658. error (0, 0, _("`-%ld' option is obsolete; omit it"),
  659. (long int) ocontext);
  660. try_help (0, 0);
  661. }
  662. }
  663. }
  664.  
  665. if (! tabsize)
  666. tabsize = 8;
  667. if (! width)
  668. width = 130;
  669.  
  670. {
  671. /* Maximize first the half line width, and then the gutter width,
  672.   according to the following constraints:
  673.  
  674. 1. Two half lines plus a gutter must fit in a line.
  675. 2. If the half line width is nonzero:
  676. a. The gutter width is at least GUTTER_WIDTH_MINIMUM.
  677. b. If tabs are not expanded to spaces,
  678. a half line plus a gutter is an integral number of tabs,
  679. so that tabs in the right column line up. */
  680.  
  681. intmax_t t = expand_tabs ? 1 : tabsize;
  682. intmax_t w = width;
  683. intmax_t off = (w + t + GUTTER_WIDTH_MINIMUM) / (2 * t) * t;
  684. sdiff_half_width = MAX (0, MIN (off - GUTTER_WIDTH_MINIMUM, w - off)),
  685. sdiff_column2_offset = sdiff_half_width ? off : w;
  686. }
  687.  
  688. /* Make the horizon at least as large as the context, so that
  689.   shift_boundaries has more freedom to shift the first and last hunks. */
  690. if (horizon_lines < context)
  691. horizon_lines = context;
  692.  
  693. summarize_regexp_list (&function_regexp_list);
  694. summarize_regexp_list (&ignore_regexp_list);
  695.  
  696. if (output_style == OUTPUT_IFDEF)
  697. {
  698. for (i = 0; i < sizeof line_format / sizeof *line_format; i++)
  699. if (!line_format[i])
  700. line_format[i] = "%l\n";
  701. if (!group_format[OLD])
  702. group_format[OLD]
  703. = group_format[CHANGED] ? group_format[CHANGED] : "%<";
  704. if (!group_format[NEW])
  705. group_format[NEW]
  706. = group_format[CHANGED] ? group_format[CHANGED] : "%>";
  707. if (!group_format[UNCHANGED])
  708. group_format[UNCHANGED] = "%=";
  709. if (!group_format[CHANGED])
  710. group_format[CHANGED] = concat (group_format[OLD],
  711. group_format[NEW], "");
  712. }
  713.  
  714. no_diff_means_no_output =
  715. (output_style == OUTPUT_IFDEF ?
  716. (!*group_format[UNCHANGED]
  717. || (strcmp (group_format[UNCHANGED], "%=") == 0
  718. && !*line_format[UNCHANGED]))
  719. : (output_style != OUTPUT_SDIFF) | suppress_common_lines);
  720.  
  721. files_can_be_treated_as_binary =
  722. (brief & binary
  723. & ~ (ignore_blank_lines | ignore_case | strip_trailing_cr
  724. | (ignore_regexp_list.regexps || ignore_white_space)));
  725.  
  726. switch_string = option_list (argv + 1, optind - 1);
  727.  
  728. if (from_file)
  729. {
  730. if (to_file)
  731. fatal ("--from-file and --to-file both specified");
  732. else
  733. for (; optind < argc; optind++)
  734. {
  735. int status = compare_files ((struct comparison *) 0,
  736. from_file, argv[optind]);
  737. if (exit_status < status)
  738. exit_status = status;
  739. }
  740. }
  741. else
  742. {
  743. if (to_file)
  744. for (; optind < argc; optind++)
  745. {
  746. int status = compare_files ((struct comparison *) 0,
  747. argv[optind], to_file);
  748. if (exit_status < status)
  749. exit_status = status;
  750. }
  751. else
  752. {
  753. if (argc - optind != 2)
  754. {
  755. if (argc - optind < 2)
  756. try_help ("missing operand after `%s'", argv[argc - 1]);
  757. else
  758. try_help ("extra operand `%s'", argv[optind + 2]);
  759. }
  760.  
  761. exit_status = compare_files ((struct comparison *) 0,
  762. argv[optind], argv[optind + 1]);
  763. }
  764. }
  765.  
  766. /* Print any messages that were saved up for last. */
  767. print_message_queue ();
  768.  
  769. check_stdout ();
  770. exit (exit_status);
  771. return exit_status;
  772. }
  773.  
  774. /* Append to REGLIST the regexp PATTERN. */
  775.  
  776. static void
  777. add_regexp (struct regexp_list *reglist, char const *pattern)
  778. {
  779. size_t patlen = strlen (pattern);
  780. char const *m = re_compile_pattern (pattern, patlen, reglist->buf);
  781.  
  782. if (m != 0)
  783. error (0, 0, "%s: %s", pattern, m);
  784. else
  785. {
  786. char *regexps = reglist->regexps;
  787. size_t len = reglist->len;
  788. bool multiple_regexps = reglist->multiple_regexps = regexps != 0;
  789. size_t newlen = reglist->len = len + 2 * multiple_regexps + patlen;
  790. size_t size = reglist->size;
  791.  
  792. if (size <= newlen)
  793. {
  794. if (!size)
  795. size = 1;
  796.  
  797. do size *= 2;
  798. while (size <= newlen);
  799.  
  800. reglist->size = size;
  801. reglist->regexps = regexps = xrealloc (regexps, size);
  802. }
  803. if (multiple_regexps)
  804. {
  805. regexps[len++] = '\\';
  806. regexps[len++] = '|';
  807. }
  808. memcpy (regexps + len, pattern, patlen + 1);
  809. }
  810. }
  811.  
  812. /* Ensure that REGLIST represents the disjunction of its regexps.
  813.   This is done here, rather than earlier, to avoid O(N^2) behavior. */
  814.  
  815. static void
  816. summarize_regexp_list (struct regexp_list *reglist)
  817. {
  818. if (reglist->regexps)
  819. {
  820. /* At least one regexp was specified. Allocate a fastmap for it. */
  821. reglist->buf->fastmap = xmalloc (1 << CHAR_BIT);
  822. if (reglist->multiple_regexps)
  823. {
  824. /* Compile the disjunction of the regexps.
  825. (If just one regexp was specified, it is already compiled.) */
  826. char const *m = re_compile_pattern (reglist->regexps, reglist->len,
  827. reglist->buf);
  828. if (m != 0)
  829. error (EXIT_TROUBLE, 0, "%s: %s", reglist->regexps, m);
  830. }
  831. }
  832. }
  833.  
  834. static void
  835. try_help (char const *reason_msgid, char const *operand)
  836. {
  837. if (reason_msgid)
  838. error (0, 0, _(reason_msgid), operand);
  839. error (EXIT_TROUBLE, 0, _("Try `%s --help' for more information."),
  840. program_name);
  841. abort ();
  842. }
  843.  
  844. static void
  845. check_stdout (void)
  846. {
  847. if (ferror (stdout))
  848. fatal ("write failed");
  849. else if (fclose (stdout) != 0)
  850. pfatal_with_name (_("standard output"));
  851. }
  852.  
  853. static char const * const option_help_msgid[] = {
  854. N_("Compare files line by line."),
  855. "",
  856. N_("-i --ignore-case Ignore case differences in file contents."),
  857. N_("--ignore-file-name-case Ignore case when comparing file names."),
  858. N_("--no-ignore-file-name-case Consider case when comparing file names."),
  859. N_("-E --ignore-tab-expansion Ignore changes due to tab expansion."),
  860. N_("-b --ignore-space-change Ignore changes in the amount of white space."),
  861. N_("-w --ignore-all-space Ignore all white space."),
  862. N_("-B --ignore-blank-lines Ignore changes whose lines are all blank."),
  863. N_("-I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE."),
  864. N_("--strip-trailing-cr Strip trailing carriage return on input."),
  865. #if HAVE_SETMODE_DOS
  866. N_("--binary Read and write data in binary mode."),
  867. #endif
  868. N_("-a --text Treat all files as text."),
  869. "",
  870. N_("-c -C NUM --context[=NUM] Output NUM (default 3) lines of copied context.\n\
  871. -u -U NUM --unified[=NUM] Output NUM (default 3) lines of unified context.\n\
  872. --label LABEL Use LABEL instead of file name.\n\
  873. -p --show-c-function Show which C function each change is in.\n\
  874. -F RE --show-function-line=RE Show the most recent line matching RE."),
  875. N_("-q --brief Output only whether files differ."),
  876. N_("-e --ed Output an ed script."),
  877. N_("--normal Output a normal diff."),
  878. N_("-n --rcs Output an RCS format diff."),
  879. N_("-y --side-by-side Output in two columns.\n\
  880. -W NUM --width=NUM Output at most NUM (default 130) print columns.\n\
  881. --left-column Output only the left column of common lines.\n\
  882. --suppress-common-lines Do not output common lines."),
  883. N_("-D NAME --ifdef=NAME Output merged file to show `#ifdef NAME' diffs."),
  884. N_("--GTYPE-group-format=GFMT Similar, but format GTYPE input groups with GFMT."),
  885. N_("--line-format=LFMT Similar, but format all input lines with LFMT."),
  886. N_("--LTYPE-line-format=LFMT Similar, but format LTYPE input lines with LFMT."),
  887. N_(" LTYPE is `old', `new', or `unchanged'. GTYPE is LTYPE or `changed'."),
  888. N_(" GFMT may contain:\n\
  889. %< lines from FILE1\n\
  890. %> lines from FILE2\n\
  891. %= lines common to FILE1 and FILE2\n\
  892. %[-][WIDTH][.[PREC]]{doxX}LETTER printf-style spec for LETTER\n\
  893. LETTERs are as follows for new group, lower case for old group:\n\
  894. F first line number\n\
  895. L last line number\n\
  896. N number of lines = L-F+1\n\
  897. E F-1\n\
  898. M L+1"),
  899. N_(" LFMT may contain:\n\
  900. %L contents of line\n\
  901. %l contents of line, excluding any trailing newline\n\
  902. %[-][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number"),
  903. N_(" Either GFMT or LFMT may contain:\n\
  904. %% %\n\
  905. %c'C' the single character C\n\
  906. %c'\\OOO' the character with octal code OOO"),
  907. "",
  908. N_("-l --paginate Pass the output through `pr' to paginate it."),
  909. N_("-t --expand-tabs Expand tabs to spaces in output."),
  910. N_("-T --initial-tab Make tabs line up by prepending a tab."),
  911. N_("--tabsize=NUM Tab stops are every NUM (default 8) print columns."),
  912. "",
  913. N_("-r --recursive Recursively compare any subdirectories found."),
  914. N_("-N --new-file Treat absent files as empty."),
  915. N_("--unidirectional-new-file Treat absent first files as empty."),
  916. N_("-s --report-identical-files Report when two files are the same."),
  917. N_("-x PAT --exclude=PAT Exclude files that match PAT."),
  918. N_("-X FILE --exclude-from=FILE Exclude files that match any pattern in FILE."),
  919. N_("-S FILE --starting-file=FILE Start with FILE when comparing directories."),
  920. N_("--from-file=FILE1 Compare FILE1 to all operands. FILE1 can be a directory."),
  921. N_("--to-file=FILE2 Compare all operands to FILE2. FILE2 can be a directory."),
  922. "",
  923. N_("--horizon-lines=NUM Keep NUM lines of the common prefix and suffix."),
  924. N_("-d --minimal Try hard to find a smaller set of changes."),
  925. N_("--speed-large-files Assume large files and many scattered small changes."),
  926. "",
  927. N_("-v --version Output version info."),
  928. N_("--help Output this help."),
  929. "",
  930. N_("FILES are `FILE1 FILE2' or `DIR1 DIR2' or `DIR FILE...' or `FILE... DIR'."),
  931. N_("If --from-file or --to-file is given, there are no restrictions on FILES."),
  932. N_("If a FILE is `-', read standard input."),
  933. N_("Exit status is 0 if inputs are the same, 1 if different, 2 if trouble."),
  934. "",
  935. N_("Report bugs to <bug-gnu-utils@gnu.org>."),
  936. 0
  937. };
  938.  
  939. static void
  940. usage (void)
  941. {
  942. char const * const *p;
  943.  
  944. printf (_("Usage: %s [OPTION]... FILES\n"), program_name);
  945.  
  946. for (p = option_help_msgid; *p; p++)
  947. {
  948. if (!**p)
  949. putchar ('\n');
  950. else
  951. {
  952. char const *msg = _(*p);
  953. char const *nl;
  954. while ((nl = strchr (msg, '\n')))
  955. {
  956. int msglen = nl + 1 - msg;
  957. printf (" %.*s", msglen, msg);
  958. msg = nl + 1;
  959. }
  960.  
  961. printf (" %s\n" + 2 * (*msg != ' ' && *msg != '-'), msg);
  962. }
  963. }
  964. }
  965.  
  966. /* Set VAR to VALUE, reporting an OPTION error if this is a
  967.   conflict. */
  968. static void
  969. specify_value (char const **var, char const *value, char const *option)
  970. {
  971. if (*var && strcmp (*var, value) != 0)
  972. {
  973. error (0, 0, _("conflicting %s option value `%s'"), option, value);
  974. try_help (0, 0);
  975. }
  976. *var = value;
  977. }
  978.  
  979. /* Set the output style to STYLE, diagnosing conflicts. */
  980. static void
  981. specify_style (enum output_style style)
  982. {
  983. if (output_style != style)
  984. {
  985. if (output_style != OUTPUT_UNSPECIFIED)
  986. try_help ("conflicting output style options", 0);
  987. output_style = style;
  988. }
  989. }
  990. /* Set the last-modified time of *ST to be the current time. */
  991.  
  992. static void
  993. set_mtime_to_now (struct stat *st)
  994. {
  995. #ifdef ST_MTIM_NSEC
  996.  
  997. # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
  998. if (clock_gettime (CLOCK_REALTIME, &st->st_mtim) == 0)
  999. return;
  1000. # endif
  1001.  
  1002. # if HAVE_GETTIMEOFDAY
  1003. {
  1004. struct timeval timeval;
  1005. if (gettimeofday (&timeval, 0) == 0)
  1006. {
  1007. st->st_mtime = timeval.tv_sec;
  1008. st->st_mtim.ST_MTIM_NSEC = timeval.tv_usec * 1000;
  1009. return;
  1010. }
  1011. }
  1012. # endif
  1013.  
  1014. #endif /* ST_MTIM_NSEC */
  1015.  
  1016. time (&st->st_mtime);
  1017. }
  1018. /* Compare two files (or dirs) with parent comparison PARENT
  1019.   and names NAME0 and NAME1.
  1020.   (If PARENT is 0, then the first name is just NAME0, etc.)
  1021.   This is self-contained; it opens the files and closes them.
  1022.  
  1023.   Value is EXIT_SUCCESS if files are the same, EXIT_FAILURE if
  1024.   different, EXIT_TROUBLE if there is a problem opening them. */
  1025.  
  1026. static int
  1027. compare_files (struct comparison const *parent,
  1028. char const *name0,
  1029. char const *name1)
  1030. {
  1031. struct comparison cmp;
  1032. #define DIR_P(f) (S_ISDIR (cmp.file[f].stat.st_mode) != 0)
  1033. register int f;
  1034. int status = EXIT_SUCCESS;
  1035. bool same_files;
  1036. char *free0, *free1;
  1037.  
  1038. /* If this is directory comparison, perhaps we have a file
  1039.   that exists only in one of the directories.
  1040.   If so, just print a message to that effect. */
  1041.  
  1042. if (! ((name0 && name1)
  1043. || (unidirectional_new_file && name1)
  1044. || new_file))
  1045. {
  1046. char const *name = name0 == 0 ? name1 : name0;
  1047. char const *dir = parent->file[name0 == 0].name;
  1048.  
  1049. /* See POSIX 1003.1-2001 for this format. */
  1050. message ("Only in %s: %s\n", dir, name);
  1051.  
  1052. /* Return EXIT_FAILURE so that diff_dirs will return
  1053. EXIT_FAILURE ("some files differ"). */
  1054. return EXIT_FAILURE;
  1055. }
  1056.  
  1057. memset (cmp.file, 0, sizeof cmp.file);
  1058. cmp.parent = parent;
  1059.  
  1060. /* cmp.file[f].desc markers */
  1061. #define NONEXISTENT (-1) /* nonexistent file */
  1062. #define UNOPENED (-2) /* unopened file (e.g. directory) */
  1063. #define ERRNO_ENCODE(errno) (-3 - (errno)) /* encoded errno value */
  1064.  
  1065. #define ERRNO_DECODE(desc) (-3 - (desc)) /* inverse of ERRNO_ENCODE */
  1066.  
  1067. cmp.file[0].desc = name0 == 0 ? NONEXISTENT : UNOPENED;
  1068. cmp.file[1].desc = name1 == 0 ? NONEXISTENT : UNOPENED;
  1069.  
  1070. /* Now record the full name of each file, including nonexistent ones. */
  1071.  
  1072. if (name0 == 0)
  1073. name0 = name1;
  1074. if (name1 == 0)
  1075. name1 = name0;
  1076.  
  1077. if (!parent)
  1078. {
  1079. free0 = 0;
  1080. free1 = 0;
  1081. cmp.file[0].name = name0;
  1082. cmp.file[1].name = name1;
  1083. }
  1084. else
  1085. {
  1086. cmp.file[0].name = free0
  1087. = dir_file_pathname (parent->file[0].name, name0);
  1088. cmp.file[1].name = free1
  1089. = dir_file_pathname (parent->file[1].name, name1);
  1090. }
  1091.  
  1092. /* Stat the files. */
  1093.  
  1094. for (f = 0; f < 2; f++)
  1095. {
  1096. if (cmp.file[f].desc != NONEXISTENT)
  1097. {
  1098. if (f && file_name_cmp (cmp.file[f].name, cmp.file[0].name) == 0)
  1099. {
  1100. cmp.file[f].desc = cmp.file[0].desc;
  1101. cmp.file[f].stat = cmp.file[0].stat;
  1102. }
  1103. else if (strcmp (cmp.file[f].name, "-") == 0)
  1104. {
  1105. cmp.file[f].desc = STDIN_FILENO;
  1106. if (fstat (STDIN_FILENO, &cmp.file[f].stat) != 0)
  1107. cmp.file[f].desc = ERRNO_ENCODE (errno);
  1108. else
  1109. {
  1110. if (S_ISREG (cmp.file[f].stat.st_mode))
  1111. {
  1112. off_t pos = lseek (STDIN_FILENO, (off_t) 0, SEEK_CUR);
  1113. if (pos < 0)
  1114. cmp.file[f].desc = ERRNO_ENCODE (errno);
  1115. else
  1116. cmp.file[f].stat.st_size =
  1117. MAX (0, cmp.file[f].stat.st_size - pos);
  1118. }
  1119.  
  1120. /* POSIX 1003.1-2001 requires current time for
  1121. stdin. */
  1122. set_mtime_to_now (&cmp.file[f].stat);
  1123. }
  1124. }
  1125. else if (stat (cmp.file[f].name, &cmp.file[f].stat) != 0)
  1126. cmp.file[f].desc = ERRNO_ENCODE (errno);
  1127. }
  1128. }
  1129.  
  1130. /* Mark files as nonexistent as needed for -N and -P, if they are
  1131.   inaccessible empty regular files (the kind of files that 'patch'
  1132.   creates to indicate nonexistent backups), or if they are
  1133.   top-level files that do not exist but their counterparts do
  1134.   exist. */
  1135. for (f = 0; f < 2; f++)
  1136. if ((new_file || (f == 0 && unidirectional_new_file))
  1137. && (cmp.file[f].desc == UNOPENED
  1138. ? (S_ISREG (cmp.file[f].stat.st_mode)
  1139. && ! (cmp.file[f].stat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))
  1140. && cmp.file[f].stat.st_size == 0)
  1141. : (cmp.file[f].desc == ERRNO_ENCODE (ENOENT)
  1142. && ! parent
  1143. && cmp.file[1 - f].desc == UNOPENED)))
  1144. cmp.file[f].desc = NONEXISTENT;
  1145.  
  1146. for (f = 0; f < 2; f++)
  1147. if (cmp.file[f].desc == NONEXISTENT)
  1148. {
  1149. memset (&cmp.file[f].stat, 0, sizeof cmp.file[f].stat);
  1150. cmp.file[f].stat.st_mode = cmp.file[1 - f].stat.st_mode;
  1151. }
  1152.  
  1153. for (f = 0; f < 2; f++)
  1154. {
  1155. int e = ERRNO_DECODE (cmp.file[f].desc);
  1156. if (0 <= e)
  1157. {
  1158. errno = e;
  1159. perror_with_name (cmp.file[f].name);
  1160. status = EXIT_TROUBLE;
  1161. }
  1162. }
  1163.  
  1164. if (status == EXIT_SUCCESS && ! parent && DIR_P (0) != DIR_P (1))
  1165. {
  1166. /* If one is a directory, and it was specified in the command line,
  1167. use the file in that dir with the other file's basename. */
  1168.  
  1169. int fnm_arg = DIR_P (0);
  1170. int dir_arg = 1 - fnm_arg;
  1171. char const *fnm = cmp.file[fnm_arg].name;
  1172. char const *dir = cmp.file[dir_arg].name;
  1173. char const *filename = cmp.file[dir_arg].name = free0
  1174. = dir_file_pathname (dir, base_name (fnm));
  1175.  
  1176. if (strcmp (fnm, "-") == 0)
  1177. fatal ("cannot compare `-' to a directory");
  1178.  
  1179. if (stat (filename, &cmp.file[dir_arg].stat) != 0)
  1180. {
  1181. perror_with_name (filename);
  1182. status = EXIT_TROUBLE;
  1183. }
  1184. }
  1185.  
  1186. if (status != EXIT_SUCCESS)
  1187. {
  1188. /* One of the files should exist but does not. */
  1189. }
  1190. else if (cmp.file[0].desc == NONEXISTENT
  1191. && cmp.file[1].desc == NONEXISTENT)
  1192. {
  1193. /* Neither file "exists", so there's nothing to compare. */
  1194. }
  1195. else if ((same_files
  1196. = (cmp.file[0].desc != NONEXISTENT
  1197. && cmp.file[1].desc != NONEXISTENT
  1198. && 0 < same_file (&cmp.file[0].stat, &cmp.file[1].stat)
  1199. && same_file_attributes (&cmp.file[0].stat,
  1200. &cmp.file[1].stat)))
  1201. && no_diff_means_no_output)
  1202. {
  1203. /* The two named files are actually the same physical file.
  1204. We know they are identical without actually reading them. */
  1205. }
  1206. else if (DIR_P (0) & DIR_P (1))
  1207. {
  1208. if (output_style == OUTPUT_IFDEF)
  1209. fatal ("-D option not supported with directories");
  1210.  
  1211. /* If both are directories, compare the files in them. */
  1212.  
  1213. if (parent && !recursive)
  1214. {
  1215. /* But don't compare dir contents one level down
  1216. unless -r was specified.
  1217. See POSIX 1003.1-2001 for this format. */
  1218. message ("Common subdirectories: %s and %s\n",
  1219. cmp.file[0].name, cmp.file[1].name);
  1220. }
  1221. else
  1222. status = diff_dirs (&cmp, compare_files);
  1223. }
  1224. else if ((DIR_P (0) | DIR_P (1))
  1225. || (parent
  1226. && (! S_ISREG (cmp.file[0].stat.st_mode)
  1227. || ! S_ISREG (cmp.file[1].stat.st_mode))))
  1228. {
  1229. if (cmp.file[0].desc == NONEXISTENT || cmp.file[1].desc == NONEXISTENT)
  1230. {
  1231. /* We have a subdirectory that exists only in one directory. */
  1232.  
  1233. if ((DIR_P (0) | DIR_P (1))
  1234. && recursive
  1235. && (new_file
  1236. || (unidirectional_new_file
  1237. && cmp.file[0].desc == NONEXISTENT)))
  1238. status = diff_dirs (&cmp, compare_files);
  1239. else
  1240. {
  1241. char const *dir
  1242. = parent->file[cmp.file[0].desc == NONEXISTENT].name;
  1243.  
  1244. /* See POSIX 1003.1-2001 for this format. */
  1245. message ("Only in %s: %s\n", dir, name0);
  1246.  
  1247. status = EXIT_FAILURE;
  1248. }
  1249. }
  1250. else
  1251. {
  1252. /* We have two files that are not to be compared. */
  1253.  
  1254. /* See POSIX 1003.1-2001 for this format. */
  1255. message5 ("File %s is a %s while file %s is a %s\n",
  1256. file_label[0] ? file_label[0] : cmp.file[0].name,
  1257. file_type (&cmp.file[0].stat),
  1258. file_label[1] ? file_label[1] : cmp.file[1].name,
  1259. file_type (&cmp.file[1].stat));
  1260.  
  1261. /* This is a difference. */
  1262. status = EXIT_FAILURE;
  1263. }
  1264. }
  1265. else if (files_can_be_treated_as_binary
  1266. && S_ISREG (cmp.file[0].stat.st_mode)
  1267. && S_ISREG (cmp.file[1].stat.st_mode)
  1268. && cmp.file[0].stat.st_size != cmp.file[1].stat.st_size)
  1269. {
  1270. message ("Files %s and %s differ\n",
  1271. file_label[0] ? file_label[0] : cmp.file[0].name,
  1272. file_label[1] ? file_label[1] : cmp.file[1].name);
  1273. status = EXIT_FAILURE;
  1274. }
  1275. else
  1276. {
  1277. /* Both exist and neither is a directory. */
  1278.  
  1279. /* Open the files and record their descriptors. */
  1280.  
  1281. if (cmp.file[0].desc == UNOPENED)
  1282. if ((cmp.file[0].desc = open (cmp.file[0].name, O_RDONLY, 0)) < 0)
  1283. {
  1284. perror_with_name (cmp.file[0].name);
  1285. status = EXIT_TROUBLE;
  1286. }
  1287. if (cmp.file[1].desc == UNOPENED)
  1288. {
  1289. if (same_files)
  1290. cmp.file[1].desc = cmp.file[0].desc;
  1291. else if ((cmp.file[1].desc = open (cmp.file[1].name, O_RDONLY, 0))
  1292. < 0)
  1293. {
  1294. perror_with_name (cmp.file[1].name);
  1295. status = EXIT_TROUBLE;
  1296. }
  1297. }
  1298.  
  1299. #if HAVE_SETMODE_DOS
  1300. if (binary)
  1301. for (f = 0; f < 2; f++)
  1302. if (0 <= cmp.file[f].desc)
  1303. set_binary_mode (cmp.file[f].desc, true);
  1304. #endif
  1305.  
  1306. /* Compare the files, if no error was found. */
  1307.  
  1308. if (status == EXIT_SUCCESS)
  1309. status = diff_2_files (&cmp);
  1310.  
  1311. /* Close the file descriptors. */
  1312.  
  1313. if (0 <= cmp.file[0].desc && close (cmp.file[0].desc) != 0)
  1314. {
  1315. perror_with_name (cmp.file[0].name);
  1316. status = EXIT_TROUBLE;
  1317. }
  1318. if (0 <= cmp.file[1].desc && cmp.file[0].desc != cmp.file[1].desc
  1319. && close (cmp.file[1].desc) != 0)
  1320. {
  1321. perror_with_name (cmp.file[1].name);
  1322. status = EXIT_TROUBLE;
  1323. }
  1324. }
  1325.  
  1326. /* Now the comparison has been done, if no error prevented it,
  1327.   and STATUS is the value this function will return. */
  1328.  
  1329. if (status == EXIT_SUCCESS)
  1330. {
  1331. if (report_identical_files && !DIR_P (0))
  1332. message ("Files %s and %s are identical\n",
  1333. file_label[0] ? file_label[0] : cmp.file[0].name,
  1334. file_label[1] ? file_label[1] : cmp.file[1].name);
  1335. }
  1336. else
  1337. {
  1338. /* Flush stdout so that the user sees differences immediately.
  1339. This can hurt performance, unfortunately. */
  1340. if (fflush (stdout) != 0)
  1341. pfatal_with_name (_("standard output"));
  1342. }
  1343.  
  1344. if (free0)
  1345. free (free0);
  1346. if (free1)
  1347. free (free1);
  1348.  
  1349. return status;
  1350. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: kpnprakash is an unknown quantity at this point 
Solved Threads: 1
kpnprakash kpnprakash is offline Offline
Newbie Poster

Re: comparing file in c

 
0
  #4
Mar 27th, 2008
yeh thanks for your timely help.i will reply after executing the codes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: comparing file in c

 
0
  #5
Mar 27th, 2008
no worries.

i've got the .h file if you need it.

and a makefile, too.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: kpnprakash is an unknown quantity at this point 
Solved Threads: 1
kpnprakash kpnprakash is offline Offline
Newbie Poster

Re: comparing file in c

 
0
  #6
Mar 27th, 2008
yeh i tried the code but it displays the following error in vc++
fatal error C1083: Cannot open include file: 'diff.h': No such file or directory
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: kpnprakash is an unknown quantity at this point 
Solved Threads: 1
kpnprakash kpnprakash is offline Offline
Newbie Poster

Re: comparing file in c

 
0
  #7
Mar 27th, 2008
yeh i need the .h file
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: comparing file in c

 
0
  #8
Mar 27th, 2008
I think jephthah might have just been sarcastic with that huge post of his. Even if it worked for you don't turn it in as your assignment because your teacher will immediately recognize it as not your work and flunk you. If he/she doesn't then he's a very stupid teacher.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: kpnprakash is an unknown quantity at this point 
Solved Threads: 1
kpnprakash kpnprakash is offline Offline
Newbie Poster

Re: comparing file in c

 
0
  #9
Mar 28th, 2008
yeh that is not the matter.but now the code is showing error in including the header files.so please help me in it
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: comparing file in c

 
0
  #10
Mar 28th, 2008
oh dear, what have i done.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC