]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/diff/diffdir.c
Hide struct inpcb, struct tcpcb from the userland.
[FreeBSD/FreeBSD.git] / usr.bin / diff / diffdir.c
1 /*      $OpenBSD: diffdir.c,v 1.45 2015/10/05 20:15:00 millert Exp $    */
2
3 /*
4  * Copyright (c) 2003, 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * Sponsored in part by the Defense Advanced Research Projects
19  * Agency (DARPA) and Air Force Research Laboratory, Air Force
20  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21  */
22
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
25
26 #include <sys/stat.h>
27
28 #include <dirent.h>
29 #include <err.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <fnmatch.h>
33 #include <paths.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <limits.h>
39
40 #include "diff.h"
41 #include "xmalloc.h"
42
43 static int selectfile(const struct dirent *);
44 static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
45
46 #define d_status        d_type          /* we need to store status for -l */
47
48 /*
49  * Diff directory traversal. Will be called recursively if -r was specified.
50  */
51 void
52 diffdir(char *p1, char *p2, int flags)
53 {
54         struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL;
55         struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL;
56         size_t dirlen1, dirlen2;
57         char path1[PATH_MAX], path2[PATH_MAX];
58         int pos;
59
60         edp1 = edp2 = NULL;
61
62         dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
63         if (dirlen1 >= sizeof(path1) - 1) {
64                 warnc(ENAMETOOLONG, "%s", p1);
65                 status = 2;
66                 return;
67         }
68         if (path1[dirlen1 - 1] != '/') {
69                 path1[dirlen1++] = '/';
70                 path1[dirlen1] = '\0';
71         }
72         dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
73         if (dirlen2 >= sizeof(path2) - 1) {
74                 warnc(ENAMETOOLONG, "%s", p2);
75                 status = 2;
76                 return;
77         }
78         if (path2[dirlen2 - 1] != '/') {
79                 path2[dirlen2++] = '/';
80                 path2[dirlen2] = '\0';
81         }
82
83         /*
84          * Get a list of entries in each directory, skipping "excluded" files
85          * and sorting alphabetically.
86          */
87         pos = scandir(path1, &dirp1, selectfile, alphasort);
88         if (pos == -1) {
89                 if (errno == ENOENT && (Nflag || Pflag)) {
90                         pos = 0;
91                 } else {
92                         warn("%s", path1);
93                         goto closem;
94                 }
95         }
96         dp1 = dirp1;
97         edp1 = dirp1 + pos;
98
99         pos = scandir(path2, &dirp2, selectfile, alphasort);
100         if (pos == -1) {
101                 if (errno == ENOENT && Nflag) {
102                         pos = 0;
103                 } else {
104                         warn("%s", path2);
105                         goto closem;
106                 }
107         }
108         dp2 = dirp2;
109         edp2 = dirp2 + pos;
110
111         /*
112          * If we were given a starting point, find it.
113          */
114         if (start != NULL) {
115                 while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0)
116                         dp1++;
117                 while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0)
118                         dp2++;
119         }
120
121         /*
122          * Iterate through the two directory lists, diffing as we go.
123          */
124         while (dp1 != edp1 || dp2 != edp2) {
125                 dent1 = dp1 != edp1 ? *dp1 : NULL;
126                 dent2 = dp2 != edp2 ? *dp2 : NULL;
127
128                 pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
129                     ignore_file_case ? strcasecmp(dent1->d_name, dent2->d_name) :
130                     strcmp(dent1->d_name, dent2->d_name) ;
131                 if (pos == 0) {
132                         /* file exists in both dirs, diff it */
133                         diffit(dent1, path1, dirlen1, path2, dirlen2, flags);
134                         dp1++;
135                         dp2++;
136                 } else if (pos < 0) {
137                         /* file only in first dir, only diff if -N */
138                         if (Nflag)
139                                 diffit(dent1, path1, dirlen1, path2, dirlen2,
140                                     flags);
141                         else
142                                 print_only(path1, dirlen1, dent1->d_name);
143                         dp1++;
144                 } else {
145                         /* file only in second dir, only diff if -N or -P */
146                         if (Nflag || Pflag)
147                                 diffit(dent2, path1, dirlen1, path2, dirlen2,
148                                     flags);
149                         else
150                                 print_only(path2, dirlen2, dent2->d_name);
151                         dp2++;
152                 }
153         }
154
155 closem:
156         if (dirp1 != NULL) {
157                 for (dp1 = dirp1; dp1 < edp1; dp1++)
158                         free(*dp1);
159                 free(dirp1);
160         }
161         if (dirp2 != NULL) {
162                 for (dp2 = dirp2; dp2 < edp2; dp2++)
163                         free(*dp2);
164                 free(dirp2);
165         }
166 }
167
168 /*
169  * Do the actual diff by calling either diffreg() or diffdir().
170  */
171 static void
172 diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
173     int flags)
174 {
175         flags |= D_HEADER;
176         strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
177         if (stat(path1, &stb1) != 0) {
178                 if (!(Nflag || Pflag) || errno != ENOENT) {
179                         warn("%s", path1);
180                         return;
181                 }
182                 flags |= D_EMPTY1;
183                 memset(&stb1, 0, sizeof(stb1));
184         }
185
186         strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2);
187         if (stat(path2, &stb2) != 0) {
188                 if (!Nflag || errno != ENOENT) {
189                         warn("%s", path2);
190                         return;
191                 }
192                 flags |= D_EMPTY2;
193                 memset(&stb2, 0, sizeof(stb2));
194                 stb2.st_mode = stb1.st_mode;
195         }
196         if (stb1.st_mode == 0)
197                 stb1.st_mode = stb2.st_mode;
198
199         if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
200                 if (rflag)
201                         diffdir(path1, path2, flags);
202                 else
203                         printf("Common subdirectories: %s and %s\n",
204                             path1, path2);
205                 return;
206         }
207         if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
208                 dp->d_status = D_SKIPPED1;
209         else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
210                 dp->d_status = D_SKIPPED2;
211         else
212                 dp->d_status = diffreg(path1, path2, flags, 0);
213         print_status(dp->d_status, path1, path2, "");
214 }
215
216 /*
217  * Returns 1 if the directory entry should be included in the
218  * diff, else 0.  Checks the excludes list.
219  */
220 static int
221 selectfile(const struct dirent *dp)
222 {
223         struct excludes *excl;
224
225         if (dp->d_fileno == 0)
226                 return (0);
227
228         /* always skip "." and ".." */
229         if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
230             (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
231                 return (0);
232
233         /* check excludes list */
234         for (excl = excludes_list; excl != NULL; excl = excl->next)
235                 if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0)
236                         return (0);
237
238         return (1);
239 }