1 /*
2  * filediff.cc: Part of GNU CSSC.
3  *
4  *  Copyright (C) 1998, 2007, 2008, 2009, 2010, 2011, 2014, 2019 Free
5  *  Software Foundation, Inc.
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * CSSC was originally Based on MySC, by Ross Ridge, which was
21  * placed in the Public Domain.
22  *
23  *
24  * Functions for diffing two files.
25  *
26  */
27 #include <config.h>
28 #include <string>
29 #include "cssc.h"
30 #include "cssc-assert.h"
31 #include "filediff.h"
32 #include "file.h"
33 
34 
FileDiff(const char * n1,const char * n2)35 FileDiff::FileDiff(const char *n1, const char *n2)
36   : fp_(0), name1(n1), name2(n2)
37 {
38 }
39 
~FileDiff()40 FileDiff::~FileDiff()
41 {
42   finish(fp_);
43 }
44 
45 void
finish(FILE * & fp)46 FileDiff::finish(FILE * &fp)
47 {
48     ASSERT(fp == fp_);
49 
50     if (fp_)
51         pclose(fp_);
52     fp_ = 0;
53     fp = 0;
54 }
55 
56 FILE*
start()57 FileDiff::start()
58 {
59   const std::string space(" ");
60   const std::string quote("'");
61   std::string cmd(std::string(CONFIG_DIFF_COMMAND) +
62                space + quote + name1 + quote +
63                space + quote + name2 + quote);
64 
65   give_up_privileges();
66   fp_ = popen(cmd.c_str(), "r");
67   restore_privileges();
68 
69   return fp_;
70 }
71 
72 /* Local variables: */
73 /* mode: c++ */
74 /* End: */
75