Merge from vendor branch GROFF:
[dragonfly.git] / sbin / jscan / subs.c
1 /*
2  * Copyright (c) 2004,2005 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sbin/jscan/subs.c,v 1.6 2005/07/06 06:21:05 dillon Exp $
35  */
36
37 #include "jscan.h"
38
39 void
40 jf_warn(struct jfile *jf, const char *ctl, ...)
41 {
42     va_list va;
43
44     fprintf(stderr, "@0x%016llx ", jf->jf_pos);
45     va_start(va, ctl);
46     vfprintf(stderr, ctl, va);
47     va_end(va);
48     fprintf(stderr, "\n");
49 }
50
51 const char *
52 type_to_name(int16_t rectype)
53 {
54     const char *str;
55
56     switch((u_int16_t)rectype & ~JMASK_LAST) {
57     case JLEAF_PAD:
58         str = "PAD";
59         break;
60     case JLEAF_ABORT:
61         str = "ABORT";
62         break;
63     case JTYPE_ASSOCIATE:
64         str = "ASSOCIATE";
65         break;
66     case JTYPE_DISASSOCIATE:
67         str = "DISASSOCIATE";
68         break;
69     case JTYPE_UNDO:
70         str = "UNDO";
71         break;
72     case JTYPE_AUDIT:
73         str = "AUDIT";
74         break;
75     case JTYPE_SETATTR:
76         str = "SETATTR";
77         break;
78     case JTYPE_WRITE:
79         str = "WRITE";
80         break;
81     case JTYPE_PUTPAGES:
82         str = "PUTPAGES";
83         break;
84     case JTYPE_SETACL:
85         str = "SETACL";
86         break;
87     case JTYPE_SETEXTATTR:
88         str = "SETEXTATTR";
89         break;
90     case JTYPE_CREATE:
91         str = "CREATE";
92         break;
93     case JTYPE_MKNOD:
94         str = "MKNOD";
95         break;
96     case JTYPE_LINK:
97         str = "LINK";
98         break;
99     case JTYPE_SYMLINK:
100         str = "SYMLINK";
101         break;
102     case JTYPE_WHITEOUT:
103         str = "WHITEOUT";
104         break;
105     case JTYPE_REMOVE:
106         str = "REMOVE";
107         break;
108     case JTYPE_MKDIR:
109         str = "MKDIR";
110         break;
111     case JTYPE_RMDIR:
112         str = "RMDIR";
113         break;
114     case JTYPE_RENAME:
115         str = "RENAME";
116         break;
117     case JTYPE_VATTR:
118         str = "vattr";
119         break;
120     case JTYPE_CRED:
121         str = "cred";
122         break;
123     case JLEAF_FILEDATA:
124         str = "filedata";
125         break;
126     case JLEAF_PATH1:
127         str = "path1";
128         break;
129     case JLEAF_PATH2:
130         str = "path2";
131         break;
132     case JLEAF_PATH3:
133         str = "path3";
134         break;
135     case JLEAF_PATH4:
136         str = "path4";
137         break;
138     case JLEAF_UID:
139         str = "uid";
140         break;
141     case JLEAF_GID:
142         str = "gid";
143         break;
144     case JLEAF_VTYPE:
145         str = "vtype";
146         break;
147     case JLEAF_MODES:
148         str = "modes";
149         break;
150     case JLEAF_FFLAGS:
151         str = "fflags";
152         break;
153     case JLEAF_PID:
154         str = "pid";
155         break;
156     case JLEAF_PPID:
157         str = "ppid";
158         break;
159     case JLEAF_COMM:
160         str = "comm";
161         break;
162     case JLEAF_ATTRNAME:
163         str = "attrname";
164         break;
165     case JLEAF_PATH_REF:
166         str = "path_ref";
167         break;
168     case JLEAF_RESERVED_0F:
169         str = "?";
170         break;
171     case JLEAF_SYMLINKDATA:
172         str = "symlinkdata";
173         break;
174     case JLEAF_SEEKPOS:
175         str = "seekpos";
176         break;
177     case JLEAF_INUM:
178         str = "inum";
179         break;
180     case JLEAF_NLINK:
181         str = "nlink";
182         break;
183     case JLEAF_FSID:
184         str = "fsid";
185         break;
186     case JLEAF_SIZE:
187         str = "size";
188         break;
189     case JLEAF_ATIME:
190         str = "atime";
191         break;
192     case JLEAF_MTIME:
193         str = "mtime";
194         break;
195     case JLEAF_CTIME:
196         str = "ctime";
197         break;
198     case JLEAF_GEN:
199         str = "gen";
200         break;
201     case JLEAF_FLAGS:
202         str = "flags";
203         break;
204     case JLEAF_UDEV:
205         str = "udev";
206         break;
207     case JLEAF_FILEREV:
208         str = "filerev";
209         break;
210     default:
211         str = "?";
212         break;
213     }
214     return (str);
215 }
216
217 void
218 stringout(FILE *fp, char c, int exact)
219 {
220     if ((c >= 'a' && c <= 'z') ||
221         (c >= 'A' && c <= 'Z') ||
222         (c >= '0' && c <= '9')
223     ) {
224         putc(c, fp);
225     } else if (isprint((unsigned char)c) && c != '\\' && c != '\"') {
226         putc(c, fp);
227     } else if (exact == 0) {
228         putc('.', fp);
229     } else if (c == 0) {
230         fprintf(fp, "\\0");
231     } else if (c == '\n') {
232         fprintf(fp, "\\n");
233     } else {
234         fprintf(fp, "\\x%02x", (int)(unsigned char)c);
235     }
236 }
237
238 void
239 jattr_reset(struct jattr *jattr)
240 {
241     struct jattr *undo;
242     struct jattr_data *data;
243
244     if (jattr->path1)
245         free(jattr->path1);
246     if (jattr->path2)
247         free(jattr->path2);
248     if (jattr->path3)
249         free(jattr->path3);
250     if (jattr->path4)
251         free(jattr->path4);
252     if (jattr->comm)
253         free(jattr->comm);
254     if (jattr->attrname)
255         free(jattr->attrname);
256     if (jattr->pathref)
257         free(jattr->pathref);
258     if (jattr->symlinkdata)
259         free(jattr->symlinkdata);
260     while ((data = jattr->data.next) != NULL) {
261         jattr->data.next = data->next;
262         free(data);
263     }
264     if ((undo = jattr->undo) != NULL)
265         jattr_reset(jattr->undo);
266     bzero(jattr, sizeof(*jattr));
267     jattr->undo = undo;
268     jattr->uid = (uid_t)-1;
269     jattr->gid = (gid_t)-1;
270     jattr->size = (off_t)-1;
271     jattr->modes = -1;
272     jattr->flags = -1;
273     jattr->seekpos = -1;
274 }
275
276 int64_t
277 buf_to_int64(const void *buf, int bytes)
278 {
279     int64_t v;
280
281     switch(bytes) {
282     case 1:
283         v = (int64_t)*(const u_int8_t *)buf;
284         break;
285     case 2:
286         v = (int64_t)*(const u_int16_t *)buf;
287         break;
288     case 4:
289         v = (int64_t)*(const u_int32_t *)buf;
290         break;
291     case 8:
292         v = *(const int64_t *)buf;
293         break;
294     default:
295         v = 0;
296     }
297     return(v);
298 }
299
300 void *
301 dupdata(const void *buf, int bytes)
302 {
303     void *res;
304
305     res = malloc(bytes);
306     bcopy(buf, res, bytes);
307
308     return(res);
309 }
310
311 char *
312 dupdatastr(const void *buf, int bytes)
313 {
314     char *res;
315
316     res = malloc(bytes + 1);
317     bcopy(buf, res, bytes);
318     res[bytes] = 0;
319
320     return(res);
321 }
322
323 /*
324  * Similar to dupdatastr() but contains sanity checks.
325  */
326 char *
327 dupdatapath(const void *buf, int bytes)
328 {
329     char *res;
330     char *scan;
331
332     res = malloc(bytes + 1);
333     bcopy(buf, res, bytes);
334     res[bytes] = 0;
335
336     if (res[0] == '/') {
337         fprintf(stderr, "Bad path: %s\n", res);
338         free(res);
339         return(NULL);
340     }
341     scan = res;
342     for (;;) {
343         if (scan[0] == '.' && scan[1] == '.' &&
344             (scan[2] == 0 || scan[2] == '/')
345         ) {
346             fprintf(stderr, "Bad path: %s\n", res);
347             free(res);
348             return(NULL);
349         }
350         if ((scan = strchr(scan, '/')) == NULL)
351             break;
352         ++scan;
353     }
354     return(res);
355 }
356