This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.sbin / mtree / compare.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
4e97a0eb 35static char sccsid[] = "@(#)compare.c 5.10 (Berkeley) 3/31/92";
15637ed4
RG
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/stat.h>
4e97a0eb 40#include <fcntl.h>
15637ed4
RG
41#include <fts.h>
42#include <errno.h>
43#include <stdio.h>
44#include <time.h>
4e97a0eb 45#include <unistd.h>
15637ed4 46#include "mtree.h"
4e97a0eb 47#include "extern.h"
15637ed4 48
4e97a0eb
NW
49extern int uflag;
50
51static char *ftype __P((u_int));
52
53#define INDENTNAMELEN 8
15637ed4 54#define LABEL \
4e97a0eb
NW
55 if (!label++) { \
56 len = printf("%s: ", RP(p)); \
57 if (len > INDENTNAMELEN) { \
58 tab = "\t"; \
59 (void)printf("\n"); \
60 } else { \
61 tab = ""; \
62 (void)printf("%*s", INDENTNAMELEN - len, ""); \
63 } \
64 }
15637ed4 65
4e97a0eb 66int
15637ed4
RG
67compare(name, s, p)
68 char *name;
69 register NODE *s;
70 register FTSENT *p;
71{
4e97a0eb
NW
72 extern int uflag;
73 u_long len, val;
74 int fd, label;
75 char *cp, *tab;
15637ed4
RG
76
77 label = 0;
78 switch(s->type) {
79 case F_BLOCK:
4e97a0eb 80 if (!S_ISBLK(p->fts_statp->st_mode))
15637ed4
RG
81 goto typeerr;
82 break;
83 case F_CHAR:
4e97a0eb 84 if (!S_ISCHR(p->fts_statp->st_mode))
15637ed4
RG
85 goto typeerr;
86 break;
87 case F_DIR:
4e97a0eb 88 if (!S_ISDIR(p->fts_statp->st_mode))
15637ed4
RG
89 goto typeerr;
90 break;
91 case F_FIFO:
4e97a0eb 92 if (!S_ISFIFO(p->fts_statp->st_mode))
15637ed4
RG
93 goto typeerr;
94 break;
95 case F_FILE:
4e97a0eb 96 if (!S_ISREG(p->fts_statp->st_mode))
15637ed4
RG
97 goto typeerr;
98 break;
99 case F_LINK:
4e97a0eb 100 if (!S_ISLNK(p->fts_statp->st_mode))
15637ed4
RG
101 goto typeerr;
102 break;
103 case F_SOCK:
4e97a0eb 104 if (!S_ISSOCK(p->fts_statp->st_mode)) {
15637ed4 105typeerr: LABEL;
4e97a0eb
NW
106 (void)printf("\ttype (%s, %s)\n",
107 ftype(s->type), inotype(p->fts_statp->st_mode));
15637ed4
RG
108 }
109 break;
110 }
4e97a0eb
NW
111 /* Set the uid/gid first, then set the mode. */
112 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
15637ed4 113 LABEL;
4e97a0eb
NW
114 (void)printf("%suser (%u, %u",
115 tab, s->st_uid, p->fts_statp->st_uid);
15637ed4 116 if (uflag)
4e97a0eb
NW
117 if (chown(p->fts_accpath, s->st_uid, -1))
118 (void)printf(", not modified: %s)\n",
15637ed4
RG
119 strerror(errno));
120 else
4e97a0eb
NW
121 (void)printf(", modified)\n");
122 else
123 (void)printf(")\n");
124 tab = "\t";
15637ed4 125 }
4e97a0eb 126 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
15637ed4 127 LABEL;
4e97a0eb
NW
128 (void)printf("%sgid (%u, %u",
129 tab, s->st_gid, p->fts_statp->st_gid);
15637ed4 130 if (uflag)
4e97a0eb 131 if (chown(p->fts_accpath, -1, s->st_gid))
15637ed4
RG
132 (void)printf(", not modified: %s)",
133 strerror(errno));
134 else
135 (void)printf(", modified)");
4e97a0eb
NW
136 else
137 (void)printf(")\n");
138 tab = "\t";
15637ed4 139 }
4e97a0eb
NW
140 if (s->flags & F_MODE &&
141 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
15637ed4 142 LABEL;
4e97a0eb
NW
143 (void)printf("%spermissions (%#o, %#o",
144 tab, s->st_mode, p->fts_statp->st_mode & MBITS);
15637ed4 145 if (uflag)
4e97a0eb 146 if (chmod(p->fts_accpath, s->st_mode))
15637ed4
RG
147 (void)printf(", not modified: %s)",
148 strerror(errno));
149 else
150 (void)printf(", modified)");
4e97a0eb
NW
151 else
152 (void)printf(")\n");
153 tab = "\t";
15637ed4
RG
154 }
155 if (s->flags & F_NLINK && s->type != F_DIR &&
4e97a0eb 156 s->st_nlink != p->fts_statp->st_nlink) {
15637ed4 157 LABEL;
4e97a0eb
NW
158 (void)printf("%slink count (%u, %u)\n",
159 tab, s->st_nlink, p->fts_statp->st_nlink);
160 tab = "\t";
15637ed4 161 }
4e97a0eb 162 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
15637ed4 163 LABEL;
4e97a0eb
NW
164 (void)printf("%ssize (%ld, %ld)\n",
165 tab, s->st_size, p->fts_statp->st_size);
166 tab = "\t";
15637ed4 167 }
4e97a0eb
NW
168 if (s->flags & F_TIME && s->st_mtime != p->fts_statp->st_mtime) {
169 LABEL;
170 (void)printf("%smodification time (%.24s, ",
171 tab, ctime(&s->st_mtime));
172 (void)printf("%.24s)\n", ctime(&p->fts_statp->st_mtime));
173 tab = "\t";
174 }
175 if (s->flags & F_CKSUM)
176 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
15637ed4 177 LABEL;
4e97a0eb
NW
178 (void)printf("%scksum: %s: %s\n",
179 tab, p->fts_accpath, strerror(errno));
180 tab = "\t";
181 } else if (crc(fd, &val, &len)) {
182 (void)close(fd);
183 LABEL;
184 (void)printf("%scksum: %s: %s\n",
185 tab, p->fts_accpath, strerror(errno));
186 tab = "\t";
187 } else {
188 (void)close(fd);
189 if (s->cksum != val) {
190 LABEL;
191 (void)printf("%scksum (%lu, %lu)\n",
192 tab, s->cksum, val);
193 }
194 tab = "\t";
15637ed4 195 }
4e97a0eb 196 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
15637ed4 197 LABEL;
4e97a0eb 198 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
15637ed4 199 }
4e97a0eb 200 return (label);
15637ed4
RG
201}
202
203char *
204inotype(type)
4e97a0eb 205 u_int type;
15637ed4
RG
206{
207 switch(type & S_IFMT) {
208 case S_IFBLK:
4e97a0eb 209 return ("block");
15637ed4 210 case S_IFCHR:
4e97a0eb 211 return ("char");
15637ed4 212 case S_IFDIR:
4e97a0eb
NW
213 return ("dir");
214 case S_IFIFO:
215 return ("fifo");
15637ed4 216 case S_IFREG:
4e97a0eb 217 return ("file");
15637ed4 218 case S_IFLNK:
4e97a0eb 219 return ("link");
15637ed4 220 case S_IFSOCK:
4e97a0eb 221 return ("socket");
15637ed4 222 default:
4e97a0eb 223 return ("unknown");
15637ed4
RG
224 }
225 /* NOTREACHED */
226}
227
4e97a0eb 228static char *
15637ed4
RG
229ftype(type)
230 u_int type;
231{
232 switch(type) {
233 case F_BLOCK:
4e97a0eb 234 return ("block");
15637ed4 235 case F_CHAR:
4e97a0eb 236 return ("char");
15637ed4 237 case F_DIR:
4e97a0eb 238 return ("dir");
15637ed4 239 case F_FIFO:
4e97a0eb 240 return ("fifo");
15637ed4 241 case F_FILE:
4e97a0eb 242 return ("file");
15637ed4 243 case F_LINK:
4e97a0eb 244 return ("link");
15637ed4 245 case F_SOCK:
4e97a0eb 246 return ("socket");
15637ed4 247 default:
4e97a0eb 248 return ("unknown");
15637ed4
RG
249 }
250 /* NOTREACHED */
251}
252
253char *
254rlink(name)
255 char *name;
256{
15637ed4 257 static char lbuf[MAXPATHLEN];
4e97a0eb 258 register int len;
15637ed4 259
4e97a0eb
NW
260 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
261 err("%s: %s", name, strerror(errno));
15637ed4 262 lbuf[len] = '\0';
4e97a0eb 263 return (lbuf);
15637ed4 264}