Merge branch 'linux1'
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / util / fclndir.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: fclndir.c /main/3 1995/11/08 11:05:25 rswiston $ */
24 /* Copyright (c) 1987-1990 Hewlett-Packard Co.
25
26 Fclndir.c compares two files, ignoring C line directives that appear
27 within them as well as blank lines, and leading white space.  It exits
28 with an error code of 0 if the files are the same, 1 if they differ, and
29 2 if the program was called incorrectly.
30 */
31
32 /* Feb. 11, 1991, CED:  Added v2 fixes to v3 version of fclndir.
33    Thus, both versions now copy the source file to the target if
34    the files differ.
35 */
36
37 #include <stdio.h>
38 #include <malloc.h>
39 #include <string.h>
40 #if defined(MSDOS)
41 #include <process.h>
42 #endif
43 #include "basic.h"
44 #define SAME 0
45 /* different defined to be 0 as of 12-1-89, because fclndir now does
46 the file-copy if the files are different, or if the second file
47 doesn't exist.  (different used to be 1.)  --ced */
48 #define DIFFERENT 0
49 #define ERROR 2
50
51 struct data {
52   int c ;
53   LOGICAL linestart ;
54   char *p ;
55   char *q ;
56   } data1, data2 ;
57
58 char linedir[] = "#line" ;
59 char *p = linedir, *q = linedir + 1 ;
60
61 void main(
62 #if defined(M_PROTO)
63   int argc, char **argv
64 #endif
65   ) ;
66
67 void copyfile(
68 #if defined(m_proto)
69   char *pfile1, char *pfile2
70 #endif
71   ) ;
72
73 int nextchar(
74 #if defined (M_PROTO)
75   FILE *file, struct data *data
76 #endif
77   ) ;
78
79 void main(argc, argv)
80   int argc ;
81   char **argv ;
82   {
83     FILE *one, *two ;
84     int c1, c2 ;
85
86     if (argc != 3) {
87       fputs("Usage: fclndir file1 (=source) file2 (=target)\n", stderr) ;
88       exit(ERROR) ;
89       }
90     if (! (one = fopen(argv[1], "r"))) {
91       fprintf(stderr, "ERROR: source file unavailable: %s\n", argv[1]) ;
92       exit(ERROR) ;
93       }
94     if (! (two = fopen(argv[2], "r"))) {
95       fprintf(stderr, "No target file %s; copying source file...\n", argv[2]) ;
96  /* call to copyfile entered by ced, 12-29-89. */
97       copyfile(argv[1],argv[2]);
98       exit(DIFFERENT) ;
99       }
100     data1.linestart = data2.linestart = TRUE ;
101     data1.p = data2.p = linedir ;
102     data1.q = data2.q = linedir + 1 ;
103     c1 = nextchar(one, &data1) ;
104     c2 = nextchar(two, &data2) ;
105     while (c1 != EOF && c2 != EOF) {
106       if (c1 != c2) {
107         fprintf(stderr, "%s and %s are different; copying source...\n", 
108                 argv[1], argv[2]) ;
109 /* call to copyfile entered by ced, 12-1-89. */
110        copyfile(argv[1],argv[2]);
111        exit(DIFFERENT) ;
112         }
113       c1 = nextchar(one, &data1) ;
114       c2 = nextchar(two, &data2) ;
115       }
116     if (c1 != c2) {
117       fprintf(stderr, "%s and %s are different; copying source...\n", 
118               argv[1], argv[2]) ;
119 /* call to copyfile entered by ced, 12-1-89. */
120        copyfile(argv[1],argv[2]);
121        exit(DIFFERENT) ;
122       }
123     fprintf(stderr, "%s and %s are the same\n", argv[1], argv[2]) ;
124     exit(SAME) ;
125     }
126
127 /* copyfile inserted by ced, 12-1-89. */
128 void copyfile(pfile1,pfile2)
129   char *pfile1, *pfile2; 
130 {
131         int ret;
132         char *pcmd;
133
134 /* malloc space for the system command: two filenames, plus a command,
135    spaces, and the terminating null */
136         pcmd = (char *) malloc(strlen(pfile1) + strlen(pfile2) + 8);
137 #if defined(MSDOS)
138         ret = sprintf(pcmd,"copy %s %s",pfile1,pfile2);
139 #else
140         ret = sprintf(pcmd,"cp %s %s",pfile1,pfile2);
141 #endif
142         ret = system(pcmd);
143         ret = sprintf(pcmd,"touch %s",pfile2);
144         ret = system(pcmd);
145 }
146
147 int nextchar(file, data)
148   FILE *file ;
149   struct data *data ;
150   {
151     while (data->linestart) {
152       data->linestart = FALSE ;
153       for (data->p = linedir ; *data->p; data->p++)
154         if ((data->c = getc(file)) != (int) *data->p) break ;
155       /* Found a line directive, skip remainder of line */
156       if (! *data->p) {
157         data->c = getc(file) ;
158         while (data->c != '\n' && data->c != EOF)
159           data->c = getc(file) ;
160         data->linestart = TRUE ;
161         continue ;
162         }
163       /* Check for leading blanks */
164       else if (data->p == linedir) {
165         while (data->c == ' ' || data->c == '\t') data->c = getc(file) ;
166         if (data->c == '\n') {
167           data->linestart = TRUE ;
168           continue ;
169           }
170         return(data->c) ;
171         }
172       /* Line began with a prefix of #line */
173       data->q = linedir ;
174       }
175
176     if (data->q < data->p) return(*data->q++) ;
177     else if (data->q++ != data->p) data->c = getc(file) ;
178     if (data->c == '\n') data->linestart = TRUE ;
179     return(data->c) ;
180     }
181